KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ContentStateController


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
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
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     /**
57      * This method handles versioning and state-control of content.
58      * Se inline documentation for further explainations.
59      */

60     
61     public static ContentVersion changeState(Integer JavaDoc oldContentVersionId, Integer JavaDoc stateId, String JavaDoc versionComment, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc contentId, List JavaDoc 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 JavaDoc 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     /**
86      * This method handles versioning and state-control of content.
87      * Se inline documentation for further explainations.
88      */

89     
90     public static ContentVersion changeState(Integer JavaDoc oldContentVersionId, Integer JavaDoc stateId, String JavaDoc versionComment, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc contentId, Database db, List JavaDoc 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 JavaDoc(oldContentVersion.getOwningContent().getContentId().intValue());
100
101             //Here we create a new version if it was a state-change back to working, it's a copy of the publish-version
102
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                 //ContentVersionController.getContentVersionController().copyDigitalAssets(oldContentVersion, newContentVersion, db);
121
copyAccessRights(oldContentVersion, newContentVersion, db);
122                 copyContentCategories(oldContentVersion, newContentVersion, db);
123             }
124
125             //If the user changes the state to publish we create a copy and set that copy to publish.
126
if (stateId.intValue() == ContentVersionVO.PUBLISH_STATE.intValue())
127             {
128                 logger.info("About to copy the working copy to a publish-one");
129
130                 //First we update the old working-version so it gets a comment
131
oldContentVersion.setVersionComment(versionComment);
132
133                 //Now we create a new version which is basically just a copy of the working-version
134
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                 //ContentVersionController.getContentVersionController().copyDigitalAssets(oldContentVersion, newContentVersion, db);
146
copyAccessRights(oldContentVersion, newContentVersion, db);
147                 copyContentCategories(oldContentVersion, newContentVersion, db);
148
149                 //Creating the event that will notify the editor...
150
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 JavaDoc(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 the user in the publish-app publishes a publish-version we change state to published.
164
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 JavaDoc(true));
169                 newContentVersion = oldContentVersion;
170             }
171
172         }
173         catch (Exception JavaDoc 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     /**
184      * This method assigns the same access rights as the old content-version has.
185      */

186     
187     private static void copyAccessRights(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
188     {
189         List JavaDoc interceptionPointList = InterceptionPointController.getController().getInterceptionPointList("ContentVersion", db);
190         logger.info("interceptionPointList:" + interceptionPointList.size());
191         Iterator JavaDoc interceptionPointListIterator = interceptionPointList.iterator();
192         while(interceptionPointListIterator.hasNext())
193         {
194             InterceptionPoint interceptionPoint = (InterceptionPoint)interceptionPointListIterator.next();
195             List JavaDoc accessRightList = AccessRightController.getController().getAccessRightListForEntity(interceptionPoint.getId(), originalContentVersion.getId().toString(), db);
196             logger.info("accessRightList:" + accessRightList.size());
197             Iterator JavaDoc 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(); //.getValueObject();
204
copiedAccessRight.setParameters(newContentVersion.getId().toString());
205                 AccessRightController.getController().create(copiedAccessRight, interceptionPoint, db);
206             }
207         }
208     }
209
210     /**
211      * Makes copies of the ContentCategories for the old ContentVersion so the new ContentVersion
212      * still has references to them.
213      *
214      * @param originalContentVersion
215      * @param newContentVersion
216      * @param db The Database to use
217      * @throws SystemException If an error happens
218      */

219     private static void copyContentCategories(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws SystemException, PersistenceException
220     {
221         List JavaDoc orignals = contentCategoryController.findByContentVersion(originalContentVersion.getId(), db);
222         for (Iterator JavaDoc 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             //newContentCategory
231
}
232     }
233
234     /**
235      * This method should never be called.
236      */

237
238     public BaseEntityVO getNewVO()
239     {
240         return null;
241     }
242
243 }
244  
245
Popular Tags