use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.
the class CandidateKeyValidator method getKeyValueMap.
/**
* Obtain a Map containing key fields and corresponding values.
*/
private Map<String, Object> getKeyValueMap(String targetClassName, IPersistent targetObject) throws ApplicationExceptions, FrameworkException {
// Search for the first available class-level primary-key rule
String[] keys = null;
Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "primary-key");
if (pkMap != null && pkMap.containsKey(null)) {
List<RuleMetaData> rules = pkMap.get(null);
if (rules != null && rules.size() > 0) {
RuleMetaData rule = rules.get(0);
keys = rule.getParameter(RuleMetaData.PARAMETER_VALUE).split(",");
}
}
// Search for corresponding DomainMeta class, if required
if (keys == null || keys.length == 0) {
try {
FieldMetaData[] keyFields = PersistentHelper.getKeyFields(targetObject.getClass().getName());
if (keyFields != null) {
keys = new String[keyFields.length];
for (int i = 0; i < keyFields.length; i++) keys[i] = keyFields[i].getName();
}
} catch (Exception e) {
// do nothing
}
}
// Create the keyValueMap
if (keys != null && keys.length > 0) {
Map<String, Object> keyValueMap = new LinkedHashMap<String, Object>();
for (String key : keys) {
Object value = null;
try {
value = BeanHelper.getField(targetObject, key);
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
log.debug("Could not get the field: " + targetObject.getClass().getName() + "." + key, e);
}
throw new JaffaRulesFrameworkException(targetObject.getClass().getName() + "." + key, null, e);
}
keyValueMap.put(key, value);
}
return keyValueMap;
} else {
return null;
}
}
Aggregations