diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.desc.xml new file mode 100644 index 0000000000..8fa58efe57 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.desc.xml @@ -0,0 +1,10 @@ + + List Persisted Replication Definitions + + Returns a simple representation of all persisted replication definitions. + + /api/replication-definitions + + admin + required + diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.json.ftl new file mode 100644 index 0000000000..4179d09dac --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.get.json.ftl @@ -0,0 +1,10 @@ +<#import "simple-replication-definition.lib.ftl" as replicationDefLib /> +{ + "data": + [ + <#list replicationDefinitions as replicationDefinition> + <@replicationDefLib.simpleReplicationDefinitionJSON replicationDefinition=replicationDefinition /> + <#if replicationDefinition_has_next>, + + ] +} diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/simple-replication-definition.lib.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/simple-replication-definition.lib.ftl new file mode 100644 index 0000000000..ab04ca5499 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/simple-replication-definition.lib.ftl @@ -0,0 +1,11 @@ +<#-- Renders a workflow definition. --> +<#macro simpleReplicationDefinitionJSON replicationDefinition> +<#escape x as jsonUtils.encodeJSONString(x)> + { + "name": "${replicationDefinition.name}", + "status" : "${replicationDefinition.status}", + "enabled" : "${replicationDefinition.enabled}", + "details": "${replicationDefinition.details_url}", + } + + diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 492753748f..e749d8cf03 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -828,4 +828,24 @@ parent="abstractAuditWebScript"> + + + + + + + + + + + + + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/replication/AbstractReplicationWebscript.java b/source/java/org/alfresco/repo/web/scripts/replication/AbstractReplicationWebscript.java new file mode 100644 index 0000000000..35a046696e --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/replication/AbstractReplicationWebscript.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2005-2010 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.repo.web.scripts.replication; + +import java.util.List; +import java.util.Map; + +import org.alfresco.repo.action.ActionTrackingServiceImpl; +import org.alfresco.service.cmr.action.ActionTrackingService; +import org.alfresco.service.cmr.action.ExecutionDetails; +import org.alfresco.service.cmr.action.ExecutionSummary; +import org.alfresco.service.cmr.replication.ReplicationDefinition; +import org.alfresco.service.cmr.replication.ReplicationService; +import org.alfresco.service.cmr.repository.NodeService; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.DeclarativeWebScript; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * @author Nick Burch + * @since 3.4 + */ +public abstract class AbstractReplicationWebscript extends DeclarativeWebScript +{ + protected static final String MODEL_DATA_LIST = "replicationDefinitions"; + + protected static final String DEFINITION_NAME = "name"; + protected static final String DEFINITION_STATUS = "status"; + protected static final String DEFINITION_STARTED_AT = "startedAt"; + protected static final String DEFINITION_ENDED_AT = "endedAt"; + protected static final String DEFINITION_ENABLED = "enabled"; + protected static final String DEFINITION_DETAILS_URL = "details"; + + protected NodeService nodeService; + protected ReplicationService replicationService; + protected ActionTrackingService actionTrackingService; + + public void setReplicationService(ReplicationService replicationService) + { + this.replicationService = replicationService; + } + + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } + + public void setActionTrackingService(ActionTrackingService actionTrackingService) + { + this.actionTrackingService = actionTrackingService; + } + + protected String getDefinitionDetailsUrl(ReplicationDefinition replicationDefinition) + { + return "/api/replication-definition/" + replicationDefinition.getReplicationName(); + } + + /** + * Figures out the status that's one of: + * New|Running|CancelRequested|Completed|Failed|Cancelled + * by merging data from the action tracking service. + * Will also set the start and end dates, from either the + * replication definition or action tracking data, depending + * on the status. + */ + protected void setStatus(ReplicationDefinition replicationDefinition, Map model) + { + // Is it currently running? + List executing = + actionTrackingService.getExecutingActions(replicationDefinition); + if(executing.size() == 0) { + // It isn't running, we can use the persisted details + model.put(DEFINITION_STATUS, replicationDefinition.getExecutionStatus().toString()); + model.put(DEFINITION_STARTED_AT, replicationDefinition.getExecutionStartDate()); + model.put(DEFINITION_ENDED_AT, replicationDefinition.getExecutionEndDate()); + return; + } + + // We have at least one copy running + ExecutionSummary es; + if(executing.size() == 1) { + es = executing.get(0); + } else { + // More than one copy, joy + // Go for the lowest execution instance id, so + // we're predictable + es = executing.get(0); + for(ExecutionSummary e : executing) { + if(e.getExecutionInstance() < es.getExecutionInstance()) { + es = e; + } + } + } + + // Update the details based on this + ExecutionDetails details = actionTrackingService.getExecutionDetails(es); + if(details.isCancelRequested()) { + model.put(DEFINITION_STATUS, "CancelRequested"); + } else { + model.put(DEFINITION_STATUS, "Running"); + } + model.put(DEFINITION_STARTED_AT, details.getStartedAt()); + model.put(DEFINITION_ENDED_AT, null); + } +} \ No newline at end of file diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsGet.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsGet.java new file mode 100644 index 0000000000..5440cec4a3 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsGet.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2005-2010 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.repo.web.scripts.replication; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.alfresco.service.cmr.replication.ReplicationDefinition; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptRequest; + + +/** + * @author Nick Burch + * @since 3.4 + */ +public class ReplicationDefinitionsGet extends AbstractReplicationWebscript +{ + @Override + protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) + { + List definitions = replicationService.loadReplicationDefinitions(); + List> models = new ArrayList>(); + + for(ReplicationDefinition rd : definitions) { + Map rdm = new HashMap(); + + // Set the basic details + rdm.put(DEFINITION_NAME, rd.getReplicationName()); + rdm.put(DEFINITION_ENABLED, rd.isEnabled()); + rdm.put(DEFINITION_DETAILS_URL, getDefinitionDetailsUrl(rd)); + + // TODO - Make this more efficient by getting all + // the running instances in one go + setStatus(rd, rdm); + } + + // Finish up + Map model = new HashMap(); + model.put(MODEL_DATA_LIST, models); + return model; + } +} \ No newline at end of file