use of jp.ossc.nimbus.util.converter.ConvertException in project nimbus by nimbus-org.
the class RecordSetCodeMasterConverter method convert.
public Object convert(Object master, Object obj) throws ConvertException {
if (!(master instanceof RecordSet)) {
throw new ConvertException("CodeMaster is not RecordSet!");
}
final RecordSet recset = (RecordSet) master;
RowData row = null;
try {
if (obj instanceof DynamicCondition) {
final DynamicCondition cnd = (DynamicCondition) obj;
final Collection col = recset.searchDynamicCondition(cnd.conditionName);
final Iterator itr = col.iterator();
if (itr.hasNext()) {
row = (RowData) itr.next();
}
} else if (obj instanceof DynamicConditionReal) {
final DynamicConditionReal cnd = (DynamicConditionReal) obj;
if (cnd.condition == null) {
throw new ConvertException("DynamicConditionReal.condition is null.");
}
final Collection col = recset.searchDynamicConditionReal(cnd.condition, cnd.orderBy, cnd.isAsc, cnd.valueMap);
final Iterator itr = col.iterator();
if (itr.hasNext()) {
row = (RowData) itr.next();
}
} else if (obj instanceof DynamicKey) {
final DynamicKey cnd = (DynamicKey) obj;
RowData key = null;
if (cnd.keyMap != null) {
if (cnd.keyMap.size() == 0) {
if (isThrowOnNullKey) {
throw new ConvertException("DynamicKey.keyMap is empty.");
}
return nullKeyObject;
}
key = recset.createNewRecord();
Iterator entries = cnd.keyMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String fieldName = (String) entry.getKey();
Object fieldValue = entry.getValue();
FieldSchema field = recset.getRowSchema().get(fieldName);
if (field == null) {
throw new ConvertException("Field '" + fieldName + "' is not found.");
}
key.setValueNative(field.getIndex(), fieldValue);
}
} else {
if (cnd.keyRecord == null) {
if (isThrowOnNullKey) {
throw new ConvertException("DynamicKey.keyRecord is null.");
}
return nullKeyObject;
}
key = cnd.keyRecord;
}
final Collection col = recset.searchDynamicKey(cnd.conditionName, key, cnd.orderBy, cnd.isAsc);
final Iterator itr = col.iterator();
if (itr.hasNext()) {
row = (RowData) itr.next();
}
} else if (obj instanceof Key) {
final Key cnd = (Key) obj;
if (cnd.key != null) {
row = recset.get(cnd.key);
} else {
RowData key = null;
if (cnd.keyMap != null) {
if (cnd.keyMap.size() == 0) {
if (isThrowOnNullKey) {
throw new ConvertException("Key.keyMap is empty.");
}
return nullKeyObject;
}
key = recset.createNewRecord();
Iterator entries = cnd.keyMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String fieldName = (String) entry.getKey();
Object fieldValue = entry.getValue();
FieldSchema field = recset.getRowSchema().get(fieldName);
if (field == null) {
throw new ConvertException("Field '" + fieldName + "' is not found.");
}
key.setValueNative(field.getIndex(), fieldValue);
}
} else {
if (cnd.keyRecord == null) {
if (isThrowOnNullKey) {
throw new ConvertException("Key.keyRecord is null.");
}
return nullKeyObject;
}
key = cnd.keyRecord;
}
row = recset.get(key);
}
} else {
if (obj == null) {
if (isThrowOnNullKey) {
throw new ConvertException("Key is null.");
}
return nullKeyObject;
}
if (obj instanceof RowData) {
row = recset.get((RowData) obj);
} else {
row = recset.get(obj.toString());
}
}
} catch (ConvertException e) {
throw e;
} catch (Exception e) {
throw new ConvertException(e);
}
if (row == null) {
if (isThrowOnNotFound) {
throw new ConvertException("Record is not found. key=" + obj);
}
return notFoundObject;
}
return valueFieldName == null ? row : row.get(valueFieldName);
}
Aggregations