View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.cms;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.util.Enumeration;
20  import java.util.Properties;
21  
22  import org.apache.commons.lang.ArrayUtils;
23  import org.apache.slide.content.NodeProperty;
24  import org.apache.slide.content.NodeRevisionContent;
25  import org.apache.slide.content.NodeRevisionDescriptor;
26  import org.apache.slide.content.NodeRevisionDescriptors;
27  import org.apache.slide.content.NodeRevisionNumber;
28  import org.seasar.tuigwaa.cms.core.Page;
29  import org.seasar.tuigwaa.cms.core.PageImpl;
30  import org.seasar.tuigwaa.cms.core.Resource;
31  import org.seasar.tuigwaa.cms.core.ResourceImpl;
32  
33  
34  /***
35   * @author nishioka
36   */
37  public class SlidePageFactory {
38  
39  	private static final String[] STANDARD_PROPERTIES = {
40  			NodeRevisionDescriptor.CONTENT_LANGUAGE,
41  			NodeRevisionDescriptor.CONTENT_LENGTH,
42  			NodeRevisionDescriptor.CONTENT_TYPE,
43  			NodeRevisionDescriptor.CREATION_DATE,
44  			NodeRevisionDescriptor.CREATION_USER, NodeRevisionDescriptor.ETAG,
45  			NodeRevisionDescriptor.LAST_MODIFIED,
46  			NodeRevisionDescriptor.MODIFICATION_DATE,
47  			NodeRevisionDescriptor.MODIFICATION_USER,
48  			NodeRevisionDescriptor.NAME, NodeRevisionDescriptor.OWNER,
49  			NodeRevisionDescriptor.RESOURCE_TYPE,
50  			NodeRevisionDescriptor.SOURCE, NodeRevisionDescriptor.TYPE };
51  
52  	public static Page createPage(String siteName, String pagePath,
53  			NodeRevisionDescriptors descs, NodeRevisionNumber number,
54  			NodeRevisionDescriptor desc, NodeRevisionDescriptor initDesc,
55  			NodeRevisionContent rcontent) {
56  
57  		String content = null;
58  		try {
59  			content = new String(rcontent.getContentBytes(),
60  					ContentsStoreServiceSlideImpl.DEFAULT_ENCODING);
61  		} catch (UnsupportedEncodingException e) {
62  			// do nothing
63  		}
64  		Resource resource = createResource(siteName, pagePath, descs, number,
65  				desc, initDesc);
66  		return new PageImpl(resource, content);
67  	}
68  
69  	public static Resource createResource(String siteName, String pagePath,
70  			NodeRevisionDescriptors descs, NodeRevisionNumber number,
71  			NodeRevisionDescriptor desc, NodeRevisionDescriptor initialDesc) {
72  		boolean folderFlag = isFolderFlag(desc);
73  
74  		Resource resource = new ResourceImpl(siteName, pagePath, desc
75  				.getContentType(), folderFlag);
76  		resource.setCreationDate(initialDesc.getCreationDate());
77  		resource.setCreationUser(initialDesc.getCreationUser());
78  		resource.setModificationDate(desc.getCreationDate());
79  		resource.setModificationUser(desc.getCreationUser());
80  		resource.setProperties(getProperties(desc));
81  		if (number != null) {
82  			resource.setVersion(number.toString());
83  		}
84  		return resource;
85  	}
86  
87  	public static boolean isFolderFlag(NodeRevisionDescriptor desc) {
88  		return desc.getResourceType().equals(
89  				NodeRevisionDescriptor.COLLECTION_TYPE);
90  	}
91  
92  	public static Properties getProperties(NodeRevisionDescriptor desc) {
93  		Properties props = new Properties();
94  		Enumeration nenum = desc.enumeratePropertiesName();
95  		while (nenum.hasMoreElements()) {
96  			String key = (String) nenum.nextElement();
97  			for (int i = 0; i < STANDARD_PROPERTIES.length; i++) {
98  				if(!ArrayUtils.contains(STANDARD_PROPERTIES, key)){
99  					NodeProperty property = desc.getProperty(key);
100 					props.setProperty(key, property.getValue().toString());
101 				}
102 			}
103 		}
104 		return props;
105 	}
106 }