Merging WorkItems which are having some common Property values

There was a customer requirement to merge WorkItems which are having same Suppliercode and Location. (Suppliercode and Location are properties). To achieve that task we used component operation step with Filenet API. This is the code,

import java.util.ArrayList;
import com.filenet.api.core.Factory;
import com.filenet.wcm.api.impl.Logger;
import com.filenet.api.util.UserContext;
import filenet.vw.api.*;

public class CFBulkSubmit {

public static Logger log = Logger.getLogger(CFBulkSubmit.class);

public static VWSession getPESession() {
 String strAppURI1 = "CONNECTION_URL";  //[example :- http://17.96.45.13:9080/wsi/FNCEWS40MTOM/]
 System.out.println("[ENTER PEManager getPESession()]");

 String connectionPoint = "CONNECTION_POINT";
 String password = "PASSWORD";
 String username = "USERNAME";

System.setProperty("java.security.auth.login.config", "C:\\IBM\\FileNet\\ContentEngine\\config\\samples\\jaas.conf.WSI");
 com.filenet.api.core.Connection conn = Factory.Connection.getConnection("http://17.96.45.13:9080/wsi/FNCEWS40MTOM/");
 javax.security.auth.Subject subject = UserContext.createSubject(conn, username, password, "FileNetP8WSI");
 log.debug("Subject Created");
 UserContext uc = UserContext.get();
 uc.pushSubject(subject);

VWSession vwSession = new VWSession();

try {
 vwSession.setBootstrapCEURI(strAppURI1);
 vwSession.logon(username, password, connectionPoint);
 String sn = vwSession.getPEServerName();
 System.out.println("++++++++++++++++" + sn);

System.out.println("PE session established:" + vwSession);
 } catch (VWException e) {
 System.out.println("Exception occured while establishing PE session.");
 e.printStackTrace();

}
 return vwSession;
 }

public static void closePESession(VWSession peSession) {
 log.debug("[Enter closePESession]");
 try {
 if (peSession != null)
 peSession.logoff();
 System.out.println("[Exit : closePESession]");
 } catch (VWException e) {
 log.error(e.getMessage(), e);
 }

log.debug("[Exit : closePESession]");
 }

public static void getVWWorkObject(String wobNum, String SupplierCode, String Location, String tempAttA, String refernaceNo, String total) {
 
 if(wobNum!="" && wobNum!=null){
 try {
 VWSession session=getPESession();
 VWQueue queue1 = session.getQueue("BulkSubmit");
 VWQueueQuery query1 = queue1.createQuery(null, null,null, VWQueue.QUERY_READ_LOCKED, null, null,VWFetchType.FETCH_TYPE_WORKOBJECT);
 System.out.println("All item count in Bulk Step : " + query1.fetchCount());
 
 VWRoster roster = session.getRoster("GLPayments");
 
 int queryFlags=VWRoster.QUERY_NO_OPTIONS;
 int queryType = VWFetchType.FETCH_TYPE_WORKOBJECT;
 String queryFilter = "F_WobNum = '"+wobNum+"'";
 VWRosterQuery query = roster.createQuery(null, null, null, queryFlags, queryFilter, null, queryType);
 
 
 while(query1.hasNext()) {
 VWWorkObject workObject1 = (VWWorkObject) query1.next();
 System.out.println("Unique Supplier Code: "+ workObject1.getDataField("SupplierCode").toString());
 String UniqueSupplierCode = workObject1.getDataField("SupplierCode").toString();
 String UniqueLocation = workObject1.getDataField("Location").toString();
 String UniqueTotal = workObject1.getDataField("total").toString();
 
 if(UniqueSupplierCode.equals(SupplierCode) && UniqueLocation.equals(Location)){
 
 System.out.println("Inside of if Condition");
 VWStepElement stepElement1 = workObject1.fetchStepElement();
 VWParameter[] parameters1 = stepElement1.getParameters(VWFieldType.ALL_FIELD_TYPES, VWStepElement.FIELD_USER_AND_SYSTEM_DEFINED);
 //get the existing attachment value

VWAttachment[] tempAttA1=(VWAttachment[])stepElement1.getParameterValue("DocBulk");
 
 ArrayList<String> law=new ArrayList<String>();
 ArrayList<String> bulkReferenceNumbers=new ArrayList<String>();

stepElement1.doLock(true);
 workObject1.doLock(true);
 
 if(tempAttA1[0].toString().equals("||0|0||")){ 
 
 System.out.println("+++++++++++++++Currently DocBulk is Empty+++++++++++++++");
 String tempAttA11 = stepElement1.getParameterValue("GLMainDoc").toString();
 tempAttA=tempAttA11.substring(0, 65)+(tempAttA.substring(65));
 System.out.println("new tempAttA : "+tempAttA);
 law.add(tempAttA11);
 law.add(tempAttA); 
 
 bulkReferenceNumbers.add(workObject1.getDataField("refernaceNo").toString());
 bulkReferenceNumbers.add(refernaceNo);
 
 
 }else{
 String[] x=(String[]) workObject1.getDataField("BulkReferenceNumbers").getValue();
 for(int i=0;i<tempAttA1.length;i++){
 
 law.add(tempAttA1[i].toString());
 bulkReferenceNumbers.add(x[i].toString());
 System.out.println(tempAttA1[i].toString()+"======"+x[i].toString());
 }
 
 tempAttA=tempAttA1[0].toString().substring(0, 65)+(tempAttA.substring(65));
 System.out.println("new tempAttA : "+tempAttA);
 law.add(tempAttA); 
 
 bulkReferenceNumbers.add(refernaceNo);
 
 }
 
 workObject1.setFieldValue("DocBulk", law.toArray(), true);
 workObject1.setFieldValue("BulkReferenceNumbers", bulkReferenceNumbers.toArray(), true);
 
 int updated_total=Integer.parseInt(total)+Integer.parseInt(UniqueTotal);
 workObject1.setFieldValue("total", Integer.toString(updated_total), true);
 
 workObject1.doSave(true);
 
 while (query.hasNext())
 {
 VWWorkObject xc = (VWWorkObject)query.next();
  xc.doDelete(true, true); 
 }
 
 
 }else{
 System.out.println("Failed to find similar supplier code");
 }
 
 }


closePESession(getPESession());

} catch (VWException vwe) {
 log.error("Exception found at PEManager.getVWWorkObject():" + vwe,
 vwe);
 vwe.printStackTrace();
 } 
 }else{ 
 System.out.println("WobNum is null or empty"); 
 }
 
 }

public static void main(String args[]) {
 
 
 }
}

In here First we have to establish a connection to process engine.

Merged WorkItems are going to “BulkSubmit” queue. When new WorkItem comes to this component operation step it check the BulkSubmit Queue. Is there any Workitem with same SupplierCode and same Location, it updates some property values in BulkSubmit queue WorkItem. Updating properties are total, DocBulk, BulkReferenceNumbers. In here we put initially attached documents (“GLMainDoc”) of both cases to “DocBulk” array type Attachment field. Because we can’t have multiple initial documents for one case. And also reference numbers of merged Workitems are going to new array type property called BulkReferenceNumbers.

Then finally terminates the new comer.

If it can’t find any matching Workitem in BulkSubmit, new WorkItem goes to the next step without any change.

In here we use small trick to add attachment to WorkItem in BulkSubmit queue. (We add new comer’s attachment to BulkSubmit workItem) GLMainDoc is the initial attachment field.

System.out.println("+++++++++++++++Currently DocBulk is Empty+++++++++++++++");
 String tempAttA11 = stepElement1.getParameterValue("GLMainDoc").toString();
 tempAttA=tempAttA11.substring(0, 65)+(tempAttA.substring(65));
 System.out.println("new tempAttA : "+tempAttA);
 law.add(tempAttA11);
 law.add(tempAttA);

We get the property value “GLMainDoc” of new comer and subString from 65. Then get the property value “GLMainDoc” from BulkSubmit’s WorkItem and subString 0 to 65. Then merge these vales and pass it to DocBulk array type Attachment field. In here I found that first 65 characters are unique for particular case and other characters starting from 65th are floating. So in here I’m modifying this floating values with new comers floating values which are placed after 65th character.

So by modifying attachment property value like this you can transfer attachments between 2 cases.