use of org.jaffa.flexfields.FlexClass in project jaffa-framework by jaffa-projects.
the class FlexBeanConverter method convertInbound.
/* (non-Javadoc)
* @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
*
* Copied from BasicObjectConverter
*
* Added custom code to invoke the setter on the FlexBean for unknown properties.
*/
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
String value = iv.getValue();
// If the text is null then the whole bean is null
if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
return null;
}
if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
}
if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
}
value = value.substring(1, value.length() - 1);
try {
FlexBean bean;
if (instanceType != null) {
bean = (FlexBean) instanceType.newInstance();
} else {
bean = (FlexBean) paramType.newInstance();
}
Map properties = getPropertyMapFromObject(bean, false, true);
// Loop through the properties passed in
Map tokens = extractInboundTokens(paramType, value);
// CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
{
String key = "dynaClass";
String val = (String) tokens.remove(key);
Property property = (Property) properties.get(key);
if (val != null && property != null) {
// property.getPropertyType();
Class propType = FlexClass.class;
String[] split = ParseUtil.splitInbound(val);
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
TypeHintContext incc = createTypeHintContext(inctx, property);
Object output = converterManager.convertInbound(propType, nested, inctx, incc);
property.setValue(bean, output);
// The input from the client may merely pass the name of the FlexClass
// Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexBean
DynaClass flexClass = bean.getDynaClass();
if (flexClass != null) {
flexClass = FlexClass.instance(flexClass.getName());
bean = FlexBean.instance((FlexClass) flexClass);
}
}
}
// is referenced later nested down in the conversion process.
if (instanceType != null) {
inctx.addConverted(iv, instanceType, bean);
} else {
inctx.addConverted(iv, paramType, bean);
}
for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
String val = (String) entry.getValue();
Property property = (Property) properties.get(key);
if (property == null) {
// CUSTOM CODE: Instead of logging a warning, assume that the inbound token
// is valid, and create a descriptor for it, such that it invokes the setter
// on the FlexBean
Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
property = new FlexDescriptor(key, propType);
}
Class propType = property.getPropertyType();
String[] split = ParseUtil.splitInbound(val);
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
TypeHintContext incc = createTypeHintContext(inctx, property);
Object output = converterManager.convertInbound(propType, nested, inctx, incc);
property.setValue(bean, output);
}
return bean;
} catch (MarshallException ex) {
throw ex;
} catch (Exception ex) {
throw new MarshallException(paramType, ex);
}
}
use of org.jaffa.flexfields.FlexClass in project jaffa-framework by jaffa-projects.
the class FlexCriteriaBeanConverter method convertInbound.
/* (non-Javadoc)
* @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
*
* Copied from BasicObjectConverter
*
* Added custom code to invoke the setter on the FlexCriteriaBean for unknown properties.
*/
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
String value = iv.getValue();
// If the text is null then the whole bean is null
if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
return null;
}
if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
}
if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
}
value = value.substring(1, value.length() - 1);
try {
FlexCriteriaBean bean;
if (instanceType != null) {
bean = (FlexCriteriaBean) instanceType.newInstance();
} else {
bean = (FlexCriteriaBean) paramType.newInstance();
}
Map properties = getPropertyMapFromObject(bean, false, true);
// Loop through the properties passed in
Map tokens = extractInboundTokens(paramType, value);
// CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
{
String key = "dynaClass";
String val = (String) tokens.remove(key);
Property property = (Property) properties.get(key);
if (val != null && property != null) {
// property.getPropertyType();
Class propType = FlexClass.class;
String[] split = ParseUtil.splitInbound(val);
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
TypeHintContext incc = createTypeHintContext(inctx, property);
Object output = converterManager.convertInbound(propType, nested, inctx, incc);
property.setValue(bean, output);
// The input from the client may merely pass the name of the FlexClass
// Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexCriteriaBean
DynaClass flexClass = bean.getDynaClass();
if (flexClass != null) {
flexClass = FlexClass.instance(flexClass.getName());
bean = FlexCriteriaBean.instance((FlexClass) flexClass);
}
}
}
// is referenced later nested down in the conversion process.
if (instanceType != null) {
inctx.addConverted(iv, instanceType, bean);
} else {
inctx.addConverted(iv, paramType, bean);
}
for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
String val = (String) entry.getValue();
Property property = (Property) properties.get(key);
if (property == null) {
// CUSTOM CODE: Instead of logging a warning, assume that the inbound token
// is valid, and create a descriptor for it, such that it invokes the setter
// on the FlexCriteriaBean
Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
propType = findCriteriaFieldClass(propType);
property = new FlexDescriptor(key, propType);
}
Class propType = property.getPropertyType();
String[] split = ParseUtil.splitInbound(val);
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
TypeHintContext incc = createTypeHintContext(inctx, property);
Object output = converterManager.convertInbound(propType, nested, inctx, incc);
property.setValue(bean, output);
}
return bean;
} catch (MarshallException ex) {
throw ex;
} catch (Exception ex) {
throw new MarshallException(paramType, ex);
}
}
use of org.jaffa.flexfields.FlexClass in project jaffa-framework by jaffa-projects.
the class CandidateKeyValidator method invoke.
/**
* Apply the candidate-key validation.
*/
private void invoke(String targetClassName, IPersistent targetObject, UOW uow, RuleMetaData rule, AtomicCriteria criteriaToDiscardCurrentObject, Map<String, Object> keyValueMap) throws ApplicationException, ApplicationExceptions, FrameworkException {
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetObject);
}
Criteria criteria = null;
String ck = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
String[] ckFields = ck.split(",");
Boolean ignoreNull = Parser.parseBoolean(rule.getParameter("ignore-null"));
// Flex fields having null values are deleted from the database table.
// The persistence engine does not support queries of the type 'where not exists...'.
// Hence we'll retrieve all rows and throw an exception only if the fields in the collection are null in any of the rows
Collection<String> nullFields = null;
FlexClass flexClass = targetObject instanceof IFlexFields && ((IFlexFields) targetObject).getFlexBean() != null ? (FlexClass) ((IFlexFields) targetObject).getFlexBean().getDynaClass() : null;
boolean keyModified = false;
for (String ckField : ckFields) {
// determine if this is a flex field
FlexProperty flexProperty = flexClass != null ? flexClass.getDynaProperty(ckField) : null;
// Obtain the value
Object ckValue;
try {
ckValue = BeanHelper.getField(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField);
if (ignoreNull != null && ignoreNull && ckValue == null) {
if (log.isDebugEnabled())
log.debug("Rule will be ignored since " + ckField + " is null");
return;
}
} catch (Exception e) {
if (log.isDebugEnabled())
log.debug("Exception thrown while trying to get value for the property " + ckField + ". It may not exist in the targetObject", e);
return;
}
// Perform the check only if the field is modified, or is NULL and Object has not been saved yet
if (!keyModified)
keyModified = isModified(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField) || (ckValue == null && !targetObject.isDatabaseOccurence());
// and any other rule may cause the actual update to be executed.
if (flexProperty != null && flexProperty.getFlexInfo() != null && flexProperty.getFlexInfo().getProperty("domain-mapping") != null) {
ckField = flexProperty.getFlexInfo().getProperty("domain-mapping");
flexProperty = null;
}
// Add to criteria
if (criteria == null)
criteria = new Criteria();
if (ckValue == null) {
if (flexProperty != null) {
if (nullFields == null)
nullFields = new LinkedList<String>();
nullFields.add(ckField);
} else {
criteria.addCriteria(StringHelper.getUpper1(ckField), Criteria.RELATIONAL_IS_NULL);
}
} else {
if (flexProperty != null) {
Criteria flexCriteria = new Criteria();
flexCriteria.setTable(FlexFieldMeta.getName());
int i = 0;
for (String key : keyValueMap.keySet()) flexCriteria.addInnerCriteria("Key" + ++i, StringHelper.getUpper1(key));
flexCriteria.addCriteria(FlexFieldMeta.OBJECT_NAME, flexClass.getLogicalName());
flexCriteria.addCriteria(FlexFieldMeta.FIELD_NAME, flexProperty.getLogicalName());
flexCriteria.addCriteria(FlexFieldMeta.VALUE, DataTypeMapper.instance().map(ckValue, String.class, flexProperty.getFlexInfo() != null ? flexProperty.getFlexInfo().getProperty("layout") : null));
criteria.addAggregate(flexCriteria);
} else {
criteria.addCriteria(StringHelper.getUpper1(ckField), ckValue);
}
}
}
if (keyModified && criteria != null) {
if (criteriaToDiscardCurrentObject != null)
criteria.addAtomic(criteriaToDiscardCurrentObject);
criteria.setTable(targetClassName);
// try {
for (Object o : uow.query(criteria)) {
boolean violation = nullFields == null;
if (!violation) {
// Not a violation if any of the fields is not null
for (String ckField : nullFields) {
try {
violation = BeanHelper.getField(targetObject, ckField) == null;
if (!violation) {
break;
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Error in invoke method during query execution.", e);
}
throw new JaffaRulesFrameworkException("Error in invoke method during query execution on object: " + targetClassName, null, e);
}
}
}
if (violation) {
if (log.isDebugEnabled()) {
log.debug("Candidate key '" + ck + "' is not unique for the object " + targetObject);
}
String objectLabel = getObjectLabel(targetClassName, targetObject);
StringBuilder ckLabel = new StringBuilder();
for (int i = 0; i < ckFields.length; i++) {
if (i > 0) {
ckLabel.append(',');
}
ckLabel.append(getPropertyLabel(targetObject, ckFields[i]));
}
throw wrapException(new DuplicateCandidateKeyException(objectLabel, ckLabel.toString()), (T) targetObject, rule);
}
}
}
}
Aggregations