Convert the wiki page version script to be java backed and using the new service

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30115 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-26 17:33:47 +00:00
parent 8281b9d9db
commit df04a07c86
5 changed files with 178 additions and 52 deletions

View File

@@ -1,48 +0,0 @@
<import resource="classpath:/alfresco/templates/webscripts/org/alfresco/slingshot/wiki/lib/wiki.lib.js">
function main()
{
var params = getTemplateArgs(["siteId", "pageTitle", "versionId"]);
var content = "";
// Get the site
var site = siteService.getSite(params.siteId);
if (site === null)
{
return "";
}
var wiki = getWikiContainer(site);
if (wiki === null)
{
return "";
}
var page = wiki.childByNamePath(params.pageTitle);
if (page === null)
{
return "";
}
var version;
var versions = page.versionHistory;
// NOTE: would it be possible to pass in the noderef and do a search for the specific
// version (directly) against the "lightWeightVersionStore"? This would depend on what
// indexing (if any) there is on the version store.
for (var i=0; i < versions.length; i++)
{
version = versions[i];
versionNode = version.node;
// If we don't create a string explicitly the comparison fails
if (String(versionNode.id) === params.versionId ||
String(version.label) == params.versionId)
{
content = versionNode.content;
break;
}
}
return content;
}
model.content = main();

View File

@@ -1577,6 +1577,14 @@
parent="abstractWikiWebScript">
</bean>
<!-- Fetches the contents of one wiki page at a specific version -->
<bean id="webscript.org.alfresco.slingshot.wiki.version.get"
class="org.alfresco.repo.web.scripts.wiki.WikiPageVersionGet"
parent="abstractWikiWebScript">
<property name="contentService" ref="ContentService" />
<property name="versionService" ref="VersionService" />
</bean>
<!-- Deletes a wiki page -->
<bean id="webscript.org.alfresco.slingshot.wiki.page.delete"
class="org.alfresco.repo.web.scripts.wiki.WikiPageDelete"

View File

@@ -37,7 +37,7 @@ import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* This class is the controller for the wiki page listing page.get webscript.
* This class is the controller for the wiki page fetching page.get webscript.
*
* @author Nick Burch
* @since 4.0

View File

@@ -0,0 +1,166 @@
/*
* 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.wiki;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.AspectMissingException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionDoesNotExistException;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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 wiki page version fetching version.get webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class WikiPageVersionGet extends AbstractWikiWebScript
{
// For spotting if a version string is an ID or a Label
private static final Pattern LABEL_PATTERN = Pattern.compile("\\d+\\.\\d+");
private static final String PARAM_CONTENT = "content";
private ContentService contentService;
private VersionService versionService;
public void setVersionService(VersionService versionService)
{
this.versionService = versionService;
}
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
@Override
protected Map<String, Object> executeImpl(SiteInfo site, String pageName,
WebScriptRequest req, JSONObject json, Status status, Cache cache) {
Map<String, Object> model = new HashMap<String, Object>();
// Grab the version string
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String versionId = templateVars.get("versionId");
if(versionId == null)
{
String error = "No versionId supplied";
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
// Try to find the page
WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageName);
if(page == null)
{
String message = "The Wiki Page could not be found";
status.setCode(Status.STATUS_NOT_FOUND);
status.setMessage(message);
// Return an empty string though
model.put(PARAM_CONTENT, "");
return model;
}
// Fetch the version history for the node
VersionHistory versionHistory = null;
Version version = null;
try
{
versionHistory = versionService.getVersionHistory(page.getNodeRef());
}
catch(AspectMissingException e) {}
if(versionHistory == null)
{
// Not been versioned, return an empty string
model.put(PARAM_CONTENT, "");
return model;
}
// Fetch the version by either ID or Label
Matcher m = LABEL_PATTERN.matcher(versionId);
if(m.matches())
{
// It's a version label like 2.3
try
{
version = versionHistory.getVersion(versionId);
}
catch(VersionDoesNotExistException e) {}
}
else
{
// It's a version ID like ed00bac1-f0da-4042-8598-45a0d39cb74d
// (The ID is usually part of the NodeRef of the frozen node, but we
// don't assume to be able to just generate the full NodeRef)
for(Version v : versionHistory.getAllVersions())
{
if(v.getFrozenStateNodeRef().getId().equals(versionId))
{
version = v;
}
}
}
// Did we find the right version in the end?
String contents;
if(version != null)
{
ContentReader reader = contentService.getReader(version.getFrozenStateNodeRef(), ContentModel.PROP_CONTENT);
if(reader != null)
{
contents = reader.getContentString();
}
else
{
// No content was stored in the version history
contents = "";
}
}
else
{
// No warning of the missing version, just return an empty string
contents = "";
}
// All done
model.put(PARAM_CONTENT, contents);
model.put("page", page);
model.put("site", site);
model.put("siteId", site.getShortName());
return model;
}
}

View File

@@ -555,7 +555,7 @@ public class WikiRestApiTest extends BaseWebScriptTest
// Fetch it at this version
String content = getPageAtVersion(name, "1.0", Status.STATUS_OK);
// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing
assertEquals(PAGE_CONTENTS_ONE, content);
// Upload a new copy without a version flag, denied
@@ -581,7 +581,7 @@ public class WikiRestApiTest extends BaseWebScriptTest
content = getPageAtVersion(name, "1.1", Status.STATUS_OK);
assertEquals(PAGE_CONTENTS_CHANGED, content);
content = getPageAtVersion(name, "1.0", Status.STATUS_OK);
// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing
assertEquals(PAGE_CONTENTS_ONE, content);
// Upload a new copy with the force flag, allowed
@@ -606,7 +606,7 @@ public class WikiRestApiTest extends BaseWebScriptTest
content = getPageAtVersion(name, "1.1", Status.STATUS_OK);
assertEquals(PAGE_CONTENTS_CHANGED, content);
content = getPageAtVersion(name, "1.0", Status.STATUS_OK);
// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing
assertEquals(PAGE_CONTENTS_ONE, content);
// You get an empty string back for invalid versions
content = getPageAtVersion(name, "1.4", Status.STATUS_OK);