mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
Switch the Links creating webscript to be Java backed using the new service, and enhance unit tests
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30108 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,88 +0,0 @@
|
||||
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/requestutils.lib.js">
|
||||
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/nodenameutils.lib.js">
|
||||
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/links/links.lib.js">
|
||||
|
||||
function ensureTagScope(node)
|
||||
{
|
||||
if (!node.isTagScope)
|
||||
{
|
||||
node.isTagScope = true;
|
||||
}
|
||||
|
||||
// also check the parent (the site!)
|
||||
if (!node.parent.isTagScope)
|
||||
{
|
||||
node.parent.isTagScope = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a link
|
||||
*/
|
||||
function createLink(linkNode)
|
||||
{
|
||||
var tags = [];
|
||||
if (json.has("tags"))
|
||||
{
|
||||
// get the tags JSONArray and copy it into a real javascript array object
|
||||
var tmp = json.get("tags");
|
||||
for (var x = 0; x < tmp.length(); x++)
|
||||
{
|
||||
tags.push(tmp.get(x));
|
||||
}
|
||||
}
|
||||
|
||||
// get a unique name
|
||||
var nodeName = getUniqueChildName(linkNode, "link");
|
||||
linkNode = linkNode.createNode(nodeName, "lnk:link",getLinkProperties());
|
||||
|
||||
if (isLinkInternal())
|
||||
{
|
||||
var pr = [];
|
||||
pr["lnk:isInternal"] = "true";
|
||||
linkNode.addAspect("lnk:internal", pr);
|
||||
}
|
||||
|
||||
linkNode.tags = tags;
|
||||
linkNode.mimetype = "text/html";
|
||||
linkNode.content = linkNode.properties["lnk:url"];
|
||||
linkNode.save();
|
||||
|
||||
var siteId = url.templateArgs.site;
|
||||
var data =
|
||||
{
|
||||
title: json.get("title"),
|
||||
page: json.get("page") + "?linkId=" + nodeName
|
||||
};
|
||||
|
||||
model.message = linkNode.properties["name"];
|
||||
activities.postActivity("org.alfresco.links.link-created", siteId, "links", jsonUtils.toJSONString(data));
|
||||
|
||||
return linkNode;
|
||||
}
|
||||
|
||||
function main()
|
||||
{
|
||||
// get requested node
|
||||
var node = getRequestNode();
|
||||
if (status.getCode() != status.STATUS_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node.hasPermission("CreateChildren"))
|
||||
{
|
||||
status.code = 403;
|
||||
var mes = "Permission to create is denied";
|
||||
status.message = mes;
|
||||
model.message = mes;
|
||||
return;
|
||||
}
|
||||
|
||||
ensureTagScope(node);
|
||||
|
||||
var link = createLink(node);
|
||||
model.item = link;
|
||||
}
|
||||
|
||||
main();
|
@@ -1525,6 +1525,12 @@
|
||||
parent="abstractLinksWebScript">
|
||||
</bean>
|
||||
|
||||
<!-- Creates a new link in the site -->
|
||||
<bean id="webscript.org.alfresco.repository.links.links.post"
|
||||
class="org.alfresco.repo.web.scripts.links.LinksPost"
|
||||
parent="abstractLinksWebScript">
|
||||
</bean>
|
||||
|
||||
<!-- Updates the details of one link -->
|
||||
<bean id="webscript.org.alfresco.repository.links.links.put"
|
||||
class="org.alfresco.repo.web.scripts.links.LinkPut"
|
||||
|
@@ -53,8 +53,9 @@ public class LinkGet extends AbstractLinksWebScript
|
||||
// Build the model
|
||||
model.put("item", renderLink(link));
|
||||
model.put("node", link.getNodeRef());
|
||||
model.put("siteId", site.getShortName());
|
||||
model.put("link", link);
|
||||
model.put("site", site);
|
||||
model.put("siteId", site.getShortName());
|
||||
|
||||
// All done
|
||||
return model;
|
||||
|
113
source/java/org/alfresco/repo/web/scripts/links/LinksPost.java
Normal file
113
source/java/org/alfresco/repo/web/scripts/links/LinksPost.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.repo.web.scripts.links;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.security.permissions.AccessDeniedException;
|
||||
import org.alfresco.service.cmr.links.LinkInfo;
|
||||
import org.alfresco.service.cmr.site.SiteInfo;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the link fetching links.post webscript.
|
||||
*
|
||||
* @author Nick Burch
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LinksPost extends AbstractLinksWebScript
|
||||
{
|
||||
private static final String PARAM_MESSAGE = "message";
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(SiteInfo site, String linkName,
|
||||
WebScriptRequest req, JSONObject json, Status status, Cache cache) {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
|
||||
// Get the new link details from the JSON
|
||||
String title;
|
||||
String description;
|
||||
String url;
|
||||
boolean internal;
|
||||
List<String> tags;
|
||||
try
|
||||
{
|
||||
// Fetch the main properties
|
||||
title = getOrNull(json, "title");
|
||||
description = getOrNull(json, "description");
|
||||
url = getOrNull(json, "url");
|
||||
|
||||
// Handle internal / not internal
|
||||
internal = json.has("internal");
|
||||
|
||||
// Do the tags
|
||||
tags = getTags(json);
|
||||
}
|
||||
catch(JSONException je)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
|
||||
}
|
||||
|
||||
|
||||
// Create the link
|
||||
LinkInfo link;
|
||||
try
|
||||
{
|
||||
link = linksService.createLink(site.getShortName(), title, description, url, internal);
|
||||
}
|
||||
catch(AccessDeniedException e)
|
||||
{
|
||||
String message = "You don't have permission to create a link";
|
||||
|
||||
status.setCode(Status.STATUS_FORBIDDEN);
|
||||
status.setMessage(message);
|
||||
model.put(PARAM_MESSAGE, message);
|
||||
return model;
|
||||
}
|
||||
|
||||
// Set the tags if required
|
||||
if(tags != null && tags.size() > 0)
|
||||
{
|
||||
link.getTags().addAll(tags);
|
||||
linksService.updateLink(link);
|
||||
}
|
||||
|
||||
// Generate an activity for the change
|
||||
addActivityEntry("created", link, site, req, json);
|
||||
|
||||
|
||||
// Build the model
|
||||
model.put(PARAM_MESSAGE, link.getSystemName()); // Really!
|
||||
model.put("item", renderLink(link));
|
||||
model.put("node", link.getNodeRef());
|
||||
model.put("link", link);
|
||||
model.put("site", site);
|
||||
model.put("siteId", site.getShortName());
|
||||
|
||||
// All done
|
||||
return model;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user