
com.adobe.fd.fp.util.FormsPortalCleanUpTask Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2015-2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.fd.fp.util;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.fd.fp.model.DraftMetadata;
public class FormsPortalCleanUpTask implements Runnable {
//TODO this thread doesn't use any external service reference api's,in case it requires to do so,this would require
//the context to be pushed along with the service call which requires resourceResolverHelper.callWith() api
//this is a note to be considered while making any changes in this file after march 2016
private final Logger log = LoggerFactory.getLogger(FormsPortalCleanUpTask.class);
private Session serviceSession;
private SlingRepository slingRepository;
public FormsPortalCleanUpTask(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
@Override
public void run() {
try{
//explore every draft and if userdataId not found delete the draft
this.serviceSession = PortalUtils.getFnDServiceUserSession(slingRepository);
if(serviceSession!= null) {
JSONArray drafts = getAllDrafts(FormsPortalConstants.STR_USERDATA_ID);
for (int i = 0; i < drafts.length(); i++) {
JSONObject draftJSON = drafts.getJSONObject(i);
String userDataId = null;
if(draftJSON.has(FormsPortalConstants.STR_USERDATA_ID)) {
userDataId = draftJSON.getString(FormsPortalConstants.STR_USERDATA_ID);
}
//only delete attachment data and metadata as the draftdata is anyways not present for scrap drafts
if( userDataId == null || userDataId.isEmpty()) {
if(draftJSON.has(FormsPortalConstants.STR_PATH)) {
String draftNodePath = draftJSON.getString(FormsPortalConstants.STR_PATH);
//this is an encoded url path so the special character in the path would have been encoded if present,so decode it before use
draftNodePath = URLDecoder.decode(draftNodePath, "UTF-8");
Node draftNode = JcrUtils.getNodeIfExists(draftNodePath, serviceSession);
if(draftNode!=null && draftNode.hasProperty(FormsPortalConstants.STR_ATTACHMENT_LIST)) {
Value [] propValueList = draftNode.getProperty(FormsPortalConstants.STR_ATTACHMENT_LIST).getValues();
for(Value propVals : propValueList){
String propValStr = propVals.getString();
String attachmentId = draftNode.getProperty(propValStr).getString();
Node attachmentNode = JcrUtils.getNodeIfExists(attachmentId, serviceSession);
if(attachmentNode != null) {
attachmentNode.remove();
}
}
}
serviceSession.getNode(draftNodePath).remove();
}
}
}
//save the session after deleting the scrap drafts
serviceSession.save();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
//logout from serviceSession leave no dangling resources behind
if(serviceSession != null && serviceSession.isLive()) {
serviceSession.logout();
}
}
}
//Api to get all drafts from all users from "/content/forms/fp" assuming heirarchy goes like content/forms/fp/userNode/drafts/metadata
public JSONArray getAllDrafts(String cutPoints) {
JSONArray draftsResult = new JSONArray();
try{
List draftsList = new ArrayList();
Node root = serviceSession.getNode(FormsPortalConstants.STR_CONTENT_FORMS_FP);
Iterator children = root.getNodes();
//basically each node is a userNode
while(children.hasNext()) {
//check if the metadata node exists for the user
String draftMetadataNodePath = children.next().getPath() + "/drafts/metadata";
Node draftsMetaDataRootNode = JcrUtils.getNodeIfExists(draftMetadataNodePath, serviceSession);
if(draftsMetaDataRootNode != null) {
draftsList.addAll(readDrafts(draftsMetaDataRootNode, cutPoints));
}
}
for (DraftMetadata draft: draftsList) {
JSONObject draftsJson = draft.getJSONObject();
draftsResult.put(draftsJson);
}
return draftsResult;
} catch(Exception e){
log.error("error occured while getting all drafts", e);
}
return draftsResult;
}
//returns the draftsJson from the metadataRootNode given along with the cutpoints
private List readDrafts(Node draftsMetaDataRootNode, String cutPoints) throws ValueFormatException, RepositoryException, Exception {
List draftsList = new ArrayList();
NodeIterator itr = draftsMetaDataRootNode.getNodes();
while(itr.hasNext()) {
Node draftNode = itr.nextNode();
DraftMetadata draft = new DraftMetadata();
if (draftNode.hasProperty(FormsPortalConstants.STR_GUIDE_NAME)) {
draft.setName((String)PropertyUtils.getPropertyValue(draftNode.getProperty(FormsPortalConstants.STR_GUIDE_NAME)));
} else if (draftNode.hasProperty(FormsPortalConstants.STR_FORM_NAME)){
draft.setName((String)PropertyUtils.getPropertyValue(draftNode.getProperty(FormsPortalConstants.STR_FORM_NAME)));
}
if (draftNode.hasProperty(DraftMetadata.OWNER)) {
draft.setOwner((String)PropertyUtils.getPropertyValue(draftNode.getProperty(DraftMetadata.OWNER)));
}
Object lastModifiedObj = PropertyUtils.getPropertyValue(draftNode.getProperty(FormsPortalConstants.STR_JCR_LAST_MODIFIED));
Date lastModifiedDate = null;
if(lastModifiedObj instanceof Date){
lastModifiedDate = (Date)lastModifiedObj;
} else if(lastModifiedObj instanceof String){
lastModifiedDate = new Date(Long.valueOf((String)lastModifiedObj));
} else if(lastModifiedObj instanceof Long){
lastModifiedDate = new Date((Long) lastModifiedObj);
}
draft.setLastModified(lastModifiedDate);
if (draftNode.hasProperty(DraftMetadata.DESCRIPTION)) {
draft.setDescription((String)PropertyUtils.getPropertyValue(draftNode.getProperty(DraftMetadata.DESCRIPTION)));
}
if (draftNode.hasProperty(DraftMetadata.DRAFT_ID)) {
draft.setDraftID((String)PropertyUtils.getPropertyValue(draftNode.getProperty(DraftMetadata.DRAFT_ID)));
}else {
continue;
}
List cutPointsList = Arrays.asList(cutPoints.split(","));
for(String cutPoint : cutPointsList){
if(draftNode.hasProperty(cutPoint)){
draft.setCustomProperty(cutPoint, String.valueOf(PropertyUtils.getPropertyValue(draftNode.getProperty(cutPoint))));
} else if (cutPoint.equals(FormsPortalConstants.STR_NAME)) {
draft.setCustomProperty(FormsPortalConstants.STR_NAME, draft.getName());
} else {
draft.setCustomProperty(cutPoint, "");
}
}
String draftNodePath = draftNode.getPath();
String[] draftNodeSplit = draftNodePath.split("/");
// /content/forms/fp//drafts/metadata/
// Only encoding username in this case as per current requiremnet. will be at 4th index (0 based index).
if(draftNodeSplit != null){
draftNodeSplit[4] = URLEncoder.encode(draftNodeSplit[4], "UTF-8");
}
draftNodePath = StringUtils.join(draftNodeSplit, "/");
draft.setPath(draftNodePath);
draftsList.add(draft);
}
return draftsList;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy