1 23 24 package org.infoglue.cms.applications.contenttool.wizards.actions; 25 26 import java.io.File ; 27 import java.io.InputStream ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 mandatoryAssetKey = null; 52 private String digitalAssetKey = ""; 53 private Integer uploadedFilesCounter = new Integer (0); 54 private ContentTypeDefinitionVO contentTypeDefinitionVO = null; 55 private Integer languageId = null; 56 57 public CreateContentWizardInputAssetsAction() 58 { 59 } 60 61 public String doInput() throws Exception 62 { 63 CreateContentWizardInfoBean createContentWizardInfoBean = this.getCreateContentWizardInfoBean(); 64 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(createContentWizardInfoBean.getContentTypeDefinitionId()); 65 66 List 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 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 doExecute() throws Exception 94 { 95 InputStream is = null; 96 File renamedFile = null; 97 98 try 99 { 100 MultiPartRequestWrapper mpr = ActionContext.getContext().getMultiPartRequest(); 101 if(mpr != null) 102 { 103 Enumeration names = mpr.getFileNames(); 104 while (names.hasMoreElements()) 105 { 106 String name = (String )names.nextElement(); 107 String contentType = mpr.getContentType(name); 108 String fileSystemName = mpr.getFilesystemName(name); 109 110 File file = mpr.getFile(name); 111 String fileName = digitalAssetKey + "_" + System.currentTimeMillis() + "_" + fileSystemName; 112 String tempFileName = "tmp_" + fileName; 113 tempFileName = new VisualFormatter().replaceNonAscii(fileName, '_'); 114 115 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 117 fileSystemName = filePath + File.separator + tempFileName; 118 119 renamedFile = new File (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 (new Long (renamedFile.length()).intValue())); 128 131 CreateContentWizardInfoBean createContentWizardInfoBean = this.getCreateContentWizardInfoBean(); 132 createContentWizardInfoBean.getDigitalAssets().put(digitalAssetKey + "_" + this.languageId, newAsset); 133 134 this.uploadedFilesCounter = new Integer (this.uploadedFilesCounter.intValue() + 1); 135 } 136 } 137 else 138 { 139 logger.error("File upload failed for some reason."); 140 } 141 } 142 catch (Exception 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 e){} 154 } 155 156 return doInput(); 157 } 158 159 public String doFinish() throws Exception 160 { 161 return "success"; 162 } 163 164 public void setDigitalAssetKey(String digitalAssetKey) 165 { 166 this.digitalAssetKey = digitalAssetKey; 167 } 168 169 public void setUploadedFilesCounter(Integer uploadedFilesCounter) 170 { 171 this.uploadedFilesCounter = uploadedFilesCounter; 172 } 173 174 public Integer getUploadedFilesCounter() 175 { 176 return this.uploadedFilesCounter; 177 } 178 179 public List getDefinedAssetKeys() 180 { 181 return ContentTypeDefinitionController.getController().getDefinedAssetKeys(this.contentTypeDefinitionVO.getSchemaValue()); 182 } 183 184 185 public String getMandatoryAssetKey() 186 { 187 return mandatoryAssetKey; 188 } 189 190 public void setMandatoryAssetKey(String string) 191 { 192 mandatoryAssetKey = string; 193 } 194 195 public Integer getLanguageId() 196 { 197 return languageId; 198 } 199 200 public void setLanguageId(Integer integer) 201 { 202 languageId = integer; 203 } 204 205 } | Popular Tags |