Search in sources :

Example 6 with ConvertException

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);
}
Also used : ConvertException(jp.ossc.nimbus.util.converter.ConvertException) ConvertException(jp.ossc.nimbus.util.converter.ConvertException)

Aggregations

ConvertException (jp.ossc.nimbus.util.converter.ConvertException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletInputStream (javax.servlet.ServletInputStream)2 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)2 PropertySchemaDefineException (jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException)2 BindingStreamConverter (jp.ossc.nimbus.util.converter.BindingStreamConverter)2 StreamStringConverter (jp.ossc.nimbus.util.converter.StreamStringConverter)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ServiceName (jp.ossc.nimbus.core.ServiceName)1 DataSetXMLConverter (jp.ossc.nimbus.util.converter.DataSetXMLConverter)1 StreamConverter (jp.ossc.nimbus.util.converter.StreamConverter)1