From ccf4f1cd5448b84a676984d5d661ea23cb6f4fd9 Mon Sep 17 00:00:00 2001 From: Tuna Aksoy Date: Tue, 2 Apr 2013 22:21:56 +0000 Subject: [PATCH] RM-664 (Not possible to edit event without changing the name) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.0@48749 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../rm-service-context.xml | 207 +++++++++--------- .../rm-webscript-context.xml | 147 +++++++------ .../event/RecordsManagementEventService.java | 56 +++-- .../RecordsManagementEventServiceImpl.java | 179 +++++++++------ .../script/admin/RMEventBase.java | 67 ++++++ .../script/admin/RmEventPut.java | 124 ++++++----- .../script/admin/RmEventsPost.java | 139 +++++++----- 7 files changed, 542 insertions(+), 377 deletions(-) create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RMEventBase.java diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-service-context.xml b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-service-context.xml index d4858561ef..06f21ffa6f 100644 --- a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-service-context.xml +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-service-context.xml @@ -5,25 +5,25 @@ - - + + + class="org.alfresco.module.org_alfresco_module_rm.disposition.DispositionSelectionStrategy" > - + - + + depends-on="org_alfresco_module_rm_dictionaryBootstrap" init-method="init"> - + org.alfresco.module.org_alfresco_module_rm.RecordsManagementService @@ -87,26 +87,26 @@ org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.createRecordCategory=RM.Write.0 org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.isRecordFolderDeclared=RM.Read.0 org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.isRecordFolderClosed=RM.Read.0 - org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.createRecordFolder=RM.Write.0 - org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.getRecords=RM.Read.0,AFTER_RM.FilterNode + org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.createRecordFolder=RM.Write.0 + org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.getRecords=RM.Read.0,AFTER_RM.FilterNode org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.getRecordMetaDataAspects=RM_ALLOW - org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.getRecordFolders=RM.Read.0,AFTER_RM.FilterNode + org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.getRecordFolders=RM.Read.0,AFTER_RM.FilterNode org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.isRecordDeclared=RM.Read.0 org.alfresco.module.org_alfresco_module_rm.RecordsManagementService.*=RM_DENY ]]> - + - - + + - + org.alfresco.module.org_alfresco_module_rm.vital.VitalRecordService @@ -146,25 +146,25 @@ - - + - - + + - + - + org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService @@ -206,32 +206,32 @@ - + - - + + - + org.alfresco.module.org_alfresco_module_rm.capability.CapabilityService @@ -277,12 +277,12 @@ - + - - + + - + @@ -297,7 +297,7 @@ { "records" : true, "vitalrecords" : true - } + } }, { "name" : "Undeclared Records", @@ -305,7 +305,7 @@ "search" : "ISNODE:T AND NOT ASPECT:\"rma:declaredRecord\"", "searchparams" : { - "records" : true, + "records" : true, "recordfolders" : false, "undeclaredrecords" : true } @@ -357,9 +357,9 @@ ] ]]> - + - + org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService @@ -404,29 +404,29 @@ org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.getSavedSearches=RM_ALLOW org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.getSavedSearch=RM_ALLOW org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.saveSearch=RM_ALLOW - org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.deleteSavedSearch=RM_ALLOW - org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.addReports=RM_ALLOW + org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.deleteSavedSearch=RM_ALLOW + org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.addReports=RM_ALLOW org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchService.*=RM_DENY ]]> - + - - + + - + - + org.alfresco.module.org_alfresco_module_rm.security.RecordsManagementSecurityService @@ -466,11 +466,11 @@ - - + - - + + - + org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionService @@ -520,7 +520,7 @@ - + @@ -533,7 +533,7 @@ - - + - + - + org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService @@ -584,7 +584,7 @@ - + @@ -597,7 +597,7 @@ - - + - + @@ -627,11 +628,11 @@ rma:recordCategory rma:recordFolder rma:record - rma:nonElectronicDocument + rma:nonElectronicDocument - + org.alfresco.module.org_alfresco_module_rm.RecordsManagementAdminService @@ -659,7 +660,7 @@ - + @@ -672,7 +673,7 @@ - - + - + - + - + @@ -731,7 +732,7 @@ - + org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditService @@ -758,7 +759,7 @@ - + @@ -771,7 +772,7 @@ - - + - - @@ -800,10 +801,10 @@ - + - - + + @@ -814,7 +815,7 @@ - + @@ -827,7 +828,7 @@ - - + @@ -855,10 +856,10 @@ - + - - + + @@ -869,17 +870,17 @@ - - + + - + - + @@ -888,12 +889,12 @@ - + - + - + @@ -901,7 +902,7 @@ - + @@ -909,11 +910,11 @@ - + - + @@ -926,8 +927,8 @@ - - + @@ -958,7 +959,7 @@ - + @@ -976,7 +977,7 @@ - + @@ -989,5 +990,5 @@ - - \ No newline at end of file + + \ No newline at end of file diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml index 2e94106163..30e2dba468 100644 --- a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml @@ -7,7 +7,7 @@ - + @@ -20,7 +20,7 @@ - + @@ -29,25 +29,25 @@ - + - + - + - + @@ -64,7 +64,7 @@ - + @@ -84,24 +84,24 @@ - + - + - + - + - + @@ -112,21 +112,21 @@ - + - + - + @@ -135,11 +135,11 @@ - + - + @@ -148,17 +148,17 @@ - + - + - + @@ -167,7 +167,7 @@ - + @@ -176,7 +176,7 @@ - + @@ -185,18 +185,18 @@ - + - + - + @@ -204,91 +204,94 @@ - + - + - + - - + + - + - + - + - - - + - + + + + - + - - + + - - + + - - + + - + - + - + - + - + - + @@ -297,7 +300,7 @@ - + @@ -312,7 +315,7 @@ - + @@ -325,32 +328,32 @@ - + - - - - - - - @@ -361,22 +364,22 @@ - - - - - + \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventService.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventService.java index a32ff9f879..b8252ea8bb 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventService.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventService.java @@ -22,79 +22,89 @@ import java.util.List; /** * Records management event service interface - * + * * @author Roy Wetherall */ public interface RecordsManagementEventService { /** * Register an event type - * + * * @param eventType event type */ void registerEventType(RecordsManagementEventType eventType); - + /** * Get a list of the event types - * + * * @return List list of the event types */ List getEventTypes(); - + /** * Get the records management event type - * + * * @param eventType name - * @return RecordsManagementEventType event type + * @return RecordsManagementEventType event type */ RecordsManagementEventType getEventType(String eventTypeName); - + /** * Get the list of available events - * + * * @return List list of events */ List getEvents(); - + /** * Get a records management event given its name. Returns null if the event name is not * recognised. - * + * * @param eventName event name * @return RecordsManagementEvent event */ RecordsManagementEvent getEvent(String eventName); - + /** * Indicates whether a perticular event exists. Returns true if it does, false otherwise. - * + * * @param eventName event name * @return boolean true if event exists, false otherwise */ boolean existsEvent(String eventName); - + /** - * Indicates whether a particular event display label exists. Returns true if it does, false otherwise. - * + * Indicates whether a particular event can be created. Returns true if it is possible, false otherwise. + * * @param eventDisplayLabel event display label - * @return true if event display label exists, false otherwise + * @param eventName event name + * @return true if the event can be created, false otherwise */ - boolean existsEventDisplayLabel(String eventDisplayLabel); - + boolean canCreateEvent(String eventDisplayLabel, String eventName); + + /** + * Indicates whether a particular event can be edited. Returns true if it is possible, false otherwise. + * + * @param eventDisplayLabel event display label + * @param eventName event name + * @return true if the event can be edited, false otherwise + */ + boolean canEditEvent(String eventDisplayLabel, String eventName); + /** * Add an event - * + * * @param eventType event type * @param eventName event name * @param eventDisplayLabel event display label */ RecordsManagementEvent addEvent(String eventType, String eventName, String eventDisplayLabel); - + /** * Remove an event - * + * * @param eventName event name */ - void removeEvent(String eventName); + void removeEvent(String eventName); } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventServiceImpl.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventServiceImpl.java index bc83fe244b..1e5ce1753e 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventServiceImpl.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/event/RecordsManagementEventServiceImpl.java @@ -33,12 +33,13 @@ import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.util.ParameterCheck; import org.json.JSONArray; import org.json.JSONObject; /** * Records management event service implementation - * + * * @author Roy Wetherall */ public class RecordsManagementEventServiceImpl implements RecordsManagementEventService @@ -46,32 +47,32 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent /** Reference to the rm event config node */ private static final StoreRef SPACES_STORE = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"); private static final NodeRef CONFIG_NODE_REF = new NodeRef(SPACES_STORE, "rm_event_config"); - + /** Node service */ private NodeService nodeService; - + /** Content service */ private ContentService contentService; - + /** Registered event types */ private Map eventTypes = new HashMap(7); - + /** Available events */ private Map events; - + /** * Set the node service - * + * * @param nodeService node service */ public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } - + /** * Set the content service - * + * * @param contentService content service */ public void setContentService(ContentService contentService) @@ -94,7 +95,7 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { return new ArrayList(this.eventTypes.values()); } - + /** * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#getEvents() */ @@ -102,11 +103,11 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { return new ArrayList(this.getEventMap().values()); } - + /** * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#getEvent(java.lang.String) */ - public RecordsManagementEvent getEvent(String eventName) + public RecordsManagementEvent getEvent(String eventName) { if (getEventMap().containsKey(eventName) == false) { @@ -114,7 +115,7 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent } return getEventMap().get(eventName); } - + /** * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#existsEvent(java.lang.String) */ @@ -122,24 +123,76 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { return getEventMap().containsKey(eventName); } - - /** - * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#existsEventDisplayLabel(java.lang.String) - */ - @SuppressWarnings("rawtypes") - public boolean existsEventDisplayLabel(String eventDisplayLabel) - { - for (Iterator iterator = getEventMap().values().iterator(); iterator.hasNext();) - { - RecordsManagementEvent recordsManagementEvent = (RecordsManagementEvent) iterator.next(); - if (recordsManagementEvent.getDisplayLabel().equalsIgnoreCase(eventDisplayLabel)) - { - return true; - } - } - return false; - } - + + /** + * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#canCreateEvent(java.lang.String, java.lang.String) + */ + @SuppressWarnings("rawtypes") + public boolean canCreateEvent(String eventDisplayLabel, String eventName) + { + ParameterCheck.mandatoryString("eventDisplayLabel", eventDisplayLabel); + ParameterCheck.mandatoryString("eventName", eventName); + + boolean canCreateEvent = true; + + if (existsEvent(eventName) == true) + { + canCreateEvent = false; + } + + if (canCreateEvent == true) + { + for (Iterator iterator = getEventMap().values().iterator(); iterator.hasNext();) + { + RecordsManagementEvent recordsManagementEvent = (RecordsManagementEvent) iterator.next(); + if (recordsManagementEvent.getDisplayLabel().equalsIgnoreCase(eventDisplayLabel)) + { + canCreateEvent = false; + break; + } + } + } + + return canCreateEvent; + } + + /** + * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#canEditEvent(java.lang.String, java.lang.String) + */ + @SuppressWarnings("rawtypes") + public boolean canEditEvent(String eventDisplayLabel, String eventName) + { + ParameterCheck.mandatoryString("eventDisplayLabel", eventDisplayLabel); + ParameterCheck.mandatoryString("eventName", eventName); + + boolean canEditEvent = true; + + if (existsEvent(eventName) == false) + { + throw new AlfrescoRuntimeException("The event '" + eventName + "' does not exist."); + } + + for (Iterator iterator = getEventMap().values().iterator(); iterator.hasNext();) + { + RecordsManagementEvent recordsManagementEvent = (RecordsManagementEvent) iterator.next(); + if (recordsManagementEvent.getDisplayLabel().equalsIgnoreCase(eventDisplayLabel)) + { + if (recordsManagementEvent.getName().equalsIgnoreCase(eventName)) + { + canEditEvent = false; + } + else + { + throw new AlfrescoRuntimeException("Cannot edit event. An event with the display label '" + + eventDisplayLabel + "' already exist."); + } + break; + } + } + + return canEditEvent; + } + /** * @see org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService#addEvent(java.lang.String, java.lang.String, java.lang.String) */ @@ -149,19 +202,19 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent if (eventTypes.containsKey(eventType) == false) { throw new AlfrescoRuntimeException( - "Can not add event because event " + - eventName + - " has an undefined eventType. (" + "Can not add event because event " + + eventName + + " has an undefined eventType. (" + eventType + ")"); } - + // Create event and add to map RecordsManagementEvent event = new RecordsManagementEvent(eventType, eventName, eventDisplayLabel); getEventMap().put(event.getName(), event); - + // Persist the changes to the event list saveEvents(); - + return new RecordsManagementEvent(eventType, eventName, eventDisplayLabel); } @@ -172,14 +225,14 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { // Remove the event from the map getEventMap().remove(eventName); - + // Persist the changes to the event list saveEvents(); - } - + } + /** * Helper method to get the event map. Loads initial instance from persisted configuration file. - * + * * @return Map map of available events by event name */ private Map getEventMap() @@ -190,7 +243,7 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent } return this.events; } - + /** * Load the events from the persistant storage */ @@ -205,46 +258,46 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { throw new AlfrescoRuntimeException("Unable to find records management event configuration node."); } - + // Read content from config node ContentReader reader = contentService.getReader(CONFIG_NODE_REF, ContentModel.PROP_CONTENT); String jsonString = reader.getContentString(); - + JSONObject configJSON = new JSONObject(jsonString); JSONArray eventsJSON = configJSON.getJSONArray("events"); - + events = new HashMap(eventsJSON.length()); - + for (int i = 0; i < eventsJSON.length(); i++) { // Get the JSON object that represents the event JSONObject eventJSON = eventsJSON.getJSONObject(i); - + // Get the details of the event String eventType = eventJSON.getString("eventType"); String eventName = eventJSON.getString("eventName"); String eventDisplayLabel = eventJSON.getString("eventDisplayLabel"); - + // Check that the eventType is valid if (eventTypes.containsKey(eventType) == false) { throw new AlfrescoRuntimeException( - "Can not load rm event configuration because event " + - eventName + - " has an undefined eventType. (" + "Can not load rm event configuration because event " + + eventName + + " has an undefined eventType. (" + eventType + ")"); } - + // Create event and add to map RecordsManagementEvent event = new RecordsManagementEvent(eventType, eventName, eventDisplayLabel); - events.put(event.getName(), event); + events.put(event.getName(), event); } return null; } - + }, AuthenticationUtil.getSystemUserName()); } - + /** * Save the events to the peristant storage */ @@ -259,10 +312,10 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent { throw new AlfrescoRuntimeException("Unable to find records management event configuration node."); } - - JSONObject configJSON = new JSONObject(); + + JSONObject configJSON = new JSONObject(); JSONArray eventsJSON = new JSONArray(); - + int index = 0; for (RecordsManagementEvent event : events.values()) { @@ -270,19 +323,19 @@ public class RecordsManagementEventServiceImpl implements RecordsManagementEvent eventJSON.put("eventType", event.getType()); eventJSON.put("eventName", event.getName()); eventJSON.put("eventDisplayLabel", event.getDisplayLabel()); - + eventsJSON.put(index, eventJSON); index++; - } + } configJSON.put("events", eventsJSON); - + // Get content writer ContentWriter contentWriter = contentService.getWriter(CONFIG_NODE_REF, ContentModel.PROP_CONTENT, true); contentWriter.putContent(configJSON.toString()); - + return null; } - + }, AuthenticationUtil.getSystemUserName()); } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RMEventBase.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RMEventBase.java new file mode 100644 index 0000000000..7483eb7ef9 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RMEventBase.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2005-2013 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 . + */ +package org.alfresco.module.org_alfresco_module_rm.script.admin; + +import org.apache.commons.lang.StringUtils; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.extensions.webscripts.DeclarativeWebScript; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; + +/** + * Base class for Records management event web scripts + * + * @author Tuna Aksoy + * @since 2.1 + */ +public class RMEventBase extends DeclarativeWebScript +{ + /** + * Helper method for getting the value for a key from a JSON object + * + * @param json The request content as JSON object + * @param key The key for which the value should be retrieved (e.g. "eventName") + * @return String The value for the provided key if the key exists, null otherwise + * @throws JSONException If there is no string value for the key + */ + protected String getValue(JSONObject json, String key) throws JSONException + { + String result = null; + if (json.has(key) == true) + { + result = json.getString(key); + } + return result; + } + + /** + * Helper method for checking the key (e.g. "eventName") + * + * @param key String The key which will be checked + * @param msg String The error message to throw if the key doesn't have a value + */ + protected void doCheck(String key, String msg) + { + if (StringUtils.isBlank(key)) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, msg); + } + } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventPut.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventPut.java index 1af46bd6e6..4b44a852f6 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventPut.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventPut.java @@ -22,35 +22,31 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEvent; import org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService; -import org.springframework.extensions.webscripts.Cache; -import org.springframework.extensions.webscripts.DeclarativeWebScript; -import org.springframework.extensions.webscripts.Status; -import org.springframework.extensions.webscripts.WebScriptException; -import org.springframework.extensions.webscripts.WebScriptRequest; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.alfresco.util.ParameterCheck; 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; /** * Records management event PUT web script - * + * * @author Roy Wetherall */ -public class RmEventPut extends DeclarativeWebScript +public class RmEventPut extends RMEventBase { - @SuppressWarnings("unused") - private static Log logger = LogFactory.getLog(RmEventPut.class); - - /** Reccords management event service */ + /** Records management event service */ private RecordsManagementEventService rmEventService; - + /** * Set the records management event service - * + * * @param rmEventService */ public void setRecordsManagementEventService(RecordsManagementEventService rmEventService) @@ -58,59 +54,49 @@ public class RmEventPut extends DeclarativeWebScript this.rmEventService = rmEventService; } + /** + * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, + * org.springframework.extensions.webscripts.Status, + * org.springframework.extensions.webscripts.Cache) + */ @Override public Map executeImpl(WebScriptRequest req, Status status, Cache cache) { - Map model = new HashMap(); + ParameterCheck.mandatory("req", req); + + Map model = new HashMap(); JSONObject json = null; try { - // Event name - Map templateVars = req.getServiceMatch().getTemplateVars(); - String eventName = templateVars.get("eventname"); - if (eventName == null) - { - throw new WebScriptException(Status.STATUS_NOT_FOUND, "No event name was provided on the URL."); - } - - // Check the event exists - if (rmEventService.existsEvent(eventName) == false) - { - throw new WebScriptException(Status.STATUS_NOT_FOUND, "The event " + eventName + " does not exist."); - } - + // Convert the request content to JSON json = new JSONObject(new JSONTokener(req.getContent().getContent())); - - String eventDisplayLabel = null; - if (json.has("eventDisplayLabel") == true) + + // Check the event name + String eventName = getValue(json, "eventName"); + doCheck(eventName, "No event name was provided."); + + // Check the event display label + String eventDisplayLabel = getValue(json, "eventDisplayLabel"); + doCheck(eventDisplayLabel, "No event display label was provided."); + + // Check if the event can be edited or not + RecordsManagementEvent event = null; + if (canEditEvent(eventDisplayLabel, eventName) == true) { - eventDisplayLabel = json.getString("eventDisplayLabel"); + // Check the event type + String eventType = getValue(json, "eventType"); + doCheck(eventType, "No event type was provided."); + + // Create event + event = rmEventService.addEvent(eventType, eventName, eventDisplayLabel); } - if (eventDisplayLabel == null || eventDisplayLabel.length() == 0) + else { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No event display label provided."); + // Get event + event = rmEventService.getEvent(eventName); } - if (rmEventService.existsEventDisplayLabel(eventDisplayLabel)) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, - "Cannot edit event. The event display label '" - + eventDisplayLabel + "' already exists."); - } - - String eventType = null; - if (json.has("eventType") == true) - { - eventType = json.getString("eventType"); - } - if (eventType == null || eventType.length() == 0) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No event type provided."); - } - - - RecordsManagementEvent event = rmEventService.addEvent(eventType, eventName, eventDisplayLabel); + model.put("event", event); - } catch (IOException iox) { @@ -122,7 +108,31 @@ public class RmEventPut extends DeclarativeWebScript throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je); } - + return model; } + + /** + * Helper method for checking if an event can be edited or not. Throws an + * error if an event with the same display label already exists. + * + * @param eventDisplayLabel The display label of the event + * @param eventName The name of the event + * @return true if the event can be edited, false otherwise + */ + private boolean canEditEvent(String eventDisplayLabel, String eventName) + { + boolean canEditEvent; + + try + { + canEditEvent = rmEventService.canEditEvent(eventDisplayLabel, eventName); + } + catch (AlfrescoRuntimeException are) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, are.getLocalizedMessage()); + } + + return canEditEvent; + } } \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventsPost.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventsPost.java index 5384764881..4d5f7b2f0b 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventsPost.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/admin/RmEventsPost.java @@ -25,33 +25,29 @@ import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEvent; import org.alfresco.module.org_alfresco_module_rm.event.RecordsManagementEventService; import org.alfresco.util.GUID; -import org.springframework.extensions.webscripts.Cache; -import org.springframework.extensions.webscripts.DeclarativeWebScript; -import org.springframework.extensions.webscripts.Status; -import org.springframework.extensions.webscripts.WebScriptException; -import org.springframework.extensions.webscripts.WebScriptRequest; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.alfresco.util.ParameterCheck; +import org.apache.commons.lang.StringUtils; 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; /** - * - * + * Records management event POST web script + * * @author Roy Wetherall */ -public class RmEventsPost extends DeclarativeWebScript +public class RmEventsPost extends RMEventBase { - @SuppressWarnings("unused") - private static Log logger = LogFactory.getLog(RmEventsPost.class); - - /** Reccords management event service */ + /** Records management event service */ private RecordsManagementEventService rmEventService; - + /** * Set the records management event service - * + * * @param rmEventService */ public void setRecordsManagementEventService(RecordsManagementEventService rmEventService) @@ -59,57 +55,41 @@ public class RmEventsPost extends DeclarativeWebScript this.rmEventService = rmEventService; } - + /** + * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, + * org.springframework.extensions.webscripts.Status, + * org.springframework.extensions.webscripts.Cache) + */ @Override public Map executeImpl(WebScriptRequest req, Status status, Cache cache) { - Map model = new HashMap(); + ParameterCheck.mandatory("req", req); + + Map model = new HashMap(); JSONObject json = null; try { + // Convert the request content to JSON json = new JSONObject(new JSONTokener(req.getContent().getContent())); - - String eventName = null; - if (json.has("eventName") == true) - { - eventName = json.getString("eventName"); - } - - if (eventName == null || eventName.length() == 0) - { - // Generate the event name - eventName = GUID.generate(); - } - - String eventDisplayLabel = null; - if (json.has("eventDisplayLabel") == true) - { - eventDisplayLabel = json.getString("eventDisplayLabel"); - } - if (eventDisplayLabel == null || eventDisplayLabel.length() == 0) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No event display label provided."); - } - if (rmEventService.existsEventDisplayLabel(eventDisplayLabel)) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, - "Cannot create event. The event display label '" - + eventDisplayLabel + "' already exists."); - } - - String eventType = null; - if (json.has("eventType") == true) - { - eventType = json.getString("eventType"); - } - if (eventType == null || eventType.length() == 0) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No event type provided."); - } - + + // Get the event name + String eventName = getEventName(json); + + // Check the event display label + String eventDisplayLabel = getValue(json, "eventDisplayLabel"); + doCheck(eventDisplayLabel, "No event display label was provided."); + + // Check if the event can be created + canCreateEvent(eventDisplayLabel, eventName); + + // Check the event type + String eventType = getValue(json, "eventType"); + doCheck(eventType, "No event type was provided."); + + // Create event RecordsManagementEvent event = rmEventService.addEvent(eventType, eventName, eventDisplayLabel); + model.put("event", event); - } catch (IOException iox) { @@ -121,7 +101,48 @@ public class RmEventsPost extends DeclarativeWebScript throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je); } - + return model; } + + /** + * Helper method for getting the event name + * + * @param json The request content as JSON object + * @return String The event name. A generated GUID if it doesn't exist + * @throws JSONException If there is no string value for the key + */ + private String getEventName(JSONObject json) throws JSONException + { + String eventName = getValue(json, "eventName"); + + if (StringUtils.isBlank(eventName)) + { + // Generate the event name + eventName = GUID.generate(); + } + + return eventName; + } + + /** + * Helper method for checking if an event can be created or not. Throws an + * error if the event already exists. + * + * @param eventDisplayLabel The display label of the event + * @param eventName The name of the event + */ + private boolean canCreateEvent(String eventDisplayLabel, String eventName) + { + boolean canCreateEvent = true; + + if (rmEventService.canCreateEvent(eventDisplayLabel, eventName) == false) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, + "Cannot create event. An event with the display label '" + + eventDisplayLabel + "' already exists."); + } + + return canCreateEvent; + } } \ No newline at end of file