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);
private QueryBuilder builder;
AssetsSearch assetSearch;
private static final String CONTENT_PATH = "/content/dam/AC-Hotels-Marriott/home-page/images/";
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();
}
}
}
Views
Replies
Total Likes
@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
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
core pom and main pom given this latest version in dependency but still getting this error
Please let me know how to write scheduler for this .
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies