1 18 package org.alfresco.web.bean; 19 20 import java.io.File ; 21 import java.io.Serializable ; 22 import java.text.MessageFormat ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.faces.context.FacesContext; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.action.executer.ImporterActionExecuter; 31 import org.alfresco.repo.content.MimetypeMap; 32 import org.alfresco.service.cmr.action.Action; 33 import org.alfresco.service.cmr.action.ActionService; 34 import org.alfresco.service.cmr.repository.ChildAssociationRef; 35 import org.alfresco.service.cmr.repository.ContentService; 36 import org.alfresco.service.cmr.repository.ContentWriter; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.repository.NodeService; 39 import org.alfresco.service.namespace.NamespaceService; 40 import org.alfresco.service.namespace.QName; 41 import org.alfresco.web.app.Application; 42 import org.alfresco.web.bean.repository.Repository; 43 import org.alfresco.web.ui.common.Utils; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 52 public class ImportBean 53 { 54 private static final Log logger = LogFactory.getLog(ImportBean.class); 55 56 private static final String DEFAULT_OUTCOME = "dialog:close"; 57 58 private static final String MSG_ERROR = "error_import"; 59 private static final String MSG_ERROR_NO_FILE = "error_import_no_file"; 60 private static final String MSG_ERROR_EMPTY_FILE = "error_import_empty_file"; 61 62 protected BrowseBean browseBean; 63 protected NodeService nodeService; 64 protected ActionService actionService; 65 protected ContentService contentService; 66 67 private File file; 68 private String fileName; 69 private String encoding = "UTF-8"; 70 private boolean runInBackground = true; 71 72 77 public String performImport() 78 { 79 String outcome = DEFAULT_OUTCOME; 80 81 if (logger.isDebugEnabled()) 82 logger.debug("Called import for file: " + this.file); 83 84 if (this.file != null && this.file.exists()) 85 { 86 if (this.file.length() > 0) 88 { 89 UserTransaction tx = null; 90 91 try 92 { 93 FacesContext context = FacesContext.getCurrentInstance(); 94 tx = Repository.getUserTransaction(context); 95 tx.begin(); 96 97 NodeRef acpNodeRef = addACPToRepository(context); 99 100 Map <String , Serializable > params = new HashMap <String , Serializable >(3); 102 params.put(ImporterActionExecuter.PARAM_DESTINATION_FOLDER, this.browseBean.getActionSpace().getNodeRef()); 103 params.put(ImporterActionExecuter.PARAM_ENCODING, this.encoding); 104 105 Action action = this.actionService.createAction(ImporterActionExecuter.NAME, params); 107 action.setExecuteAsynchronously(this.runInBackground); 108 109 this.actionService.executeAction(action, acpNodeRef); 111 112 if (logger.isDebugEnabled()) 113 { 114 logger.debug("Executed import action with action params of " + params); 115 } 116 117 tx.commit(); 119 120 reset(); 122 } 123 catch (Throwable e) 124 { 125 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 127 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 128 FacesContext.getCurrentInstance(), MSG_ERROR), e.toString()), e); 129 outcome = null; 130 } 131 } 132 else 133 { 134 Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_EMPTY_FILE)); 135 outcome = null; 136 } 137 } 138 else 139 { 140 Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_NO_FILE)); 141 outcome = null; 142 } 143 144 return outcome; 145 } 146 147 152 public String cancel() 153 { 154 reset(); 155 156 return DEFAULT_OUTCOME; 157 } 158 159 162 public void reset() 163 { 164 this.file = null; 165 this.fileName = null; 166 this.runInBackground = true; 167 168 if (this.file != null) 170 { 171 this.file.delete(); 172 } 173 174 FacesContext ctx = FacesContext.getCurrentInstance(); 176 ctx.getExternalContext().getSessionMap().remove(FileUploadBean.FILE_UPLOAD_BEAN_NAME); 177 } 178 179 182 public String getFileUploadSuccessMsg() 183 { 184 String msg = Application.getMessage(FacesContext.getCurrentInstance(), "file_upload_success"); 185 return MessageFormat.format(msg, new Object [] {getFileName()}); 186 } 187 188 191 public String getFileName() 192 { 193 FacesContext ctx = FacesContext.getCurrentInstance(); 196 FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap(). 197 get(FileUploadBean.FILE_UPLOAD_BEAN_NAME); 198 if (fileBean != null) 199 { 200 this.fileName = fileBean.getFileName(); 201 this.file = fileBean.getFile(); 202 } 203 204 return this.fileName; 205 } 206 207 212 public String getEncoding() 213 { 214 return this.encoding; 215 } 216 217 222 public void setEncoding(String encoding) 223 { 224 this.encoding = encoding; 225 } 226 227 232 public boolean getRunInBackground() 233 { 234 return this.runInBackground; 235 } 236 237 242 public void setRunInBackground(boolean runInBackground) 243 { 244 this.runInBackground = runInBackground; 245 } 246 247 252 public void setBrowseBean(BrowseBean browseBean) 253 { 254 this.browseBean = browseBean; 255 } 256 257 262 public void setActionService(ActionService actionService) 263 { 264 this.actionService = actionService; 265 } 266 267 272 public void setNodeService(NodeService nodeService) 273 { 274 this.nodeService = nodeService; 275 } 276 277 282 public void setContentService(ContentService contentService) 283 { 284 this.contentService = contentService; 285 } 286 287 293 private NodeRef addACPToRepository(FacesContext context) 294 { 295 Map <QName, Serializable > contentProps = new HashMap <QName, Serializable >(1); 297 contentProps.put(ContentModel.PROP_NAME, this.fileName); 298 299 String assocName = QName.createValidLocalName(this.fileName); 301 ChildAssociationRef assocRef = this.nodeService.createNode( 302 this.browseBean.getActionSpace().getNodeRef(), ContentModel.ASSOC_CONTAINS, 303 QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, assocName), 304 ContentModel.TYPE_CONTENT, contentProps); 305 306 NodeRef acpNodeRef = assocRef.getChildRef(); 307 308 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(3, 1.0f); 310 titledProps.put(ContentModel.PROP_TITLE, this.fileName); 311 titledProps.put(ContentModel.PROP_DESCRIPTION, Application.getMessage(context, "import_package_description")); 312 this.nodeService.addAspect(acpNodeRef, ContentModel.ASPECT_TITLED, titledProps); 313 314 ContentWriter writer = this.contentService.getWriter(acpNodeRef, ContentModel.PROP_CONTENT, true); 316 writer.setEncoding(this.encoding); 317 writer.setMimetype(MimetypeMap.MIMETYPE_ACP); 318 writer.putContent(this.file); 319 320 return acpNodeRef; 321 } 322 } 323 | Popular Tags |