Thursday, August 9, 2007

SAP MDM COM API Get Attribute By Category ID

public static DataTable GetByCategoryID(int attributeID, int categoryID, XCATCOMLib.Catalog catalog)
{
// There are 3 types of attributes:
// Feature - text values;
// Characteristic - numeric values;
// Coupled - coupled numeric values.

DataRow row = null;
DataTable dt = new DataTable();
dt.Columns.Add("AttributeID");
dt.Columns.Add("AttributeValue");
dt.Columns.Add("AttributeCoupleValue");
dt.Columns.Add("AttributeDisplayValue");
dt.Columns.Add("AttributeRating");
dt.Columns.Add("ShowAllFlag");
dt.Columns.Add("AttributePriority");
dt.Columns.Add("AttributeUnitID");
dt.Columns.Add("AttributeCoupleUnitID");

XCATCOMLib.Attribute prodAttribute = catalog.GetAttribute(CATEGORY_TABLE, attributeID);
XCATCOMLib.Search search = new XCATCOMLib.SearchClass();
XCATCOMLib.TaxonomyLookupParameter param = search.Parameters.NewTaxonomyLookupParameter(PRODUCT_TABLE, CATEGORY_FIELD);
param.NodeID = categoryID;


XCATCOMLib.FreeFormTableParameter tableParam = search.Parameters.NewFreeFormParameter(PRODUCT_TABLE);
XCATCOMLib.FeatureParameter featureParam = null;


int attribType = prodAttribute.Type;
string attribDefinition = prodAttribute.Definition;
byte attribPriority = prodAttribute.Priority;

if (attribType == 0) //xcFeatureValueType
{
XCATCOMLib.FeatureValues fvs = catalog.GetLimitedFeatureValues(search, CATEGORY_TABLE, attributeID);
foreach (FeatureValue fv in fvs)
{
row = dt.NewRow();
row["AttributeValue"] = fv.ID;
row["AttributeID"] = attributeID;
row["AttributeDisplayValue"] = fv.Value;
row["AttributeRating"] = 0;
row["ShowAllFlag"] = false;
row["AttributePriority"] = attribPriority;
row["AttributeUnitID"] = "";
row["AttributeCoupleUnitID"] = "";
dt.Rows.Add(row);
}
}
else if ((attribType & 0xFF) > 0) //xcCharacteristicValueType
{
string attribName = Helper.FormatAttributeName(prodAttribute.Name);
string attribAlias = prodAttribute.Alias;

int[] gRatingArray = new int[5];
gRatingArray[0] = 8;//"xcNominalRating";
gRatingArray[1] = 1;//"xcMinimumRating";
gRatingArray[2] = 2;//"xcMaximumRating";
gRatingArray[3] = 16;//"xcAverageRating";
gRatingArray[4] = 4;//"xcTypicalRating";

int attribTypeInt = prodAttribute.Type;
for (int k = 0; k < gRatingArray.Length; k++)
{
int rating = gRatingArray[k];

if ((attribTypeInt & rating) == rating)
{
XCATCOMLib.CharacteristicValues cvs = catalog.GetLimitedCharacteristicValues(search, CATEGORY_TABLE, attributeID, (RatingEnum)rating);

foreach (CharacteristicValue cv in cvs)
{
row = dt.NewRow();
row["AttributeID"] = attributeID;
row["AttributeDisplayValue"] = cv.GetString(prodAttribute.DimensionID, prodAttribute.DecimalPlaces, prodAttribute.AllowFractions, cv.UnitID);;
row["AttributeValue"] = cv.Value;
row["AttributeCoupleValue"] = "";
row["AttributeRating"] = rating;
row["ShowAllFlag"] = false;
row["AttributePriority"] = attribPriority;
row["AttributeUnitID"] = cv.UnitID;
row["AttributeCoupleUnitID"] = "";
dt.Rows.Add(row);
}
}
}
}
else if (attribType == 0x8000) //xcCoupledValueType
{
XCATCOMLib.CoupledValues cpvs = catalog.GetLimitedCoupledValues(search, CATEGORY_TABLE, attributeID);
foreach (CoupledValue cpv in cpvs)
{
string primary;
string coupled;
primary = cpv.Primary.GetString(prodAttribute.DimensionID, prodAttribute.DecimalPlaces, prodAttribute.AllowFractions, cpv.Primary.UnitID);
coupled = cpv.Coupled.GetString(prodAttribute.CoupledDimensionID, prodAttribute.CoupledDecimalPlaces, prodAttribute.CoupledAllowFractions, cpv.Coupled.UnitID);

row = dt.NewRow();
row["AttributeDisplayValue"] = primary + " " + prodAttribute.CoupledDelimiter + " " + coupled;;
row["AttributeValue"] = cpv.Primary.Value;
row["AttributeCoupleValue"] = cpv.Coupled.Value;
row["AttributeID"] = attributeID;
row["AttributeRating"] = 0;
row["ShowAllFlag"] = false;
row["AttributePriority"] = attribPriority;
row["AttributeUnitID"] = cpv.Primary.UnitID;
row["AttributeCoupleUnitID"] = cpv.Coupled.UnitID;
dt.Rows.Add(row);
}
}

return dt;
}