cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

Windchill WTDocument Usage Update API

bmessenger
7-Bedrock

Windchill WTDocument Usage Update API

Hopefully someone can tell me what I am doing incorrectly.  I use API to uypdate a part to part usage (parent /child) link with this code

 

				WTPart partParent = getPartByNumber(sParentPartNo);
				Folder checkoutFolder = wt.vc.wip.WorkInProgressHelper.service.getCheckoutFolder();
				CheckoutLink col = null;
				col = WorkInProgressHelper.service.checkout((WTPart)partParent,checkoutFolder,"");
				WTPart partParent2 = (WTPart)col.getWorkingCopy();
				
				
				//Ensure you get the link AFTER checking out the parent part otherwise you cannot update it.
				WTPartUsageLink link = getPartPartLinkFromProductionOrderNo(sParentPartNo, sChildPartNumber, lOldLineNumber, sProdOrderNo);
				Quantity partQty = new Quantity();
				partQty.setAmount(dQty);
				if(sUnit.equals(""))
					partQty.setUnit(QuantityUnit.EA);
				else
					partQty.setUnit(QuantityUnit.toQuantityUnit(sUnit));
				
//				System.out.println("ABout to set qty Prod Order part part link");
				link.setQuantity(partQty);
//				System.out.println("Set qty Prod Order part part link");
				if(lNewLineNumber != -1 && lNewLineNumber != lOldLineNumber)
				{
//					System.out.println("About to set line number Prod Order part part link");
					LineNumber ln = new LineNumber();
					ln.setValue(lNewLineNumber);
					link.setLineNumber(ln);
//					System.out.println("Set line number Prod Order part part link");
				}
				
				for(i=0;i<sAttributeName.length;i++)
				{
//					System.out.println("Doing attribute i=" + i);
					PersistableAdapter obj = new PersistableAdapter(link,null,Locale.US,new UpdateOperationIdentifier());
					obj.load(sAttributeName[i]);
					Object objAttributeValue = null;
					switch(sAttributeType[i])
					{
						case "string":
						default:
						if(sAttributeValue[i].equals("null"))
							objAttributeValue = null;
						else
							objAttributeValue = sAttributeValue[i];
						break;
						case "date":
						case "datetime":
							objAttributeValue = GetDateFromString(sAttributeValue[i], "dd/MM/yyyy hh:mm:ss a");
							break;
						case "bool":
						case "boolean":
							objAttributeValue = Boolean.valueOf(sAttributeValue[i]);
							break;
						case "int":
						case "integer":
							objAttributeValue = Integer.valueOf(sAttributeValue[i]);
							break;
						case "doub":
						case "double":
						case "real":
						case "float":
							objAttributeValue = com.ptc.core.meta.common.FloatingPoint.valueOf(sAttributeValue[i]);
							break;					
					}
//					System.out.println("About to set attribute i=" + i);
					obj.set(sAttributeName[i], objAttributeValue);
					obj.apply();
//					System.out.println("Set attribute i=" + i);
				}
//				System.out.println("About to set originator on parent");
				PersistableAdapter obj2 = new PersistableAdapter(partParent2,null,Locale.US,new UpdateOperationIdentifier());
				obj2.load("Originator");
				obj2.set("Originator", sUser);
				obj2.apply();
//				System.out.println("Set originator on parent");
				PersistenceHelper.manager.modify(partParent2);				
				
				PersistenceHelper.manager.modify(link);
				PersistenceHelper.manager.refresh(link);
				
//				System.out.println("About to checkin parent");
				partParent2 = (WTPart)WorkInProgressHelper.service.checkin(partParent2,sCheckInComments);
//				System.out.println("Done checkin parent");

 

 

Basically, I check out the parent part, get the part usage link, update attributes on the part usage link (note I do NOT upadte attributes on the parent part), modify the parent part, modify the part usage link, refresh the part usage link and then check in the parent part. All this works beautifully.

 

But if I try the same with a WTDocumentUsageLink is fails on the link.modify step. This is my code for document to document parent child usage link, which is identical to the part usage except for documents.

				 WTDocumentMaster masterChild = (WTDocumentMaster)DocChild.getMaster();
				 WTDocument DocParent = getDocumentByNumber(sParentDocNo);
				WTDocumentUsageLink linknew = new WTDocumentUsageLink();

				 
				  Folder checkoutFolder = wt.vc.wip.WorkInProgressHelper.service.getCheckoutFolder();
				  CheckoutLink col = null;
				  col = WorkInProgressHelper.service.checkout((WTDocument)DocParent,checkoutFolder,"");
					System.out.println("About to get doc parent working copy " + DocParent.getNumber());
				  WTDocument DocParent2 = (WTDocument)col.getWorkingCopy();
					System.out.println("Got doc parent working copy " + DocParent2.getNumber());
					System.out.println("About to get link doc parent to child");
				 WTDocumentUsageLink link = getDocDocUsageLink(sParentDocNo, sChildDocNumber);
					System.out.println("Got link doc parent to child with id " + link.getDisplayIdentity());
//					PersistenceHelper.manager.delete(link);
				  for(i=0;i<sAttributeName.length;i++)
				  {
					  System.out.println("Got to attributes with name = " + sAttributeName[i]);
					  PersistableAdapter obj = new PersistableAdapter(linknew,null,Locale.US,new UpdateOperationIdentifier());
					  obj.load(sAttributeName[i]);
					  Object objAttributeValue = null;
					  switch(sAttributeType[i])
					  {
					  case "string":
					  default:
						  if(sAttributeValue[i].equals("null"))
							  objAttributeValue = null;
						  else
							  objAttributeValue = sAttributeValue[i];
						  break;
					  case "date":
					  case "datetime":
						  objAttributeValue = GetDateFromString(sAttributeValue[i], "dd/MM/yyyy hh:mm:ss a");
						  System.out.println("Got date attribute with value " + objAttributeValue.toString());
						  break;
					  case "bool":
					  case "boolean":
						  objAttributeValue = Boolean.valueOf(sAttributeValue[i]);
						  break;
					  case "int":
					  case "integer":
						  //System.out.println("Got to attributes with int value = " + sAttributeValue[i]);
						  objAttributeValue = Integer.valueOf(sAttributeValue[i]);
						  break;
					  case "long":
						  //System.out.println("Got to attributes with int value = " + sAttributeValue[i]);
						  objAttributeValue = Long.valueOf(sAttributeValue[i]);
						  break;
					  case "doub":
					  case "double":
					  case "real":
					  case "float":
						  objAttributeValue = com.ptc.core.meta.common.FloatingPoint.valueOf(sAttributeValue[i]);
						  break;

					  }
					  System.out.println("About to set attribute " + sAttributeName[i]);
					  obj.set(sAttributeName[i], objAttributeValue);
					  System.out.println("About to apply attribute " + sAttributeName[i]);
					  obj.apply();
					  System.out.println("Attribute applied " + sAttributeName[i]);
				  }
				  PersistenceHelper.manager.modify(DocParent2);				
				  System.out.println("Modified doc parent");

/*				  linknew.setTypeDefinitionReference(getTypeDef(sDocUsageType));
				  linknew.setRoleAObject(DocParent2);
				  linknew.setRoleBObject(masterChild);
*/	
				  //link = (WTDocumentUsageLink)PersistenceHelper.manager.modify(link);
//				  PersistenceHelper.manager.modify(link);
//PersistenceHelper.manager.modify(DocParent2);				
/*System.out.println("About to store link");
link = (WTDocumentUsageLink)PersistenceHelper.manager.store(linknew);
System.out.println("About to refresh link");
PersistenceHelper.manager.refresh(linknew);
*/
/*link = (WTDocumentUsageLink)PersistenceHelper.manager.store(link); */
System.out.println("About to modify link");
PersistenceHelper.manager.modify(link);
PersistenceHelper.manager.refresh(link);
				  System.out.println("Modified link");
/*				  PersistenceHelper.manager.refresh(link);
				  System.out.println("Refreshed link");
*/				  DocParent2 = (WTDocument)WorkInProgressHelper.service.checkin(DocParent2,sCheckInComments);
				  System.out.println("Checked in parent");

 

I know there are a lot of comments because I am going crazy to see the differences. But if fails on the line 

 

PersistenceHelper.manager.modify(link);

 

 

I know I have the correct WTDocumentUsage link because I print out the ID (idA2A2) and it is the correct one. (Plus I do it identically to the WTPartUsage which does work)

 

The error it raises is 

wt.verification.VerificationException: Illegal Operation.
at wt.verification.VerificationHelper.verify(VerificationHelper.java:138)
at wt.access.StandardAccessControlManager._hasAccess(StandardAccessControlManager.java:10496)
at wt.access.StandardAccessControlManager.checkAccess(StandardAccessControlManager.java:7812)

 

The WTDocumentUsage seems to work differently to the WTPartUsage in the Windchil fronbt end too. Any customn attributes (IBA's) you place against the link you can view in the uses tab but you cannot edit them, whereas with WTPartUsage you can edit them from the front end.

 

The only way I can update a WTDocumentUsage through API is to get the link, delete it and create a new one with the updated values. Maybe this is the only way it can be done, but I would think that WTPartUsage and WTDocumentUsage would inherit fromt he same parent object and operate identically otherwsie.

 

Anybody managed to solve this, please put me out of my misery 😫

 

 

2 REPLIES 2
akhan_eplm
5-Regular Member
(To:bmessenger)

Hi,

Hopefully, you have found solution for this issue. If yes, could you please let me know the solution?

Actually, I am also facing similar (wt.verification.VerificationException: Illegal Operation), when I move a part from library to product container and then perform revise on part. The revise operation fails with this exception.


Thanks

Hi,

I have not found a solution other than deleting the existing document usage link and recreating with the modified attributes.

 

For your issue with the moving of the part, do you

1. Move the part from product to library and then check in

2. Then revise the part

 

Or are you trying to revise the part before you have checked in?

 

I actually do not have any code that I have developed for moving parts around, so maybe you do not need to check it out to move. I'm just guessing but it is possible that the error is a generic type of java error and they are unrelated to what we are doing.

 

It is extremely frustrating that things do not work consustently across documents and parts (or everywhere to be perfectly honest). Most things I achieve through a frustrating series of trials and errors.

 

I don;t know if this is useful but this is the function I use to revise a document (I know you are talking a part but you may be able to replace document with part). Don't worry about the iWebAppId parameter. I simply use that to determine what user I place against the operation.

	 public Boolean setDocumentRevision(String sDocNumber, String sRevision, int iWebAppId) throws WTException, RemoteException, InvocationTargetException
	 {
        if (!RemoteMethodServer.ServerFlag)
        {
            RemoteMethodServer ms = RemoteMethodServer.getDefault();
            Class[] argTypes = {String.class, String.class, int.class};
            Object[] argValues = {sDocNumber, sRevision, iWebAppId};
            ms.invoke("setDocumentRevision", null, this, argTypes, argValues);
            return true;
        }

        SessionContext.newContext();
        String sUsername = getBackendUser(iWebAppId);
        SessionHelper.manager.setPrincipal(sUsername);

        Transaction trans = new Transaction();
	        
        try
        {
	        trans.start();
			WTDocument doc = getDocumentByNumber(sDocNumber);
			if(doc != null)
			{
//				System.out.println("about to version document");
				MultilevelSeries mls = MultilevelSeries.newMultilevelSeries("wt.series.HarvardSeries.RegainStateBased", sRevision);
				VersionIdentifier version_id = VersionIdentifier.newVersionIdentifier(mls);
				IterationIdentifier iteration_id = VersionControlHelper.firstIterationId(doc);

//				System.out.println("about to store document");
				WTDocument newVersion = (WTDocument)VersionControlHelper.service.newVersion(doc, version_id, iteration_id);
				newVersion = (WTDocument)PersistenceHelper.manager.store(newVersion);

//				System.out.println("got document with revision ");						
			}
		 
			trans.commit();
			trans = null;
			return true;
	    }
	    catch(Throwable t) 
	    {
	        throw new ExceptionInInitializerError(t);
	    } 
	    finally 
	    {
	        if(trans != null) 
	        {
	        	trans.rollback();
	        }
	    }	        
	 }

 

Sorry I cannot be of any more help. Essentially my answer to your question is NO on both counts.

 

Ben

 

Top Tags