Last active
May 19, 2016 14:49
-
-
Save obedmhg/586fd7e0fa9155b3684a534ff165202e to your computer and use it in GitHub Desktop.
PropertyDescriptor to Concatenate properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.common.propertydescriptor; | |
import org.apache.commons.lang3.StringUtils; | |
import atg.adapter.gsa.GSAPropertyDescriptor; | |
import atg.nucleus.logging.ApplicationLogging; | |
import atg.nucleus.logging.ClassLoggingFactory; | |
import atg.repository.RepositoryItemImpl; | |
import com.common.Constants; | |
/** | |
* Method that will concatenate n values that will be set as attribute tags as property, and will separe them by parameter called separator. | |
* | |
* @author Obed Murillo | |
* | |
*/ | |
public class ConcatenateValuesPropertyDescriptor extends GSAPropertyDescriptor { | |
/** properties word. */ | |
public static final String PROPERTIES = "properties"; | |
/** separator word. */ | |
public static final String SEPARATOR = "separator"; | |
/** Serial Version. */ | |
private static final long serialVersionUID = 1297883861580494381L; | |
/** Logger property. */ | |
private static ApplicationLogging logger = ClassLoggingFactory.getFactory().getLoggerForClass( | |
ConcatenateValuesPropertyDescriptor.class); | |
/** | |
* Method that will concatenate n values that will be set as attribute tags as property, and will separate them by parameter called | |
* separator. | |
* | |
* An example for a property using this PropertyDescriptor is described below: | |
* | |
* <property name="pName" data-type="string" property-type="com.common.propertydescriptor.CocaenateValuesPropertyDescriptor"> | |
* <attribute name="separator" value=" - " /> <attribute name="properties" value="displayName,colorName,colorCode" /> | |
* </property> | |
* | |
* <b>this</b> means that it will use the value from the current property. | |
* | |
* if separator is not defined it will use a blank space to separate the properties. | |
* | |
* if property sent in properties does not exist it will be concatenated as string, meaning that separator can be sent as property as | |
* follows: | |
* | |
* <attribute name="properties" value="this, ,color, ,[,colorCode,], ,size,,[,sizeCode,]"/> | |
* | |
* Or chars to group data as follows: | |
* | |
* <attribute name="properties" value="this,color,[,colorCode,],size,[,sizeCode,]"/> | |
* | |
* @param pItem the repository item. | |
* @param pValue the current value. | |
* @return Object corresponding value. | |
*/ | |
@Override | |
public Object getPropertyValue(final RepositoryItemImpl pItem, final Object pValue) { | |
Object resp = Constants.EMPTY; | |
try { | |
StringBuilder concatenatedString = new StringBuilder(Constants.EMPTY); | |
String separator = (String) this.getValue(SEPARATOR); | |
if (StringUtils.isBlank(separator)) { | |
separator = Constants.BLANK_SPACE; | |
} | |
String properties = (String) this.getValue(PROPERTIES); | |
for (String prop : properties.split(Constants.COMMA)) { | |
if (Constants.THIS.equals(prop)) { | |
concatenatedString.append(pValue.toString() + separator); | |
} else { | |
if (prop.contains(Constants.DOT)) { | |
concatenatedString.append(getPropertyFromInnerProperty(prop, pItem)); | |
} | |
Object valueFromProperty = null; | |
try { | |
valueFromProperty = pItem.getPropertyValue(prop); | |
} catch (final IllegalArgumentException e) { | |
if (!StringUtils.isBlank(separator)) { | |
concatenatedString.append(concatenatedString.substring(0, concatenatedString.length() - separator.length()) | |
+ prop); | |
} else { | |
concatenatedString.append(prop); | |
} | |
} | |
if (null != valueFromProperty || valueFromProperty instanceof RepositoryItemImpl.NullObject) { | |
concatenatedString.append(valueFromProperty + separator); | |
} | |
} | |
} | |
resp = concatenatedString.substring(0, concatenatedString.length() - separator.length()); | |
} catch (final Exception e) { | |
logger.logError("There was an exception trying concatenate properties for " + pItem.getRepositoryId()); | |
} | |
return resp; | |
} | |
/** | |
* It will get property if the property is inside an other property on the same itemDescriptor. ie. defaultSku.id | |
* @param prop property to return. | |
* @param pItem to get the property. | |
* @return property from inner property. | |
*/ | |
protected Object getPropertyFromInnerProperty(final String prop, final RepositoryItemImpl pItem) { | |
RepositoryItemImpl innerProp = null; | |
Object value = null; | |
try { | |
innerProp = (RepositoryItemImpl) pItem.getPropertyValue(prop.split(Constants.BACK_SLASH + Constants.DOT)[0]); | |
value = innerProp.getPropertyValue(prop.split(Constants.BACK_SLASH + Constants.DOT)[1]); | |
} catch (final IllegalArgumentException e) { | |
logger.logError("There was an exception trying get property " + prop + " null will be returned."); | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment