0 Replies Latest reply on Sep 13, 2018 8:33 AM by saurabh08tiwari

    Hi I am moving an asset at that time while moving the asset one unique id generating so according to my requirement is after moving an asset I have to send notification to the owner so in that I have to send that moving asset unique id ,so after moving th

    saurabh08tiwari

      public class MoveAssetTmpToOriginalPath implements WorkflowProcess {

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

       

         @Reference
         private SessionService sessionService;

         private ResourceResolver resourceResolver;

         @Reference
         private MailService mailer;

         @Reference
         private UserPropertiesService userPropertiesService;

       

       

         @Override
         public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

         try {

         resourceResolver = getSessionService().getReadServiceResourceResolver();

        WorkflowData workflowData = null != workItem ? workItem.getWorkflowData() : null;

        String tempPath = null != workflowData ? workflowData.getPayload().toString() : null;

        String[] tempPathSplt = null != tempPath ? tempPath.split("/") : null;

        String replctAsstName = null != tempPathSplt ? tempPathSplt[tempPathSplt.length - 1] : null;

         if (StringUtils.isNotEmpty(tempPath)) {

        Resource resource = resourceResolver.resolve(tempPath);

        Node tempAsstNode = resource.adaptTo(Node.class);

         if (tempAsstNode != null && tempAsstNode.hasNode(WorkflowConstants.JCR_CONTENT)) {

        Node jcrNode = tempAsstNode.getNode(WorkflowConstants.JCR_CONTENT);

        String asstReplctKey = getReplicatAssetKey(jcrNode);

        String assetReplctPath = getAssetReplctPath(resourceResolver, asstReplctKey) + "/" + replctAsstName;

        moveReplicateAsset(tempPath, assetReplctPath);

        Thread.sleep(30000);

        sendNotificationMail(resourceResolver, assetReplctPath);

        }

        }

        } catch (Exception e) {

         LOGGER.error("Exception occured while executing {}", e);

        }

        }

       

         private String getAssetReplctPath(ResourceResolver resourceResolver, String asstReplctKey) throws RepositoryException {

        String result = WorkflowConstants.CONTENT_DAM_INTEL_DIGITAL_LIBRARY_SOURCE;

         try {

        Resource resource = resourceResolver.resolve(WorkflowConstants.ETC_PROPERTIES_REPLICATED_ASSET_PATH);

        Node mapnode = resource.adaptTo(Node.class);

         if (null != mapnode && mapnode.hasProperty(WorkflowConstants.DESTINATION_PATH)) {

        Value[] destArray = mapnode.getProperty(WorkflowConstants.DESTINATION_PATH).getValues();

        result = getDestPath(asstReplctKey, result, destArray);

        }

        } catch (Exception e) {

         LOGGER.error("An Exception occurred while getAssetReplctPath!! {}", e);

        }

         return result;

        }

       

         private static String getDestPath(String asstReplctKey, String result, Value[] destArray) throws RepositoryException {

        String val = result;

         for (Value destArrayValues : destArray) {

        String destintonPthKeyValu = destArrayValues.getString();

        String[] destValue = destintonPthKeyValu.split("=");

         if (destValue[WorkflowConstants.ZERO].equals(asstReplctKey)) {

        val = destValue[WorkflowConstants.ONE];

         break;

        }

        }

         return val;

        }

       

         private void moveReplicateAsset(String tempPath, String assetReplctPath) throws WorkflowException {

         try {

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

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

         if (null != assetManager && null != session) {

        assetManager.moveAsset(tempPath, assetReplctPath);

        session.save();

        session.refresh(true);

        }

        } catch (Exception e) {

         LOGGER.error("An Exception occurred while moveReplicateAsset!! {}", e);

        }

        }

       

         private void sendNotificationMail(ResourceResolver resourceResolver, String assetReplctPath) {

        Resource resource = resourceResolver.resolve(assetReplctPath);

        Node replictdPathNode = resource.adaptTo(Node.class);

         try {

         if (replictdPathNode != null && replictdPathNode.hasNode(WorkflowConstants.JCR_CONTENT)) {

        String assetOwnerFullName = getAssetOwnerFullName(replictdPathNode);

        String title = getAssetTitle(replictdPathNode);

        String uniqueId = getUniqueId(replictdPathNode);

        String ownerEmail = getAssetOwnerEmail(replictdPathNode);

        sendSubNotifctionToAssetOwner(ownerEmail, resourceResolver, title, assetReplctPath, uniqueId, assetOwnerFullName);

        }

        } catch (Exception e) {

         LOGGER.error("An Exception occurred while sendNotificationMail!! {}", e);

        }

        }

       

         private static String getReplicatAssetKey(Node jcrNode) {

         try {

         if (null != jcrNode && jcrNode.hasProperty(WorkflowConstants.DEST_KEY)) {

         return jcrNode.getProperty(WorkflowConstants.DEST_KEY).getString();

        }

        } catch (Exception e) {

         LOGGER.error("An Exception occurred getReplicatAssetKey!! {}", e);

        }

         return "";

        }

       

         private static String getAssetOwnerEmail(Node node) {

        String assetOwnerEmailId = "";

         try {

         if (node.hasNode(WorkflowConstants.JCR_CONTENT)) {

        Node jcrNode = node.getNode(WorkflowConstants.JCR_CONTENT);

         if (jcrNode.hasProperty(WorkflowConstants.EMAIL)) {

        assetOwnerEmailId = jcrNode.getProperty(WorkflowConstants.EMAIL).getString();

        }

        }

        } catch (Exception e) {

         LOGGER.error("Exception occured while getAssetOwnerEmail", e.getMessage(), e);

        }

         return assetOwnerEmailId;

        }

       

         public SessionService getSessionService() {

         return sessionService;

        }

       

         private void sendSubNotifctionToAssetOwner(String ownerEmail, ResourceResolver resourceResolver, String title, String assetReplctPath, String uniQueID, String assetOwnerFullName) throws LoginException, RepositoryException {

        ConcurrentMap<String, String> parameters = new ConcurrentHashMap<>();

        parameters.put(WorkflowConstants.SUBJECT, WorkflowConstants.ASSET_MOVE_INTEL_BOX_TO_INTEL_DIGITAL_LIBRARY);

        parameters.put(WorkflowConstants.ASSET_PATHS, assetReplctPath);

        parameters.put(WorkflowConstants.ASSET_TITLE, title);

        parameters.put(WorkflowConstants.UNIQUE_ID, uniQueID);

         LOGGER.debug("uniQueID::{}",uniQueID);

        parameters.put(WorkflowConstants.ASSET_OWNER_FULL_NAME, assetOwnerFullName);

        parameters.put(WorkflowConstants.TEMPLATE_ROOT_PATH, WorkflowConstants.REPLICATED_ASSET_NOTIFICATION_TO_OWNER);

        AssetMailHelper assetMailHelper = new AssetMailHelper();

         try {

        assetMailHelper.sendHtmlMail(resourceResolver, getMailer(), Collections.singletonMap(ownerEmail, ownerEmail), parameters, userPropertiesService);

        } catch (Exception e) {

         LOGGER.error("Exception occured while executing mail{}", e);

        }

        }

       

         private static String getAssetTitle(Node node) {

        String assetTitle = "";

         try {

         if (node != null && node.hasNode(WorkflowConstants.JCRCONTENT_METADATA)) {

        Node metadata = node.getNode(WorkflowConstants.JCRCONTENT_METADATA);

         if (metadata != null && metadata.hasProperty(WorkflowConstants.TITLE)) {

        assetTitle = metadata.getProperty(WorkflowConstants.TITLE).getString();

        }

        }

        } catch (Exception e) {

         LOGGER.error("Exception occured while getAssetTitle", e);

        }

         return assetTitle;

        }

       

         private static String getUniqueId(Node replictdPathNode) {

        String uniqueId = "";

         try {

         if (replictdPathNode.hasNode(WorkflowConstants.JCRCONTENT_METADATA)) {

        Node metadata = replictdPathNode.getNode(WorkflowConstants.JCRCONTENT_METADATA);

         LOGGER.debug("metadata getUniqueId::{}",metadata.getPath());

         if (metadata.hasProperty(WorkflowConstants.UNIQUE_ID)) {

        uniqueId = metadata.getProperty(WorkflowConstants.UNIQUE_ID).getString();

        }

        }

        } catch (Exception e) {

         LOGGER.error("Exception occured while getUniqueId", e.getMessage(), e);

        }

         return uniqueId;

        }

       

         private static String getAssetOwnerFullName(Node node) {

        String assetOwnerFullName = "";

         try {

         if (node.hasNode(WorkflowConstants.JCR_CONTENT)) {

        Node jcrNode = node.getNode(WorkflowConstants.JCR_CONTENT);

         if (jcrNode.hasProperty(WorkflowConstants.ASSET_OWNER_FULL_NAME)) {

        assetOwnerFullName = jcrNode.getProperty(WorkflowConstants.ASSET_OWNER_FULL_NAME).getString();

        }

        }

        } catch (Exception e) {

         LOGGER.error("Exception occured while getAssetOwnerFullName", e.getMessage(), e);

        }

         return assetOwnerFullName;

        }

       

       

         public MailService getMailer() {

         return mailer;

        }

       

         public void setSessionService(SessionService sessionService) {

         this.sessionService = sessionService;

        }

       

       

      }