ALF-10109 Convert the last two wiki webscripts to be Java backed, using the new Service

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30144 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-31 16:08:58 +00:00
parent ec600def09
commit 2fbd91d3f9
9 changed files with 362 additions and 351 deletions

View File

@@ -25,7 +25,6 @@ import java.util.Map;
import org.alfresco.query.PagingRequest;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.activities.ActivityService;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.PersonService;

View File

@@ -0,0 +1,118 @@
/*
* 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.ResourceBundle;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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 wiki page renaming move.post webscript.
*
* TODO Track links to pages, so we can avoid creating the "This page has been moved"
* stubs as now, for cases where nothing links to the page being renamed. (ALF-3844)
*
* @author Nick Burch
* @since 4.0
*/
public class WikiPageMovePost extends AbstractWikiWebScript
{
private static final String MSG_MOVED = "page-moved";
private static final String MSG_MOVED_HERE = "page-moved-here";
@Override
protected Map<String, Object> executeImpl(SiteInfo site, String pageName,
WebScriptRequest req, JSONObject json, Status status, Cache cache) {
final Map<String, Object> model = new HashMap<String, Object>();
final ResourceBundle rb = getResources();
// Try to find the page we're renaming
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);
// Wrap and bail
model.put("error", message);
Map<String, Object> result = new HashMap<String, Object>();
result.put("result", model);
return result;
}
// Grab the new Title
String newTitle;
try
{
// The "name" in the JSON is actually the title!
newTitle = json.getString("name");
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// Have the page re-named, if possible
String oldTitle = page.getTitle();
try
{
page.setTitle(newTitle);
page = wikiService.updateWikiPage(page);
}
catch(FileExistsException e)
{
throw new WebScriptException(Status.STATUS_CONFLICT, "Duplicate page name");
}
// Create the "This page has been moved" entry for the old page
String movedContent = rb.getString(MSG_MOVED) + " [[" + page.getSystemName() +
"|" + rb.getString(MSG_MOVED_HERE) + "]].";
wikiService.createWikiPage(site.getShortName(), oldTitle, movedContent);
// Add an activity entry for the rename
addActivityEntry("renamed", page, site, req, json);
// All done
model.put("name", page.getSystemName());
model.put("page", page);
model.put("siteId", site.getShortName());
model.put("site", site);
// Double wrap
Map<String, Object> result = new HashMap<String, Object>();
result.put("result", model);
return result;
}
}

View File

@@ -0,0 +1,210 @@
/*
* 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.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
import org.alfresco.service.namespace.QName;
import org.json.JSONArray;
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 wiki page creating/editing page.put webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class WikiPagePut extends AbstractWikiWebScript
{
private static final String DEFAULT_PAGE_CONTENT = "This is a new page. It has no content";
private VersionService versionService;
public void setVersionService(VersionService versionService)
{
this.versionService = versionService;
}
@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 details of the change
List<String> tags = null;
String currentVersion = null;
boolean forceSave;
String contents;
String title;
try
{
// Fetch the contents
contents = json.getString("pagecontent");
// Fetch the title, used only when creating
if(json.has("title"))
{
title = json.getString("title");
}
else
{
title = pageName;
}
// Fetch the versioning details
forceSave = json.has("forceSave");
if(json.has("currentVersion"))
{
currentVersion = json.getString("currentVersion");
}
// Fetch the tags, if given
if(json.has("tags"))
{
tags = new ArrayList<String>();
if(json.get("tags").equals(""))
{
// Empty list given as a string, eg "tags":""
}
else
{
// Array of tags
JSONArray tagsA = json.getJSONArray("tags");
for(int i=0; i<tagsA.length(); i++)
{
tags.add(tagsA.getString(i));
}
}
}
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// Are we creating or editing?
WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageName);
if(page == null)
{
// Create the page
page = wikiService.createWikiPage(site.getShortName(), title, contents);
// Add tags if given
if(tags != null && tags.size() > 0)
{
page.getTags().addAll(tags);
wikiService.updateWikiPage(page);
}
// Make it versioned
makeVersioned(page);
// Generate the activity
addActivityEntry("created", page, site, req, json);
}
else
{
// Updating, check about versioning first
if(forceSave || pageVersionMatchesSubmitted(page, currentVersion))
{
// Update the page
page.setContents(contents);
if(tags != null && tags.size() > 0)
{
page.getTags().clear();
page.getTags().addAll(tags);
}
wikiService.updateWikiPage(page);
}
else
{
// Editing the wrong version
String message = "Repository version is newer.";
throw new WebScriptException(Status.STATUS_CONFLICT, message);
}
// Generate the activity
addActivityEntry("edited", page, site, req, json);
}
// All done
model.put("page", page);
model.put("site", site);
model.put("siteId", site.getShortName());
// Double wrap
Map<String, Object> result = new HashMap<String, Object>();
result.put("result", model);
return result;
}
private boolean pageVersionMatchesSubmitted(WikiPageInfo page, String currentVersion)
{
// If they didn't give version, it can't be right
if(currentVersion == null)
{
return false;
}
// Grab the current version
Version version = versionService.getCurrentVersion(page.getNodeRef());
if(version == null)
{
// It should be versioned already, fix that
makeVersioned(page);
// Wasn't versioned before, so can't detect conflict
return true;
}
// Check the label
if(version.getVersionLabel().equals(currentVersion))
{
// Match, no changes
return true;
}
else
{
// Someone else has edited it
return false;
}
}
private void makeVersioned(WikiPageInfo page)
{
Map<QName,Serializable> versionProps = new HashMap<QName, Serializable>();
versionProps.put(ContentModel.PROP_AUTO_VERSION, true);
versionProps.put(ContentModel.PROP_AUTO_VERSION_PROPS, true);
versionService.ensureVersioningEnabled(page.getNodeRef(), versionProps);
}
}

View File

@@ -523,6 +523,23 @@ public class WikiRestApiTest extends BaseWebScriptTest
assertEquals(name, page.getString("name"));
assertEquals(PAGE_TITLE_TWO, page.getString("title"));
assertEquals("This page has been moved [["+name2+"|here]].", page.getString("pagetext"));
// Ensure you can't rename onto an existing page
page = createOrUpdatePage(PAGE_TITLE_ONE, PAGE_CONTENTS_THREE, null, Status.STATUS_OK);
String name1 = PAGE_TITLE_ONE.replace(' ', '_');
// Check the rename won't work
renamePage(PAGE_TITLE_THREE, PAGE_TITLE_ONE, Status.STATUS_CONFLICT);
// Check that the pages weren't changed
page = getPage(name2, Status.STATUS_OK);
assertEquals(name2, page.getString("name"));
assertEquals(PAGE_TITLE_THREE, page.getString("title"));
page = getPage(name1, Status.STATUS_OK);
assertEquals(name1, page.getString("name"));
assertEquals(PAGE_TITLE_ONE, page.getString("title"));
}
public void testVersioning() throws Exception