From 92749700ae66c6438614158cfcd6f68e0530f340 Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Thu, 24 May 2012 06:00:37 +0000 Subject: [PATCH] RM-104: Split email attachments doesn't work git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.0@36847 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../rm-action-context.xml | 2 +- .../RecordsManagementAdminServiceImpl.java | 3 +- .../action/impl/SplitEmailAction.java | 124 +++++++----------- .../bootstrap/RecordsManagementBootstrap.java | 8 +- 4 files changed, 52 insertions(+), 85 deletions(-) diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-action-context.xml b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-action-context.xml index 190450be0b..bd90339ffb 100644 --- a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-action-context.xml +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-action-context.xml @@ -726,7 +726,7 @@ - + diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/RecordsManagementAdminServiceImpl.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/RecordsManagementAdminServiceImpl.java index 0b15d38b5d..1f02c05ccd 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/RecordsManagementAdminServiceImpl.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/RecordsManagementAdminServiceImpl.java @@ -945,8 +945,7 @@ public class RecordsManagementAdminServiceImpl implements RecordsManagementAdmin */ public Map getCustomReferenceDefinitions() { - QName relevantAspectQName = QName.createQName(RMC_CUSTOM_ASSOCS, namespaceService); - AspectDefinition aspectDefn = dictionaryService.getAspect(relevantAspectQName); + AspectDefinition aspectDefn = dictionaryService.getAspect(ASPECT_CUSTOM_ASSOCIATIONS); Map assocDefns = aspectDefn.getAssociations(); return assocDefns; diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/SplitEmailAction.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/SplitEmailAction.java index 74f6737eb7..9523e21558 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/SplitEmailAction.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/SplitEmailAction.java @@ -33,14 +33,13 @@ import javax.mail.Part; import javax.mail.internet.ContentType; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; -import javax.transaction.UserTransaction; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.model.ImapModel; import org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase; -import org.alfresco.repo.transaction.RetryingTransactionHelper; -import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.repository.AssociationRef; @@ -68,11 +67,36 @@ public class SplitEmailAction extends RMActionExecuterAbstractBase private static final String MSG_NO_READ_MIME_MESSAGE = "rm.action.no-read-mime-message"; private static final String MSG_EMAIL_DECLARED = "rm.action.email-declared"; private static final String MSG_EMAIL_NOT_RECORD = "rm.action.email-not-record"; - private static final String MSG_EMAIL_CREATE_CHILD_ASSOC = "rm.action.email-create-child-assoc"; + + /** Relationship Labels */ + private static final String REL_FROM = "Message"; + private static final String REL_TO = "Attachment"; /** Logger */ private static Log logger = LogFactory.getLog(SplitEmailAction.class); + private QName relationshipQName; + + public void bootstrap() + { + String compoundId = recordsManagementAdminService.getCompoundIdFor(REL_FROM, REL_TO); + + Map map = recordsManagementAdminService.getCustomReferenceDefinitions(); + for (Map.Entry entry : map.entrySet()) + { + if (compoundId.equals(entry.getValue().getTitle()) == true) + { + relationshipQName = entry.getKey(); + break; + } + } + + if (relationshipQName == null) + { + relationshipQName = recordsManagementAdminService.addCustomChildAssocDefinition(REL_FROM, REL_TO); + } + } + /** * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, * org.alfresco.service.cmr.repository.NodeRef) @@ -242,85 +266,27 @@ public class SplitEmailAction extends RMActionExecuterAbstractBase } - QName assocDef = null; - /** * Create a link from the message to the attachment */ - private void createRMReference(NodeRef parentRef, NodeRef childRef) + private void createRMReference(final NodeRef parentRef, final NodeRef childRef) { - String sourceId = "message"; - String targetId = "attachment"; - - String compoundId = recordsManagementAdminService.getCompoundIdFor(sourceId, targetId); - - Map refs = recordsManagementAdminService.getCustomReferenceDefinitions(); - for(QName name : refs.keySet()) + AuthenticationUtil.runAsSystem(new RunAsWork() { - // TODO how to find assocDef? - // Refs seems to be null - } - - if(assocDef == null) - { - assocDef = createReference(); - } - - recordsManagementAdminService.addCustomReference(parentRef, childRef, assocDef); - - // add the IMAP attachment aspect - nodeService.createAssociation( - parentRef, - childRef, - ImapModel.ASSOC_IMAP_ATTACHMENT); - } - - /** - * Create the custom reference - need to jump through hoops with the transaction handling here - * since the association is created in the post commit phase, so it can't be used within the - * current transaction. - * - * @return - */ - private QName createReference() - { - UserTransaction txn = null; - - try - { - txn = transactionService.getNonPropagatingUserTransaction(); - txn.begin(); - RetryingTransactionHelper helper = transactionService.getRetryingTransactionHelper(); - RetryingTransactionCallback addCustomChildAssocDefinitionCallback = new RetryingTransactionCallback() + @Override + public Void doWork() throws Exception { - public QName execute() throws Throwable - { - String sourceId = "message"; - String targetId = "attachment"; - QName assocDef = recordsManagementAdminService.addCustomChildAssocDefinition(sourceId, targetId); - return assocDef; - } - }; - QName ret = helper.doInTransaction(addCustomChildAssocDefinitionCallback); - - txn.commit(); - return ret; - } - catch (Exception e) - { - if(txn != null) - { - try - { - txn.rollback(); - } - catch (Exception se) - { - logger.error("error during creation of custom child association", se); - // we can do nothing with this rollback exception. - } - } - throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_EMAIL_CREATE_CHILD_ASSOC), e); - } - } + // add the relationship + recordsManagementAdminService.addCustomReference(parentRef, childRef, relationshipQName); + + // add the IMAP attachment aspect + nodeService.createAssociation( + parentRef, + childRef, + ImapModel.ASSOC_IMAP_ATTACHMENT); + + return null; + } + }); + } } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/bootstrap/RecordsManagementBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/bootstrap/RecordsManagementBootstrap.java index 35c1073f57..347ab9db06 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/bootstrap/RecordsManagementBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/bootstrap/RecordsManagementBootstrap.java @@ -19,6 +19,7 @@ package org.alfresco.module.org_alfresco_module_rm.bootstrap; import org.alfresco.module.org_alfresco_module_rm.RecordsManagementAdminService; +import org.alfresco.module.org_alfresco_module_rm.action.impl.SplitEmailAction; import org.alfresco.module.org_alfresco_module_rm.caveat.RMCaveatConfigService; import org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService; import org.alfresco.repo.security.authentication.AuthenticationUtil; @@ -81,12 +82,13 @@ public class RecordsManagementBootstrap extends AbstractLifecycleBean // initialise caveat config caveatConfigService.init(); - // initialise custom email mapping - //customEmailMappingService.init(); - // Initialise the custom model adminService.initialiseCustomModel(); + // Initialise the SplitEmailAction + SplitEmailAction action = (SplitEmailAction)getApplicationContext().getBean("splitEmail"); + action.bootstrap(); + return null; } };