Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.zylk.action.executer;
- import org.alfresco.error.AlfrescoRuntimeException;
- import org.alfresco.model.ContentModel;
- import org.alfresco.repo.action.ParameterDefinitionImpl;
- import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
- import org.alfresco.repo.security.authentication.AuthenticationUtil;
- import org.alfresco.repo.transaction.RetryingTransactionHelper;
- import org.alfresco.service.cmr.action.Action;
- import org.alfresco.service.cmr.action.ParameterDefinition;
- import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
- import org.alfresco.service.cmr.model.*;
- import org.alfresco.service.cmr.repository.*;
- import org.apache.log4j.Logger;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class Folderizer extends ActionExecuterAbstractBase {
- private static Logger log = Logger.getLogger(Folderizer.class);
- protected NodeService nodeService;
- protected FileFolderService fileFolderService;
- protected RetryingTransactionHelper retryingTransactionHelper;
- public static final String NAME = "zylk-folderizer-action";
- public static final String PARAM_DESTINATION_FOLDER = "destination-folder";
- public static final String PARAM_DATE_PATTERN = "date-pattern";
- public Folderizer() {
- super();
- log.warn("---------- Folderizer INIT -------");
- }
- @Override
- protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
- paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER,
- DataTypeDefinition.NODE_REF,
- false,
- getParamDisplayLabel(PARAM_DESTINATION_FOLDER)));
- paramList.add(new ParameterDefinitionImpl(PARAM_DATE_PATTERN,
- DataTypeDefinition.TEXT,
- false,
- getParamDisplayLabel(PARAM_DATE_PATTERN)));
- }
- @Override
- protected void executeImpl(final Action action, final NodeRef actionedUponNodeRef) {
- final String LOG_FOLDERIZER_HEADER = "[ZK] " + String.valueOf(UUID.randomUUID()).substring(0, 2) + " ";
- try {
- // get rule params
- final NodeRef destinationFolder = (NodeRef) action.getParameterValue(PARAM_DESTINATION_FOLDER);
- log.warn(LOG_FOLDERIZER_HEADER + "Destination Folder: " + fileFolderService.getFileInfo(destinationFolder).getName());
- final String datePathPattern = action.getParameterValue(PARAM_DATE_PATTERN).toString();
- log.warn(LOG_FOLDERIZER_HEADER + "Date Pattern: " + datePathPattern);
- // uploaded file info trace
- System.out.printf(LOG_FOLDERIZER_HEADER + "executeImpl");
- log.warn(LOG_FOLDERIZER_HEADER + "Uploaded file: " + actionedUponNodeRef);
- final FileInfo updateFile = fileFolderService.getFileInfo(actionedUponNodeRef);
- logUploadFile(LOG_FOLDERIZER_HEADER, updateFile);
- final Date creationDate = (Date) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CREATED);
- log.warn(LOG_FOLDERIZER_HEADER + "Creation date:" + creationDate);
- // create destination folder
- log.info(LOG_FOLDERIZER_HEADER + "@@@@@ op1. createIfNotExists - retrying transaction ");
- NodeRef destinationDateFolder = createFolderIfNotExistingByDatePattern(destinationFolder, datePathPattern, creationDate, LOG_FOLDERIZER_HEADER);
- log.warn(LOG_FOLDERIZER_HEADER + "Final destination folder: " + destinationDateFolder);
- if (destinationDateFolder != null && fileFolderService.exists(destinationDateFolder)) {
- log.info(LOG_FOLDERIZER_HEADER + "@@@@@ op2. move - retrying transaction ");
- FileInfo movedFile = fileFolderService.move(actionedUponNodeRef, destinationDateFolder, null);
- logUploadFile(LOG_FOLDERIZER_HEADER, movedFile);
- log.info(LOG_FOLDERIZER_HEADER + "File folderized : " + movedFile.getNodeRef());
- log.info(LOG_FOLDERIZER_HEADER + "File folderized : " + movedFile);
- } else {
- log.error(LOG_FOLDERIZER_HEADER + "Destination folder not created");
- }
- }catch (FileNotFoundException e){
- log.error(LOG_FOLDERIZER_HEADER + "FileNotFoundException", e);
- throw new RuntimeException(e);
- }catch(Exception e){
- log.error(LOG_FOLDERIZER_HEADER + "Exception", e);
- throw e;
- } catch (Throwable t) {
- log.error(LOG_FOLDERIZER_HEADER + "Throwable", t);
- throw t;
- }
- }
- private void logUploadFile(String LOG_FOLDERIZER_HEADER, FileInfo file) {
- log.warn("------------------------------------------------------------------------");
- log.warn(LOG_FOLDERIZER_HEADER + "Executing Action for: " + file.getName());
- log.warn(LOG_FOLDERIZER_HEADER + "UpdateFileInfo name= " + file.getName());
- log.warn(LOG_FOLDERIZER_HEADER + "UpdateFileInfo noderef= " + file.getNodeRef());
- log.warn(LOG_FOLDERIZER_HEADER + "UpdateFileInfo createdDate= " + file.getCreatedDate());
- log.warn("------------------------------------------------------------------------");
- }
- private NodeRef createFolderIfNotExistingByDatePattern(NodeRef parent, String datePathPattern, Date creationDate, String LOG_FOLDERIZER_HEADER) {
- String formattedDate;
- try {
- SimpleDateFormat formatter = new SimpleDateFormat(datePathPattern, Locale.ENGLISH);
- formattedDate = formatter.format(creationDate);
- } catch (IllegalArgumentException exInvalid) {
- throw new AlfrescoRuntimeException("zylk-folderizer-action.label.error.invalidPattern");
- } catch (NullPointerException exNull) {
- throw new AlfrescoRuntimeException("zylk-folderizer-action.label.error.nullCreationDate");
- }
- List<String> structuredFolders = Arrays.asList(formattedDate.split("/"));
- FileInfo lastPathFolder = FileFolderUtil.makeFolders(fileFolderService, parent, structuredFolders, ContentModel.TYPE_FOLDER);
- log.info(LOG_FOLDERIZER_HEADER + "post makeFolders | " + lastPathFolder.toString());
- return lastPathFolder.getNodeRef();
- }
- public void setNodeService(NodeService nodeService) {
- this.nodeService = nodeService;
- }
- public void setFileFolderService(FileFolderService fileFolderService) {
- this.fileFolderService = fileFolderService;
- }
- public void setRetryingTransactionHelper(RetryingTransactionHelper retryingTransactionHelper) {
- this.retryingTransactionHelper = retryingTransactionHelper;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement