1 23 package org.infoglue.cms.applications.workflowtool.util; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 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 56 public class ContentFactory 57 { 58 61 private final static Logger logger = Logger.getLogger(ContentFactory.class.getName()); 62 63 64 67 private final ContentTypeDefinitionVO contentTypeDefinitionVO; 68 69 72 private final ContentValues contentValues; 73 74 77 private final ContentVersionValues contentVersionValues; 78 79 82 private final InfoGluePrincipal principal; 83 84 87 private final LanguageVO language; 88 89 92 private Database db; 93 94 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 119 public ContentVO create(final ContentVO parentContent, final Map 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 140 public ContentVO update(final ContentVO contentVO, final Map 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 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 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 185 private ContentVO createContent(final ContentVO parentContent, final ContentVO contentVO, final ContentVersionVO contentVersionVO, final Map 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 e) 195 { 196 logger.warn(e); 197 } 198 return null; 199 } 200 201 208 private ContentVO updateContent(final ContentVO contentVO, final ContentVersionVO contentVersionVO, final Map 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 e) 223 { 224 logger.warn(e); 225 } 226 return null; 227 } 228 229 235 private void deleteCategories(final ContentVersion contentVersion) throws Exception 236 { 237 ContentCategoryController.getController().deleteByContentVersion(contentVersion.getId(), db); 238 } 239 240 246 private void createCategories(final ContentVersion contentVersion, final Map categorieVOs) throws Exception 247 { 248 if(categorieVOs != null) 249 { 250 for(Iterator i=categorieVOs.keySet().iterator(); i.hasNext(); ) 251 { 252 String attributeName = (String ) i.next(); 253 List categoryVOs = (List ) categorieVOs.get(attributeName); 254 createCategory(contentVersion, attributeName, categoryVOs); 255 } 256 } 257 } 258 259 266 private void createCategory(final ContentVersion contentVersion, final String attributeName, final List categoryVOs) throws Exception 267 { 268 final List categories = categoryVOListToCategoryList(categoryVOs); 269 ContentCategoryController.getController().create(categories, contentVersion, attributeName, db); 270 } 271 272 279 private List categoryVOListToCategoryList(final List categoryVOList) throws Exception 280 { 281 final List result = new ArrayList (); 282 for(Iterator 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 296 private ContentVO createContentVO() 297 { 298 final ContentVO contentVO = new ContentVO(); 299 populateContentVO(contentVO); 300 return contentVO; 301 } 302 303 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 323 private ContentVersionVO createContentVersionVO(final String 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 337 private Collection getContentTypeAttributes() 338 { 339 return ContentTypeDefinitionController.getController().getContentTypeAttributes(contentTypeDefinitionVO.getSchemaValue()); 340 } 341 342 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 366 private void buildAttributes(final DOMBuilder domBuilder, final Element parentElement) 367 { 368 final Collection typeAttributes = getContentTypeAttributes(); 369 for(final Iterator 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 value = contentVersionValues.get(attribute.getName()); 374 if(value != null) 375 { 376 domBuilder.addCDATAElement(element, value); 377 } 378 } 379 } 380 } 381 | Popular Tags |