cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve attribute values from the taxonomy table with MDM Java API

Former Member
0 Kudos

Hello MDM Java API Experts,

I'm trying to retrieve attribute values from the taxonomy table assigned to the record in the maintable.

The problem is that the values in the taxonomy table are multivalued. And not all possible values for one attribute are assigned to the main table record.

But how can I retrieve only the values assigned to the record?

With the command retrieveRecordsByIdCommand I've got the record from the maintable.

And with retrieveRecordsByIdCommand.getRecords().getRecord(0).findLookupRecords(<FieldId>)[0].getDisplayValue() I've got the attribute name.

Can anyone give me some helpful hints? Sample code would be great.

Thanks and best regards

Nico Razlow

MDM API 2, MDM 5.5 SP 6 Patch 2

Points will be rewarded if helpfull.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hallo Subramanian,

can you give me some information about MDMAttribute Class? I can't find this Class in the Java Documentation for MDM Java API.

Thank you in advance!

Best regards

Nico

Former Member
0 Kudos

You cannot copy-paste my code. You need to understand what I do in the code.

MDMAttribute class is a custom class which I have generated to store all the values for attributes.

Without looking at MDMAttribute class, you should be in a position to figure out what I am trying to do in that method.

Regards,

Subramanian V.

Former Member
0 Kudos

Hi Subramanian,

thank you for trying to explane me your code. But sorry, I can't follow your code.

In which line you retrieve attribute values from the taxonomy table?

I guess here:

mdmAttribute.setAttributeValue(Attribute.valueOf(region, attribute, record.getAttributeValue(fieldId, attributeIDs<i>)));

Unfortunately I don't understand where you define the Class Attribute?

If I use the command record.getAttribute(fieldId, attributeId) I get a coded names or Ids for the attribute values: TA4, TA5, TA3

How can I resolve these Ids in clear attribute values?

Best regards

Nico

Edited by: Nico Razlow on Jul 7, 2008 3:56 PM

Edited by: Nico Razlow on Jul 7, 2008 3:57 PM

Former Member
0 Kudos

Please refer to the code provided by SAP as I have mentioned in my first reply. You will have all the information.

Below is the code for Attribute class which I have extracted from the MDM_Examples file.


/*
 * Created on Jun 19, 2007
 *
 */
package com.sap.nw.mdm.rig.data.attributes;

import java.util.HashMap;
import java.util.Map;

import com.sap.mdm.data.RegionProperties;
import com.sap.mdm.schema.AttributeProperties;
import com.sap.mdm.valuetypes.MdmValue;

/**
 * This abstract class represents a generic Attribute and provides helper methods to 
 * help retrieve the attribute's value. Depending on the type of attribute, 
 * it redirects the calls to one of the appropriate subclasses located in this
 * same package.
 * 
 * 
 * @author Richard LeBlanc
 */
abstract public class Attribute {
	
	/*
	 * Used to get the name and value from a couple numeric attribute
	 */
	static final Attribute COUPLED_ATTRIBUTE = new CoupledNumericAttribute();
	
	/*
	 * Used to get the name and value from a numeric attribute
	 */
	static final Attribute NUMERIC_ATTRIBUTE = new NumericAttribute();
	
	/*
	 * Used to get the name and value from a text attribute
	 */
	static final Attribute TEXT_ATTRIBUTE = new TextAttribute();
	
	//contains a mapping of the MDM attribute type to one of my Attribute subclasses
	static private Map map = new HashMap(3);
	
	static {
		
		map.put(new Integer(AttributeProperties.COUPLED_TYPE), COUPLED_ATTRIBUTE);
		
		map.put(new Integer(AttributeProperties.NUMERIC_TYPE), NUMERIC_ATTRIBUTE);
		
		map.put(new Integer(AttributeProperties.TEXT_TYPE), TEXT_ATTRIBUTE);		
		
	}
	
	/**
	 * Returns the value of the given attribute as a String.
	 * 
	 * @param region - the region for which to provide the value, if the value is multilingual
	 * @param attribute - the attribute whose value to return
	 * @param attributeValue - the value that must be converted to a string
	 */
	static public String valueOf(RegionProperties region, AttributeProperties attribute, MdmValue attributeValue) {
		
		//get the appropriate Attribute subclass from the map
		Attribute myAttribute = (Attribute)map.get(new Integer(attribute.getType()));
		
		return myAttribute.getValue(region, attribute, attributeValue);		

		
	}
	
	
	/**
	 * Returns the value of the given MdmValue as a String.
	 * The AttributeProperties instance is also required because the type of attribute
	 * will affect the value that is returned.
	 *
	 * @param region - the region for which to provide the value, if the value is multilingual	 
	 * @param attribute - the attribute whose value to return
	 * @param value - the value that must be converted to a string
	 * @return
	 */
	abstract String getValue(RegionProperties region, AttributeProperties attribute, MdmValue value);
	
}

Former Member
0 Kudos

Hi Subramanian,

I've got it. Thank you for your help!!!

Best regards

Nico

Former Member
0 Kudos

Go to Articles -> MDM and download the MDM example.zip file for 5.5 SP6 and check for the application


program = CRUDDataProgram.CRUD_TAXONOMY_TABLE_WITH_ATTRIBUTES;

This should solve your problems.

Regards,

Subramanian V.

P.S. If you are able to follow this code


  /**
   *  Retrieve the values from Taxonomy lookup field for the given record and fill the values in the list.
   *  @param  MDMAttributesList - a list object which stores all the MDM attributes and its values
   *  @param  Record            - the record from where the attributes needs to be retrieved
   *  @param  fieldProperties   - Field Properties of Classification
   *  @param  attributeSchema   - Attribute schema 
   *  @param  region            - Retrieve the attribute values based on region which is decided by the user locale
   *  @author Subramanian V.     
   */

  private void fillAttributeValuesFromTaxonomy(List MDMAttributesList,Record record,FieldProperties fieldProperties, AttributeSchema attributeSchema,RegionProperties region)
  {
	/********************************************************************************************************
	 *                 Taxonomy Look Up Field - Classification Attributes retrieval                         *
	 ********************************************************************************************************/    	
  	FieldId       fieldId       = fieldProperties.getId();
	AttributeId[] attributeIDs  = record.getAttributes(fieldId);
	TableId       lookupTableID = ((LookupFieldProperties)fieldProperties).getLookupTableId();
	
	MDMAttribute mdmAttribute = null;
	AttributeProperties attribute = null;
	
	for(int i=0, j=attributeIDs.length; i<j; i++) {
		
		attribute = attributeSchema.getAttribute(lookupTableID, attributeIDs<i>);
		
		for(Iterator iter = MDMAttributesList.iterator();iter.hasNext();)
		{
			mdmAttribute = (MDMAttribute) iter.next();

			if(mdmAttribute.getAttribute()!= null && mdmAttribute.getAttribute().equalsIgnoreCase(attribute.getName().get(record.getDefaultRegionCode())))
			{
				/*** If multivalued enter the values in multiValuedAttribute 
				 *   else enter values in attributeValue
				 */
				if(mdmAttribute.isMultiValued() && mdmAttribute.getAttributeType() == MDMAttribute.TEXT_TYPE)
				{
					mdmAttribute.setAttributeValue(Attribute.valueOf(region, 
					                                                 attribute, 
					                                                 record.getAttributeValue(fieldId, attributeIDs<i>)));
					mdmAttribute.setMultiValuedTextAttribute(mdmAttribute.getAttributeValue());
									
				}
				else if(mdmAttribute.isMultiValued() && mdmAttribute.getAttributeType() == MDMAttribute.NUMERIC_TYPE)
				{
					mdmAttribute.setMultiValuedNumericAttribute(Attribute.valueOf(region, attribute, 
					record.getAttributeValue(fieldId, attributeIDs<i>)));
				}
				else
				{
					mdmAttribute.setAttributeValue(Attribute.valueOf(region, attribute, 
					record.getAttributeValue(fieldId, attributeIDs<i>)));
				}
				
				break;
			}
			
		}
	}
  	
  }