1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.log4j.Logger; 30 import org.exolab.castor.jdo.Database; 31 import org.exolab.castor.jdo.PersistenceException; 32 import org.infoglue.cms.entities.content.ContentCategory; 33 import org.infoglue.cms.entities.content.ContentCategoryVO; 34 import org.infoglue.cms.entities.content.ContentVersion; 35 import org.infoglue.cms.entities.content.ContentVersionVO; 36 import org.infoglue.cms.entities.kernel.BaseEntityVO; 37 import org.infoglue.cms.entities.management.AccessRight; 38 import org.infoglue.cms.entities.management.AccessRightVO; 39 import org.infoglue.cms.entities.management.InterceptionPoint; 40 import org.infoglue.cms.entities.workflow.EventVO; 41 import org.infoglue.cms.exception.ConstraintException; 42 import org.infoglue.cms.exception.SystemException; 43 import org.infoglue.cms.security.InfoGluePrincipal; 44 import org.infoglue.cms.util.ConstraintExceptionBuffer; 45 import org.infoglue.cms.util.DateHelper; 46 47 public class ContentStateController extends BaseController 48 { 49 private final static Logger logger = Logger.getLogger(ContentStateController.class.getName()); 50 51 public static final ContentCategoryController contentCategoryController = ContentCategoryController.getController(); 52 53 public static final int OVERIDE_WORKING = 1; 54 public static final int LEAVE_WORKING = 2; 55 56 60 61 public static ContentVersion changeState(Integer oldContentVersionId, Integer stateId, String versionComment, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Integer contentId, List resultingEvents) throws ConstraintException, SystemException 62 { 63 Database db = CastorDatabaseService.getDatabase(); 64 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 65 66 ContentVersion newContentVersion = null; 67 68 beginTransaction(db); 69 try 70 { 71 newContentVersion = changeState(oldContentVersionId, stateId, versionComment, overrideVersionModifyer, infoGluePrincipal, contentId, db, resultingEvents); 72 commitTransaction(db); 73 } 74 catch(Exception e) 75 { 76 logger.error("An error occurred so we should not complete the transaction:" + e, e); 77 rollbackTransaction(db); 78 throw new SystemException(e.getMessage()); 79 } 80 81 return newContentVersion; 82 } 83 84 85 89 90 public static ContentVersion changeState(Integer oldContentVersionId, Integer stateId, String versionComment, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Integer contentId, Database db, List resultingEvents) throws SystemException 91 { 92 ContentVersion newContentVersion = null; 93 94 try 95 { 96 ContentVersion oldContentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(oldContentVersionId, db); 97 98 if (contentId == null) 99 contentId = new Integer (oldContentVersion.getOwningContent().getContentId().intValue()); 100 101 if (stateId.intValue() == ContentVersionVO.WORKING_STATE.intValue()) 103 { 104 logger.info("About to create a new working version"); 105 106 ContentVersionVO newContentVersionVO = new ContentVersionVO(); 107 newContentVersionVO.setStateId(stateId); 108 if(versionComment != null && !versionComment.equals("")) 109 newContentVersionVO.setVersionComment(versionComment); 110 else 111 newContentVersionVO.setVersionComment("New working version"); 112 newContentVersionVO.setModifiedDateTime(DateHelper.getSecondPreciseDate()); 113 if(overrideVersionModifyer) 114 newContentVersionVO.setVersionModifier(infoGluePrincipal.getName()); 115 else 116 newContentVersionVO.setVersionModifier(oldContentVersion.getVersionModifier()); 117 newContentVersionVO.setVersionValue(oldContentVersion.getVersionValue()); 118 newContentVersion = ContentVersionController.getContentVersionController().create(contentId, oldContentVersion.getLanguage().getLanguageId(), newContentVersionVO, oldContentVersion.getContentVersionId(), db); 119 120 copyAccessRights(oldContentVersion, newContentVersion, db); 122 copyContentCategories(oldContentVersion, newContentVersion, db); 123 } 124 125 if (stateId.intValue() == ContentVersionVO.PUBLISH_STATE.intValue()) 127 { 128 logger.info("About to copy the working copy to a publish-one"); 129 130 oldContentVersion.setVersionComment(versionComment); 132 133 ContentVersionVO newContentVersionVO = new ContentVersionVO(); 135 newContentVersionVO.setStateId(stateId); 136 newContentVersionVO.setVersionComment(versionComment); 137 newContentVersionVO.setModifiedDateTime(DateHelper.getSecondPreciseDate()); 138 if(overrideVersionModifyer) 139 newContentVersionVO.setVersionModifier(infoGluePrincipal.getName()); 140 else 141 newContentVersionVO.setVersionModifier(oldContentVersion.getVersionModifier()); 142 newContentVersionVO.setVersionValue(oldContentVersion.getVersionValue()); 143 newContentVersion = ContentVersionController.getContentVersionController().create(contentId, oldContentVersion.getLanguage().getLanguageId(), newContentVersionVO, oldContentVersion.getContentVersionId(), db); 144 145 copyAccessRights(oldContentVersion, newContentVersion, db); 147 copyContentCategories(oldContentVersion, newContentVersion, db); 148 149 if(!newContentVersion.getOwningContent().getContentTypeDefinition().getName().equalsIgnoreCase("Meta info")) 151 { 152 EventVO eventVO = new EventVO(); 153 eventVO.setDescription(newContentVersion.getVersionComment()); 154 eventVO.setEntityClass(ContentVersion.class.getName()); 155 eventVO.setEntityId(new Integer (newContentVersion.getId().intValue())); 156 eventVO.setName(newContentVersion.getOwningContent().getName()); 157 eventVO.setTypeId(EventVO.PUBLISH); 158 eventVO = EventController.create(eventVO, newContentVersion.getOwningContent().getRepository().getId(), infoGluePrincipal, db); 159 resultingEvents.add(eventVO); 160 } 161 } 162 163 if (stateId.intValue() == ContentVersionVO.PUBLISHED_STATE.intValue()) 165 { 166 logger.info("About to publish an existing version"); 167 oldContentVersion.setStateId(stateId); 168 oldContentVersion.setIsActive(new Boolean (true)); 169 newContentVersion = oldContentVersion; 170 } 171 172 } 173 catch (Exception e) 174 { 175 logger.error("An error occurred so we should not complete the transaction:" + e, e); 176 throw new SystemException(e.getMessage()); 177 } 178 179 return newContentVersion; 180 } 181 182 183 186 187 private static void copyAccessRights(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws ConstraintException, SystemException, Exception 188 { 189 List interceptionPointList = InterceptionPointController.getController().getInterceptionPointList("ContentVersion", db); 190 logger.info("interceptionPointList:" + interceptionPointList.size()); 191 Iterator interceptionPointListIterator = interceptionPointList.iterator(); 192 while(interceptionPointListIterator.hasNext()) 193 { 194 InterceptionPoint interceptionPoint = (InterceptionPoint)interceptionPointListIterator.next(); 195 List accessRightList = AccessRightController.getController().getAccessRightListForEntity(interceptionPoint.getId(), originalContentVersion.getId().toString(), db); 196 logger.info("accessRightList:" + accessRightList.size()); 197 Iterator accessRightListIterator = accessRightList.iterator(); 198 while(accessRightListIterator.hasNext()) 199 { 200 AccessRight accessRight = (AccessRight)accessRightListIterator.next(); 201 logger.info("accessRight:" + accessRight.getId()); 202 203 AccessRightVO copiedAccessRight = accessRight.getValueObject().createCopy(); copiedAccessRight.setParameters(newContentVersion.getId().toString()); 205 AccessRightController.getController().create(copiedAccessRight, interceptionPoint, db); 206 } 207 } 208 } 209 210 219 private static void copyContentCategories(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws SystemException, PersistenceException 220 { 221 List orignals = contentCategoryController.findByContentVersion(originalContentVersion.getId(), db); 222 for (Iterator iter = orignals.iterator(); iter.hasNext();) 223 { 224 ContentCategory contentCategory = (ContentCategory)iter.next(); 225 ContentCategoryVO vo = new ContentCategoryVO(); 226 vo.setAttributeName(contentCategory.getAttributeName()); 227 vo.setCategory(contentCategory.getCategory().getValueObject()); 228 vo.setContentVersionId(newContentVersion.getId()); 229 ContentCategory newContentCategory = contentCategoryController.createWithDatabase(vo, db); 230 } 232 } 233 234 237 238 public BaseEntityVO getNewVO() 239 { 240 return null; 241 } 242 243 } 244 245 | Popular Tags |