Merged 5.2.N (5.2.1) to HEAD (5.2)

131742 rneamtu:       131742 rneamtu: SHA-1629 : Creating a link to file in a different location
            - Added support for multiple files in doclink.post webscript


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@132292 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alan Davis
2016-11-03 13:56:56 +00:00
parent 74dc12f5c3
commit 53cbbd39fe
6 changed files with 776 additions and 621 deletions

View File

@@ -41,12 +41,23 @@
<![CDATA[{ "destinationNodeRef" : string }]]> <![CDATA[{ "destinationNodeRef" : string }]]>
</type> </type>
</request> </request>
<request>
<format>json</format>
<type>
<![CDATA[
{
"destinationNodeRef": string,
"multipleFiles": string
}
]]>
</type>
</request>
</requests> </requests>
<responses> <responses>
<response> <response>
<format>json</format> <format>json</format>
<type> <type>
<![CDATA[{ "linkNodeRef" : string }]]> <![CDATA[{ "result" : string }]]>
</type> </type>
</response> </response>
</responses> </responses>

View File

@@ -1,5 +1,16 @@
<#escape x as jsonUtils.encodeJSONString(x)> <#escape x as jsonUtils.encodeJSONString(x)>
{ {
"linkNodeRef": "${linkNodeRef}" "linkNodes" :
[
<#list results as result>
{
"nodeRef" : "${result.nodeRef}"
}
<#if result_has_next>,</#if>
</#list>
],
"successCount": "${successCount}",
"failureCount": "${failureCount}",
"overallSuccess": "${overallSuccess?c}"
} }
</#escape> </#escape>

View File

@@ -1846,6 +1846,7 @@
<property name="nodeService" ref="NodeService" /> <property name="nodeService" ref="NodeService" />
<property name="siteService" ref="SiteService" /> <property name="siteService" ref="SiteService" />
<property name="documentLinkService" ref="DocumentLinkService" /> <property name="documentLinkService" ref="DocumentLinkService" />
<property name="activityService" ref="activityService"/>
</bean> </bean>
<bean id="webscript.org.alfresco.repository.doclink.doclinks.delete" <bean id="webscript.org.alfresco.repository.doclink.doclinks.delete"
@@ -1854,6 +1855,7 @@
<property name="nodeService" ref="NodeService" /> <property name="nodeService" ref="NodeService" />
<property name="siteService" ref="SiteService" /> <property name="siteService" ref="SiteService" />
<property name="documentLinkService" ref="DocumentLinkService" /> <property name="documentLinkService" ref="DocumentLinkService" />
<property name="activityService" ref="activityService"/>
</bean> </bean>
<!-- CMM - Custom model import API --> <!-- CMM - Custom model import API -->

View File

@@ -25,10 +25,14 @@
*/ */
package org.alfresco.repo.web.scripts.doclink; package org.alfresco.repo.web.scripts.doclink;
import java.io.StringWriter;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.web.scripts.links.AbstractLinksWebScript;
import org.alfresco.service.cmr.activities.ActivityService;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.repository.DocumentLinkService; import org.alfresco.service.cmr.repository.DocumentLinkService;
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;
@@ -36,9 +40,15 @@ import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService; import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck; import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONStringer;
import org.json.simple.JSONObject;
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.json.JSONWriter;
/** /**
* This class contains common code for doclink webscripts controllers * This class contains common code for doclink webscripts controllers
@@ -55,9 +65,14 @@ public abstract class AbstractDocLink extends DeclarativeWebScript
private static String PARAM_CONTAINER = "container"; private static String PARAM_CONTAINER = "container";
private static String PARAM_PATH = "path"; private static String PARAM_PATH = "path";
private static final String ACTIVITY_TOOL = "documentLinkService";
protected NodeService nodeService; protected NodeService nodeService;
protected SiteService siteService; protected SiteService siteService;
protected DocumentLinkService documentLinkService; protected DocumentLinkService documentLinkService;
protected ActivityService activityService;
private static Log logger = LogFactory.getLog(AbstractDocLink.class);
protected NodeRef parseNodeRefFromTemplateArgs(Map<String, String> templateVars) protected NodeRef parseNodeRefFromTemplateArgs(Map<String, String> templateVars)
{ {
@@ -127,6 +142,35 @@ public abstract class AbstractDocLink extends DeclarativeWebScript
return null; return null;
} }
/**
* Generates an activity entry for the link
*/
protected void addActivityEntry(String activityType, String title, String nodeRef, String site)
{
try
{
StringWriter activityJson = new StringWriter();
JSONWriter activity = new JSONWriter(activityJson);
activity.startObject();
activity.writeValue("title", title);
activity.writeValue("nodeRef", nodeRef);
activity.writeValue("page", "document-details?nodeRef=" + nodeRef);
activity.endObject();
activityService.postActivity(
activityType,
site,
ACTIVITY_TOOL,
activityJson.toString());
}
catch (Exception e)
{
// Warn, but carry on
logger.warn("Error adding link event to activities feed", e);
}
}
public void setNodeService(NodeService nodeService) public void setNodeService(NodeService nodeService)
{ {
this.nodeService = nodeService; this.nodeService = nodeService;
@@ -141,4 +185,9 @@ public abstract class AbstractDocLink extends DeclarativeWebScript
{ {
this.documentLinkService = documentLinkService; this.documentLinkService = documentLinkService;
} }
public void setActivityService(ActivityService activityService)
{
this.activityService = activityService;
}
} }

View File

@@ -26,13 +26,19 @@
package org.alfresco.repo.web.scripts.doclink; package org.alfresco.repo.web.scripts.doclink;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.activities.ActivityType;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.permissions.AccessDeniedException; import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
@@ -50,7 +56,8 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class DocLinkPost extends AbstractDocLink public class DocLinkPost extends AbstractDocLink
{ {
private static String PARAM_DESTINATION_NODE = "destinationNodeRef"; private static final String PARAM_DESTINATION_NODE = "destinationNodeRef";
private static final String PARAM_MULTIPLE_FILES = "multipleFiles";
@Override @Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
@@ -94,7 +101,82 @@ public class DocLinkPost extends AbstractDocLink
ParameterCheck.mandatoryString("destinationNodeParam", destinationNodeParam); ParameterCheck.mandatoryString("destinationNodeParam", destinationNodeParam);
destinationNodeRef = new NodeRef(destinationNodeParam); destinationNodeRef = new NodeRef(destinationNodeParam);
List<NodeRef> nodeRefs = new ArrayList<NodeRef>();
if (json.containsKey(PARAM_MULTIPLE_FILES))
{
JSONArray multipleFiles = (JSONArray) json.get(PARAM_MULTIPLE_FILES);
for (int i = 0; i < multipleFiles.size(); i++)
{
String nodeRefString = (String) multipleFiles.get(i);
if (nodeRefString != null)
{
try
{
NodeRef nodeRefToCreateLink = new NodeRef(nodeRefString);
nodeRefs.add(nodeRefToCreateLink);
}
catch (AlfrescoRuntimeException ex)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid Arguments: " + ex.getMessage());
}
}
}
}
else
{
nodeRefs.add(sourceNodeRef);
}
// getSite for destination folder
String siteName = siteService.getSiteShortName(destinationNodeRef);
ArrayList<Object> linksResults = new ArrayList<Object>();
Map<String, Object> linkResult = new HashMap<String, Object>();
NodeRef linkNodeRef = null;
int successCount = 0;
int failureCount = 0;
if (nodeRefs != null && nodeRefs.size() > 0)
{
for (NodeRef sourceNode : nodeRefs)
{
/* Create link */ /* Create link */
linkNodeRef = createLink(destinationNodeRef, sourceNode);
if (linkNodeRef != null)
{
String sourceName = (String) nodeService.getProperty(sourceNode, ContentModel.PROP_NAME);
if (siteName != null)
{
addActivityEntry(ActivityType.DOCLINK_CREATED, sourceName, sourceNode.toString(), siteName);
}
linkResult.put("nodeRef", linkNodeRef.toString());
linksResults.add(linkResult);
successCount++;
}
}
}
failureCount = nodeRefs.size() - successCount;
Map<String, Object> model = new HashMap<String, Object>();
model.put("results", linksResults);
model.put("successCount", successCount);
model.put("failureCount", failureCount);
model.put("overallSuccess", failureCount == 0);
return model;
}
/**
* Create link for sourceNodeRef in destinationNodeRef location
*
* @param destinationNodeRef
* @param sourceNodeRef
* @return
*/
private NodeRef createLink(NodeRef destinationNodeRef, NodeRef sourceNodeRef)
{
NodeRef linkNodeRef = null; NodeRef linkNodeRef = null;
try try
{ {
@@ -102,16 +184,16 @@ public class DocLinkPost extends AbstractDocLink
} }
catch (IllegalArgumentException ex) catch (IllegalArgumentException ex)
{ {
if (ex.getMessage().contains("filelink") || ex.getMessage().contains("folderLink"))
{
return null;
}
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid Arguments: " + ex.getMessage()); throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid Arguments: " + ex.getMessage());
} }
catch (AccessDeniedException e) catch (AccessDeniedException e)
{ {
throw new WebScriptException(Status.STATUS_FORBIDDEN, "You don't have permission to perform this operation"); throw new WebScriptException(Status.STATUS_FORBIDDEN, "You don't have permission to perform this operation");
} }
return linkNodeRef;
/* Build response */
Map<String, Object> model = new HashMap<String, Object>();
model.put("linkNodeRef", linkNodeRef.toString());
return model;
} }
} }