mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
RM-2047 (Set classification repository action)
+review RM git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@102123 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.classification;
|
||||
|
||||
import static org.alfresco.util.WebScriptUtils.getJSONArrayFromJSONObject;
|
||||
import static org.alfresco.util.WebScriptUtils.getJSONArrayValue;
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestContentAsJsonObject;
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.script.AbstractRmWebScript;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* Implementation for Java backed webscript to classify a content.
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ClassifyContentPost extends AbstractRmWebScript
|
||||
{
|
||||
/** Constants */
|
||||
private static final String CLASSIFICATION_LEVEL_ID = "classificationLevelId";
|
||||
private static final String CLASSIFICATION_AUTHORITY = "classificationAuthority";
|
||||
private static final String CLASSIFICATION_REASONS = "classificationReasons";
|
||||
|
||||
/** Classification service */
|
||||
private ClassificationService classificationService;
|
||||
|
||||
/**
|
||||
* @return the classificationService
|
||||
*/
|
||||
protected ClassificationService getClassificationService()
|
||||
{
|
||||
return this.classificationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param classificationService the classificationService to set
|
||||
*/
|
||||
public void setClassificationService(ClassificationService classificationService)
|
||||
{
|
||||
this.classificationService = classificationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest,
|
||||
* org.springframework.extensions.webscripts.Status,
|
||||
* org.springframework.extensions.webscripts.Cache)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
|
||||
JSONObject jsonObject = getRequestContentAsJsonObject(req);
|
||||
String classificationLevelId = getStringValueFromJSONObject(jsonObject, CLASSIFICATION_LEVEL_ID);
|
||||
String classificationAuthority = getStringValueFromJSONObject(jsonObject, CLASSIFICATION_AUTHORITY);
|
||||
Set<String> classificationReasonIds = getClassificationReasonIds(jsonObject);
|
||||
NodeRef document = parseRequestForNodeRef(req);
|
||||
|
||||
getClassificationService().addClassificationToDocument(classificationLevelId, classificationAuthority, classificationReasonIds, document);
|
||||
model.put(SUCCESS, true);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to get the classification reason ids
|
||||
*
|
||||
* @param jsonObject The json object representing the request body
|
||||
* @return {@link Set}<{@link String}> classification ids
|
||||
*/
|
||||
private Set<String> getClassificationReasonIds(JSONObject jsonObject)
|
||||
{
|
||||
Set<String> classificationReasonIds = new HashSet<>();
|
||||
|
||||
JSONArray classificationReasons = getJSONArrayFromJSONObject(jsonObject, CLASSIFICATION_REASONS);
|
||||
for (int i = 0; i < classificationReasons.length(); i++)
|
||||
{
|
||||
JSONObject classificationReason = (JSONObject) getJSONArrayValue(classificationReasons, i);
|
||||
classificationReasonIds.add(getStringValueFromJSONObject(classificationReason, ID));
|
||||
}
|
||||
|
||||
return classificationReasonIds;
|
||||
}
|
||||
}
|
@@ -317,4 +317,30 @@ public final class WebScriptUtils
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link JSONArray} value of a given key from a json object
|
||||
*
|
||||
* @param jsonObject The json object
|
||||
* @param key The key
|
||||
* @return The {@link JSONArray} value of the given key from the json object
|
||||
*/
|
||||
public static JSONArray getJSONArrayFromJSONObject(JSONObject jsonObject, String key)
|
||||
{
|
||||
JSONArray jsonArray;
|
||||
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatory("key", key);
|
||||
|
||||
try
|
||||
{
|
||||
jsonArray = jsonObject.getJSONArray(key);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get the json array for the key '" + key + "'.", error);
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user