tl60614
tl60614

Reputation: 43

Adobe CQ/AEM - Groovy to Activate a page

I’m very new to Groovy. Using Groovy I am trying to determine a page’s activation status and also activate/deactivate a page. This is the core elements of my code and it’s throwing an exception.

import com.day.cq.replication.Replicator;
import javax.jcr.Session;

Session session = slingRequest.getResourceResolver().adaptTo(Session.class);

path='/content/geometrixx/en/';

def getStats = getReplicationStatus(session, path);
def rp = replicate(session, "ACTIVATE", path);

Here's the exception it throws:

groovy.lang.MissingMethodException: No signature of method: Script1.getReplicationStatus() is applicable for argument types: ($Proxy10, java.lang.String) values: [session-admin-977089, /content/geometrixx/en/]
at Script1.run(Script1.groovy:9)

I tried a few things but they don't seem to be working. replicate returns void but that didn't seem to change anything. Declaring if of Replicator doesn't seem to work. It's almost like it can't find the import.

Help would be appreciated.

Upvotes: 1

Views: 4147

Answers (3)

Dawid Lewandowski
Dawid Lewandowski

Reputation: 186

Groovy Console provides some extension methods. One of them are:

activate(String path) - Activate the node at the given path.

activate(String path, ReplicationOptions options) - Activate the node at the given path with supplied options.

deactivate(String path) - Deactivate the node at the given path.

deactivate(String path, ReplicationOptions options) - Deactivate the node at the given path with supplied options.

So your script should look like the following one:

activate('/content/geometrixx/en/')

Full list of the methods is available in Methods tab on Groovy Console page (/etc/groovyconsole.html).

Upvotes: 1

tl60614
tl60614

Reputation: 43

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationStatus;
import javax.jcr.Session;  
import java.util.GregorianCalendar;

def replicator = sling.getService(Replicator.class);
def replicationstatus = sling.getService(ReplicationStatus.class);
def path = '/content/geometrixx/en/';

// Find current Replication Status
rs = replicator.getReplicationStatus(session, path);

GregorianCalendar date = rs.getLastPublished();

println("Published on: " + date.getTime());
println("Last Published By: " + rs.getLastPublishedBy());
println("Path is activated: " + rs.isActivated());
println("Path is delivered: " +rs.isDelivered());


// Activate the Page
def rp = replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

// Verify new Replication Status
rs = replicator.getReplicationStatus(session, path);

date = rs.getLastPublished();

println("Published on: " + date.getTime());
println("Last Published By: " + rs.getLastPublishedBy());
println("Path is activated: " + rs.isActivated());
println("Path is deactivated: " + rs.isDeactivated());

Upvotes: 1

Ignat Sachivko
Ignat Sachivko

Reputation: 354

There are couple of issues in the code you provided:

  1. Methods getReplicationStatus() and replicate() are not defined in the scope.
    To use replicate() you need to get com.day.cq.replication.Replicator object.
 def replicator = sling.getService(Replicator.class)
  1. com.day.cq.replication.Replicator.replicate() method has different signature comparing with which you are using.
    There is need to provide ReplicationActionType.ACTIVATE object instead of "ACTIVATE" String.
public abstract void replicate(Session paramSession, ReplicationActionType paramReplicationActionType, String paramString)

  1. "session" variable already defined in the scope, no need to get it from ResourceResolver.

Final code sample:

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType
import javax.jcr.Session;

def replicator = sling.getService(Replicator.class)
def path = '/content/geometrixx/en/';
def rp = replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

Upvotes: 0

Related Questions