Created
May 19, 2016 14:44
-
-
Save obedmhg/55e25cf93b8d3e20770392444ca9bd9a to your computer and use it in GitHub Desktop.
Property descriptor that will compare 2 values that will be set as attribute tags, along with the comparison to do, it will return the boolean with the result of the operation.
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 atg.adapter.gsa.GSAPropertyDescriptor; | |
import atg.core.util.StringUtils; | |
import atg.nucleus.logging.ApplicationLogging; | |
import atg.nucleus.logging.ClassLoggingFactory; | |
import atg.repository.RepositoryItemImpl; | |
import com.common.Constants; | |
/** | |
* Property descriptor that will compare 2 values that will be set as attribute tags, along with the comparison to do. | |
* | |
* @author Obed Murillo | |
* | |
*/ | |
public class CompareValuesPropertyDescriptor extends GSAPropertyDescriptor { | |
/** Serial Version. */ | |
private static final long serialVersionUID = -6797823753559056979L; | |
/** Constant to hold property. */ | |
private static final String PROPERTY = "property"; | |
/** Constant to hold Float. */ | |
private static final String FLOAT = "Float"; | |
/** Constant to hold Double. */ | |
private static final String DOUBLE = "Double"; | |
/** Constant to hold int. */ | |
private static final String INT = "int"; | |
/** Constant to hold Integer. */ | |
private static final String INTEGER = "Integer"; | |
/** leftSideValue attribute name. */ | |
private static final String LEFT_SIDE_VALUE = "leftSideValue"; | |
/** leftSideType attribute name. */ | |
private static final String LEFT_SIDE_TYPE = "leftSideType"; | |
/** rightSideValue attribute name. */ | |
private static final String RIGHT_SIDE_VALUE = "rightSideValue"; | |
/** rightSideType attribute name. */ | |
private static final String RIGHT_SIDE_TYPE = "rightSideType"; | |
/** compareOperator attribute name. */ | |
private static final String COMPARE_OPERATOR = "compareOperator"; | |
/** Logger property. */ | |
private static ApplicationLogging logger = ClassLoggingFactory.getFactory() | |
.getLoggerForClass(CompareValuesPropertyDescriptor.class); | |
/** | |
* Method that will compare 2 values that will be set as attribute tags, along with the comparison to do. | |
* | |
* An example for a property using this PropertyDescriptor is described below: | |
* | |
* <property name="pName" data-type="boolean" property-type="com.common.propertydescriptor.CompareValuesPropertyDescriptor"> | |
* <attribute name="compareOperator" value="gt" /> | |
* <attribute name="leftSideValue" value="30" /> | |
* <attribute name="leftSideType" value="int" /> | |
* <attribute name="rightSideValue" value="daysAvailable" /> | |
* <attribute name="rightSideType" value="property" /> | |
* </property> | |
* | |
* @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 = false; | |
Double rightSide; | |
Double leftSide; | |
try { | |
String compareOperator = (String) this.getValue(COMPARE_OPERATOR); | |
String rightSideType = (String) this.getValue(RIGHT_SIDE_TYPE); | |
String rightSideValue = (String) this.getValue(RIGHT_SIDE_VALUE); | |
String leftSideType = (String) this.getValue(LEFT_SIDE_TYPE); | |
String leftSideValue = (String) this.getValue(LEFT_SIDE_VALUE); | |
if (!StringUtils.isBlank(compareOperator) && !StringUtils.isBlank(rightSideType) && !StringUtils.isBlank(rightSideValue) | |
&& !StringUtils.isBlank(leftSideType) && !StringUtils.isBlank(leftSideValue)) { | |
rightSide = getValueToCompare(pItem, rightSideType, rightSideValue); | |
leftSide = getValueToCompare(pItem, leftSideType, leftSideValue); | |
resp = compare(compareOperator, rightSide, leftSide); | |
} else { | |
logger.logError("Missing attributes to use CompareValuesPropertyDescriptor @ " + this.getName()); | |
} | |
} catch (final Exception e) { | |
logger.logError("There was an exception trying to sync Spawn products properties for ancestor product " | |
+ pItem.getRepositoryId()); | |
} | |
return resp; | |
} | |
/** | |
* It will get the values that will be used to compare from attributes. | |
* | |
* @param pItem to get. | |
* @param sideType to get type. | |
* @param sideValue to get value. | |
* @return actual value. | |
*/ | |
private Double getValueToCompare(final RepositoryItemImpl pItem, final String sideType, final String sideValue) { | |
Double value = 0.0d; | |
try { | |
if (INTEGER.equalsIgnoreCase(sideType) || INT.equalsIgnoreCase(sideType) || DOUBLE.equalsIgnoreCase(sideType) | |
|| FLOAT.equalsIgnoreCase(sideType)) { | |
value = Double.parseDouble(sideValue); | |
} else if (PROPERTY.equals(sideType)) { | |
Object valueFromProperty = pItem.getPropertyValue(sideValue); | |
if (valueFromProperty instanceof Double) { | |
value = (Double) valueFromProperty; | |
} else { | |
value = Double.parseDouble(valueFromProperty.toString()); | |
} | |
} else { | |
logger.logError("attribute sideType must be a number 0.0 will be returned."); | |
} | |
} catch (final NumberFormatException e) { | |
logger.logError("attribute sideType must be a number 0.0 will be returned."); | |
} | |
return value; | |
} | |
/** | |
* It will compare 2 numbers by the comparator as parameter. Compare Operator must follow expression language notation: | |
* eq Test for equality | |
* ne Test for inequality | |
* lt Test for less than | |
* gt Test for greater than | |
* le Test for less than or equal | |
* ge Test for greater than or equal | |
* | |
* @param compareOperator to use. | |
* @param left side of compare. | |
* @param right side if compare. | |
* @return the result of the compare. | |
*/ | |
private boolean compare(final String compareOperator, final Double right, final Double left) { | |
boolean resp = false; | |
if (Constants.EQUALS_OP.equals(compareOperator)) { | |
resp = left == right; | |
} else if (Constants.GT_EQUALS.equals(compareOperator)) { | |
resp = left >= right; | |
} else if (Constants.GT.equals(compareOperator)) { | |
resp = left > right; | |
} else if (Constants.LT_EQUALS.equals(compareOperator)) { | |
resp = left <= right; | |
} else if (Constants.LT.equals(compareOperator)) { | |
resp = left < right; | |
} else if (Constants.DIFFERENT.equals(compareOperator)) { | |
resp = left != right; | |
} else { | |
resp = false; | |
} | |
return resp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment