KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > util > ContentFactory


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.applications.workflowtool.util;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.dom4j.Document;
33 import org.dom4j.Element;
34 import org.exolab.castor.jdo.Database;
35 import org.infoglue.cms.controllers.kernel.impl.simple.CategoryController;
36 import org.infoglue.cms.controllers.kernel.impl.simple.ContentCategoryController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
38 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
39 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
40 import org.infoglue.cms.entities.content.Content;
41 import org.infoglue.cms.entities.content.ContentVO;
42 import org.infoglue.cms.entities.content.ContentVersion;
43 import org.infoglue.cms.entities.content.ContentVersionVO;
44 import org.infoglue.cms.entities.management.CategoryVO;
45 import org.infoglue.cms.entities.management.ContentTypeAttribute;
46 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
47 import org.infoglue.cms.entities.management.LanguageVO;
48 import org.infoglue.cms.exception.ConstraintException;
49 import org.infoglue.cms.security.InfoGluePrincipal;
50 import org.infoglue.cms.util.ConstraintExceptionBuffer;
51 import org.infoglue.cms.util.dom.DOMBuilder;
52
53 /**
54  *
55  */

56 public class ContentFactory
57 {
58     /**
59      * The class logger.
60      */

61     private final static Logger logger = Logger.getLogger(ContentFactory.class.getName());
62     
63     
64     /**
65      * The content type.
66      */

67     private final ContentTypeDefinitionVO contentTypeDefinitionVO;
68     
69     /**
70      * The content bean.
71      */

72     private final ContentValues contentValues;
73     
74     /**
75      * The content version bean.
76      */

77     private final ContentVersionValues contentVersionValues;
78     
79     /**
80      * The creator.
81      */

82     private final InfoGluePrincipal principal;
83
84     /**
85      * The language associated with the content version.
86      */

87     private final LanguageVO language;
88
89     /**
90      * The database to use.
91      */

92     private Database db;
93     
94     /**
95      *
96      * @param contentTypeDefinitionVO
97      * @param contentValues
98      * @param contentVersionValues
99      * @param principal
100      * @param language
101      */

102     public ContentFactory(final ContentTypeDefinitionVO contentTypeDefinitionVO, final ContentValues contentValues, final ContentVersionValues contentVersionValues, final InfoGluePrincipal principal, final LanguageVO language)
103     {
104         this.contentTypeDefinitionVO = contentTypeDefinitionVO;
105         this.contentValues = contentValues;
106         this.contentVersionValues = contentVersionValues;
107         this.principal = principal;
108         this.language = language;
109     }
110
111     /**
112      *
113      * @param parentContent
114      * @param categories
115      * @param db
116      * @return
117      * @throws ConstraintException
118      */

119     public ContentVO create(final ContentVO parentContent, final Map JavaDoc categories, final Database db) throws ConstraintException
120     {
121         this.db = db;
122         final ContentVO contentVO = createContentVO();
123         final Document contentVersionDocument = buildContentVersionDocument();
124         final ContentVersionVO contentVersionVO = createContentVersionVO(contentVersionDocument.asXML());
125         
126         if(validate(contentVO, contentVersionVO).isEmpty())
127         {
128             return createContent(parentContent, contentVO, contentVersionVO, categories);
129         }
130         return null;
131     }
132
133     /**
134      *
135      * @param contentVO
136      * @param categories
137      * @return
138      * @throws ConstraintException
139      */

140     public ContentVO update(final ContentVO contentVO, final Map JavaDoc categories, final Database db) throws ConstraintException
141     {
142         this.db = db;
143         populateContentVO(contentVO);
144         final ContentVersionVO contentVersionVO = createContentVersionVO(buildContentVersionDocument().asXML());
145
146         if(validate(contentVO, contentVersionVO).isEmpty())
147         {
148             return updateContent(contentVO, contentVersionVO, categories);
149         }
150         return null;
151     }
152
153     /**
154      *
155      * @return
156      */

157     public ConstraintExceptionBuffer validate()
158     {
159         final ContentVO contentVO = createContentVO();
160         final ContentVersionVO contentVersionVO = createContentVersionVO(buildContentVersionDocument().asXML());
161         return validate(contentVO, contentVersionVO);
162     }
163
164     /**
165      *
166      * @param contentVO
167      * @param contentVersionVO
168      * @return
169      */

170     private ConstraintExceptionBuffer validate(final ContentVO contentVO, final ContentVersionVO contentVersionVO)
171     {
172         final ConstraintExceptionBuffer ceb = contentVO.validate();
173         ceb.add(contentVersionVO.validateAdvanced(contentTypeDefinitionVO));
174         return ceb;
175     }
176
177     /**
178      *
179      * @param parentContent
180      * @param contentVO
181      * @param contentVersionVO
182      * @param categories
183      * @return
184      */

185     private ContentVO createContent(final ContentVO parentContent, final ContentVO contentVO, final ContentVersionVO contentVersionVO, final Map JavaDoc categories)
186     {
187         try
188         {
189             final Content content = ContentControllerProxy.getContentController().create(db, parentContent.getId(), contentTypeDefinitionVO.getId(), parentContent.getRepositoryId(), contentVO);
190             final ContentVersion newContentVersion = ContentVersionController.getContentVersionController().create(content.getId(), language.getId(), contentVersionVO, null, db);
191             createCategories(newContentVersion, categories);
192             return content.getValueObject();
193         }
194         catch(Exception JavaDoc e)
195         {
196             logger.warn(e);
197         }
198         return null;
199     }
200
201     /**
202      *
203      * @param contentVO
204      * @param contentVersionVO
205      * @param categories
206      * @return
207      */

208     private ContentVO updateContent(final ContentVO contentVO, final ContentVersionVO contentVersionVO, final Map JavaDoc categories)
209     {
210         try
211         {
212             final Content content = ContentControllerProxy.getContentController().getContentWithId(contentVO.getId(), db);
213             content.setValueObject(contentVO);
214             
215             final ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(content.getId(), language.getId(), db);
216             contentVersion.getValueObject().setVersionValue(contentVersionVO.getVersionValue());
217             
218             deleteCategories(contentVersion);
219             createCategories(contentVersion, categories);
220             return content.getValueObject();
221         }
222         catch(Exception JavaDoc e)
223         {
224             logger.warn(e);
225         }
226         return null;
227     }
228
229     /**
230      * Deletes all content categories associated with the specified content version.
231      *
232      * @param contentVersion the content version.
233      * @throws Exception if an exception occurs while deleting the content categories.
234      */

235     private void deleteCategories(final ContentVersion contentVersion) throws Exception JavaDoc
236     {
237         ContentCategoryController.getController().deleteByContentVersion(contentVersion.getId(), db);
238     }
239
240     /**
241      *
242      * @param contentVersion
243      * @param categorieVOs
244      * @throws Exception
245      */

246     private void createCategories(final ContentVersion contentVersion, final Map JavaDoc categorieVOs) throws Exception JavaDoc
247     {
248         if(categorieVOs != null)
249         {
250             for(Iterator JavaDoc i=categorieVOs.keySet().iterator(); i.hasNext(); )
251             {
252                 String JavaDoc attributeName = (String JavaDoc) i.next();
253                 List JavaDoc categoryVOs = (List JavaDoc) categorieVOs.get(attributeName);
254                 createCategory(contentVersion, attributeName, categoryVOs);
255             }
256         }
257     }
258
259     /**
260      *
261      * @param contentVersion
262      * @param attributeName
263      * @param categoryVOs
264      * @throws Exception
265      */

266     private void createCategory(final ContentVersion contentVersion, final String JavaDoc attributeName, final List JavaDoc categoryVOs) throws Exception JavaDoc
267     {
268         final List JavaDoc categories = categoryVOListToCategoryList(categoryVOs);
269         ContentCategoryController.getController().create(categories, contentVersion, attributeName, db);
270     }
271
272     /**
273      *
274      * @param db the database to use in the operation.
275      * @param categoryVOList
276      * @return
277      * @throws Exception
278      */

279     private List JavaDoc categoryVOListToCategoryList(final List JavaDoc categoryVOList) throws Exception JavaDoc
280     {
281         final List JavaDoc result = new ArrayList JavaDoc();
282         for(Iterator JavaDoc i=categoryVOList.iterator(); i.hasNext(); )
283         {
284             CategoryVO categoryVO = (CategoryVO) i.next();
285             result.add(CategoryController.getController().findById(categoryVO.getCategoryId(), db));
286         }
287         return result;
288     }
289     
290
291     /**
292      * Creates a new content value object and populates it using the content bean.
293      *
294      * @return the content value object.
295      */

296     private ContentVO createContentVO()
297     {
298         final ContentVO contentVO = new ContentVO();
299         populateContentVO(contentVO);
300         return contentVO;
301     }
302
303     /**
304      * Populates the specified content value object using the content bean.
305      *
306      * @param contentVO the content value object.
307      */

308     private void populateContentVO(final ContentVO contentVO)
309     {
310         contentVO.setName(contentValues.getName());
311         contentVO.setPublishDateTime(contentValues.getPublishDateTime());
312         contentVO.setExpireDateTime(contentValues.getExpireDateTime());
313         contentVO.setIsBranch(Boolean.FALSE);
314         contentVO.setCreatorName(principal.getName());
315     }
316
317     /**
318      * Creates a new content version value object with the specified version value.
319      *
320      * @param versionValue the version value.
321      * @return the content version value object.
322      */

323     private ContentVersionVO createContentVersionVO(final String JavaDoc versionValue)
324     {
325         final ContentVersionVO contentVersion = new ContentVersionVO();
326         contentVersion.setVersionComment("");
327         contentVersion.setVersionModifier(principal.getName());
328         contentVersion.setVersionValue(versionValue);
329         return contentVersion;
330     }
331
332     /**
333      * Returns all attributes of the content type.
334      *
335      * @return the attributes.
336      */

337     private Collection JavaDoc getContentTypeAttributes()
338     {
339         return ContentTypeDefinitionController.getController().getContentTypeAttributes(contentTypeDefinitionVO.getSchemaValue());
340     }
341     
342     /**
343      * Builds the content version value document using the content version bean.
344      *
345      * @return the document.
346      */

347     private Document buildContentVersionDocument()
348     {
349         final DOMBuilder builder = new DOMBuilder();
350         final Document document = builder.createDocument();
351         final Element rootElement = builder.addElement(document, "root");
352         builder.addAttribute(rootElement, "xmlns", "x-schema:Schema.xml");
353         final Element attributesRoot = builder.addElement(rootElement, "attributes");
354         
355         buildAttributes(builder, attributesRoot);
356         
357         return document;
358     }
359
360     /**
361      * Builds the attributes part of the content version value document.
362      *
363      * @param domBuilder the document builder.
364      * @param parentElement the parent element of all the attributes.
365      */

366     private void buildAttributes(final DOMBuilder domBuilder, final Element parentElement)
367     {
368         final Collection JavaDoc typeAttributes = getContentTypeAttributes();
369         for(final Iterator JavaDoc i=typeAttributes.iterator(); i.hasNext(); )
370         {
371             final ContentTypeAttribute attribute = (ContentTypeAttribute) i.next();
372             final Element element = domBuilder.addElement(parentElement, attribute.getName());
373             final String JavaDoc value = contentVersionValues.get(attribute.getName());
374             if(value != null)
375             {
376                 domBuilder.addCDATAElement(element, value);
377             }
378         }
379     }
380 }
381
Popular Tags