Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Paras_AU
Participant
0 Kudos

Now this was while working for a prototype, I realised this drawback of MDM Rich clients, this was an attempt to exploit the datamanager to the max and have all its capabilities replicated over the web using MDM Java APIs and see if somewhere it has inbuilt ability to publish taxonomy attribute key/value pairs as one string.  

Also, there can be several business specific use-cases, the end user or your client would like to have Taxonomy attribute values attached to a record displayed on the screen or use the same to populate certain other fields in MDM Table schema. MDM Rich client (viz. DataManager..) till MDM 5.5 SP06 have this drawback of not being able to extract out Taxonomy attribute values as a concatenated string.

This is where Java means MDM Java APIs came to my rescue.MDM Java API SP06 provides certain methods, using them we can do what the Data Manager can't do !!

Using the algo/code steps beneath developers can have taxonomy attribute value pairs as a strings and use it as per their requirements:

1. As a first step while retriving the target record, we need to setLoadAttributes = true i.e while specifying the resultdefinition, as demonstrated in the code snippet below (using RetrieveRecordByIdCommand)

RetrieveRecordsByIdCommand retRecByID = new RetrieveRecordsByIdCommand(connection);

//setting the recordID of the record to retrieve and modify

rec[0] = mrecID; 

retRecByID.setId(rec); 

retRecByID.setSession(authUserSession);

mTableId = repoSchema.getTableId("Main Table");

flds[0] = repoSchema.getFieldId(repoSchema.getTableCode(mTableId),"MATERIAL_TAXONOMY");

flds[1] = repoSchema.getFieldId(repoSchema.getTableCode(mTableId),"NAME");

flds[2] = repoSchema.getFieldId(repoSchema.getTableCode(mTableId),"XYZ"); 

ResultDefinition rd = new ResultDefinition(mTableId); 

rd.setSelectFields(flds);

"rd.setLoadAttributes(true);"

retRecByID.setResultDefinition(rd);

retRecByID.execute();

2. One can use RetreieveHierAncestorsCommand to retrieve ancestors of the taxonomy node,

RetrieveHierAncestorsCommand retAncestorCommand = new RetrieveHierAncestorsCommand(connection);

retAncestorCommand.setSession(authUserSession);

ResultDefinition resultDefinition = new ResultDefinition(taxTableID);

//setting all the fieldIds in the table as part of resultdefinition

resultDefinition.setSelectFields(repoSchema.getTableSchema(taxTableID).getFieldIds());

retAncestorCommand.setResultDefinition(resultDefinition);

retAncestorCommand.setChildNode(mTaxRecId); "mTaxRecId = Taxonomy node's recordID"

retAncestorCommand.execute();

RecordResultSet ancestors = retAncestorCommand.getAncestors();

*3. As a next step, to start processing taxonomy attributes attached to a particular record, at first we </p><p>  (i) Retrieve Array of attributeIDs attached to a particular record            </p><p>      AttributeId[] txAttIds = modifyRecord.getAttributes(taxFieldID);</p><p>  (ii) Initialise RetrieveAttributeCommand for retriving attribute properties</p><p>    RetrieveAttributeCommand attProp = new RetrieveAttributeCommand(connection);</p><p>    attProp.setSession(authUserSession);</p><p>    attProp.setTaxonomyTableId(taxTableID);</p><p>  (iii) Iterate over attributeID array obtained at step(i), execute RetrieveAttributeCommand at each iteration, use attributeId to retrieve attribute value from the record object and use the attributeProperties object returned to determine the 'type' of taxonomy attribute, in turn getting a composite handle to taxonomy attribute key/value pairs attached to a main table record.....      </p><p>//Iterating Over Attribute IDs</p><p>for(int i = 0;i<txAttIds.length;i++){</p><p>try{</p><p>//retriving record(taxonomy) attribute value </p><p>aVal = modifyRecord.getAttributeValue(taxFieldID,txAttIds[i]);</p><p>if(aVal == null)</p><p>continue;</p><p>}</p><p>catch(IllegalArgumentException ie)</p><p>{   }</p><p>//setting AttributeID to retrieve correponding Attribute properties</p><p>attProp.setAttributeId(txAttIds[i]);</p><p>try</p><p>{</p><p>attProp.execute();</p><p>}catch(CommandException ce)</p><p>{  }</p><p>AttributeProperties attributeProp = attProp.getAttribute();</p><p>//Retriving Taxonomy Attribute name* 

String aName = attributeProp.getName().toString();

//Using attributeProperties object to determine the type of taxonomy attribute and fetch values for single/multi-valued attributes accordingly

if(attributeProp.getType() == AttributeProperties.TEXT_TYPE)

{

TextAttributeProperties textAttribute = (TextAttributeProperties)attributeProp;

aName = textAttribute.getName().toString();

//fetching attribute Value

if(aVal.isMultivalue())

{

StringBuffer buffer = new StringBuffer();

MultiValue multiValue = (MultiValue)aVal;

for(int k=0, j=multiValue.getValuesCount(); k<j; k+) </p><p>{</p><p>if(k>0)</p><p>{</p><p>buffer.append(",");</p><p>}</p><p>buffer.append(getValue(textAttribute, (TextAttributeValue)multiValue.getValue(k)));</p><p>}</p><p>result = buffer.toString(); </p><p>}</p><p>else</p><p>{</p><p>if(aVal.getType() == MdmValue.Type.TEXT_ATTRIBUTE) </p><p>{</p><p>result = getValue(textAttribute, (TextAttributeValue)aVal);</p><p>} </p><p>else </p><p>{</p><p>throw new IllegalArgumentException(); </p><p>}</p><p>}</p><p>}</p><p>else if(attributeProp.getType() == AttributeProperties.NUMERIC_TYPE)</p><p>{</p><p>NumericAttributeProperties numericAttribute = (NumericAttributeProperties)attributeProp;</p><p>aName = numericAttribute.getName().toString();</p><p>//fetching Numeric Attribute Value</p><p>if(aVal.isMultivalue()) </p><p>{</p><p>StringBuffer stringBuffer = new StringBuffer();</p><p>MdmValue[] values = ((MultiValue)aVal).getValues();</p><p>for(int l=0, j=values.length; l<j; l) </p><p>{</p><p>if(values[l].getType() == MdmValue.Type.MEASUREMENT) </p><p>{</p><p>if(l>0) </p><p>{</p><p>stringBuffer.append(";");</p><p>}</p><p>stringBuffer.append(getNumericAttValue(numericAttribute, (MeasurementValue)values[l]));</p><p>}</p><p>}</p><p>result = stringBuffer.toString(); </p><p>} </p><p>else </p><p>{</p><p>if(aVal.getType() == MdmValue.Type.MEASUREMENT) </p><p>{</p><p>result = getNumericAttValue(numericAttribute, (MeasurementValue)aVal);</p><p>}</p><p>else</p><p>{</p><p>throw new IllegalArgumentException();</p><p>}</p><p>} </p><p>}</p><p>else if(attributeProp.getType() == AttributeProperties.COUPLED_TYPE)</p><p>{</p><p>CoupledAttributeProperties coupledAttribute = (CoupledAttributeProperties)attributeProp;</p><p>aName = coupledAttribute.getName().toString();</p><p>//fetching Coupled Numeric Attribute Value</p><p>if(aVal.isMultivalue()) </p><p>{</p><p>StringBuffer stringBuffer = new StringBuffer();</p><p>MdmValue[] values = ((MultiValue)aVal).getValues();</p><p>for(int m=0, j=values.length; m<j; m) </p><p>{</p><p>if(values[m].getType() == MdmValue.Type.COUPLED_MEASUREMENT) </p><p>{</p><p>if(m>0) </p><p>{</p><p>stringBuffer.append(";");</p><p>}</p><p>stringBuffer.append(getCoupledAttValue(coupledAttribute, (CoupledMeasurementValue)values[m]));</p><p>} </p><p>}</p><p>result = stringBuffer.toString();</p><p>} </p><p>else </p><p>{</p><p>if(aVal.getType() == MdmValue.Type.COUPLED_MEASUREMENT) </p><p>{</p><p>result = getCoupledAttValue(coupledAttribute, (CoupledMeasurementValue)aVal);</p><p>} </p><p>else </p><p>{</p><p>throw new IllegalArgumentException();</p><p>}</p><p>}</p><p>}</p><p>Following are the helper methods, which might help in extending and executing the algo/code piece given above</p><p>//method to retrieve (String)value of a text attribute</p><p>private String getValue(TextAttributeProperties attribute, TextAttributeValue value) </p><p>{</p><p>TextAttributeValueProperties[] values = attribute.getTextAttributeValues();</p><p>String result = null;</p><p>for(int i=0, j=values.length; i<j; i) {</p><p>if(values[i].getId().getIdValue() == value.getId().getIdValue()) </p><p>{</p><p>result = values[i].getName().toString();</p><p>}</p><p>}</p><p>return result;</p><p>}</p><p>//method to retrieve numeric attribute (String)value</p><p>private String getNumericAttValue(NumericAttributeProperties attribute, MeasurementValue value) </p><p>{</p><p>String fracVal = null;</p><p>String unitOfMeasurement = UOM.getUnit(attribute.getMeasurement().getDimensionId(),value.getUnitId()).getSuffix().toUpperCase();</p><p>fracVal = new Double(value.getMagnitude()).toString();</p><p>return fracValunitOfMeasurement;

}

//method to retrieve Coupled numeric attribute (String)value

private String getCoupledAttValue(CoupledAttributeProperties attribute, CoupledMeasurementValue value)

{

String primaryUnit = UOM.getUnit(attribute.getMeasurement().getDimensionId(), value.getPrimaryValue().getUnitId()).getName();

String secondaryUnit = UOM.getUnit(attribute.getCoupledMeasurement().getDimensionId(), value.getSecondaryValue().getUnitId()).getName();

return value.getPrimaryValue().getMagnitude() + " " +

primaryUnit + " " +

attribute.getCoupledDelimiter() + " " +

value.getSecondaryValue().getMagnitude() + " " +

secondaryUnit;

}

The approach/code piece depicted above gives a handle to taxonomy attributes attached to a record object, and would enable the developer to fetch taxonomy attribute values.

Developers can use the algo/code piece above,extend it, build on top of and use it to fetch taxonomy attribute key/value pairs, and then use them to create on single concatenated string, populate certain other fields in the MDM Table schema or simply display them on screen as a single string attached to a particular record i.e. whatever suits the requirement.