RM-230: RM Email Mapping tool behaves strangely

* Added custom email service unit test
 * refactored service implementation



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.0@36754 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2012-05-23 08:14:23 +00:00
parent 5e1a56c595
commit e2ef37c0da
5 changed files with 395 additions and 284 deletions

View File

@@ -82,7 +82,7 @@ public class RecordsManagementBootstrap extends AbstractLifecycleBean
caveatConfigService.init();
// initialise custom email mapping
customEmailMappingService.init();
//customEmailMappingService.init();
// Initialise the custom model
adminService.initialiseCustomModel();

View File

@@ -20,13 +20,31 @@ package org.alfresco.module.org_alfresco_module_rm.email;
import java.util.Set;
/**
* Custom EMail Mapping Service
*/
public interface CustomEmailMappingService
{
/**
* Get the list of custom mappings
*
* @return {@link Set}<{@link CustomMapping}>
*/
public Set<CustomMapping> getCustomMappings();
/**
* Add custom mapping
*
* @param from
* @param to
*/
public void addCustomMapping(String from, String to);
/**
* Delete custom mapping
*
* @param from
* @param to
*/
public void deleteCustomMapping(String from, String to);
public void init();
}

View File

@@ -18,20 +18,20 @@
*/
package org.alfresco.module.org_alfresco_module_rm.email;
import java.util.Collections;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.repo.content.ContentServicePolicies;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.metadata.RFC822MetadataExtracter;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
@@ -39,6 +39,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespacePrefixResolver;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.logging.Log;
@@ -47,85 +48,213 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.context.ApplicationEvent;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
public class CustomEmailMappingServiceImpl implements CustomEmailMappingService
/**
* Custom Email Mapping Service
*/
public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean implements CustomEmailMappingService
{
/** Logger */
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(CustomEmailMappingServiceImpl.class);
/** Node reference's to configuration elements */
private static final NodeRef CONFIG_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "rm_emailmapping_config");
private static final NodeRef CONFIG_FOLDER_NODE_REF = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "rm_config_folder");
/** Config file name */
private static final String CONFIG_NAME = "imapConfig.json";
/** Default custom mappings (TODO move to spring config) */
private static final CustomMapping[] DEFAULT_MAPPINGS =
{
new CustomMapping("Date", "rma:dateReceived"),
new CustomMapping("messageTo", "rma:address"),
new CustomMapping("messageFrom", "rma:originator"),
new CustomMapping("messageSent", "rma:publicationDate"),
new CustomMapping("messageCc", "rma:otherAddress")
};
/** Extractor */
private RFC822MetadataExtracter extracter;
/** Services */
private NodeService nodeService;
private NamespacePrefixResolver nspr;
@SuppressWarnings("unused")
private PolicyComponent policyComponent;
private ContentService contentService;
private TransactionService transactionService;
private Set<CustomMapping> customMappings = Collections.synchronizedSet(new HashSet<CustomMapping>());
private static Log logger = LogFactory.getLog(CustomEmailMappingServiceImpl.class);
/** Transient set of custom mappings */
private Set<CustomMapping> customMappings;
/**
* @param policyComponent policy component
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* Get the name space prefix resolver
* @return the name space prefix resolver
*/
public NamespacePrefixResolver getNamespacePrefixResolver()
{
return nspr;
}
/**
* Set the name space prefix resolver
* @param nspr
* @param nspr namespace service
*/
public void setNamespacePrefixResolver(NamespacePrefixResolver nspr)
{
this.nspr = nspr;
}
/**
* @param extractor extractor component
*/
public void setExtracter(RFC822MetadataExtracter extractor)
{
this.extracter = extractor;
}
/**
* @param nodeService node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* @param contentService content service
*/
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
/**
* @param transactionService transaction service
*/
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
/**
*
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#getCustomMappings()
*/
public void init()
{
CustomMapping[] rmHardCodedMappings = {
new CustomMapping("Date", "rma:dateReceived"),
new CustomMapping("messageTo", "rma:address"),
new CustomMapping("messageFrom", "rma:originator"),
new CustomMapping("messageSent", "rma:publicationDate"),
new CustomMapping("messageCc", "rma:otherAddress")
};
NodeRef configNode = getConfigNode();
if(configNode != null)
{
/**
* Get any custom mappings.
*/
customMappings = readConfig(configNode);
}
/**
* ensure that the customMappings contain the RM specific mappings
*/
for(CustomMapping mapping : rmHardCodedMappings)
public Set<CustomMapping> getCustomMappings()
{
if (customMappings == null)
{
if(!customMappings.contains(mapping))
// if we have a config file
if (nodeService.exists(CONFIG_NODE_REF) == true)
{
customMappings.add(mapping);
// load the contents of the config file
customMappings = loadConfig();
}
else
{
customMappings = new HashSet<CustomMapping>();
// load the contents of the extractors property file
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
for(String key : currentMapping.keySet())
{
Set<QName> set = currentMapping.get(key);
for(QName qname : set)
{
CustomMapping value = new CustomMapping();
value.setFrom(key);
QName resolvedQname = qname.getPrefixedQName(nspr);
value.setTo(resolvedQname.toPrefixString());
customMappings.add(value);
}
}
// TODO
// if we have an old config file
{
// load the contents of the old config file
}
//else
{
// load the hard coded mappings
for(CustomMapping mapping : DEFAULT_MAPPINGS)
{
if(!customMappings.contains(mapping))
{
customMappings.add(mapping);
}
}
}
// create the config file
saveConfig(customMappings);
}
}
return customMappings;
}
// Get the read only existing configuration
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#addCustomMapping(java.lang.String, java.lang.String)
*/
public void addCustomMapping(String from, String to)
{
// create custom mapping
CustomMapping customMapping = new CustomMapping(from, to);
// check whether we already have this mapping or not
Set<CustomMapping> customMappings = getCustomMappings();
if (customMappings.contains(customMapping) == true)
{
throw new AlfrescoRuntimeException("Can not add custom email mapping, because duplicate mapping already exists.");
}
// else add the custom mapping (since we have already called getCustomMapping we can be sure
// the member variable is populated)
customMappings.add(customMapping);
// save the changes into the config file
saveConfig(customMappings);
// update the extractors configuration
updateExtractor();
}
Map<String, Set<QName>> newMapping = new HashMap<String, Set<QName>>(17);
newMapping.putAll(currentMapping);
for(CustomMapping mapping : customMappings)
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#deleteCustomMapping(java.lang.String, java.lang.String)
*/
public void deleteCustomMapping(String from, String to)
{
// create custom mapping
CustomMapping customMapping = new CustomMapping(from, to);
// check whether we already have this mapping or not
Set<CustomMapping> customMappings = getCustomMappings();
if (customMappings.contains(customMapping) == true)
{
// else remove the custom mapping (since we have already called getCustomMapping we can be sure
// the member variable is populated)
customMappings.remove(customMapping);
// save the changes into the config file
saveConfig(customMappings);
// update the extractors configuration
updateExtractor();
}
}
/**
* Updates the extractor with the custom configuration.
*/
private void updateExtractor()
{
// convert the mapping information into the form understood by the extractor
Map<String, Set<QName>> newMapping = new HashMap<String, Set<QName>>(17);
for(CustomMapping mapping : getCustomMappings())
{
QName newQName = QName.createQName(mapping.getTo(), nspr);
Set<QName> values = newMapping.get(mapping.getFrom());
@@ -137,159 +266,19 @@ public class CustomEmailMappingServiceImpl implements CustomEmailMappingService
values.add(newQName);
}
// Now update the metadata extracter
extracter.setMapping(newMapping);
// Register interest in the onContentUpdate policy
policyComponent.bindClassBehaviour(
ContentServicePolicies.OnContentUpdatePolicy.QNAME,
RecordsManagementModel.TYPE_EMAIL_CONFIG,
new JavaBehaviour(this, "onContentUpdate"));
// update the metadata extracter
extracter.setMapping(newMapping);
}
public void onContentUpdate(NodeRef nodeRef, boolean newContent)
{
NodeRef configNode = getConfigNode();
if(configNode != null)
{
Set<CustomMapping> newMappings = readConfig(configNode);
customMappings.addAll(newMappings);
for(CustomMapping mapping : customMappings)
{
if(!newMappings.contains(mapping))
{
customMappings.remove(mapping);
}
}
}
}
public void beforeDeleteNode(NodeRef nodeRef)
{
}
public void onCreateNode(ChildAssociationRef childAssocRef)
{
}
public Set<CustomMapping> getCustomMappings()
{
// add all the lists data to a Map
Set<CustomMapping> emailMap = new HashSet<CustomMapping>();
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
for(String key : currentMapping.keySet())
{
Set<QName> set = currentMapping.get(key);
for(QName qname : set)
{
CustomMapping value = new CustomMapping();
value.setFrom(key);
QName resolvedQname = qname.getPrefixedQName(nspr);
value.setTo(resolvedQname.toPrefixString());
emailMap.add(value);
}
}
return emailMap;
}
public void addCustomMapping(String from, String to)
{
// Get the read only existing configuration
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
Map<String, Set<QName>> newMapping = new HashMap<String, Set<QName>>(17);
newMapping.putAll(currentMapping);
QName newQName = QName.createQName(to, nspr);
Set<QName> values = newMapping.get(from);
if(values == null)
{
values = new HashSet<QName>();
newMapping.put(from, values);
}
values.add(newQName);
CustomMapping xxx = new CustomMapping();
xxx.setFrom(from);
xxx.setTo(to);
customMappings.add(xxx);
updateOrCreateEmailConfig(customMappings);
// Crash in the new config.
extracter.setMapping(newMapping);
}
public void deleteCustomMapping(String from, String to)
{
// Get the read only existing configuration
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
Map<String, Set<QName>> newMapping = new HashMap<String, Set<QName>>(17);
newMapping.putAll(currentMapping);
QName oldQName = QName.createQName(to, nspr);
Set<QName> values = newMapping.get(from);
if(values != null)
{
values.remove(oldQName);
}
CustomMapping toDelete = new CustomMapping(from, to);
customMappings.remove(toDelete);
updateOrCreateEmailConfig(customMappings);
// Crash in the new config.
extracter.setMapping(newMapping);
}
public void setExtracter(RFC822MetadataExtracter extractor)
{
this.extracter = extractor;
}
public RFC822MetadataExtracter getExtracter()
{
return extracter;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public NodeService getNodeService()
{
return nodeService;
}
// Default
private StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
private static final String CONFIG_NAME = "imapConfig.json";
/**
* Loads the custom mappings from the configuration file.
*
* @param nodeRef
* @return
* @return
*/
private Set<CustomMapping> readConfig(NodeRef nodeRef)
private Set<CustomMapping> loadConfig()
{
Set<CustomMapping> newMappings = new HashSet<CustomMapping>();
ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
Set<CustomMapping> result = new HashSet<CustomMapping>();
ContentReader cr = contentService.getReader(CONFIG_NODE_REF, ContentModel.PROP_CONTENT);
if (cr != null)
{
String text = cr.getContentString();
@@ -303,109 +292,95 @@ public class CustomEmailMappingServiceImpl implements CustomEmailMappingService
CustomMapping mapping = new CustomMapping();
mapping.setFrom(obj.getString("from"));
mapping.setTo(obj.getString("to"));
newMappings.add(mapping);
result.add(mapping);
}
return newMappings;
}
catch (JSONException je)
{
logger.warn("unable to read custom email configuration", je);
return newMappings;
}
throw new AlfrescoRuntimeException("Unable to read custom email configuration", je);
}
}
return newMappings;
}
public NodeRef updateOrCreateEmailConfig(Set<CustomMapping> customMappings)
{
NodeRef caveatConfig = updateOrCreateEmailConfig();
return result;
}
/**
*
* @param customMappingsToSave
*/
private void saveConfig(Set<CustomMapping> customMappingsToSave)
{
if (nodeService.exists(CONFIG_NODE_REF) == false)
{
// create the config node
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(2);
properties.put(ContentModel.PROP_NAME, CONFIG_NAME);
properties.put(ContentModel.PROP_NODE_UUID, CONFIG_NODE_REF.getId());
nodeService.createNode(
CONFIG_FOLDER_NODE_REF,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, CONFIG_NAME),
ContentModel.TYPE_CONTENT,
properties);
}
// build JSON array of mappings
JSONArray jsonMappings = new JSONArray();
try
{
JSONArray mappings = new JSONArray();
for(CustomMapping mapping : customMappings)
for(CustomMapping mapping : customMappingsToSave)
{
JSONObject obj = new JSONObject();
obj.put("from", mapping.getFrom());
obj.put("to", mapping.getTo());
mappings.put(obj);
jsonMappings.put(obj);
}
// Update the content
ContentWriter writer = this.contentService.getWriter(caveatConfig, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
writer.putContent(mappings.toString());
}
catch (JSONException je)
{
throw new AlfrescoRuntimeException("Unable to create JSON email mapping configuration during save.", je);
}
return caveatConfig;
}
public NodeRef updateOrCreateEmailConfig(String txt)
{
NodeRef caveatConfig = updateOrCreateEmailConfig();
// Update the content
ContentWriter writer = this.contentService.getWriter(caveatConfig, ContentModel.PROP_CONTENT, true);
// update the content
ContentWriter writer = this.contentService.getWriter(CONFIG_NODE_REF, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
writer.putContent(txt);
return caveatConfig;
writer.putContent(jsonMappings.toString());
}
private NodeRef updateOrCreateEmailConfig()
/**
* @see org.springframework.extensions.surf.util.AbstractLifecycleBean#onBootstrap(org.springframework.context.ApplicationEvent)
*/
@Override
protected void onBootstrap(ApplicationEvent event)
{
NodeRef caveatConfig = getConfigNode();
if (caveatConfig == null)
// run as System on bootstrap
AuthenticationUtil.runAs(new RunAsWork<Object>()
{
logger.debug("custom email configuration does not exist - creating new");
NodeRef rootNode = nodeService.getRootNode(storeRef);
//nodeService.addAspect(rootNode, VersionModel.ASPECT_VERSION_STORE_ROOT, null);
// Create caveat config
caveatConfig = nodeService.createNode(rootNode,
RecordsManagementModel.ASSOC_EMAIL_CONFIG,
QName.createQName(RecordsManagementModel.RM_URI, CONFIG_NAME),
RecordsManagementModel.TYPE_EMAIL_CONFIG).getChildRef();
nodeService.setProperty(caveatConfig, ContentModel.PROP_NAME, CONFIG_NAME);
}
return caveatConfig;
}
public NodeRef getConfigNode()
{
NodeRef rootNode = nodeService.getRootNode(storeRef);
return nodeService.getChildByName(rootNode, RecordsManagementModel.ASSOC_EMAIL_CONFIG, CONFIG_NAME);
public Object doWork()
{
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
// update the extractor with the custom mappings
updateExtractor();
return null;
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(callback);
return null;
}
}, AuthenticationUtil.getSystemUserName());
}
public void setContentService(ContentService contentService)
/**
* @see org.springframework.extensions.surf.util.AbstractLifecycleBean#onShutdown(org.springframework.context.ApplicationEvent)
*/
@Override
protected void onShutdown(ApplicationEvent arg0)
{
this.contentService = contentService;
}
public ContentService getContentService()
{
return contentService;
}
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
public TransactionService getTransactionService()
{
return transactionService;
}
// No implementation
}
}

View File

@@ -18,17 +18,26 @@
*/
package org.alfresco.module.org_alfresco_module_rm.email;
/**
* Custom EMail Mapping
*/
public class CustomMapping
{
private String from;
private String to;
/**
* Default constructor.
*/
public CustomMapping()
{
{
}
/**
* Default constructor.
* @param from
* @param to
*/
public CustomMapping(String from, String to)
{
this.from = from;

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.test.service;
import java.util.Set;
import org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService;
import org.alfresco.module.org_alfresco_module_rm.email.CustomMapping;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase;
/**
* Custom EMail Mapping Service
*
* @author Roy Wetherall
* @since 2.0
*/
public class CustomEMailMappingServiceServiceImplTest extends BaseRMTestCase
{
private CustomEmailMappingService eMailMappingService;
@Override
protected void initServices()
{
super.initServices();
eMailMappingService = (CustomEmailMappingService)applicationContext.getBean("customEmailMappingService");
}
@Override
protected boolean isUserTest()
{
return true;
}
public void testCRUD() throws Exception
{
doTestInTransaction(new Test<Void>()
{
public Void run()
{
checkCustomMappingSize(20);
eMailMappingService.addCustomMapping("monkey", "cm:monkeyFace");
CustomMapping monkeyMapping = getCustomMapping("monkey", "cm:monkeyFace", checkCustomMappingSize(21), true);
assertNotNull(monkeyMapping);
assertEquals("monkey", monkeyMapping.getFrom());
assertEquals("cm:monkeyFace", monkeyMapping.getTo());
eMailMappingService.deleteCustomMapping("monkey", "cm:monkeyFace");
getCustomMapping("monkey", "cm:monkeyFace", checkCustomMappingSize(20), false);
return null;
}
}, rmAdminName);
}
private CustomMapping getCustomMapping(String from, String to, Set<CustomMapping> maps, boolean contains)
{
CustomMapping result = null;
CustomMapping key = new CustomMapping(from, to);
assertEquals(contains, maps.contains(key));
if (contains == true)
{
for (CustomMapping map : maps)
{
if (map.equals(key) == true)
{
result = key;
break;
}
}
}
return result;
}
private Set<CustomMapping> checkCustomMappingSize(int expected)
{
Set<CustomMapping> maps = eMailMappingService.getCustomMappings();
assertEquals(expected, maps.size());
return maps;
}
@SuppressWarnings("unused")
private void print(Set<CustomMapping> maps)
{
for (CustomMapping map : maps)
{
System.out.println(map.getFrom() + " -> " + map.getTo());
}
}
}