1 23 24 package org.infoglue.cms.applications.structuretool.actions; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.InputStream ; 29 import java.util.Enumeration ; 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.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 61 62 public class CreatePageTemplateAction extends InfoGlueAbstractAction implements ViewContentTreeActionInterface 63 { 64 private final static Logger logger = Logger.getLogger(CreatePageTemplateAction.class.getName()); 65 66 private List repositories; 68 private Integer contentId; 69 private String tree; 70 private String hideLeafs; 71 72 private Integer parentContentId; 73 private Integer repositoryId; 74 private ConstraintExceptionBuffer ceb; 75 76 private Integer siteNodeId; 77 private String name; 78 79 private String returnAddress; 80 81 82 public String doInput() throws Exception 83 { 84 this.repositories = RepositoryController.getController().getAuthorizedRepositoryVOList(this.getInfoGluePrincipal(), false); 85 86 return Action.INPUT; 87 } 88 89 public String doExecute() throws Exception 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 (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 componentStructure = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><components></components>"; 111 112 ContentVO metaInfoContentVO = ContentController.getContentController().getContentVOWithId(this.contentId); 113 Integer originalMetaInfoMasterLanguageId = LanguageController.getController().getMasterLanguage(metaInfoContentVO.getRepositoryId()).getId(); 114 Integer 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 String 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 is = null; 134 File file = null; 135 136 try 137 { 138 MultiPartRequestWrapper mpr = ActionContext.getContext().getMultiPartRequest(); 139 logger.info("mpr:" + mpr); 140 if(mpr != null) 141 { 142 Enumeration names = mpr.getFileNames(); 143 while (names.hasMoreElements()) 144 { 145 String name = (String )names.nextElement(); 146 String contentType = mpr.getContentType(name); 147 String 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 fileName = fileSystemName; 155 fileName = new VisualFormatter().replaceNonAscii(fileName, '_'); 156 157 String tempFileName = "tmp_" + System.currentTimeMillis() + "_" + fileName; 158 String 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 (new Long (file.length()).intValue())); 167 is = new FileInputStream (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 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 e){} 189 } 190 191 192 193 return Action.SUCCESS; 194 } 195 196 197 public Integer getTopRepositoryId() throws ConstraintException, SystemException, Bug 198 { 199 List repositories = RepositoryController.getController().getAuthorizedRepositoryVOList(this.getInfoGluePrincipal(), false); 200 201 Integer 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 hideLeafs) 215 { 216 this.hideLeafs = hideLeafs; 217 } 218 219 public String getHideLeafs() 220 { 221 return this.hideLeafs; 222 } 223 224 public String getTree() 225 { 226 return tree; 227 } 228 229 public void setTree(String tree) 230 { 231 this.tree = tree; 232 } 233 234 public void setParentContentId(Integer parentContentId) 235 { 236 this.parentContentId = parentContentId; 237 } 238 239 public Integer getParentContentId() 240 { 241 return this.parentContentId; 242 } 243 244 public List getRepositories() 245 { 246 return this.repositories; 247 } 248 249 public void setRepositoryId(Integer repositoryId) 250 { 251 this.repositoryId = repositoryId; 252 } 253 254 public Integer getRepositoryId() 255 { 256 try 257 { 258 if(this.repositoryId == null) 259 { 260 this.repositoryId = (Integer )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 e) 270 { 271 } 272 273 return repositoryId; 274 } 275 276 public void setContentId(Integer contentId) 277 { 278 this.contentId = contentId; 279 } 280 281 public Integer getContentId() 282 { 283 return this.contentId; 284 } 285 286 public String getReturnAddress() 287 { 288 return returnAddress; 289 } 290 291 public void setReturnAddress(String string) 292 { 293 returnAddress = string; 294 } 295 296 public Integer getSiteNodeId() 297 { 298 return siteNodeId; 299 } 300 301 public void setSiteNodeId(Integer siteNodeId) 302 { 303 this.siteNodeId = siteNodeId; 304 } 305 306 public String getName() 307 { 308 return name; 309 } 310 public void setName(String name) 311 { 312 this.name = name; 313 } 314 } 315 | Popular Tags |