Skip to main content
10-Marble
Jan 10, 2020 9:25 PM UTC
Solved

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

dsodlingAnswer
10-Marble
Jan 11, 2020 11:33 AM UTC

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! 

7 replies

3-Newcomer
Jan 10, 2020 10:11 PM UTC

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.

Translate
dsodling10-MarbleAuthorAnswer
10-Marble
Jan 11, 2020 11:33 AM UTC

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! 

Translate
12-Amethyst
Jan 28, 2025 9:38 AM UTC

Hi @dsodling, the method getAllEnumerationEntries() returns the list but is there any way to get the exact order of the enumeration list? 

Translate