KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > contenttool > wizards > actions > CreateContentWizardInputAssetsAction


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.contenttool.wizards.actions;
25
26 import java.io.File JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator 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.contenttool.actions.ViewMultiSelectContentTreeForServiceBindingAction;
35 import org.infoglue.cms.applications.databeans.AssetKeyDefinition;
36 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
38 import org.infoglue.cms.entities.content.DigitalAssetVO;
39 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
40 import org.infoglue.cms.entities.management.LanguageVO;
41 import org.infoglue.cms.util.CmsPropertyHandler;
42
43 import webwork.action.ActionContext;
44 import webwork.multipart.MultiPartRequestWrapper;
45
46
47 public class CreateContentWizardInputAssetsAction extends CreateContentWizardAbstractAction
48 {
49     private final static Logger logger = Logger.getLogger(CreateContentWizardInputAssetsAction.class.getName());
50
51     private String JavaDoc mandatoryAssetKey = null;
52     private String JavaDoc digitalAssetKey = "";
53     private Integer JavaDoc uploadedFilesCounter = new Integer JavaDoc(0);
54     private ContentTypeDefinitionVO contentTypeDefinitionVO = null;
55     private Integer JavaDoc languageId = null;
56     
57     public CreateContentWizardInputAssetsAction()
58     {
59     }
60             
61     public String JavaDoc doInput() throws Exception JavaDoc
62     {
63         CreateContentWizardInfoBean createContentWizardInfoBean = this.getCreateContentWizardInfoBean();
64         this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(createContentWizardInfoBean.getContentTypeDefinitionId());
65             
66         List JavaDoc assetKeys = ContentTypeDefinitionController.getController().getDefinedAssetKeys(this.contentTypeDefinitionVO.getSchemaValue());
67         
68         if(this.languageId == null)
69         {
70             LanguageVO masterLanguageVO = LanguageController.getController().getMasterLanguage(createContentWizardInfoBean.getRepositoryId());
71             this.languageId = masterLanguageVO.getLanguageId();
72         }
73
74         boolean missingAsset = false;
75         Iterator JavaDoc assetKeysIterator = assetKeys.iterator();
76         while(assetKeysIterator.hasNext())
77         {
78             AssetKeyDefinition assetKeyDefinition = (AssetKeyDefinition)assetKeysIterator.next();
79             if(!createContentWizardInfoBean.getDigitalAssets().containsKey(assetKeyDefinition.getAssetKey() + "_" + this.languageId))
80             {
81                 mandatoryAssetKey = assetKeyDefinition.getAssetKey();
82                 missingAsset = true;
83                 break;
84             }
85         }
86         
87         if(missingAsset)
88             return "input";
89         else
90             return "success";
91     }
92
93     public String JavaDoc doExecute() throws Exception JavaDoc
94     {
95         InputStream JavaDoc is = null;
96         File JavaDoc renamedFile = null;
97         
98         try
99         {
100             MultiPartRequestWrapper mpr = ActionContext.getContext().getMultiPartRequest();
101             if(mpr != null)
102             {
103                 Enumeration JavaDoc names = mpr.getFileNames();
104                 while (names.hasMoreElements())
105                 {
106                     String JavaDoc name = (String JavaDoc)names.nextElement();
107                     String JavaDoc contentType = mpr.getContentType(name);
108                     String JavaDoc fileSystemName = mpr.getFilesystemName(name);
109                     
110                     File JavaDoc file = mpr.getFile(name);
111                     String JavaDoc fileName = digitalAssetKey + "_" + System.currentTimeMillis() + "_" + fileSystemName;
112                     String JavaDoc tempFileName = "tmp_" + fileName;
113                     tempFileName = new VisualFormatter().replaceNonAscii(fileName, '_');
114                     
115                     //String filePath = file.getParentFile().getPath();
116
String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
117                     fileSystemName = filePath + File.separator + tempFileName;
118                     
119                     renamedFile = new File JavaDoc(fileSystemName);
120                     boolean isRenamed = file.renameTo(renamedFile);
121                     
122                     DigitalAssetVO newAsset = new DigitalAssetVO();
123                     newAsset.setAssetContentType(contentType);
124                     newAsset.setAssetKey(digitalAssetKey);
125                     newAsset.setAssetFileName(fileName);
126                     newAsset.setAssetFilePath(filePath);
127                     newAsset.setAssetFileSize(new Integer JavaDoc(new Long JavaDoc(renamedFile.length()).intValue()));
128                     //is = new FileInputStream(renamedFile);
129
//DigitalAssetController.create(newAsset, is, this.contentVersionId);
130

131                     CreateContentWizardInfoBean createContentWizardInfoBean = this.getCreateContentWizardInfoBean();
132                     createContentWizardInfoBean.getDigitalAssets().put(digitalAssetKey + "_" + this.languageId, newAsset);
133                     
134                     this.uploadedFilesCounter = new Integer JavaDoc(this.uploadedFilesCounter.intValue() + 1);
135                 }
136             }
137             else
138             {
139                 logger.error("File upload failed for some reason.");
140             }
141         }
142         catch (Exception JavaDoc e)
143         {
144             logger.error("An error occurred when we tried to upload a new asset:" + e.getMessage(), e);
145         }
146         finally
147         {
148             try
149             {
150                 is.close();
151                 renamedFile.delete();
152             }
153             catch(Exception JavaDoc e){}
154         }
155         
156         return doInput();
157     }
158     
159     public String JavaDoc doFinish() throws Exception JavaDoc
160     {
161         return "success";
162     }
163     
164     public void setDigitalAssetKey(String JavaDoc digitalAssetKey)
165     {
166         this.digitalAssetKey = digitalAssetKey;
167     }
168     
169     public void setUploadedFilesCounter(Integer JavaDoc uploadedFilesCounter)
170     {
171         this.uploadedFilesCounter = uploadedFilesCounter;
172     }
173
174     public Integer JavaDoc getUploadedFilesCounter()
175     {
176         return this.uploadedFilesCounter;
177     }
178        
179     public List JavaDoc getDefinedAssetKeys()
180     {
181         return ContentTypeDefinitionController.getController().getDefinedAssetKeys(this.contentTypeDefinitionVO.getSchemaValue());
182     }
183
184
185     public String JavaDoc getMandatoryAssetKey()
186     {
187         return mandatoryAssetKey;
188     }
189
190     public void setMandatoryAssetKey(String JavaDoc string)
191     {
192         mandatoryAssetKey = string;
193     }
194
195     public Integer JavaDoc getLanguageId()
196     {
197         return languageId;
198     }
199
200     public void setLanguageId(Integer JavaDoc integer)
201     {
202         languageId = integer;
203     }
204
205 }
Popular Tags