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.

How to set a URL attribute of WTPartUsageLink?

smorla
9-Granite

How to set a URL attribute of WTPartUsageLink?

Hi,

I have a soft attribute (URL) for WTPartUsageLink called LabelControl.

Under the Uses tab of Structure tab of a WTPart, I want to be able to set a URL based on the label entered in the URL Label  field.

The label will be the name of a WTDocument in Windchill and the URL is the details page of the WTDocument.

I have tried to give the label and the URL using the following code, but the value of the attribute is not reflecting:

     WTPartUsageLink usageLink = WTPartUsageLink.newWTPartUsageLink(part, part.getMaster());

     part = (WTPart) WorkInProgressHelper.service.checkout(part, WorkInProgressHelper.service.getCheckoutFolder(), "").getWorkingCopy();

     PersistableAdapter obj = new PersistableAdapter(usageLink, null, Locale.US, new UpdateOperationIdentifier());

     obj.load("LabelControl");

     String url = getDetailsURL(doc); //gets the url of the WTDocument

     Hyperlink link = new Hyperlink(url, label);

     obj.set("LabelControl", link);

     obj.apply();

     PersistenceHelper.manager.modify(usageLink);

     WorkInProgressHelper.service.checkin(part,"success part");

How do I do this?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

That's because you are trying to persist the usagelink that was obtained before you checked out the WTPart. 


Follow this sequence


  1. get the correct parent
  2. check it out
  3. filter the child from the persist able array - p[1]
  4. get the usage link from the persist able - p[0]
  5. Set the attribute. 

View solution in original post

5 REPLIES 5

I did not test this, but looking at your code, you are trying to get the link between the part and its own master. That is not the right usage of this API. It should be newWTPartUsageLink(WTPart usedBy, WTPartMaster uses)

Thank you for the reply.

I have updated the code as:

    QueryResult result = WTPartHelper.service.getUsesWTPartMasters(part);

    part = (WTPart) WorkInProgressHelper.service.checkout(part, WorkInProgressHelper.service.getCheckoutFolder(), "").getWorkingCopy();

    while(result.hasMoreElements()){

        WTPartUsageLink usageLink = (WTPartUsageLink) result.nextElement();

        PersistableAdapter obj = new PersistableAdapter(usageLink, null, Locale.US, new UpdateOperationIdentifier());

        obj.load("LabelControl");

        Hyperlink link = new Hyperlink(url, label);

        obj.apply();

        PersistenceHelper.manager.modify(part);

    }

    WorkInProgressHelper.service.checkin(part,"success part");

I am still unable to update the value of Label Control. However, if I use PersistableAdapter to get the value of LabelControl - it works perfectly fine. However, setting the value doesn't seem to work at all.

Try something like this and it should work.

boolean flag = false;
WTPartUsageLink usageLink = new WTPartUsageLink();

try {

    System.out.println("Querying for the part " + part.getIdentity());
    if (!WorkInProgressHelper.isCheckedOut(part)) {
        part = (WTPart) WorkInProgressHelper.service
            .checkout(part, WorkInProgressHelper.service.getCheckoutFolder(), "checked out")
            .getWorkingCopy();
    }
    QueryResult queryResult = WTPartHelper.service.getUsesWTParts(part, new LatestConfigSpec());
    while (queryResult.hasMoreElements()) {
        WTPart cpart = null;
        Persistable[] persistable = (Persistable[]) queryResult.nextElement();
        cpart = (WTPart) persistable[1];
        System.out.println("Child Part " + cpart.getNumber());
        usageLink = (WTPartUsageLink) persistable[0];
        PersistableAdapter obj = new PersistableAdapter(usageLink, null, Locale.US,
            new UpdateOperationIdentifier());
        obj.load("testurl");
        obj.set("testurl", "https:\\\\google.com\\ (Google)");
        usageLink = (WTPartUsageLink) obj.apply();
        wt.fc.PersistenceHelper.manager.modify(usageLink);
        flag = true;
    }

    if (WorkInProgressHelper.isCheckedOut(part)) {
        WorkInProgressHelper.service.checkin(part, "");
    }

} catch (WTException | WTPropertyVetoException e) {
    e.printStackTrace();
}
return flag;
}
}

This worked. perfectly well.

However, now I just need a little tweak.

What is have right now is a handle on the WTPartUsageLink object and not the WTPart. My code, now, looks like this. I don't want to set the value for all of usage links associated with that parent - but just the one I've got.

     WTPart parent = usageLink.getUsedBy();

     try {

          if (!WorkInProgressHelper.isCheckedOut(parent)) {

               parent = (WTPart) WorkInProgressHelper.service.checkout(parent, WorkInProgressHelper.service.getCheckoutFolder(), "").getWorkingCopy();

          }

     PersistableAdapter obj = new PersistableAdapter(usageLink, null, Locale.US, new UpdateOperationIdentifier());

     obj.load("LabelControl");

     Hyperlink URL = new Hyperlink(link, label);

     obj.set("LabelControl", URL);

     usageLink = (WTPartUsageLink) obj.apply();

     PersistenceHelper.manager.modify(usageLink); //this is the line that gives me the exception

     if (WorkInProgressHelper.isCheckedOut(parent)) {

          WorkInProgressHelper.service.checkin(parent, "Parent checkin");

          }

     } catch (WTPropertyVetoException | WTException e) {

          e.printStackTrace();

     }

That's because you are trying to persist the usagelink that was obtained before you checked out the WTPart. 


Follow this sequence


  1. get the correct parent
  2. check it out
  3. filter the child from the persist able array - p[1]
  4. get the usage link from the persist able - p[0]
  5. Set the attribute. 
Top Tags