Search in sources :

Example 1 with ReplicationResult

use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateVersionServlet method doPost.

@Override
public final void doPost(SlingHttpServletRequest req, SlingHttpServletResponse res) throws ServletException, IOException {
    log.debug("Entering ReplicatePageVersionServlet.doPost(..)");
    String[] rootPaths = req.getParameterValues("rootPaths");
    Date date = getDate(req.getParameter("datetimecal"));
    String[] agents = req.getParameterValues("cmbAgent");
    JsonObject obj = validate(rootPaths, agents, date);
    if (!obj.has(KEY_ERROR)) {
        log.debug("Initiating version replication");
        List<ReplicationResult> response = replicateVersion.replicate(req.getResourceResolver(), rootPaths, agents, date);
        if (log.isDebugEnabled()) {
            for (final ReplicationResult replicationResult : response) {
                log.debug("Replication result: {} -- {}", replicationResult.getPath(), replicationResult.getStatus());
            }
        }
        JsonArray arr = convertResponseToJson(response);
        obj = new JsonObject();
        obj.add(KEY_RESULT, arr);
    } else {
        log.debug("Did not attempt to replicate version due to issue with input params");
        res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        obj.addProperty(KEY_STATUS, KEY_ERROR);
    }
    res.setContentType("application/json");
    res.getWriter().print(obj.toString());
}
Also used : JsonArray(com.google.gson.JsonArray) ReplicationResult(com.adobe.acs.commons.replication.ReplicationResult) JsonObject(com.google.gson.JsonObject) Date(java.util.Date)

Example 2 with ReplicationResult

use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateVersionServlet method convertResponseToJson.

private JsonArray convertResponseToJson(List<ReplicationResult> list) {
    JsonArray arr = new JsonArray();
    for (ReplicationResult result : list) {
        JsonObject resultObject = new JsonObject();
        resultObject.addProperty(KEY_PATH, result.getPath());
        resultObject.addProperty(KEY_STATUS, result.getStatus().name());
        resultObject.addProperty(KEY_VERSION, result.getVersion());
        arr.add(resultObject);
    }
    return arr;
}
Also used : JsonArray(com.google.gson.JsonArray) ReplicationResult(com.adobe.acs.commons.replication.ReplicationResult) JsonObject(com.google.gson.JsonObject)

Example 3 with ReplicationResult

use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateVersionImplTest method testReplicate.

@Test
public final void testReplicate() throws Exception {
    final String[] agents = new String[] { "agent1" };
    final String[] rootPaths = new String[] { "/content/geometrixx/en" };
    final Date date = getDate("2013-12-21T00:00:00");
    final Date vDate = getDate("2013-12-01T00:00:00");
    final Date vDate1 = getDate("2013-12-22T00:00:00");
    final Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(vDate);
    final Calendar cal1 = GregorianCalendar.getInstance();
    cal1.setTime(vDate1);
    final ResourceResolver resourceResolver = mock(ResourceResolver.class);
    final Session session = mock(Session.class);
    final Workspace wk = mock(Workspace.class);
    final VersionManager vm = mock(VersionManager.class);
    final VersionHistory vh = mock(VersionHistory.class);
    final VersionIterator vi = mock(VersionIterator.class);
    final Version v = mock(Version.class);
    final Version v1 = mock(Version.class);
    when(v.getCreated()).thenReturn(cal);
    when(v1.getCreated()).thenReturn(cal1);
    when(v.getName()).thenReturn("version1");
    when(vi.nextVersion()).thenReturn(v, v1);
    when(vi.hasNext()).thenReturn(true, true, false);
    when(session.getWorkspace()).thenReturn(wk);
    when(wk.getVersionManager()).thenReturn(vm);
    when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
    when(vm.getVersionHistory(rootPaths[0])).thenReturn(vh);
    when(vh.getAllVersions()).thenReturn(vi);
    final Resource res = mock(Resource.class);
    when(res.getPath()).thenReturn(rootPaths[0]);
    final Node node = mock(Node.class);
    when(resourceResolver.getResource(rootPaths[0])).thenReturn(res);
    when(res.adaptTo(Node.class)).thenReturn(node);
    when(session.getNode(rootPaths[0])).thenReturn(node);
    when(node.hasNode(NameConstants.NN_CONTENT)).thenReturn(true);
    final Node node1 = mock(Node.class);
    when(node1.getPath()).thenReturn(rootPaths[0]);
    when(node.getNode(NameConstants.NN_CONTENT)).thenReturn(node1);
    when(node1.isNodeType(JcrConstants.MIX_VERSIONABLE)).thenReturn(true);
    when(node.isNodeType("nt:hierarchyNode")).thenReturn(true);
    final Resource res1 = mock(Resource.class);
    final Resource res2 = mock(Resource.class);
    @SuppressWarnings("unchecked") final Iterator<Resource> itr = mock(Iterator.class);
    when(resourceResolver.listChildren(res)).thenReturn(itr);
    when(itr.hasNext()).thenReturn(true, true, false);
    when(itr.next()).thenReturn(res1, res2);
    when(res1.adaptTo(Node.class)).thenReturn(node1);
    when(res2.adaptTo(Node.class)).thenReturn(node1);
    when(node1.isNodeType("nt:hierarchyNode")).thenReturn(false);
    List<ReplicationResult> list = rpvs.replicate(resourceResolver, rootPaths, agents, date);
    Assert.assertEquals("/content/geometrixx/en", list.get(0).getPath());
    Assert.assertEquals(Status.replicated, list.get(0).getStatus());
    Assert.assertEquals("version1", list.get(0).getVersion());
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) Date(java.util.Date) ReplicationResult(com.adobe.acs.commons.replication.ReplicationResult) Version(javax.jcr.version.Version) ReplicateVersion(com.adobe.acs.commons.replication.ReplicateVersion) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) VersionIterator(javax.jcr.version.VersionIterator) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 4 with ReplicationResult

use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateVersionImpl method replicateResource.

private List<ReplicationResult> replicateResource(ResourceResolver resolver, List<Resource> resources, String[] agents, Date date) {
    List<ReplicationResult> results = new ArrayList<ReplicationResult>();
    ReplicationOptions opts = new ReplicationOptions();
    AgentIdFilter agentFilter = new AgentIdFilter(agents);
    opts.setFilter(agentFilter);
    Session session = resolver.adaptTo(Session.class);
    for (Resource resource : resources) {
        String path = resource.getPath();
        try {
            Version v = getAppropriateVersion(resource, date, session);
            if (v == null) {
                results.add(new ReplicationResult(path, Status.not_replicated));
                continue;
            }
            String versionName = v.getName();
            opts.setRevision(versionName);
            replicator.replicate(session, ReplicationActionType.ACTIVATE, path, opts);
            results.add(new ReplicationResult(path, Status.replicated, versionName));
        } catch (RepositoryException e) {
            results.add(new ReplicationResult(path, Status.error));
            log.error("Exception while replicating version of " + path, e);
        } catch (ReplicationException e) {
            results.add(new ReplicationResult(path, Status.error));
            log.error("Exception while replicating version of " + path, e);
        }
    }
    return results;
}
Also used : ReplicationResult(com.adobe.acs.commons.replication.ReplicationResult) ReplicationOptions(com.day.cq.replication.ReplicationOptions) AgentIdFilter(com.day.cq.replication.AgentIdFilter) Version(javax.jcr.version.Version) ReplicateVersion(com.adobe.acs.commons.replication.ReplicateVersion) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) ReplicationException(com.day.cq.replication.ReplicationException) Session(javax.jcr.Session)

Aggregations

ReplicationResult (com.adobe.acs.commons.replication.ReplicationResult)4 ReplicateVersion (com.adobe.acs.commons.replication.ReplicateVersion)2 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 Date (java.util.Date)2 Session (javax.jcr.Session)2 Version (javax.jcr.version.Version)2 Resource (org.apache.sling.api.resource.Resource)2 AgentIdFilter (com.day.cq.replication.AgentIdFilter)1 ReplicationException (com.day.cq.replication.ReplicationException)1 ReplicationOptions (com.day.cq.replication.ReplicationOptions)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Workspace (javax.jcr.Workspace)1 VersionHistory (javax.jcr.version.VersionHistory)1 VersionIterator (javax.jcr.version.VersionIterator)1 VersionManager (javax.jcr.version.VersionManager)1