Finding values of global enumerations with Java
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Finding values of global enumerations with Java
All,
is there anyone who has managed to find out how to go from a TypeIdentifier to an attribute that uses a Global Enumeration and also retrieve the values for that enumeration?
The closest I've gotten to a suggested solution is what is shown in this support case (CS133237) but it assumes that I have an object from which I can get the values. I would like to be able to start with only the TypeIdentifier or even better just the type id (for instance wt.part.WTPart).
Any ideas?
Regards
Daniel Södling
Solved! Go to Solution.
- Labels:
-
General Customization
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you for the suggestion. I couldn't get that exact method to work but it did put me on the right path and I ended up with the following. Note that this collects both global enumerations and legal value lists.
public RequestResponse handleRequest() {
logger.trace("handleRequest()");
HashMap<String, List<LocalizedValue>> result = new HashMap<>();
try {
Locale loc = SessionHelper.getLocale();
String type = request.getParameter("type");
WCTypeIdentifier wti = new WCTypeIdentifier(type);
AttributeTypeIdentifier[] attrs = TypeInstanceHelper.getSoftAttributeTypeIdentifiers(wti);
Map<AttributeTypeIdentifier, AttributeTypeSummary> summaries = AttributeTypeSummaryHelper.SERVICE.getATS(Arrays.asList(attrs), loc);
for (AttributeTypeIdentifier ati : attrs) {
logger.debug("attr: {}", ati.getAttributeName());
AttributeDefinitionReadView attrDef = TypeDefinitionServiceHelper.service.getAttributeDefView(ati);
ConstraintDefinitionReadView globalEnumConstraint = EnumerationConstraintHelper.getGlobalEnumeratedValueListConstraint(attrDef);
EnumerationDefinitionReadView enumDef = globalEnumConstraint != null ? globalEnumConstraint.getEnumDef() : null;
Map<String, EnumerationEntryReadView> entries = enumDef != null ? enumDef.getAllEnumerationEntries() : null;
DataSet ds = (DataSet) summaries.get(ati).getLegalValueSet();
ArrayList<LocalizedValue> values = new ArrayList<>();
// Collect gloal enumeration entties
if (entries != null) {
for (Map.Entry<String, EnumerationEntryReadView> entry : entries.entrySet()) {
EnumerationEntryReadView value = entry.getValue();
PropertyValueReadView display = value.getPropertyValueByName("displayName");
String localizedDisplay = display.getValueAsString(loc, true);
values.add(new LocalizedValue(entry.getKey(), localizedDisplay));
}
}
// Collect legal value list entties
if (ds != null && ds instanceof DiscreteSet) {
DiscreteSet set = (DiscreteSet) ds;
for (Object el : set.getElements()) {
String strValue = el.toString();
values.add(new LocalizedValue(strValue, strValue));
}
}
if (values.size() > 0) {
result.put(ati.getAttributeName(), values);
}
}
} catch (WTException ex) {
throw new RuntimeException(ex);
}
return success(result);
}
Thanks again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi Daniel,
Take a look on this helper: com.ptc.core.lwc.client.util.EnumerationConstraintHelper.
There is at least the method getEnumInternalAndDisplayValuesFromConstraints() that might help you.
www.4cad.ca
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you for the suggestion. I couldn't get that exact method to work but it did put me on the right path and I ended up with the following. Note that this collects both global enumerations and legal value lists.
public RequestResponse handleRequest() {
logger.trace("handleRequest()");
HashMap<String, List<LocalizedValue>> result = new HashMap<>();
try {
Locale loc = SessionHelper.getLocale();
String type = request.getParameter("type");
WCTypeIdentifier wti = new WCTypeIdentifier(type);
AttributeTypeIdentifier[] attrs = TypeInstanceHelper.getSoftAttributeTypeIdentifiers(wti);
Map<AttributeTypeIdentifier, AttributeTypeSummary> summaries = AttributeTypeSummaryHelper.SERVICE.getATS(Arrays.asList(attrs), loc);
for (AttributeTypeIdentifier ati : attrs) {
logger.debug("attr: {}", ati.getAttributeName());
AttributeDefinitionReadView attrDef = TypeDefinitionServiceHelper.service.getAttributeDefView(ati);
ConstraintDefinitionReadView globalEnumConstraint = EnumerationConstraintHelper.getGlobalEnumeratedValueListConstraint(attrDef);
EnumerationDefinitionReadView enumDef = globalEnumConstraint != null ? globalEnumConstraint.getEnumDef() : null;
Map<String, EnumerationEntryReadView> entries = enumDef != null ? enumDef.getAllEnumerationEntries() : null;
DataSet ds = (DataSet) summaries.get(ati).getLegalValueSet();
ArrayList<LocalizedValue> values = new ArrayList<>();
// Collect gloal enumeration entties
if (entries != null) {
for (Map.Entry<String, EnumerationEntryReadView> entry : entries.entrySet()) {
EnumerationEntryReadView value = entry.getValue();
PropertyValueReadView display = value.getPropertyValueByName("displayName");
String localizedDisplay = display.getValueAsString(loc, true);
values.add(new LocalizedValue(entry.getKey(), localizedDisplay));
}
}
// Collect legal value list entties
if (ds != null && ds instanceof DiscreteSet) {
DiscreteSet set = (DiscreteSet) ds;
for (Object el : set.getElements()) {
String strValue = el.toString();
values.add(new LocalizedValue(strValue, strValue));
}
}
if (values.size() > 0) {
result.put(ati.getAttributeName(), values);
}
}
} catch (WTException ex) {
throw new RuntimeException(ex);
}
return success(result);
}
Thanks again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @dsodling, the method getAllEnumerationEntries() returns the list but is there any way to get the exact order of the enumeration list?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @MV_10441462
You have to reorder it by your self.
You can get the order information from the entry or value.
try to investigate the PropertyValueReadView display = virew.getPropertyValueByName("displayName");
I would say that you can get the order by the property name. but who knows. 😄
I would expect that the order is already done because the propertyValueReadView contains SortedPropertyValueMap
PetrH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I agree with PetrH on this.
Thanks Petr, for the quick reply.
