KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > contenttool > actions > UpdateDigitalAssetAction


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.actions;
25
26 import java.awt.Image JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.applications.common.VisualFormatter;
34 import org.infoglue.cms.applications.databeans.AssetKeyDefinition;
35 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
36 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController;
37 import org.infoglue.cms.entities.content.DigitalAssetVO;
38 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
39 import org.infoglue.cms.util.CmsPropertyHandler;
40 import org.infoglue.cms.util.ConstraintExceptionBuffer;
41
42 import webwork.action.ActionContext;
43 import webwork.multipart.MultiPartRequestWrapper;
44
45
46 /**
47   * This is the action-class for UpdateDigitalAssetVersion
48   *
49   * @author Mattias Bogeblad
50   */

51
52 public class UpdateDigitalAssetAction extends ViewDigitalAssetAction
53 {
54     private final static Logger logger = Logger.getLogger(UpdateDigitalAssetAction.class.getName());
55
56     private static final long serialVersionUID = 1L;
57     
58     private Integer JavaDoc contentVersionId = null;
59     private Integer JavaDoc digitalAssetId = null;
60     private String JavaDoc digitalAssetKey = null;
61     private boolean isUpdated = false;
62     private String JavaDoc reasonKey;
63     private ContentTypeDefinitionVO contentTypeDefinitionVO;
64     private DigitalAssetVO digitalAssetVO = null;
65     private DigitalAssetVO updatedDigitalAssetVO = null;
66     private String JavaDoc closeOnLoad;
67     private Integer JavaDoc contentTypeDefinitionId;
68     
69     private ConstraintExceptionBuffer ceb;
70     
71     public UpdateDigitalAssetAction()
72     {
73         //this.digitalAssetVO = new DigitalAssetVO();
74
this.ceb = new ConstraintExceptionBuffer();
75     }
76             
77     public void setDigitalAssetKey(String JavaDoc digitalAssetKey)
78     {
79         this.digitalAssetKey = digitalAssetKey;
80     }
81            
82     public String JavaDoc doExecute() throws Exception JavaDoc
83     {
84         ceb.throwIfNotEmpty();
85         
86         InputStream JavaDoc is = null;
87         File JavaDoc file = null;
88         
89         try
90         {
91             MultiPartRequestWrapper mpr = ActionContext.getMultiPartRequest();
92             if(mpr == null)
93             {
94                 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText";
95                 return "uploadFailed";
96             }
97             
98             DigitalAssetVO digitalAssetVO = DigitalAssetController.getDigitalAssetVOWithId(this.digitalAssetId);
99             digitalAssetVO.setAssetKey(this.digitalAssetKey);
100
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                                         
108                     file = mpr.getFile(name);
109                     if(file != null)
110                     {
111                         String JavaDoc contentType = mpr.getContentType(name);
112                         String JavaDoc fileSystemName = mpr.getFilesystemName(name);
113                     
114                         String JavaDoc fileName = fileSystemName;
115                         fileName = new VisualFormatter().replaceNonAscii(fileName, '_');
116
117                         String JavaDoc tempFileName = "tmp_" + System.currentTimeMillis() + "_" + fileName;
118                         //String filePath = file.getParentFile().getPath();
119
String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
120                         fileSystemName = filePath + File.separator + tempFileName;
121                         
122                         /*
123                         file = new File(tempFileName);
124                         boolean isRenamed = file.renameTo(renamedFile);
125                         */

126                         
127                         digitalAssetVO.setAssetContentType(contentType);
128                         digitalAssetVO.setAssetFileName(fileName);
129                         digitalAssetVO.setAssetFilePath(filePath);
130                         digitalAssetVO.setAssetFileSize(new Integer JavaDoc(new Long JavaDoc(file.length()).intValue()));
131                         is = new FileInputStream JavaDoc(file);
132                         
133                         String JavaDoc fileUploadMaximumSize = getPrincipalPropertyValue("fileUploadMaximumSize", false, true);
134                         logger.info("fileUploadMaximumSize:" + fileUploadMaximumSize);
135                         
136                         if(!fileUploadMaximumSize.equalsIgnoreCase("-1") && new Integer JavaDoc(fileUploadMaximumSize).intValue() < new Long JavaDoc(file.length()).intValue())
137                         {
138                             file.delete();
139                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText";
140                             return "uploadFailed";
141                         }
142
143                         if(this.contentTypeDefinitionId != null && digitalAssetKey != null)
144                         {
145                             this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(this.contentTypeDefinitionId);
146                             AssetKeyDefinition assetKeyDefinition = ContentTypeDefinitionController.getController().getDefinedAssetKey(contentTypeDefinitionVO.getSchemaValue(), digitalAssetKey);
147                             
148                             if(assetKeyDefinition != null)
149                             {
150                                 if(assetKeyDefinition.getMaximumSize().intValue() < new Long JavaDoc(file.length()).intValue())
151                                 {
152                                     this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText";
153                                     return "uploadFailed";
154                                 }
155                                 if(assetKeyDefinition.getAllowedContentTypes().startsWith("image"))
156                                 {
157                                     if(!contentType.startsWith("image"))
158                                     {
159                                         file.delete();
160                                         this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnTypeNotImageText";
161                                         return "uploadFailed";
162                                     }
163         
164                                     Image JavaDoc image = javax.imageio.ImageIO.read(file);
165                                     int width = image.getWidth(null);
166                                     int height = image.getHeight(null);
167                                     
168                                     String JavaDoc allowedWidth = assetKeyDefinition.getImageWidth();
169                                     String JavaDoc allowedHeight = assetKeyDefinition.getImageHeight();
170                                     
171                                     if(!allowedWidth.equals("*"))
172                                     {
173                                         Integer JavaDoc allowedWidthNumber = new Integer JavaDoc(allowedWidth.substring(1));
174                                         if(allowedWidth.startsWith("<") && width >= allowedWidthNumber.intValue())
175                                         {
176                                             file.delete();
177                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageToWideText";
178                                             return "uploadFailed";
179                                         }
180                                         if(allowedWidth.startsWith(">") && width <= allowedWidthNumber.intValue())
181                                         {
182                                             file.delete();
183                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageNotWideEnoughText";
184                                             return "uploadFailed";
185                                         }
186                                         if(!allowedWidth.startsWith(">") && !allowedWidth.startsWith("<") && width != new Integer JavaDoc(allowedWidth).intValue())
187                                         {
188                                             file.delete();
189                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageWrongWidthText";
190                                             return "uploadFailed";
191                                         }
192                                     }
193                                     
194                                     if(!allowedHeight.equals("*"))
195                                     {
196                                         Integer JavaDoc allowedHeightNumber = new Integer JavaDoc(allowedHeight.substring(1));
197                                         if(allowedHeight.startsWith("<") && height >= allowedHeightNumber.intValue())
198                                         {
199                                             file.delete();
200                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageToHighText";
201                                             return "uploadFailed";
202                                         }
203                                         if(allowedHeight.startsWith(">") && height <= allowedHeightNumber.intValue())
204                                         {
205                                             file.delete();
206                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageNotHighEnoughText";
207                                             return "uploadFailed";
208                                         }
209                                         if(!allowedHeight.startsWith(">") && !allowedHeight.startsWith("<") && height != new Integer JavaDoc(allowedHeight).intValue())
210                                         {
211                                             file.delete();
212                                             this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageWrongHeightText";
213                                             return "uploadFailed";
214                                         }
215                                     }
216                                 }
217                             }
218                         }
219                     }
220                 }
221             }
222             else
223             {
224                 logger.error("File upload failed for some reason.");
225             }
226             
227             updatedDigitalAssetVO = DigitalAssetController.update(digitalAssetVO, is);
228             isUpdated = true;
229
230         }
231         catch (Exception JavaDoc e)
232         {
233           logger.error("An error occurred when we tried to upload a new asset:" + e.getMessage(), e);
234         }
235         finally
236         {
237             try
238             {
239                 is.close();
240                 file.delete();
241             }
242             catch(Exception JavaDoc e){}
243         }
244                         
245         return "success";
246     }
247
248     /**
249      * This method fetches the blob from the database and saves it on the disk.
250      * Then it returnes a url for it
251      */

252     
253     public String JavaDoc getDigitalAssetUrl() throws Exception JavaDoc
254     {
255         String JavaDoc imageHref = null;
256         try
257         {
258             imageHref = DigitalAssetController.getDigitalAssetUrl(updatedDigitalAssetVO.getDigitalAssetId());
259         }
260         catch(Exception JavaDoc e)
261         {
262             logger.warn("We could not get the url of the digitalAsset: " + e.getMessage(), e);
263         }
264         
265         return imageHref;
266     }
267     
268     public String JavaDoc getAssetThumbnailUrl()
269     {
270         String JavaDoc imageHref = null;
271         try
272         {
273             imageHref = DigitalAssetController.getDigitalAssetThumbnailUrl(updatedDigitalAssetVO.getDigitalAssetId());
274         }
275         catch(Exception JavaDoc e)
276         {
277             logger.warn("We could not get the url of the thumbnail: " + e.getMessage(), e);
278         }
279         
280         return imageHref;
281     }
282     
283     public Integer JavaDoc getDigitalAssetId()
284     {
285         return digitalAssetId;
286     }
287
288     public void setDigitalAssetId(Integer JavaDoc digitalAssetId)
289     {
290         this.digitalAssetId = digitalAssetId;
291     }
292
293     public String JavaDoc getDigitalAssetKey()
294     {
295         return digitalAssetKey;
296     }
297
298     public boolean getIsUpdated()
299     {
300         return isUpdated;
301     }
302
303     public Integer JavaDoc getContentVersionId()
304     {
305         return contentVersionId;
306     }
307
308     public void setContentVersionId(Integer JavaDoc contentVersionId)
309     {
310         this.contentVersionId = contentVersionId;
311     }
312
313     public String JavaDoc getReasonKey()
314     {
315         return reasonKey;
316     }
317     
318     public String JavaDoc getCloseOnLoad()
319     {
320         return closeOnLoad;
321     }
322     
323     public void setCloseOnLoad(String JavaDoc closeOnLoad)
324     {
325         this.closeOnLoad = closeOnLoad;
326     }
327     
328     public void setContentTypeDefinitionId(Integer JavaDoc contentTypeDefinitionId)
329     {
330         this.contentTypeDefinitionId = contentTypeDefinitionId;
331     }
332     
333     public ContentTypeDefinitionVO getContentTypeDefinitionVO()
334     {
335         return contentTypeDefinitionVO;
336     }
337 }
338
Popular Tags