Skip to main content
13-Aquamarine
May 3, 2022
Solved

How do I get the previous version of a part?

  • May 3, 2022
  • 1 reply
  • 5632 views

 I am trying to retrieve the predecessor version (A.1 -> B.1) of a part. Current solution using VersionControlHelper.service.predecessorOf(part) only returns the previous iteration not version. Is there an api or a specific way to retrieve the previous part object? 

Best answer by HelesicPetr

Hi,

Yes this is a correct API. I use allVersionsFrom().

If you need previous revision you need to go throw all versions. They are usually sort by creation time. 

First version is the latest one. 

Check one by one VersionSortId if it is changed then it must be previous revision.

 

My solution to get previous revision. (code works for all objects not just WTPart...)

 

private static RevisionControlled getprevisousRevisionOfWTPart(RevisionControlled lastWTP)
{
RevisionControlled retValue = null;
String actualRev = lastWTP.getVersionIdentifier().getVersionSortId();

if (lastWTP != null)
{
try
{
QueryResult allVersions = VersionControlHelper.service.allVersionsFrom(lastWTP);
RevisionControlled prevRevision = null;

while (allVersions.hasMoreElements())
{
Object obj = allVersions.nextElement();
if (obj instanceof RevisionControlled)
{
String checkRevision = ((RevisionControlled) obj).getVersionIdentifier().getVersionSortId();
if (checkRevision.equals(actualRev))
{
System.out.println("next version");
} else
{
prevRevision = (RevisionControlled) obj;
break;
}
}
}

if (prevRevision != null)
{
retValue = prevRevision;
}
} catch (WTException e)
{
e.printStackTrace();
}
}
return retValue;
}

Hope this can help.

PetrH

1 reply

13-Aquamarine
May 4, 2022

Hi,

 

I use the following API to get the previous revision's object.

QueryResult qs = wt.vc.VersionControlHelper.service.allVersionsOf(part.getMaster());


The qs includes all version objects as descending order.
So first element of the qs is latest version.

HelesicPetr
22-Sapphire II
22-Sapphire II
May 4, 2022

Hi,

Yes this is a correct API. I use allVersionsFrom().

If you need previous revision you need to go throw all versions. They are usually sort by creation time. 

First version is the latest one. 

Check one by one VersionSortId if it is changed then it must be previous revision.

 

My solution to get previous revision. (code works for all objects not just WTPart...)

 

private static RevisionControlled getprevisousRevisionOfWTPart(RevisionControlled lastWTP)
{
RevisionControlled retValue = null;
String actualRev = lastWTP.getVersionIdentifier().getVersionSortId();

if (lastWTP != null)
{
try
{
QueryResult allVersions = VersionControlHelper.service.allVersionsFrom(lastWTP);
RevisionControlled prevRevision = null;

while (allVersions.hasMoreElements())
{
Object obj = allVersions.nextElement();
if (obj instanceof RevisionControlled)
{
String checkRevision = ((RevisionControlled) obj).getVersionIdentifier().getVersionSortId();
if (checkRevision.equals(actualRev))
{
System.out.println("next version");
} else
{
prevRevision = (RevisionControlled) obj;
break;
}
}
}

if (prevRevision != null)
{
retValue = prevRevision;
}
} catch (WTException e)
{
e.printStackTrace();
}
}
return retValue;
}

Hope this can help.

PetrH

13-Aquamarine
May 4, 2022

How can I get the part based on the version method? predecessorof() gets this in WTPart or any object I want