Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Trying to archive assets based on prism:expiration date using scheduler, but its not working ,please guide me where I went wrong?

Avatar

Level 2

package com.AcHotels.core.servlets;

 

 

import java.time.OffsetDateTime;

import java.util.HashMap;

import java.util.Map;

import javax.jcr.Node;

import javax.jcr.NodeIterator;

import javax.jcr.Property;

import javax.jcr.PropertyIterator;

import javax.jcr.Session;

import javax.jcr.Value;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.servlets.HttpConstants;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.*;

import com.AcHotels.core.services.search.AssetsSearch;

import com.adobe.granite.asset.api.AssetManager;

import com.day.cq.search.PredicateGroup;

import com.day.cq.search.Query;

import com.day.cq.search.QueryBuilder;

import com.day.cq.search.result.Hit;

import com.day.cq.search.result.SearchResult;

 

@component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "= Asset Expired Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/servlet/expired" })

public class AssetMigrationServlet extends SlingSafeMethodsServlet {

 

private static final long serialVersionUID = 7524136240611031422L;

private static final Logger LOGGER = LoggerFactory.getLogger(AssetMigrationServlet.class);

 

@reference

private QueryBuilder builder;

@reference

AssetsSearch assetSearch;

 

private static final String CONTENT_PATH = "/content/dam/AC-Hotels-Marriott/home-page/images/";

 

@Override

protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {

LOGGER.info("Asset Expired Servlet Called successfully!! ");

 

try {

ResourceResolver resourceResolver = request.getResourceResolver();

AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

 

Session session = resourceResolver.adaptTo(Session.class);

if (session.nodeExists(CONTENT_PATH)) {

 

Node parentNode = session.getNode(CONTENT_PATH);

 

if (parentNode.hasNodes()) {

NodeIterator childNode = parentNode.getNodes();

 

while (childNode.hasNext()) {

Node assetNode = childNode.nextNode();

String assetName = assetNode.getName();

 

String assetPath = CONTENT_PATH + assetName;

String movePath = "/content/dam/AC-Hotels-Marriott/archive/expired/" + assetName;

 

if (assetName.equalsIgnoreCase("jcr:content")) {

LOGGER.info("JCR:CONTENT Node found, hence skipped movement...");

} else {

//assetManager.moveAsset(assetPath, movePath);

final PredicateGroup pGroup= assetSearch.createExpireAssetsQuery(OffsetDateTime.now(), "/content/dam/AC-Hotels-Marriott/home-page/images/", true);

final List<Hit> expiredAssets=assetSearch.searchAssets(pGroup, resourceResolver);

final int hitscount=expiredAssets.size();

for(final Hit expiredAsset :expiredAssets) {

final Node node=expiredAsset.getNode();

assetManager.moveAsset(assetPath, movePath);

LOGGER.info("Asset Node"+node);

}

 

response.getWriter().println("***********************************Asset With Name: "

+ assetPath + " is Moved to: " + movePath);

 

}

}

}

}

session.save();

session.logout();

} catch (Exception e) {

e.printStackTrace();

}

 

}

 

}

4 Replies

Avatar

Employee Advisor

@jyo2902 The code snippet added over here is a servlet and not a scheduler. Would you be able to add Scheduler Code?

Also, please add if you have AEM On Prem set up or AEMaaCs

Avatar

Level 2

 I am trying this in local aem as cloud instance.

   I created a servlet to perform that asset search and movement and yet to write scheduler code but getting this error for performing asset movement in 

jyo2902_0-1685534446112.png

core pom and main pom given this latest version in dependency but still getting this error

jyo2902_1-1685534503393.png

Please let me know how to write scheduler for this .

@krati_garg 

 

Avatar

Level 2

jyo2902_0-1685593070063.png

Its aem sdk only its pointing to ,no uber jar given but that issue still persists

@krati_garg