Tuesday, May 13, 2014

Find segment value from one dimension and use in another dimension

The following set of function is used in the scenario as :
- Get one or more segment values from a dimension
- Set or merge one or more segment values  in another dimension and returns a new dimension

Main function:
///
///     Setting a new dimension value by using segment vales from other dimension
///
///
///     The segment values will be used from this dimension
///
///
///     The segment values will be incorporated in this dimension
///
///
///     After incorporating the segment values, function returns a new dimension
///

public RecId mergeSegmentValueInDimension(RecId  _getDimension, RecId _setDimension)
{
    #define.AccountingDim('Accounting')
    #define.DepartmentDim('Department')
    #define.BusinessUnitDim('BusinessUnit')
    #define.SpaceDim('Space')

    RecId       newDimension = _setDimension;
    Struct      s;
    String255   dimValue;
    container   c = this.getDimensionValueBySegmentName(_getDimension);

    s = Struct::create(c);

    dimValue = s.exists(#AccountingDim) ? s.value(#AccountingDim) : '';
    if (dimValue)
    {
        newDimension = this.setSegmentValueInDimension(newDimension, #AccountingDim, dimValue);
        dimValue = '';
    }

    dimValue = s.exists(#DepartmentDim) ? s.value(#DepartmentDim) : '';
    if (dimValue)
    {
        newDimension = this.setSegmentValueInDimension(newDimension, #DepartmentDim, dimValue);
        dimValue = '';
    }

    dimValue = s.exists(#BusinessUnitDim) ? s.value(#BusinessUnitDim) : '';
    if (dimValue)
    {
        newDimension = this.setSegmentValueInDimension(newDimension, #BusinessUnitDim, dimValue);
        dimValue = '';
    }

    dimValue = s.exists(#SpaceDim) ? s.value(#SpaceDim) : '';
    if (dimValue)
    {
        newDimension = this.setSegmentValueInDimension(newDimension, #SpaceDim, dimValue);
        dimValue = '';
    }

    return newDimension;
}


Sub-function-1:
public container getDimensionValueBySegmentName(RecId   _defaultDimension)
{
    Struct s = new struct();

    DimensionAttribute              dimAttr;
    DimensionAttributeValue         dimAttrValue;
    DimensionAttributeValueSet      dimAttrValueSet;
    DimensionAttributeValueSetItem  dimAttrValueSetItem;

    dimAttrValueSet = DimensionAttributeValueSet::find(_defaultDimension);

    while select dimAttrValueSetItem
        where   dimAttrValueSetItem.DimensionAttributeValueSet   == dimAttrValueSet.RecId
    {
        dimAttrValue        = DimensionAttributeValue::find(dimAttrValueSetItem.DimensionAttributeValue);
        dimAttr             = DimensionAttribute::find(dimAttrValue.DimensionAttribute);

        s.add(dimAttr.Name, dimAttrValue.getValue());
    }

    return s.pack();
}

Sub-function-2:
public RecId setSegmentValueInDimension(RecId  _defaultDimension,
                                        Name   _dimName,
                                        str    _dimValue)
{
    DimensionAttributeValueSetStorage  dimStorage   =   DimensionAttributeValueSetStorage::find(_defaultDimension);
    DimensionAttribute                 dimAttribute =  DimensionAttribute::findByName(_dimName);
    DimensionAttributeValue            dimAttributeValue;

    if(_dimValue)
    {
        dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, _dimValue, true, true);
        dimStorage.addItem(dimAttributeValue);
    }
    else
        dimStorage.removeDimensionAttribute(DimensionAttribute::findByName(_dimName).RecId);

    return dimStorage.save();

}

Cheers !

No comments:

Post a Comment