RM-1641 (Create Relationship Service)

* Refactored the existing customReferenceDefinition classes

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@85423 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2014-09-22 09:50:24 +00:00
parent f32accb46f
commit a26b8710fd
6 changed files with 622 additions and 391 deletions

View File

@@ -62,27 +62,26 @@
<property name="recordsManagementAdminService" ref="RecordsManagementAdminService" /> <property name="recordsManagementAdminService" ref="RecordsManagementAdminService" />
</bean> </bean>
<!-- REST impl for GET Custom Reference Definitions --> <!-- Base bean for custom reference definition web scripts -->
<bean id="webscript.org.alfresco.rma.customrefdefinitions.get" <bean id="rmCustomReferenceDefinitionBase" parent="rmBaseWebscript" abstract="true">
class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionsGet"
parent="webscript">
<property name="recordsManagementAdminService" ref="RecordsManagementAdminService" /> <property name="recordsManagementAdminService" ref="RecordsManagementAdminService" />
<property name="dictionaryService" ref="DictionaryService" /> <property name="dictionaryService" ref="DictionaryService" />
</bean> </bean>
<!-- REST impl for GET Custom Reference Definitions -->
<bean id="webscript.org.alfresco.rma.customrefdefinitions.get"
class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionsGet"
parent="rmCustomReferenceDefinitionBase" />
<!-- REST impl for PUT Custom Reference Definitions --> <!-- REST impl for PUT Custom Reference Definitions -->
<bean id="webscript.org.alfresco.rma.customrefdefinition.put" <bean id="webscript.org.alfresco.rma.customrefdefinition.put"
class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionPut" class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionPut"
parent="rmBaseWebscript"> parent="rmCustomReferenceDefinitionBase" />
<property name="recordsManagementAdminService" ref="RecordsManagementAdminService" />
</bean>
<!-- REST impl for POST Custom Reference Definitions --> <!-- REST impl for POST Custom Reference Definitions -->
<bean id="webscript.org.alfresco.rma.customrefdefinition.post" <bean id="webscript.org.alfresco.rma.customrefdefinition.post"
class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionPost" class="org.alfresco.module.org_alfresco_module_rm.script.CustomReferenceDefinitionPost"
parent="rmBaseWebscript"> parent="rmCustomReferenceDefinitionBase" />
<property name="recordsManagementAdminService" ref="RecordsManagementAdminService" />
</bean>
<!-- REST impl for GET Custom Reference Instances --> <!-- REST impl for GET Custom Reference Instances -->
<bean id="webscript.org.alfresco.rma.customrefs.get" <bean id="webscript.org.alfresco.rma.customrefs.get"

View File

@@ -18,6 +18,8 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.script; package org.alfresco.module.org_alfresco_module_rm.script;
import java.io.IOException;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -26,24 +28,68 @@ import javax.servlet.http.HttpServletResponse;
import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService; import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
/** /**
* Abstract base class for all RM webscript classes.
* Includes util methods for processing the webscript request.
* *
* @author Neil McErlean * @author Neil McErlean
* @author Tuna Aksoy
*/ */
public abstract class AbstractRmWebScript extends DeclarativeWebScript public abstract class AbstractRmWebScript extends DeclarativeWebScript
{ {
protected NodeService nodeService; /** Constants */
protected static final String PATH_SEPARATOR = "/";
/** Disposition service */
protected DispositionService dispositionService; protected DispositionService dispositionService;
/** Namespace service */
protected NamespaceService namespaceService; protected NamespaceService namespaceService;
/** Node service */
protected NodeService nodeService;
/**
* Sets the disposition service
*
* @param dispositionService The disposition serviceS
*/
public void setDispositionService(DispositionService dispositionService)
{
this.dispositionService = dispositionService;
}
/**
* Sets the namespace service
*
* @param namespaceService The namespace service
*/
public void setNamespaceService(NamespaceService namespaceService)
{
this.namespaceService = namespaceService;
}
/**
* Sets the node service
*
* @param nodeService The node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/** /**
* Parses the request and providing it's valid returns the NodeRef. * Parses the request and providing it's valid returns the NodeRef.
* *
@@ -56,52 +102,23 @@ public abstract class AbstractRmWebScript extends DeclarativeWebScript
{ {
// get the parameters that represent the NodeRef, we know they are present // get the parameters that represent the NodeRef, we know they are present
// otherwise this webscript would not have matched // otherwise this webscript would not have matched
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars(); Map<String, String> templateVars = getTemplateVars(req);
String storeType = templateVars.get("store_type"); String storeType = templateVars.get("store_type");
String storeId = templateVars.get("store_id"); String storeId = templateVars.get("store_id");
String nodeId = templateVars.get("id"); String nodeId = templateVars.get("id");
// create the NodeRef and ensure it is valid // create the NodeRef and ensure it is valid
StoreRef storeRef = new StoreRef(storeType, storeId); NodeRef nodeRef = new NodeRef(storeType, storeId, nodeId);
NodeRef nodeRef = new NodeRef(storeRef, nodeId);
if (!this.nodeService.exists(nodeRef)) if (!nodeService.exists(nodeRef))
{ {
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: " + throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: '" +
nodeRef.toString()); nodeRef.toString() + "'.");
} }
return nodeRef; return nodeRef;
} }
/**
* @param dispositionService the disposition serviceS
*/
public void setDispositionService(DispositionService dispositionService)
{
this.dispositionService = dispositionService;
}
/**
* Sets the NodeService instance
*
* @param nodeService The NodeService instance
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Sets the NamespaceService instance
*
* @param namespaceService The NamespaceService instance
*/
public void setNamespaceService(NamespaceService namespaceService)
{
this.namespaceService = namespaceService;
}
/** /**
* This method checks if the json object contains an entry with the specified name. * This method checks if the json object contains an entry with the specified name.
* *
@@ -129,7 +146,123 @@ public abstract class AbstractRmWebScript extends DeclarativeWebScript
{ {
for (String name : paramNames) for (String name : paramNames)
{ {
this.checkMandatoryJsonParam(json, name); checkMandatoryJsonParam(json, name);
} }
} }
/**
* Gets the template variable substitutions map
*
* @param req The webscript request
* @return The template variable substitutions
*/
protected Map<String, String> getTemplateVars(WebScriptRequest req)
{
if (req == null)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The webscript request is null.");
}
if (req.getServiceMatch() == null)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The matching API Service for the request is null.");
}
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
if (templateVars == null)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The template variable substitutions map is null");
}
return templateVars;
}
/**
* Gets the value of a request parameter
*
* @param req The webscript request
* @param parameter The request parameter
* @return The value of the request parameter
*/
protected String getRequestParameterValue(WebScriptRequest req, String parameter)
{
Map<String, String> templateVars = getTemplateVars(req);
return templateVars.get(parameter);
}
/**
* Gets the request content as JSON object
*
* @param req The webscript request
* @return The request content as JSON object
*/
protected JSONObject getRequestContentAsJsonObject(WebScriptRequest req)
{
Content reqContent = req.getContent();
if (reqContent == null)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing request body.");
}
String content;
try
{
content = reqContent.getContent();
}
catch (IOException error)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get conent from the request.", error);
}
if (StringUtils.isBlank(content))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Content does not exist.");
}
JSONTokener jsonTokener;
try
{
jsonTokener = new JSONTokener(req.getContent().getContent());
}
catch (IOException error)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get content.", error);
}
JSONObject json;
try
{
json = new JSONObject(jsonTokener);
}
catch (JSONException error)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request body.", error);
}
return json;
}
/**
* Gets the value of a given key from a json object
*
* @param jsonObject The json object from which the value should be retrieved
* @param key The key for which the value is requested
* @return The value of the given key from the json object
*/
protected Serializable getJSONObjectValue(JSONObject jsonObject, String key)
{
Serializable value;
try
{
checkMandatoryJsonParam(jsonObject, key);
value = (Serializable) jsonObject.get(key);
}
catch (JSONException error)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get value for the key '" + key + "'.", error);
}
return value;
}
} }

View File

@@ -0,0 +1,142 @@
/*
* Copyright (C) 2005-2014 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.script;
import org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* Base class for custom reference definition classes
*
* @author Tuna Aksoy
* @since 2.3
*/
public class CustomReferenceDefinitionBase extends AbstractRmWebScript
{
/** Constants for the custom reference definition classes */
protected static final String REFERENCE_TYPE = "referenceType";
protected static final String REF_ID = "refId";
protected static final String LABEL = "label";
protected static final String SOURCE = "source";
protected static final String TARGET = "target";
protected static final String CUSTOM_REFS = "customRefs";
protected static final String URL = "url";
protected static final String SUCCESS = "success";
/** Records Management Admin Service */
private RecordsManagementAdminService rmAdminService;
/** Dictionary Service */
private DictionaryService dictionaryService;
/**
* Sets the records management admin service
*
* @param rmAdminService The records management admin service
*/
public void setRecordsManagementAdminService(RecordsManagementAdminService rmAdminService)
{
this.rmAdminService = rmAdminService;
}
/**
* Gets the records management admin service instance
*
* @return The records management admin service instance
*/
protected RecordsManagementAdminService getRmAdminService()
{
return this.rmAdminService;
}
/**
* Sets the dictionary service
*
* @param dictionaryService The dictionary service
*/
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}
/**
* Gets the dictionary service instance
*
* @return The dictionary service instance
*/
protected DictionaryService getDictionaryService()
{
return this.dictionaryService;
}
/**
* Gets the QName for the given custom reference id
*
* @param referenceId The reference id
* @return The QName for the given custom reference id
*/
protected QName getCustomReferenceQName(String referenceId)
{
QName customReferenceQName = getRmAdminService().getQNameForClientId(referenceId);
if (customReferenceQName == null)
{
StringBuilder msg = new StringBuilder();
msg.append("Unable to find QName for the reference: '");
msg.append(referenceId);
msg.append("'.");
String errorMsg = msg.toString();
throw new WebScriptException(Status.STATUS_NOT_FOUND, errorMsg);
}
return customReferenceQName;
}
/**
* Gets the custom reference type from the json object
*
* @param requestContent The request content as json object
* @return Returns the custom reference type which is either parent/child or bidirectional
*/
protected CustomReferenceType getCustomReferenceType(JSONObject requestContent)
{
String referenceType = (String) getJSONObjectValue(requestContent, REFERENCE_TYPE);
if (StringUtils.isBlank(referenceType))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Reference type is missing.");
}
return CustomReferenceType.getEnumFromString(referenceType);
}
/**
* Gets the service path from the webscript request
*
* @param req The webscript request
* @return The service path
*/
protected String getServicePath(WebScriptRequest req)
{
return req.getServicePath();
}
}

View File

@@ -18,162 +18,105 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.script; package org.alfresco.module.org_alfresco_module_rm.script;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.springframework.extensions.surf.util.ParameterCheck; import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
/** /**
* Implementation for Java backed webscript to add RM custom reference definitions * Implementation for Java backed webscript to add RM custom reference definitions
* to the custom model. * to the custom model.
* *
* @author Neil McErlean * @author Neil McErlean
* @author Tuna Aksoy
*/ */
public class CustomReferenceDefinitionPost extends AbstractRmWebScript public class CustomReferenceDefinitionPost extends CustomReferenceDefinitionBase
{ {
private static final String URL = "url"; /**
private static final String REF_ID = "refId"; * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
private static final String TARGET = "target";
private static final String SOURCE = "source";
private static final String LABEL = "label";
private static final String REFERENCE_TYPE = "referenceType";
private static Log logger = LogFactory.getLog(CustomReferenceDefinitionPost.class);
private RecordsManagementAdminService rmAdminService;
public void setRecordsManagementAdminService(RecordsManagementAdminService rmAdminService)
{
this.rmAdminService = rmAdminService;
}
/*
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
*/ */
@Override @Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{ {
JSONObject json = null; JSONObject requestContent = getRequestContentAsJsonObject(req);
Map<String, Object> ftlModel = null; CustomReferenceType customReferenceType = getCustomReferenceType(requestContent);
try QName customReference = addCustomReference(requestContent, customReferenceType);
{
json = new JSONObject(new JSONTokener(req.getContent().getContent())); Map<String, Object> model = new HashMap<String, Object>();
String servicePath = getServicePath(req);
ftlModel = addCustomReference(req, json); Map<String, Object> customReferenceData = getCustomReferenceData(customReferenceType, customReference, servicePath);
} model.putAll(customReferenceData);
catch (IOException iox)
{ return model;
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not read content from req.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", je);
}
catch (IllegalArgumentException iae)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
iae.getMessage(), iae);
}
return ftlModel;
} }
/** /**
* Applies custom properties. * Adds custom reference to the model
*
* @param requestContent The request content as json object
* @param customReferenceType The custom reference type
* @return Returns the {@link QName} of the new custom reference
*/ */
@SuppressWarnings("rawtypes") private QName addCustomReference(JSONObject requestContent, CustomReferenceType customReferenceType)
protected Map<String, Object> addCustomReference(WebScriptRequest req, JSONObject json) throws JSONException {
QName referenceQName;
if (CustomReferenceType.PARENT_CHILD.equals(customReferenceType))
{
String source = (String) getJSONObjectValue(requestContent, SOURCE);
if (StringUtils.isBlank(source))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Source is blank.");
}
String target = (String) getJSONObjectValue(requestContent, TARGET);
if (StringUtils.isBlank(target))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Target is blank.");
}
referenceQName = getRmAdminService().addCustomChildAssocDefinition(source, target);
}
else if (CustomReferenceType.BIDIRECTIONAL.equals(customReferenceType))
{
String label = (String) getJSONObjectValue(requestContent, LABEL);
if (StringUtils.isBlank(label))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Label is blank.");
}
referenceQName = getRmAdminService().addCustomAssocDefinition(label);
}
else
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unsupported custom reference type.");
}
return referenceQName;
}
/**
* Gets the custom reference data
*
* @param customReferenceType The custom reference type
* @param customReference The qualified name of the custom reference
* @param servicePath The service path
* @return The custom reference data
*/
private Map<String, Object> getCustomReferenceData(CustomReferenceType customReferenceType, QName customReference, String servicePath)
{ {
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
Map<String, Serializable> params = new HashMap<String, Serializable>(); String qnameLocalName = customReference.getLocalName();
result.put(REFERENCE_TYPE, customReferenceType.toString());
for (Iterator iter = json.keys(); iter.hasNext(); ) result.put(REF_ID, qnameLocalName);
{ result.put(URL, servicePath + PATH_SEPARATOR + qnameLocalName);
String nextKeyString = (String)iter.next(); result.put(SUCCESS, Boolean.TRUE);
Serializable nextValue = (Serializable)json.get(nextKeyString);
params.put(nextKeyString, nextValue);
}
String refTypeParam = (String)params.get(REFERENCE_TYPE);
ParameterCheck.mandatory(REFERENCE_TYPE, refTypeParam);
CustomReferenceType refTypeEnum = CustomReferenceType.getEnumFromString(refTypeParam);
boolean isChildAssoc = refTypeEnum.equals(CustomReferenceType.PARENT_CHILD);
if (logger.isDebugEnabled())
{
StringBuilder msg = new StringBuilder();
msg.append("Creating custom ");
if (isChildAssoc)
{
msg.append("child ");
}
msg.append("assoc");
logger.debug(msg.toString());
}
QName generatedQName;
if (isChildAssoc)
{
String source = (String)params.get(SOURCE);
String target = (String)params.get(TARGET);
generatedQName = rmAdminService.addCustomChildAssocDefinition(source, target);
}
else
{
String label = (String)params.get(LABEL);
generatedQName = rmAdminService.addCustomAssocDefinition(label);
}
result.put(REFERENCE_TYPE, refTypeParam);
String qnameLocalName;
if (refTypeParam.equals(CustomReferenceType.BIDIRECTIONAL.toString()))
{
Serializable labelParam = params.get(LABEL);
// label is mandatory for bidirectional refs only
ParameterCheck.mandatory(LABEL, labelParam);
qnameLocalName = generatedQName.getLocalName();
result.put(REF_ID, qnameLocalName);
}
else if (refTypeParam.equals(CustomReferenceType.PARENT_CHILD.toString()))
{
Serializable sourceParam = params.get(SOURCE);
Serializable targetParam = params.get(TARGET);
// source,target mandatory for parent/child refs only
ParameterCheck.mandatory(SOURCE, sourceParam);
ParameterCheck.mandatory(TARGET, targetParam);
qnameLocalName = generatedQName.getLocalName();
result.put(REF_ID, qnameLocalName);
}
else
{
throw new WebScriptException("Unsupported reference type: " + refTypeParam);
}
result.put(URL, req.getServicePath() + "/" + qnameLocalName);
result.put("success", Boolean.TRUE);
return result; return result;
} }
} }

View File

@@ -18,19 +18,12 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.script; package org.alfresco.module.org_alfresco_module_rm.script;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.json.JSONException; import org.apache.commons.lang.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptException;
@@ -42,113 +35,100 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
* the source/target (for parent/child references). * the source/target (for parent/child references).
* *
* @author Neil McErlean * @author Neil McErlean
* @author Tuna Aksoy
*/ */
public class CustomReferenceDefinitionPut extends AbstractRmWebScript public class CustomReferenceDefinitionPut extends CustomReferenceDefinitionBase
{ {
private static final String URL = "url"; /**
private static final String REF_ID = "refId";
private static final String TARGET = "target";
private static final String SOURCE = "source";
private static final String LABEL = "label";
private RecordsManagementAdminService rmAdminService;
public void setRecordsManagementAdminService(RecordsManagementAdminService rmAdminService)
{
this.rmAdminService = rmAdminService;
}
/*
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache) * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
*/ */
@Override @Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{ {
JSONObject json = null; JSONObject requestContent = getRequestContentAsJsonObject(req);
Map<String, Object> ftlModel = null; String referenceId = getReferenceId(req);
try updateCustomReference(requestContent, referenceId);
{
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
ftlModel = updateCustomReference(req, json); Map<String, Object> model = new HashMap<String, Object>();
} String servicePath = getServicePath(req);
catch (IOException iox) Map<String, Object> customReferenceData = getCustomReferenceData(servicePath, referenceId);
{ model.putAll(customReferenceData);
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not read content from req.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", je);
}
catch (IllegalArgumentException iae)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
iae.getMessage(), iae);
}
return ftlModel; return model;
} }
/** /**
* Applies custom properties. * Gets the reference id from the webscript request
*
* @param req The webscript request
* @return The reference id
*/ */
@SuppressWarnings("rawtypes") private String getReferenceId(WebScriptRequest req)
protected Map<String, Object> updateCustomReference(WebScriptRequest req, JSONObject json) throws JSONException {
String referenceId = getRequestParameterValue(req, REF_ID);
if (StringUtils.isBlank(referenceId))
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Reference id is blank.");
}
return referenceId;
}
/**
* Updates the custom reference
*
* @param requestContent The request content as json object
* @param referenceId The reference id
*/
private void updateCustomReference(JSONObject requestContent, String referenceId)
{
QName referenceQName = getCustomReferenceQName(referenceId);
CustomReferenceType customReferenceType = getCustomReferenceType(requestContent);
if (CustomReferenceType.PARENT_CHILD.equals(customReferenceType))
{
String source = (String) getJSONObjectValue(requestContent, SOURCE);
if (StringUtils.isBlank(source))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Source is blank.");
}
String target = (String) getJSONObjectValue(requestContent, TARGET);
if (StringUtils.isBlank(target))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Target is blank.");
}
getRmAdminService().updateCustomChildAssocDefinition(referenceQName, source, target);
}
else if (CustomReferenceType.BIDIRECTIONAL.equals(customReferenceType))
{
String label = (String) getJSONObjectValue(requestContent, LABEL);
if (StringUtils.isBlank(label))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Label is blank.");
}
getRmAdminService().updateCustomAssocDefinition(referenceQName, label);
}
else
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unsupported custom reference type.");
}
}
/**
* Gets the custom reference data
*
* @param servicePath The service path
* @param String The reference id
* @return The custom reference data
*/
private Map<String, Object> getCustomReferenceData(String servicePath, String referenceId)
{ {
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
Map<String, Serializable> params = new HashMap<String, Serializable>(); result.put(URL, servicePath);
result.put(REF_ID, referenceId);
for (Iterator iter = json.keys(); iter.hasNext(); ) result.put(SUCCESS, Boolean.TRUE);
{
String nextKeyString = (String)iter.next();
Serializable nextValue = (Serializable)json.get(nextKeyString);
params.put(nextKeyString, nextValue);
}
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String refId = templateVars.get(REF_ID);
// refId cannot be null as it is defined within the URL
params.put(REF_ID, (Serializable)refId);
// Ensure that the reference actually exists.
QName refQName = rmAdminService.getQNameForClientId(refId);
if (refQName == null)
{
throw new WebScriptException(Status.STATUS_NOT_FOUND,
"Could not find reference definition for: " + refId);
}
String newLabel = (String)params.get(LABEL);
String newSource = (String)params.get(SOURCE);
String newTarget = (String)params.get(TARGET);
// Determine whether it's a bidi or a p/c ref
AssociationDefinition assocDef = rmAdminService.getCustomReferenceDefinitions().get(refQName);
if (assocDef == null)
{
throw new WebScriptException(Status.STATUS_NOT_FOUND,
"Could not find reference definition for: " + refId);
}
if (assocDef instanceof ChildAssociationDefinition)
{
if (newSource != null || newTarget != null)
{
rmAdminService.updateCustomChildAssocDefinition(refQName, newSource, newTarget);
}
}
else if (newLabel != null)
{
rmAdminService.updateCustomAssocDefinition(refQName, newLabel);
}
result.put(URL, req.getServicePath());
result.put("refId", refQName.getLocalName());
result.put("success", Boolean.TRUE);
return result; return result;
} }
} }

View File

@@ -24,135 +24,169 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService;
import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition; import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
/** /**
* This class provides the implementation for the customrefdefinitions.get webscript. * Implementation for Java backed webscript to get RM custom reference definitions.
* *
* @author Neil McErlean * @author Neil McErlean
* @author Tuna Aksoy
*/ */
public class CustomReferenceDefinitionsGet extends DeclarativeWebScript public class CustomReferenceDefinitionsGet extends CustomReferenceDefinitionBase
{ {
private static final String REFERENCE_TYPE = "referenceType"; /**
private static final String REF_ID = "refId"; * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
private static final String LABEL = "label"; */
private static final String SOURCE = "source";
private static final String TARGET = "target";
private static final String CUSTOM_REFS = "customRefs";
private static Log logger = LogFactory.getLog(CustomReferenceDefinitionsGet.class);
private RecordsManagementAdminService rmAdminService;
private DictionaryService dictionaryService;
public void setRecordsManagementAdminService(RecordsManagementAdminService rmAdminService)
{
this.rmAdminService = rmAdminService;
}
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}
@Override @Override
public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{ {
String referenceId = getRequestParameterValue(req, REF_ID);
Map<QName, AssociationDefinition> customReferenceDefinitions = getCustomReferenceDefinitions(referenceId);
List<Map<String, String>> customReferenceData = getCustomReferenceData(customReferenceDefinitions);
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
model.put(CUSTOM_REFS, customReferenceData);
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String refId = templateVars.get(REF_ID);
if (logger.isDebugEnabled())
{
logger.debug("Getting custom reference definitions with refId: " + refId);
}
Map<QName, AssociationDefinition> currentCustomRefs = rmAdminService.getCustomReferenceDefinitions();
// If refId has been provided then this is a request for a single custom-ref-defn.
// else it is a request for them all.
if (refId != null)
{
QName qn = rmAdminService.getQNameForClientId(refId);
AssociationDefinition assDef = currentCustomRefs.get(qn);
if (assDef == null)
{
StringBuilder msg = new StringBuilder();
msg.append("Unable to find reference: ").append(refId);
if (logger.isDebugEnabled())
{
logger.debug(msg.toString());
}
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND,
msg.toString());
}
currentCustomRefs = new HashMap<QName, AssociationDefinition>(1);
currentCustomRefs.put(qn, assDef);
}
List<Map<String, String>> listOfReferenceData = new ArrayList<Map<String, String>>();
for (Entry<QName, AssociationDefinition> entry : currentCustomRefs.entrySet())
{
Map<String, String> data = new HashMap<String, String>();
AssociationDefinition nextValue = entry.getValue();
CustomReferenceType referenceType = nextValue instanceof ChildAssociationDefinition ?
CustomReferenceType.PARENT_CHILD : CustomReferenceType.BIDIRECTIONAL;
data.put(REFERENCE_TYPE, referenceType.toString());
// It is the title which stores either the label, or the source and target.
String nextTitle = nextValue.getTitle(dictionaryService);
if (CustomReferenceType.PARENT_CHILD.equals(referenceType))
{
if (nextTitle != null)
{
String[] sourceAndTarget = rmAdminService.splitSourceTargetId(nextTitle);
data.put(SOURCE, sourceAndTarget[0]);
data.put(TARGET, sourceAndTarget[1]);
data.put(REF_ID, entry.getKey().getLocalName());
}
}
else if (CustomReferenceType.BIDIRECTIONAL.equals(referenceType))
{
if (nextTitle != null)
{
data.put(LABEL, nextTitle);
data.put(REF_ID, entry.getKey().getLocalName());
}
}
else
{
throw new WebScriptException("Unsupported custom reference type: " + referenceType);
}
listOfReferenceData.add(data);
}
if (logger.isDebugEnabled())
{
logger.debug("Retrieved custom reference definitions: " + listOfReferenceData.size());
}
model.put(CUSTOM_REFS, listOfReferenceData);
return model; return model;
} }
/**
* Gets the custom reference definition(s) for the given reference id
*
* @param referenceId The reference id
* @return If the reference id is not blank the custom definition for the given reference id will be returned,
* otherwise all custom definitions will be returned.
*/
private Map<QName, AssociationDefinition> getCustomReferenceDefinitions(String referenceId)
{
Map<QName, AssociationDefinition> customReferenceDefinitions = new HashMap<QName, AssociationDefinition>();
if (StringUtils.isNotBlank(referenceId))
{
QName referenceQName = getCustomReferenceQName(referenceId);
AssociationDefinition associationDefinition = getAssosiationDefinitionForCustomReference(referenceQName);
customReferenceDefinitions.put(referenceQName, associationDefinition);
}
else
{
customReferenceDefinitions.putAll(getRmAdminService().getCustomReferenceDefinitions());
}
return customReferenceDefinitions;
}
/**
* Gets the association definition for the given reference QName
*
* @param referenceQName The reference QName
* @return The association definition for the given reference QName
*/
private AssociationDefinition getAssosiationDefinitionForCustomReference(QName referenceQName)
{
AssociationDefinition associationDefinition = getRmAdminService().getCustomReferenceDefinitions().get(referenceQName);
if (associationDefinition == null)
{
StringBuilder msg = new StringBuilder();
msg.append("Unable to find association definition for the reference: '");
msg.append(referenceQName.getLocalName());
msg.append("'.");
String errorMsg = msg.toString();
throw new WebScriptException(Status.STATUS_NOT_FOUND, errorMsg);
}
return associationDefinition;
}
/**
* Gets the custom reference type from the association definition
*
* @param associationDefinition The association definition
* @return Returns the custom reference type which is either parent/child or bidirectional
*/
private CustomReferenceType getCustomReferenceType(AssociationDefinition associationDefinition)
{
CustomReferenceType referenceType;
if (associationDefinition instanceof ChildAssociationDefinition)
{
referenceType = CustomReferenceType.PARENT_CHILD;
}
else if (associationDefinition instanceof AssociationDefinition)
{
referenceType = CustomReferenceType.BIDIRECTIONAL;
}
else
{
StringBuilder msg = new StringBuilder();
msg.append("Unsupported association definition: '");
msg.append(associationDefinition.getName().getLocalName());
msg.append("'.");
String errorMsg = msg.toString();
throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, errorMsg);
}
return referenceType;
}
/**
* Gets the custom reference data
*
* @param customReferenceDefinitions The custom reference definitions
* @return Custom reference data
*/
private List<Map<String, String>> getCustomReferenceData(Map<QName, AssociationDefinition> customReferenceDefinitions)
{
List<Map<String, String>> customReferences = new ArrayList<Map<String, String>>();
for (Entry<QName, AssociationDefinition> entry : customReferenceDefinitions.entrySet())
{
Map<String, String> customReference = new HashMap<String, String>();
AssociationDefinition associationDefinition = entry.getValue();
CustomReferenceType referenceType = getCustomReferenceType(associationDefinition);
String title = getAssociationDefinitionTitle(associationDefinition);
if (CustomReferenceType.PARENT_CHILD.equals(referenceType))
{
String[] sourceAndTarget = getRmAdminService().splitSourceTargetId(title);
customReference.put(SOURCE, sourceAndTarget[0]);
customReference.put(TARGET, sourceAndTarget[1]);
}
else if (CustomReferenceType.BIDIRECTIONAL.equals(referenceType))
{
customReference.put(LABEL, title);
}
String referenceId = entry.getKey().getLocalName();
customReference.put(REF_ID, referenceId);
customReference.put(REFERENCE_TYPE, referenceType.toString());
customReferences.add(customReference);
}
return customReferences;
}
/**
* Gets the association definition title
*
* @param associationDefinition The association definition
* @return The title of the association definition
*/
private String getAssociationDefinitionTitle(AssociationDefinition associationDefinition)
{
String title = associationDefinition.getTitle(getDictionaryService());
if (StringUtils.isBlank(title))
{
throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Association definition title is blank.");
}
return title;
}
} }