KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > structuretool > actions > CreatePageTemplateAction


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.applications.structuretool.actions;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.applications.common.VisualFormatter;
34 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
35 import org.infoglue.cms.applications.contenttool.actions.ViewContentTreeActionInterface;
36 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
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.controllers.kernel.impl.simple.DigitalAssetController;
41 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
42 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController;
43 import org.infoglue.cms.entities.content.ContentVO;
44 import org.infoglue.cms.entities.content.ContentVersionVO;
45 import org.infoglue.cms.entities.content.DigitalAssetVO;
46 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
47 import org.infoglue.cms.entities.management.RepositoryVO;
48 import org.infoglue.cms.exception.Bug;
49 import org.infoglue.cms.exception.ConstraintException;
50 import org.infoglue.cms.exception.SystemException;
51 import org.infoglue.cms.util.CmsPropertyHandler;
52 import org.infoglue.cms.util.ConstraintExceptionBuffer;
53
54 import webwork.action.Action;
55 import webwork.action.ActionContext;
56 import webwork.multipart.MultiPartRequestWrapper;
57
58 /**
59  * This action represents the CreatePageTemplate Usecase.
60  */

61
62 public class CreatePageTemplateAction extends InfoGlueAbstractAction implements ViewContentTreeActionInterface
63 {
64     private final static Logger logger = Logger.getLogger(CreatePageTemplateAction.class.getName());
65
66     //Used by the tree only
67
private List JavaDoc repositories;
68     private Integer JavaDoc contentId;
69     private String JavaDoc tree;
70     private String JavaDoc hideLeafs;
71     
72     private Integer JavaDoc parentContentId;
73     private Integer JavaDoc repositoryId;
74     private ConstraintExceptionBuffer ceb;
75
76     private Integer JavaDoc siteNodeId;
77     private String JavaDoc name;
78     
79     private String JavaDoc returnAddress;
80
81     
82     public String JavaDoc doInput() throws Exception JavaDoc
83     {
84         this.repositories = RepositoryController.getController().getAuthorizedRepositoryVOList(this.getInfoGluePrincipal(), false);
85
86         return Action.INPUT;
87     }
88
89     public String JavaDoc doExecute() throws Exception JavaDoc
90     {
91         logger.info("contentId:" + contentId);
92         logger.info("parentContentId:" + parentContentId);
93         logger.info("repositoryId:" + repositoryId);
94         logger.info("siteNodeId:" + siteNodeId);
95         logger.info("name:" + name);
96         
97         ContentTypeDefinitionVO contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithName("PageTemplate");
98         if(contentTypeDefinitionVO == null)
99             throw new SystemException("The system does not have the content type named 'PageTemplate' which is required for this operation.");
100         
101         ContentVO contentVO = new ContentVO();
102         
103         contentVO.setCreatorName(this.getInfoGluePrincipal().getName());
104         contentVO.setIsBranch(new Boolean JavaDoc(false));
105         contentVO.setName(name);
106         contentVO.setRepositoryId(this.repositoryId);
107
108         contentVO = ContentControllerProxy.getController().create(parentContentId, contentTypeDefinitionVO.getId(), this.repositoryId, contentVO);
109         
110         String JavaDoc componentStructure = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><components></components>";
111         
112         ContentVO metaInfoContentVO = ContentController.getContentController().getContentVOWithId(this.contentId);
113         Integer JavaDoc originalMetaInfoMasterLanguageId = LanguageController.getController().getMasterLanguage(metaInfoContentVO.getRepositoryId()).getId();
114         Integer JavaDoc destinationMasterLanguageId = LanguageController.getController().getMasterLanguage(this.repositoryId).getId();
115         ContentVersionVO originalContentVersionVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(this.contentId, originalMetaInfoMasterLanguageId);
116         logger.info("originalMetaInfoMasterLanguageId:" + originalMetaInfoMasterLanguageId);
117         logger.info("contentId:" + contentId);
118         logger.info("originalContentVersionVO:" + originalContentVersionVO);
119         
120         componentStructure = ContentVersionController.getContentVersionController().getAttributeValue(originalContentVersionVO.getId(), "ComponentStructure", false);
121         logger.info("componentStructure:" + componentStructure);
122         
123         //Create initial content version also... in masterlanguage
124
String JavaDoc versionValue = "<?xml version='1.0' encoding='UTF-8'?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes><Name><![CDATA[" + this.name + "]]></Name><ComponentStructure><![CDATA[" + componentStructure + "]]></ComponentStructure></attributes></article>";
125     
126         ContentVersionVO contentVersionVO = new ContentVersionVO();
127         contentVersionVO.setVersionComment("Saved page template");
128         contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
129         contentVersionVO.setVersionValue(versionValue);
130         ContentVersionVO newContentVersion = ContentVersionController.getContentVersionController().create(contentVO.getId(), destinationMasterLanguageId, contentVersionVO, null);
131         
132         
133         InputStream JavaDoc is = null;
134         File JavaDoc file = null;
135         
136         try
137         {
138             MultiPartRequestWrapper mpr = ActionContext.getContext().getMultiPartRequest();
139             logger.info("mpr:" + mpr);
140             if(mpr != null)
141             {
142                 Enumeration JavaDoc names = mpr.getFileNames();
143                 while (names.hasMoreElements())
144                 {
145                     String JavaDoc name = (String JavaDoc)names.nextElement();
146                     String JavaDoc contentType = mpr.getContentType(name);
147                     String JavaDoc fileSystemName = mpr.getFilesystemName(name);
148                     
149                     logger.info("name:" + name);
150                     logger.info("contentType:" + contentType);
151                     logger.info("fileSystemName:" + fileSystemName);
152                     
153                     file = mpr.getFile(name);
154                     String JavaDoc fileName = fileSystemName;
155                     fileName = new VisualFormatter().replaceNonAscii(fileName, '_');
156                     
157                     String JavaDoc tempFileName = "tmp_" + System.currentTimeMillis() + "_" + fileName;
158                     String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
159                     fileSystemName = filePath + File.separator + tempFileName;
160                     
161                     DigitalAssetVO newAsset = new DigitalAssetVO();
162                     newAsset.setAssetContentType(contentType);
163                     newAsset.setAssetKey("thumbnail");
164                     newAsset.setAssetFileName(fileName);
165                     newAsset.setAssetFilePath(filePath);
166                     newAsset.setAssetFileSize(new Integer JavaDoc(new Long JavaDoc(file.length()).intValue()));
167                     is = new FileInputStream JavaDoc(file);
168                     
169                     DigitalAssetController.create(newAsset, is, newContentVersion.getContentVersionId());
170                 }
171             }
172             else
173             {
174                 logger.error("File upload failed for some reason.");
175             }
176         }
177         catch (Exception JavaDoc e)
178         {
179             logger.error("An error occurred when we tried to upload a new asset:" + e.getMessage(), e);
180         }
181         finally
182         {
183             try
184             {
185                 is.close();
186                 file.delete();
187             }
188             catch(Exception JavaDoc e){}
189         }
190
191         
192         
193         return Action.SUCCESS;
194     }
195     
196     
197     public Integer JavaDoc getTopRepositoryId() throws ConstraintException, SystemException, Bug
198     {
199         List JavaDoc repositories = RepositoryController.getController().getAuthorizedRepositoryVOList(this.getInfoGluePrincipal(), false);
200         
201         Integer JavaDoc topRepositoryId = null;
202
203         if (repositoryId != null)
204             topRepositoryId = repositoryId;
205
206         if(repositories.size() > 0)
207         {
208             topRepositoryId = ((RepositoryVO)repositories.get(0)).getRepositoryId();
209         }
210     
211         return topRepositoryId;
212     }
213   
214     public void setHideLeafs(String JavaDoc hideLeafs)
215     {
216         this.hideLeafs = hideLeafs;
217     }
218
219     public String JavaDoc getHideLeafs()
220     {
221         return this.hideLeafs;
222     }
223
224     public String JavaDoc getTree()
225     {
226         return tree;
227     }
228
229     public void setTree(String JavaDoc tree)
230     {
231         this.tree = tree;
232     }
233
234     public void setParentContentId(Integer JavaDoc parentContentId)
235     {
236         this.parentContentId = parentContentId;
237     }
238
239     public Integer JavaDoc getParentContentId()
240     {
241         return this.parentContentId;
242     }
243     
244     public List JavaDoc getRepositories()
245     {
246         return this.repositories;
247     }
248
249     public void setRepositoryId(Integer JavaDoc repositoryId)
250     {
251         this.repositoryId = repositoryId;
252     }
253
254     public Integer JavaDoc getRepositoryId()
255     {
256         try
257         {
258             if(this.repositoryId == null)
259             {
260                 this.repositoryId = (Integer JavaDoc)getHttpSession().getAttribute("repositoryId");
261                     
262                 if(this.repositoryId == null)
263                 {
264                     this.repositoryId = getTopRepositoryId();
265                     getHttpSession().setAttribute("repositoryId", this.repositoryId);
266                 }
267             }
268         }
269         catch(Exception JavaDoc e)
270         {
271         }
272             
273         return repositoryId;
274     }
275
276     public void setContentId(Integer JavaDoc contentId)
277     {
278         this.contentId = contentId;
279     }
280
281     public Integer JavaDoc getContentId()
282     {
283         return this.contentId;
284     }
285     
286     public String JavaDoc getReturnAddress()
287     {
288         return returnAddress;
289     }
290
291     public void setReturnAddress(String JavaDoc string)
292     {
293         returnAddress = string;
294     }
295     
296     public Integer JavaDoc getSiteNodeId()
297     {
298         return siteNodeId;
299     }
300
301     public void setSiteNodeId(Integer JavaDoc siteNodeId)
302     {
303         this.siteNodeId = siteNodeId;
304     }
305     
306     public String JavaDoc getName()
307     {
308         return name;
309     }
310     public void setName(String JavaDoc name)
311     {
312         this.name = name;
313     }
314 }
315
Popular Tags