cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Finding values of global enumerations with Java

SOLVED
Participant

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Finding values of global enumerations with Java

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! 

View solution in original post

2 REPLIES 2

Re: Finding values of global enumerations with Java

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.

Florent ROUSSEL
www.4cad.ca

Re: Finding values of global enumerations with Java

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! 

View solution in original post

Announcements
LiveWorx Call For Papers Happening Now!