Last active
December 18, 2015 04:59
-
-
Save jdorrance/5729734 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mavendc.cq.test.workflow.impl | |
import org.mavendc.cq.test.workflow.WorkflowUtils | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.jackrabbit.api.security.user.Authorizable; | |
import org.apache.jackrabbit.api.security.user.Group | |
import org.apache.jackrabbit.api.security.user.UserManager; | |
import org.apache.sling.api.resource.Resource | |
import org.apache.sling.api.resource.ResourceResolver | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import com.day.cq.dam.api.Asset; | |
import com.day.cq.wcm.api.Page; | |
import com.day.cq.workflow.WorkflowSession; | |
import com.day.cq.workflow.exec.HistoryItem | |
import com.day.cq.workflow.exec.WorkflowData; | |
import com.day.cq.workflow.exec.WorkItem; | |
@Component(immediate = true) | |
@Service | |
class WorkflowUtilsImpl implements WorkflowUtils { | |
@Reference | |
private ResourceResolverFactory resFactory; | |
private WorkItem item | |
private WorkflowSession session | |
private ResourceResolver resolver | |
public WorkflowUtilsImpl(){ | |
//THIS WILL NOT DO ANYTHING without having set w/ setWorkflow | |
} | |
@Override | |
public Boolean setWorkflow(WorkItem wkitem, WorkflowSession wkflwsession) { | |
this.item = wkitem | |
this.session = wkflwsession | |
return true | |
} | |
@Override | |
public String getStartComment() { | |
String strtCm = item?.getWorkflowData()?.getMetaDataMap().get("startComment",String.class) | |
return strtCm | |
} | |
@Override | |
public String getLatestComment() { | |
String lstCm = getLastUserHistoryItem()?.comment | |
return lstCm | |
} | |
@Override | |
public String[] getComments() { | |
def cmnts = [] | |
for(hi in session?.getHistory(item.workflow)){ | |
if(hi?.comment)cmnts.add(hi.comment) | |
} | |
return cmnts.toArray() | |
} | |
@Override | |
public String getLastContribEmail() { | |
def emails = getLastContrib()?.getProperty("profile/email") | |
if (emails as List){ | |
return emails[0].string | |
} | |
return '' | |
} | |
@Override | |
public Authorizable getInitiator() { | |
Authorizable ath = resolveAuthorizable(item.workflow.initiator) | |
return ath | |
} | |
@Override | |
public Authorizable getLastContrib() { | |
return resolveAuthorizable(getLastUserHistoryItem()?.userId) | |
} | |
@Override | |
public Page getPayloadPage() { | |
return getResource()?.adaptTo(Page.class) | |
} | |
@Override | |
public Asset getPayloadAsset() { | |
return getResource()?.adaptTo(Asset.class) | |
} | |
@Override | |
public Resource getResource() { | |
Resource res = getResolver()?.getResource(item?.workflowData?.payload?.toString()) | |
return res | |
} | |
@Override | |
public String getObjectTitle() { | |
if(this.payloadPage) return this.payloadPage.title | |
if(this.payloadAsset) return this.payloadAsset.name | |
} | |
@Override | |
public String getObjectPath() { | |
if(this.payloadPage) return this.payloadPage.path | |
if(this.payloadAsset) return this.payloadAsset.path | |
} | |
@Override | |
public String getObjectSuffix() { | |
def payloadSuffix = '.html' | |
if (this.payloadAsset) | |
payloadSuffix = '' | |
return payloadSuffix | |
} | |
@Override | |
public String getLastContribID() { | |
return this.lastContrib?.getID() | |
} | |
@Override | |
public String getInitiatorEmail() { | |
def arr = this.initiator?.getProperty("profile/email") | |
if(arr as List){ | |
return arr[0].string | |
} | |
return '' | |
} | |
@Override | |
public String getInitiatorID() { | |
String id = this.initiator?.getID() | |
return id | |
} | |
@Override | |
public String getLastContribGivenName() { | |
def arr = this.lastContrib?.getProperty("profile/givenName") | |
if(arr as List){ | |
return arr[0].string | |
} | |
return '' | |
} | |
@Override | |
public String getLastContribFamilyName() { | |
def arr = this.lastContrib?.getProperty("profile/familyName") | |
if(arr as List){ | |
return arr[0].string | |
} | |
return '' | |
} | |
@Override | |
public String getInitiatorGivenName() { | |
def arr = this.initiator?.getProperty("profile/givenName") | |
if(arr as List){ | |
return arr[0].string | |
} | |
return '' | |
} | |
@Override | |
public String getInitiatorFamilyName() { | |
def arr = this.initiator?.getProperty("profile/familyName") | |
if(arr as List){ | |
return arr[0].string | |
} | |
return '' | |
} | |
@Override | |
public Boolean closeResolver() { | |
if(resolver && resolver.live)resolver.close() | |
resolver = null | |
return true | |
} | |
@Override | |
public String getRelatedGroupID(String prefix) { | |
Authorizable init = this.getInitiator() | |
List<Group> initMemberhips = init.memberOf().toList() | |
UserManager um = getResolver()?.adaptTo(UserManager.class) | |
def authz = um.findAuthorizables('jcr:primaryType', 'rep:Group') | |
for(a in authz){ | |
if(a.group && a.getID().toLowerCase().startsWith(prefix.toLowerCase())){ | |
for(g in initMemberhips){ | |
if(a.getID().toLowerCase().endsWith(g.getID().toLowerCase())){ | |
return a.getID() | |
} | |
} | |
} | |
} | |
return null | |
} | |
private Authorizable resolveAuthorizable(String id){ | |
UserManager um = getResolver()?.adaptTo(UserManager.class) | |
return um?.getAuthorizable(id) | |
} | |
private ResourceResolver getResolver(){ | |
if(resolver && resolver.live)return resolver | |
resolver = resFactory.getAdministrativeResourceResolver(null) | |
return resolver | |
} | |
private HistoryItem getLastUserHistoryItem(){ | |
def orderedHistory = (List<HistoryItem>)session.getHistory(item.workflow)?.reverse() | |
for(hi in orderedHistory){ | |
if(hi?.userId!='system' && hi.workItem.currentAssignee != 'system' && hi !=null) return hi | |
} | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment