KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > exchange > simple > ContentWriter


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.exchange.simple;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.HierarchyManager;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.core.MetaData;
19 import info.magnolia.cms.core.NodeData;
20 import info.magnolia.cms.security.AccessDeniedException;
21 import info.magnolia.cms.security.Authenticator;
22
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.jcr.PathNotFoundException;
31 import javax.jcr.PropertyType;
32 import javax.jcr.RepositoryException;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.log4j.Logger;
37
38
39 /**
40  * Date: Jun 25, 2004 Time: 9:48:44 AM
41  * @author Sameer Charles
42  * @version 2.0
43  */

44 public class ContentWriter {
45
46     private static Logger log = Logger.getLogger(ContentWriter.class);
47
48     private String JavaDoc repositoryName;
49
50     private String JavaDoc baseURL;
51
52     private String JavaDoc credentials;
53
54     private HttpServletRequest JavaDoc request;
55
56     private HierarchyManager hierarchyManager;
57
58     public ContentWriter() {
59     }
60
61     public ContentWriter(HierarchyManager hm, String JavaDoc repositoryName, String JavaDoc baseURL, HttpServletRequest JavaDoc request) {
62         this.repositoryName = repositoryName;
63         this.baseURL = baseURL;
64         this.request = request;
65         this.credentials = Authenticator.getCredentials(this.request);
66         this.hierarchyManager = hm;
67     }
68
69     public void writeObject(String JavaDoc destination, Object JavaDoc content) throws RepositoryException {
70         if (content instanceof SerializableContentNode) {
71             this.writeObject(this.hierarchyManager.getContent(destination), (SerializableContentNode) content);
72         }
73         else if (content instanceof SerializableContent) {
74             this.writeObject(this.hierarchyManager.getContent(destination), (SerializableContent) content);
75         }
76     }
77
78     public void writeObject(Content destination, SerializableContent content) throws RepositoryException {
79         this.writeContent(destination, content);
80         this.hierarchyManager.save();
81         if (log.isDebugEnabled()) {
82             log.debug("Path " + destination.getHandle() + " saved"); //$NON-NLS-1$ //$NON-NLS-2$
83
}
84     }
85
86     public void writeObject(Content destination, SerializableContentNode contentNode) throws RepositoryException {
87         this.writeContent(destination, contentNode);
88         this.hierarchyManager.save();
89         if (log.isDebugEnabled()) {
90             log.debug("Path " + destination.getHandle() + " saved"); //$NON-NLS-1$ //$NON-NLS-2$
91
}
92     }
93
94     /**
95      * Writes the serialized object to the specified location. If the content already exists in a persistent layer,
96      * update values else, create persistent object.
97      */

98     private void writeContent(Content parent, SerializableContent serializableContent) throws RepositoryException {
99         String JavaDoc newPageName = serializableContent.getName();
100         String JavaDoc parentHandle = parent.getHandle();
101         if (parentHandle.equals("/")) { //$NON-NLS-1$
102
parentHandle = StringUtils.EMPTY;
103         }
104         Content content = null;
105         if (serializableContent instanceof SerializableContentNode) {
106             try {
107                 content = this.hierarchyManager.getContent(parentHandle + "/" + newPageName); //$NON-NLS-1$
108
this.safeDelete(content);
109             }
110             catch (PathNotFoundException e) {
111                 content = this.hierarchyManager.createContent(parent.getHandle(), newPageName, ItemType.CONTENTNODE
112                     .getSystemName());
113             }
114         }
115         else {
116             try {
117                 content = this.hierarchyManager.getContent(parentHandle + "/" + newPageName); //$NON-NLS-1$
118
this.safeDelete(content);
119             }
120             catch (PathNotFoundException e) {
121                 content = this.hierarchyManager.createPage(parent.getHandle(), newPageName);
122             }
123         }
124         try {
125             /* write meta data */
126             this.writeMetaData(content, serializableContent.getMetaData());
127             /* write all node data */
128             this.writeNodeData(content, serializableContent);
129         }
130         catch (Exception JavaDoc e) {
131             log.error(e.getMessage(), e);
132         }
133         /* write all content nodes */
134         this.writeContentNode(content, serializableContent);
135         Iterator JavaDoc contentIterator = serializableContent.getContentCollection().iterator();
136         while (contentIterator.hasNext()) {
137             SerializableContent sContent = (SerializableContent) contentIterator.next();
138             try {
139                 this.writeContent(content, sContent);
140             }
141             catch (RepositoryException re) {
142                 log.error("Failed to update " + (parent.getHandle() + "/" + newPageName)); //$NON-NLS-1$ //$NON-NLS-2$
143
log.error(re.getMessage(), re);
144             }
145         }
146     }
147
148     private void safeDelete(Content content) {
149         // this.backup = new SerializableContent(content);
150
// todo restore? in case of exception
151
if (log.isDebugEnabled()) {
152             log.debug("Removing existing page " + content.getHandle()); //$NON-NLS-1$
153
}
154         this.removeNodedataList(content);
155         this.removeContentNodeList(content);
156     }
157
158     // todo test this
159
private void removeNodedataList(Content node) {
160         Collection JavaDoc subNodes = node.getNodeDataCollection();
161         if (subNodes.size() > 0) {
162             Iterator JavaDoc nodeIterator = subNodes.iterator();
163             while (nodeIterator.hasNext()) {
164                 NodeData subNode = (NodeData) nodeIterator.next();
165                 try {
166                     node.deleteNodeData(subNode.getName());
167                 }
168                 catch (RepositoryException re) {
169                     log.error("Failed to remove node data - " + subNode.getHandle()); //$NON-NLS-1$
170
log.error(re.getMessage(), re);
171                 }
172             }
173         }
174     }
175
176     private void removeContentNodeList(Content node) {
177         Collection JavaDoc subNodes = node.getChildren(ItemType.CONTENTNODE);
178         if (subNodes.size() > 0) {
179             Iterator JavaDoc nodeIterator = subNodes.iterator();
180             while (nodeIterator.hasNext()) {
181                 Content subNode = (Content) nodeIterator.next();
182                 try {
183                     node.delete(subNode.getName());
184                 }
185                 catch (RepositoryException re) {
186                     log.error("Failed to remove content node - " + subNode.getHandle()); //$NON-NLS-1$
187
log.error(re.getMessage(), re);
188                 }
189             }
190         }
191     }
192
193     /**
194      * @param parent
195      * @param serializableContent
196      */

197     private void writeContentNode(Content parent, SerializableContent serializableContent) {
198         Iterator JavaDoc contentNodeIterator = serializableContent.getContentNodeCollection().iterator();
199         while (contentNodeIterator.hasNext()) {
200             SerializableContentNode sContentNode = (SerializableContentNode) contentNodeIterator.next();
201             try {
202                 Content newContentNode = parent.createContent(sContentNode.getName(), ItemType.CONTENTNODE);
203                 try {
204                     /* write meta data */
205                     this.writeMetaData(newContentNode, sContentNode.getMetaData());
206                     this.writeNodeData(newContentNode, sContentNode);
207                 }
208                 catch (Exception JavaDoc e) {
209                     log.error(e.getMessage(), e);
210                 }
211                 /* check for sub content Nodes */
212                 if (sContentNode.getContentNodeCollection().size() > 0) {
213                     this.writeContentNode(newContentNode, sContentNode);
214                 }
215             }
216             catch (RepositoryException re) {
217                 log.error(re.getMessage(), re);
218             }
219         }
220     }
221
222     private void writeMetaData(Content content, SerializableMetaData serializableMetaData) throws AccessDeniedException {
223         if (serializableMetaData == null) {
224             return;
225         }
226         MetaData metaData = content.getMetaData();
227         List JavaDoc propertyList = serializableMetaData.getMetaProperties();
228         for (int index = 0; index < propertyList.size(); index++) {
229             MetaDataProperty property = (MetaDataProperty) propertyList.get(index);
230             switch (property.getType()) {
231                 case PropertyType.STRING:
232                     metaData.setProperty(property.getName(), property.getString());
233                     break;
234                 case PropertyType.LONG:
235                     metaData.setProperty(property.getName(), property.getLong());
236                     break;
237                 case PropertyType.DOUBLE:
238                     metaData.setProperty(property.getName(), property.getDouble());
239                     break;
240                 case PropertyType.BOOLEAN:
241                     metaData.setProperty(property.getName(), property.getBoolean());
242                     break;
243                 case PropertyType.DATE:
244                     metaData.setProperty(property.getName(), property.getDate());
245                     break;
246                 case PropertyType.NAME:
247                     // can't be set
248
log.debug("Ignoring NAME property"); //$NON-NLS-1$
249
break;
250                 default:
251                     log.error("Unknown property type - " + property.getType()); //$NON-NLS-1$
252
}
253         }
254     }
255
256     /**
257      * @param content parent content object
258      * @param serializableContent as received
259      */

260     private void writeNodeData(Content content, SerializableContent serializableContent) throws Exception JavaDoc {
261         Iterator JavaDoc nodeDataIterator = serializableContent.getNodeDataCollection().iterator();
262         while (nodeDataIterator.hasNext()) {
263             SerializableNodeData sNodeData = (SerializableNodeData) nodeDataIterator.next();
264             if (log.isDebugEnabled()) {
265                 log.debug("Writing NodeData list for " + content.getHandle()); //$NON-NLS-1$
266
log.debug("Writing NodeData [ " //$NON-NLS-1$
267
+ sNodeData.getName() + " ] Type [ " //$NON-NLS-1$
268
+ PropertyType.nameFromValue(sNodeData.getType()) + " ]"); //$NON-NLS-1$
269
}
270             try {
271                 NodeData nodeData = content.createNodeData(sNodeData.getName());
272                 switch (sNodeData.getType()) {
273                     case PropertyType.STRING:
274                         nodeData.setValue(sNodeData.getString());
275                         break;
276                     case PropertyType.LONG:
277                         nodeData.setValue(sNodeData.getLong());
278                         break;
279                     case PropertyType.DOUBLE:
280                         nodeData.setValue(sNodeData.getDouble());
281                         break;
282                     case PropertyType.BOOLEAN:
283                         nodeData.setValue(sNodeData.getBoolean());
284                         break;
285                     case PropertyType.DATE:
286                         nodeData.setValue(sNodeData.getDate());
287                         break;
288                     case PropertyType.BINARY:
289                         String JavaDoc binaryResourceHandle = sNodeData.getBinaryAsLink();
290                         URL JavaDoc url = new URL JavaDoc(this.baseURL);
291                         URLConnection JavaDoc urlConnection = url.openConnection();
292                         urlConnection.setRequestProperty("Authorization", credentials); //$NON-NLS-1$
293
urlConnection.addRequestProperty(Syndicator.PAGE, binaryResourceHandle);
294                         urlConnection.addRequestProperty(Syndicator.ACTION, Syndicator.GET);
295                         urlConnection.addRequestProperty(Syndicator.GET_TYPE, Syndicator.GET_TYPE_BINARY);
296                         urlConnection.addRequestProperty(Syndicator.WORKING_CONTEXT, this.repositoryName);
297                         nodeData.setValue(urlConnection.getInputStream());
298                 }
299             }
300             catch (RepositoryException re) {
301                 log.error(re.getMessage(), re);
302             }
303             catch (IOException JavaDoc e) {
304                 log.error(e.getMessage(), e);
305             }
306         }
307     }
308 }
309
Popular Tags