RM-1633 (Recorded Version Configuration Action)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@82333 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2014-09-01 21:23:08 +00:00
parent 4d1be8da79
commit c053dbc14c
7 changed files with 246 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
/*
* 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.slingshot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.script.AbstractRmWebScript;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionModel;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionPolicy;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* REST API to get the recorded version config for a document
*
* @author Tuna Aksoy
* @since 2.3
*/
public class RecordedVersionConfigGet extends AbstractRmWebScript implements RecordableVersionModel
{
/**
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
*/
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
RecordableVersionPolicy[] recordableVersionPolicies = RecordableVersionPolicy.values();
List<Map<String, Object>> recordableVersions = new ArrayList<Map<String,Object>>(recordableVersionPolicies.length);
NodeRef documentNodeRef = parseRequestForNodeRef(req);
for (RecordableVersionPolicy recordableVersionPolicy : recordableVersionPolicies)
{
recordableVersions.add(buildRecordableVersionData(recordableVersionPolicy, documentNodeRef));
}
Map<String, Object> model = new HashMap<String, Object>(1);
model.put("recordableVersions", recordableVersions);
return model;
}
/**
* Builds the recordable version data
*
* @param recordableVersionPolicy The recordable version policy
* @param nodeRef Node reference of the document
* @return A map containing the information about recordable version policy and if this policy is selected for the document
*/
private Map<String, Object> buildRecordableVersionData(RecordableVersionPolicy recordableVersionPolicy, NodeRef nodeRef)
{
Map<String, Object> recordableVersionData = new HashMap<String, Object>(2);
recordableVersionData.put("policy", recordableVersionPolicy.toString());
recordableVersionData.put("selected", isVersionPolicySelected(recordableVersionPolicy, nodeRef));
return recordableVersionData;
}
/**
* Checks if the specified recordable version policy has been selected for the document
*
* @param recordableVersionPolicy The recordable version policy
* @param nodeRef Node reference of the document
* @return <code>true</code> if the specified recordable version policy has been selected for the document, <code>false</code> otherwise
*/
private boolean isVersionPolicySelected(RecordableVersionPolicy recordableVersionPolicy, NodeRef nodeRef)
{
boolean isVersionPolicySelected = false;
String policy = (String) nodeService.getProperty(nodeRef, PROP_RECORDABLE_VERSION_POLICY);
if (StringUtils.isNotBlank(policy))
{
if (RecordableVersionPolicy.valueOf(policy).equals(recordableVersionPolicy))
{
isVersionPolicySelected = true;
}
}
else
{
if (recordableVersionPolicy.equals(RecordableVersionPolicy.NONE))
{
isVersionPolicySelected = true;
}
}
return isVersionPolicySelected;
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.slingshot;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.script.AbstractRmWebScript;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionModel;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionPolicy;
import org.alfresco.service.cmr.repository.NodeRef;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* REST API to set the recorded version config for a document
*
* @author Tuna Aksoy
* @since 2.3
*/
public class RecordedVersionConfigPost extends AbstractRmWebScript implements RecordableVersionModel
{
// Constant for recorded version parameter
private static final String RECORDED_VERSION = "recordedVersion";
/**
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
*/
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
NodeRef nodeRef = parseRequestForNodeRef(req);
RecordableVersionPolicy recordableVersionPolicy = getRecordableVersionPolicy(req);
nodeService.setProperty(nodeRef, PROP_RECORDABLE_VERSION_POLICY, recordableVersionPolicy);
return new HashMap<String, Object>(1);
}
/**
* Gets the recordable version policy from the request
*
* @param The webscript request
* @return The recordable version policy
*/
private RecordableVersionPolicy getRecordableVersionPolicy(WebScriptRequest req)
{
String recordedVersion = getRecordedVersion(req);
return RecordableVersionPolicy.valueOf(recordedVersion);
}
/**
* Gets the recorded version parameter value from the request
*
* @param req The webscript request
* @return The recorded version parameter value
*/
private String getRecordedVersion(WebScriptRequest req)
{
try
{
// Convert the request content to JSON
String content = req.getContent().getContent();
JSONObject jsonObject = new JSONObject(new JSONTokener(content));
checkMandatoryJsonParam(jsonObject, RECORDED_VERSION);
return jsonObject.getString(RECORDED_VERSION);
}
catch (JSONException | IOException ex)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", ex);
}
}
}