Merged methods from the EmailMappingKeyService to CustomEmailMappingService and deleted the EMailMappingKeyService.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@44324 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2012-12-04 16:13:30 +00:00
parent 414fc1a5f3
commit 4b8b65d023
9 changed files with 162 additions and 272 deletions

View File

@@ -18,6 +18,7 @@
*/
package org.alfresco.module.org_alfresco_module_rm.email;
import java.util.List;
import java.util.Set;
/**
@@ -27,24 +28,38 @@ 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);
/**
* Gets the list of email mapping keys
*
* @return Email mapping keys
*/
public List<String> getEmailMappingKeys();
/**
* Registers an email mapping key with the existing list of email mapping keys
*
* @param emailMappingKey emailMappingKey to register
*/
public void registerEMailMappingKey(String emailMappingKey);
}

View File

@@ -21,6 +21,7 @@ package org.alfresco.module.org_alfresco_module_rm.email;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -43,6 +44,7 @@ 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.alfresco.util.ParameterCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
@@ -58,28 +60,28 @@ import org.springframework.extensions.surf.util.AbstractLifecycleBean;
public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean implements CustomEmailMappingService
{
/** Logger */
private static Log logger = LogFactory.getLog(CustomEmailMappingServiceImpl.class);
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 =
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;
@@ -87,10 +89,13 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
private PolicyComponent policyComponent;
private ContentService contentService;
private TransactionService transactionService;
/** Transient set of custom mappings */
private Set<CustomMapping> customMappings;
/** List of email mapping keys */
private List<String> emailMappingKeys;
/**
* @param policyComponent policy component
*/
@@ -138,7 +143,15 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
{
this.transactionService = transactionService;
}
/**
* @param emailMappingKeys email mapping keys
*/
public void setEmailMappingKeys(List<String> emailMappingKeys)
{
this.emailMappingKeys = emailMappingKeys;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#getCustomMappings()
*/
@@ -154,24 +167,24 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
}
else
{
customMappings = new HashSet<CustomMapping>();
customMappings = new HashSet<CustomMapping>();
// load the contents of the extractors property file
Map<String, Set<QName>> currentMapping = extracter.getCurrentMapping();
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());
value.setTo(resolvedQname.toPrefixString());
customMappings.add(value);
}
}
// if we have an old config file
NodeRef oldConfigNode = getOldConfigNode();
if (oldConfigNode != null)
@@ -180,18 +193,18 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
Set<CustomMapping> oldMappings = readOldConfig(oldConfigNode);
customMappings.addAll(oldMappings);
}
// load the hard coded mappings
for(CustomMapping mapping : DEFAULT_MAPPINGS)
{
customMappings.add(mapping);
}
customMappings.add(mapping);
}
// create the config file
saveConfig(customMappings);
}
}
return customMappings;
}
@@ -200,23 +213,23 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
*/
public void addCustomMapping(String from, String to)
{
// create custom mapping
// 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
// 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();
}
@@ -226,32 +239,52 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
*/
public void deleteCustomMapping(String from, String to)
{
// create custom mapping
// 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
{
// 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();
}
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#getEmailMappingKeys()
*/
@Override
public List<String> getEmailMappingKeys()
{
return emailMappingKeys;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.EmailMappingKeyService#registerEMailMappingKey(java.lang.String)
*/
@Override
public void registerEMailMappingKey(String emailMappingKey)
{
ParameterCheck.mandatoryString("emailMappingKey", emailMappingKey);
emailMappingKeys.add(emailMappingKey);
}
/**
* 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);
Map<String, Set<QName>> newMapping = new HashMap<String, Set<QName>>(17);
for(CustomMapping mapping : getCustomMappings())
{
QName newQName = QName.createQName(mapping.getTo(), nspr);
@@ -265,13 +298,13 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
}
// update the metadata extracter
extracter.setMapping(newMapping);
extracter.setMapping(newMapping);
}
/**
* Loads the custom mappings from the configuration file.
*
* @return
*
* @return
*/
private Set<CustomMapping> loadConfig()
{
@@ -280,9 +313,9 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
if (cr != null)
{
String text = cr.getContentString();
try
{
{
JSONArray jsonArray = new JSONArray(new JSONTokener(text));
for(int i = 0 ; i < jsonArray.length(); i++)
{
@@ -296,14 +329,14 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
catch (JSONException je)
{
throw new AlfrescoRuntimeException("Unable to read custom email configuration", je);
}
}
}
return result;
}
/**
*
*
* @param customMappingsToSave
*/
private void saveConfig(Set<CustomMapping> customMappingsToSave)
@@ -315,13 +348,13 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
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,
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
@@ -338,14 +371,14 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
{
throw new AlfrescoRuntimeException("Unable to create JSON email mapping configuration during save.", je);
}
// 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(jsonMappings.toString());
writer.putContent(jsonMappings.toString());
}
/**
* @see org.springframework.extensions.surf.util.AbstractLifecycleBean#onBootstrap(org.springframework.context.ApplicationEvent)
*/
@@ -373,10 +406,10 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
{
logger.warn(e.getMessage());
}
// reset the mappings
customMappings = null;
// rethrow
throw e;
}
@@ -384,7 +417,7 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(callback);
return null;
}
}, AuthenticationUtil.getSystemUserName());
@@ -398,13 +431,13 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
{
// No implementation
}
/**
* Helper method to get the old configuration node. This is used during the migration
* from 1.0 to 2.0.
* <p>
* Returns null if it does not exist.
*
*
* @return {@link NodeRef} node reference of the old configuration node, null otherwise
*/
private NodeRef getOldConfigNode()
@@ -412,24 +445,24 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
NodeRef rootNode = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
return nodeService.getChildByName(rootNode, RecordsManagementModel.ASSOC_EMAIL_CONFIG, CONFIG_NAME);
}
/**
* Reads the old configuration node. This is used during the migration from 1.0 to 2.0.
*
*
* @param nodeRef the old configuration node reference
* @return {@link Set}<{@link CustomMapping}> set of the custom mappings stored in the old configuration
*/
private Set<CustomMapping> readOldConfig(NodeRef nodeRef)
{
Set<CustomMapping> newMappings = new HashSet<CustomMapping>();
ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
if (cr != null)
{
String text = cr.getContentString();
try
{
{
JSONArray jsonArray = new JSONArray(new JSONTokener(text));
for(int i = 0 ; i < jsonArray.length(); i++)
{
@@ -446,8 +479,8 @@ public class CustomEmailMappingServiceImpl extends AbstractLifecycleBean impleme
logger.warn("unable to read custom email configuration", je);
return newMappings;
}
}
return newMappings;
}
}
}

View File

@@ -31,8 +31,8 @@ public class CustomisableEmailMappingKeyBootstrap
/** List of mappings to register as customisable */
private List<String> customisable;
/** Email mapping key service */
private EmailMappingKeyService emailMappingKeyService;
/** Custom email mapping service */
private CustomEmailMappingService customEmailMappingService;
/**
* @param customizable list of mappings to register as customisable
@@ -43,13 +43,13 @@ public class CustomisableEmailMappingKeyBootstrap
}
/**
* Email mapping key service
* Custom email mapping service
*
* @param emailMappingKeyService the email mapping key service
* @param customEmailMappingService the custom email mapping service
*/
public void setEmailMappingKeyService(EmailMappingKeyService emailMappingKeyService)
public void setCustomEmailMappingService(CustomEmailMappingService customEmailMappingService)
{
this.emailMappingKeyService = emailMappingKeyService;
this.customEmailMappingService = customEmailMappingService;
}
/**
@@ -59,7 +59,7 @@ public class CustomisableEmailMappingKeyBootstrap
{
for (String customEmailMappingKey : customisable)
{
emailMappingKeyService.makeCustomisable(customEmailMappingKey);
customEmailMappingService.registerEMailMappingKey(customEmailMappingKey);
}
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright (C) 2005-2012 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.email;
import java.util.List;
/**
* EMail Mapping Key Service
*
* @author Tuna Aksoy
* @since @2.1
*/
public interface EmailMappingKeyService
{
/**
* Gets the list of email mapping keys
*
* @return Email mapping keys
*/
public List<String> getEmailMappingKeys();
/**
* Makes a email mapping key customisable.
*
* @param emailMappingKey emailMappingKey to make customisable
*/
public void makeCustomisable(String emailMappingKey);
}

View File

@@ -1,59 +0,0 @@
/*
* Copyright (C) 2005-2012 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.email;
import java.util.List;
import org.alfresco.util.ParameterCheck;
/**
* EMail Mapping Key Service
*
* @author Tuna Aksoy
* @since 2.1
*/
public class EmailMappingKeyServiceImpl implements EmailMappingKeyService
{
List<String> emailMappingKeys;
public void setEmailMappingKeys(List<String> emailMappingKeys)
{
this.emailMappingKeys = emailMappingKeys;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService#getEmailMappingKeys()
*/
@Override
public List<String> getEmailMappingKeys()
{
return emailMappingKeys;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.email.EmailMappingKeyService#makeCustomisable(java.lang.String)
*/
@Override
public void makeCustomisable(String emailMappingKey)
{
ParameterCheck.mandatoryString("emailMappingKey", emailMappingKey);
emailMappingKeys.add(emailMappingKey);
}
}

View File

@@ -21,7 +21,7 @@ package org.alfresco.module.org_alfresco_module_rm.script;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.email.EmailMappingKeyService;
import org.alfresco.module.org_alfresco_module_rm.email.CustomEmailMappingService;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
@@ -35,17 +35,17 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/
public class EmailMapKeysGet extends DeclarativeWebScript
{
/** Email mapping key service */
private EmailMappingKeyService emailMappingKeyService;
/** Custom email mapping service */
private CustomEmailMappingService customEmailMappingService;
/**
* Email mapping key service
* Custom email mapping service
*
* @param emailMappingKeyService the email mapping key service
* @param customEmailMappingService the custom email mapping service
*/
public void setEmailMappingKeyService(EmailMappingKeyService emailMappingKeyService)
public void setCustomEmailMappingService(CustomEmailMappingService customEmailMappingService)
{
this.emailMappingKeyService = emailMappingKeyService;
this.customEmailMappingService = customEmailMappingService;
}
@Override
@@ -53,7 +53,7 @@ public class EmailMapKeysGet extends DeclarativeWebScript
{
// Create model object with the lists of email mapping keys
Map<String, Object> model = new HashMap<String, Object>(1);
model.put("emailmapkeys", emailMappingKeyService.getEmailMappingKeys());
model.put("emailmapkeys", customEmailMappingService.getEmailMappingKeys());
return model;
}
}