Skip to main content
11-Garnet
January 25, 2017
Solved

How to set a URL attribute of WTPartUsageLink?

  • January 25, 2017
  • 1 reply
  • 4273 views

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.

Best answer by BineshKumar1

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. 

1 reply

1-Visitor
January 27, 2017

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)

smorla11-GarnetAuthor
11-Garnet
January 27, 2017

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.

1-Visitor
January 30, 2017

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;
}
}