Expand my Community achievements bar.

SOLVED

Inconsistent Property Update in Custom Workflow for Livecopy Pages:

Avatar

Level 1

We have created custom workflow to rollout the page to country site and cq:lastModifiedBy and cq:lastRolledoutBy properties needs to be updated with logged in user once the workflow executed. Currently we have written the below code to update the properties for all the livecopy pages but only few livecopy pages are getting updated with logged in user and other livecopy pages are updating with writeservice user. 

 

What could be solution for this can anyone suggest here.

Code reference:

public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        try(ResourceResolver writeService = adminService.getWriteService()) {
                String currentUserID = StringUtils.EMPTY;
    UserManager userManager = writeService.adaptTo(UserManager.class);
    Authorizable authorizable;
        authorizable = userManager.getAuthorizable(item.getWorkflow().getInitiator());
        if (null != userManager.getAuthorizable(authorizable.getID())) {
            currentUserID = authorizable.getID();
        }
            final WorkflowData workflowData = item.getWorkflowData();
            final PageManager pageManager = writeService.adaptTo(PageManager.class);
            final String path = workflowData.getPayload().toString();
            final boolean rollOutPageFlag = rolloutPage(path, writeService, pageManager,currentUserID);
            if(!rollOutPageFlag){
                throw new WorkflowException("Unable to process further workflow from RolloutWorkflow Process : RolloutManager IS NULL");
            }
        }catch (RepositoryException e) {
            LOG.error("Repo login exception",e);
        }
    }
     
    private boolean rolloutPage(String pagePath, ResourceResolver writeService, PageManager pageManager, String currentUserID) throws WorkflowException, RepositoryException {
        boolean rollOutPageFlag = Boolean.FALSE;
        LOG.debug("-----------------Start rollout for page= "+pagePath+" ---------------------------");
        try {
            if (Objects.nonNull(rolloutManager) && Objects.nonNull(pageManager)) {
                final Page currentPage = pageManager.getPage(pagePath);
                final RolloutManager.RolloutParams rolloutParams = new RolloutManager.RolloutParams();
                rolloutParams.isDeep = false;
                rolloutParams.master = currentPage;
                rolloutParams.reset = false;
                rolloutParams.trigger = RolloutManager.Trigger.ROLLOUT;
                rolloutManager.rollout(rolloutParams);
                LOG.debug("----------------------------------Rollout Complete-------------------------------");
                rollOutPageFlag = Boolean.TRUE;
                final Session session = writeService.adaptTo(Session.class);
                Node jcrNode = session.getNode(pagePath +CommonConstants.SLASH_STRING+ JcrConstants.JCR_CONTENT);
                if (jcrNode!=null) {
                    LOG.debug("##################################JcrNode cq:lastModifiedBy before save =="+jcrNode.getProperty("cq:lastModifiedBy").getValue()+" ########################################################");
                    jcrNode.setProperty("cq:lastRolledoutBy", currentUserID);
                    LOG.debug("-------------------------------------"+pagePath +"== [ cq:lastRolledoutBy ] ==" + currentUserID+"-----------------------------");
                    jcrNode.setProperty("cq:lastModifiedBy",currentUserID);
                    LOG.debug("----------------------------------"+pagePath +"== [ cq:lastModifiedBy ] ==" + currentUserID+"----------------------------------");
                    session.save();
                    LOG.debug("################################# JcrNode cq:lastModifiedBy after save =="+jcrNode.getProperty("cq:lastModifiedBy").getValue()+" ########################################################");
                }
                final Resource resource = writeService.getResource(pagePath);
                final RangeIterator liveRelationships = liveRelationshipManager.getLiveRelationships(resource, null, RolloutManager.Trigger.ROLLOUT);
                while (liveRelationships.hasNext()) {
                    final LiveRelationship liveCopyPath = (LiveRelationship) liveRelationships.next();
                    final String targetPath = liveCopyPath.getTargetPath();
                    LOG.debug("-----------------------Target path ="+targetPath+" CurrentUserID= "+currentUserID+" -----------------------");
                    rolloutPage(targetPath, writeService, pageManager,currentUserID);
                }
                LOG.debug("---------------------------method end pagePath =="+pagePath+" ------------------------------------");
            }else {
                LOG.debug("RolloutWorkflow Process : RolloutManager IS NULL");
            }
            } catch (WCMException e) {
                throw new WorkflowException("Unable to process further workflow from RolloutWorkflow Process : RolloutManager IS NULL");
            }
        LOG.debug("---------------------------------RolloutPageFLag ="+rollOutPageFlag+" ------------------------------");
        return rollOutPageFlag;
    }

:

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @divya4,

 

The currentUserID is determined based on the initiator of the workflow - Are we sure the service user is not triggering the workflow? Can you check the logs and tell the authorizable and UserID value when the page is being modified by the service user?

View solution in original post

2 Replies

Avatar

Administrator

@Rohan_Garg @pulkitvashisth @Harwinder-singh @BrianKasingli When you have a chance, could you please review this question and offer your perspective? I'd love to hear your insights.



Kautuk Sahni

Avatar

Correct answer by
Community Advisor

Hi @divya4,

 

The currentUserID is determined based on the initiator of the workflow - Are we sure the service user is not triggering the workflow? Can you check the logs and tell the authorizable and UserID value when the page is being modified by the service user?