Search in sources :

Example 41 with Property

use of jp.ossc.nimbus.beans.Property in project nimbus by nimbus-org.

the class DefaultPersistentManagerService method fillOutput.

private Object fillOutput(ResultSet resultSet, Object output, Map outputMapping, boolean isCursor) throws PersistentException {
    if (output == null) {
        if (isCursor) {
            output = new LinkedHashMap();
        } else {
            output = new ArrayList();
        }
    }
    try {
        final ResultSetMetaData metadata = resultSet.getMetaData();
        final int colCount = metadata.getColumnCount();
        boolean isOutputMappingFromMetaData = false;
        if (outputMapping == null && (output instanceof RecordList || output instanceof Record || !(output instanceof List))) {
            outputMapping = new LinkedHashMap();
            for (int i = 1; i <= colCount; i++) {
                outputMapping.put(metadata.getColumnName(i), metadata.getColumnName(i).toUpperCase());
            }
            isOutputMappingFromMetaData = true;
        }
        if (output instanceof DataSet) {
            final Set headerSet = new LinkedHashSet();
            final Map recordListMap = new LinkedHashMap();
            final Map recordListPropMap = new LinkedHashMap();
            Iterator itr = outputMapping.entrySet().iterator();
            while (itr.hasNext()) {
                final Map.Entry entry = (Map.Entry) itr.next();
                final Property prop = propertyAccess.getProperty((String) entry.getValue());
                if (prop instanceof NestedProperty) {
                    Object obj = ((NestedProperty) prop).getThisProperty().getProperty(output);
                    if (obj instanceof RecordList) {
                        recordListMap.put(entry.getKey(), (RecordList) obj);
                        recordListPropMap.put(entry.getKey(), ((NestedProperty) prop).getNestedProperty());
                    } else {
                        headerSet.add(entry.getKey());
                    }
                } else {
                    throw new PersistentException("Output bean fill error.");
                }
            }
            final Map recordMap = new HashMap();
            while (true) {
                if (!isCursor) {
                    if (!resultSet.next()) {
                        break;
                    }
                }
                if (headerSet.size() != 0) {
                    itr = headerSet.iterator();
                    while (itr.hasNext()) {
                        final String columnName = (String) itr.next();
                        setValue(output, (String) outputMapping.get(columnName), resultSet, columnName, isOutputMappingFromMetaData);
                    }
                    headerSet.clear();
                }
                recordMap.clear();
                itr = recordListMap.entrySet().iterator();
                while (itr.hasNext()) {
                    final Map.Entry entry = (Map.Entry) itr.next();
                    final RecordList list = (RecordList) entry.getValue();
                    Record record = (Record) recordMap.get(list);
                    if (record == null) {
                        record = list.createRecord();
                        recordMap.put(list, record);
                        list.addRecord(record);
                    }
                    Property prop = (Property) recordListPropMap.get(entry.getKey());
                    prop.setProperty(record, getValue(record, prop, resultSet, (String) entry.getKey()));
                }
                if (isCursor) {
                    break;
                }
            }
            return output;
        } else if (output instanceof RecordList) {
            RecordList list = (RecordList) output;
            if (list.getSchema() == null) {
                list.setSchema(createSchema(metadata));
            }
            while (true) {
                if (!isCursor) {
                    if (!resultSet.next()) {
                        break;
                    }
                }
                final Record record = list.createRecord();
                final Iterator itr = outputMapping.entrySet().iterator();
                while (itr.hasNext()) {
                    final Map.Entry entry = (Map.Entry) itr.next();
                    setValue(record, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
                }
                list.addRecord(record);
                if (isCursor) {
                    break;
                }
            }
            return list;
        } else if (output instanceof List) {
            List list = (List) output;
            while (true) {
                if (!isCursor) {
                    if (!resultSet.next()) {
                        break;
                    }
                }
                final Map record = new LinkedHashMap();
                for (int i = 1; i <= colCount; i++) {
                    record.put(metadata.getColumnName(i), resultSet.getObject(i));
                }
                list.add(record);
                if (isCursor) {
                    break;
                }
            }
            return list;
        } else if (output instanceof Class) {
            final Class outputClass = (Class) output;
            if (Record.class.isAssignableFrom(outputClass)) {
                if (!isCursor) {
                    if (!resultSet.next()) {
                        return null;
                    }
                }
                Record record = null;
                try {
                    record = (Record) outputClass.newInstance();
                    if (record.getSchema() == null) {
                        record.setSchema(createSchema(metadata));
                    }
                } catch (InstantiationException e) {
                    throw new PersistentException("Output bean instantiate error.", e);
                } catch (IllegalAccessException e) {
                    throw new PersistentException("Output bean instantiate error.", e);
                }
                final Iterator itr = outputMapping.entrySet().iterator();
                while (itr.hasNext()) {
                    final Map.Entry entry = (Map.Entry) itr.next();
                    setValue(record, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
                }
                return record;
            } else if (RecordList.class.isAssignableFrom(outputClass)) {
                RecordList list = null;
                try {
                    list = (RecordList) outputClass.newInstance();
                    if (list.getSchema() == null) {
                        list.setSchema(createSchema(metadata));
                    }
                } catch (InstantiationException e) {
                    throw new PersistentException("Output bean instantiate error.", e);
                } catch (IllegalAccessException e) {
                    throw new PersistentException("Output bean instantiate error.", e);
                }
                while (true) {
                    if (!isCursor) {
                        if (!resultSet.next()) {
                            break;
                        }
                    }
                    final Record record = list.createRecord();
                    final Iterator itr = outputMapping.entrySet().iterator();
                    while (itr.hasNext()) {
                        final Map.Entry entry = (Map.Entry) itr.next();
                        setValue(record, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
                    }
                    list.addRecord(record);
                    if (isCursor) {
                        break;
                    }
                }
                return list;
            } else if (outputClass.isArray()) {
                List list = new ArrayList();
                while (true) {
                    Object bean = fillOutput(resultSet, outputClass.getComponentType(), outputMapping, isCursor);
                    if (bean == null && (resultSet.isAfterLast() || resultSet.getRow() == 0)) {
                        break;
                    }
                    list.add(bean);
                    if (isCursor) {
                        break;
                    }
                }
                return listToArray(list, outputClass.getComponentType());
            } else if (String.class.equals(outputClass) || outputClass.isPrimitive() || Number.class.isAssignableFrom(outputClass)) {
                if (!isCursor) {
                    if (!resultSet.next()) {
                        return null;
                    }
                }
                return getValue(outputClass, resultSet, 1);
            } else {
                if (isCursor) {
                    Object bean = null;
                    try {
                        bean = outputClass.newInstance();
                    } catch (InstantiationException e) {
                        throw new PersistentException("Output bean instantiate error.", e);
                    } catch (IllegalAccessException e) {
                        throw new PersistentException("Output bean instantiate error.", e);
                    }
                    final Iterator itr = outputMapping.entrySet().iterator();
                    while (itr.hasNext()) {
                        final Map.Entry entry = (Map.Entry) itr.next();
                        setValue(bean, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
                    }
                    return bean;
                } else {
                    final List list = new ArrayList();
                    while (resultSet.next()) {
                        Object bean = null;
                        try {
                            bean = outputClass.newInstance();
                        } catch (InstantiationException e) {
                            if (isOutputMappingFromMetaData && outputMapping.size() == 1 && list.size() == 0) {
                                return getValue(outputClass, resultSet, 1);
                            }
                            throw new PersistentException("Output bean instantiate error.", e);
                        } catch (IllegalAccessException e) {
                            if (isOutputMappingFromMetaData && outputMapping.size() == 1 && list.size() == 0) {
                                return getValue(outputClass, resultSet, 1);
                            }
                            throw new PersistentException("Output bean instantiate error.", e);
                        }
                        final Iterator itr = outputMapping.entrySet().iterator();
                        while (itr.hasNext()) {
                            final Map.Entry entry = (Map.Entry) itr.next();
                            try {
                                setValue(bean, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
                            } catch (PersistentException e) {
                                if (isOutputMappingFromMetaData && outputMapping.size() == 1 && list.size() == 0) {
                                    return getValue(outputClass, resultSet, 1);
                                }
                                throw e;
                            }
                        }
                        list.add(bean);
                    }
                    return list;
                }
            }
        } else {
            if (!isCursor) {
                if (!resultSet.next()) {
                    return null;
                }
            }
            if (output instanceof Record) {
                Record record = (Record) output;
                if (record.getSchema() == null) {
                    record.setSchema(createSchema(metadata));
                }
            }
            final Iterator itr = outputMapping.entrySet().iterator();
            while (itr.hasNext()) {
                final Map.Entry entry = (Map.Entry) itr.next();
                setValue(output, (String) entry.getValue(), resultSet, (String) entry.getKey(), isOutputMappingFromMetaData);
            }
            return output;
        }
    } catch (IllegalArgumentException e) {
        throw new PersistentException("Output bean fill error.", e);
    } catch (NoSuchPropertyException e) {
        throw new PersistentException("Output bean fill error.", e);
    } catch (InvocationTargetException e) {
        throw new PersistentException("Output bean fill error.", e.getTargetException());
    } catch (SQLException e) {
        throw new PersistentException("Output bean fill error.", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) Set(java.util.Set) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) Iterator(java.util.Iterator) Record(jp.ossc.nimbus.beans.dataset.Record) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) ArrayList(java.util.ArrayList) List(java.util.List) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) Property(jp.ossc.nimbus.beans.Property) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 42 with Property

use of jp.ossc.nimbus.beans.Property in project nimbus by nimbus-org.

the class DefaultPersistentManagerService method persistQueryInternal.

private int persistQueryInternal(String sql, PreparedStatement statement, Object input, List inputProps, boolean isBatch) throws PersistentException {
    if (input instanceof DataSet) {
        if (inputProps == null) {
            throw new PersistentException("Input bean get error.");
        }
        final List beans = new ArrayList();
        final List properties = new ArrayList();
        int count = -1;
        try {
            final Iterator itr = inputProps.iterator();
            while (itr.hasNext()) {
                final Object propStr = itr.next();
                final Property prop = propertyAccess.getProperty(propStr.toString());
                if (prop instanceof NestedProperty) {
                    Object obj = ((NestedProperty) prop).getThisProperty().getProperty(input);
                    if (obj instanceof RecordList) {
                        final int size = ((RecordList) obj).size();
                        if (count == -1) {
                            count = size;
                        } else if (count != size) {
                            throw new PersistentException("Input bean get error.");
                        }
                    }
                    beans.add(obj);
                    properties.add(((NestedProperty) prop).getNestedProperty());
                } else {
                    throw new PersistentException("Input bean get error.");
                }
            }
        } catch (NoSuchPropertyException e) {
            throw new PersistentException("Input bean get error.", e);
        } catch (InvocationTargetException e) {
            throw new PersistentException("Input bean get error.", e.getTargetException());
        }
        if (count == 0) {
            return 0;
        }
        if (count == -1) {
            try {
                for (int i = 0, imax = beans.size(); i < imax; i++) {
                    Object param = ((Property) properties.get(i)).getProperty(beans.get(i));
                    setObject(statement, i + 1, param);
                }
            } catch (NoSuchPropertyException e) {
                throw new PersistentException("Input bean get error.", e);
            } catch (InvocationTargetException e) {
                throw new PersistentException("Input bean get error.", e.getTargetException());
            }
            try {
                if (isBatch) {
                    statement.addBatch();
                    return -1;
                } else {
                    return statement.executeUpdate();
                }
            } catch (SQLException e) {
                throw new PersistentException("SQL execute error : " + sql, e);
            }
        } else {
            for (int i = 0; i < count; i++) {
                try {
                    for (int j = 0, jmax = beans.size(); j < jmax; j++) {
                        Object bean = beans.get(j);
                        if (bean instanceof RecordList) {
                            bean = ((RecordList) bean).get(i);
                        }
                        Object param = ((Property) properties.get(j)).getProperty(bean);
                        setObject(statement, j + 1, param);
                    }
                } catch (NoSuchPropertyException e) {
                    throw new PersistentException("Input bean get error.", e);
                } catch (InvocationTargetException e) {
                    throw new PersistentException("Input bean get error.", e.getTargetException());
                }
                try {
                    statement.addBatch();
                } catch (SQLException e) {
                    throw new PersistentException("SQL add batch error : " + sql, e);
                }
            }
            if (isBatch) {
                return -1;
            } else {
                int[] updateCounts = null;
                try {
                    updateCounts = statement.executeBatch();
                } catch (SQLException e) {
                    throw new PersistentException("SQL execute error : " + sql, e);
                }
                int result = 0;
                for (int i = 0; i < updateCounts.length; i++) {
                    if (updateCounts[i] > 0) {
                        result += updateCounts[i];
                    }
                }
                if (result == 0) {
                    try {
                        result = statement.getUpdateCount();
                    } catch (SQLException e) {
                    }
                }
                return result;
            }
        }
    } else if ((input instanceof List) || (input != null && input.getClass().isArray())) {
        List list = null;
        if (input instanceof List) {
            list = (List) input;
        } else {
            list = (List) Arrays.asList((Object[]) input);
        }
        if (inputProps != null) {
            if (list.size() == 0) {
                return 0;
            }
            for (int j = 0, jmax = list.size(); j < jmax; j++) {
                Object bean = list.get(j);
                try {
                    for (int i = 0, imax = inputProps.size(); i < imax; i++) {
                        Object param = propertyAccess.get(bean, inputProps.get(i).toString());
                        setObject(statement, i + 1, param);
                    }
                } catch (NoSuchPropertyException e) {
                    throw new PersistentException("Input bean get error.", e);
                } catch (InvocationTargetException e) {
                    throw new PersistentException("Input bean get error.", e.getTargetException());
                }
                try {
                    statement.addBatch();
                } catch (SQLException e) {
                    throw new PersistentException("SQL add batch error : " + sql, e);
                }
            }
            if (isBatch) {
                return -1;
            } else {
                int[] updateCounts = null;
                try {
                    updateCounts = statement.executeBatch();
                } catch (SQLException e) {
                    throw new PersistentException("SQL execute error : " + sql, e);
                }
                int result = 0;
                for (int i = 0; i < updateCounts.length; i++) {
                    if (updateCounts[i] > 0) {
                        result += updateCounts[i];
                    }
                }
                if (result == 0) {
                    try {
                        result = statement.getUpdateCount();
                    } catch (SQLException e) {
                    }
                }
                return result;
            }
        } else {
            if (list.size() == 0) {
                return 0;
            }
            int result = 0;
            for (int i = 0, imax = list.size(); i < imax; i++) {
                Object bean = list.get(i);
                if (bean instanceof Map) {
                    try {
                        Iterator propItr = ((Map) bean).keySet().iterator();
                        int j = 0;
                        while (propItr.hasNext()) {
                            Object param = propertyAccess.get(bean, propItr.next().toString());
                            setObject(statement, ++j, param);
                        }
                    } catch (NoSuchPropertyException e) {
                        throw new PersistentException("Input bean get error.", e);
                    } catch (InvocationTargetException e) {
                        throw new PersistentException("Input bean get error.", e.getTargetException());
                    }
                    try {
                        statement.addBatch();
                    } catch (SQLException e) {
                        throw new PersistentException("SQL add batch error : " + sql, e);
                    }
                    if (i == imax - 1) {
                        if (isBatch) {
                            result = -1;
                        } else {
                            int updateCount = 0;
                            int[] updateCounts = null;
                            try {
                                updateCounts = statement.executeBatch();
                            } catch (SQLException e) {
                                throw new PersistentException("SQL execute error : " + sql, e);
                            }
                            for (int j = 0; j < updateCounts.length; j++) {
                                if (updateCounts[j] > 0) {
                                    updateCount += updateCounts[j];
                                }
                            }
                            if (updateCount == 0) {
                                try {
                                    updateCount = statement.getUpdateCount();
                                } catch (SQLException e) {
                                }
                            }
                            result = updateCount;
                        }
                    }
                } else {
                    setObject(statement, i + 1, bean);
                    if (i == imax - 1) {
                        if (isBatch) {
                            try {
                                statement.addBatch();
                            } catch (SQLException e) {
                                throw new PersistentException("SQL add batch error : " + sql, e);
                            }
                            result = -1;
                        } else {
                            try {
                                result = statement.executeUpdate();
                            } catch (SQLException e) {
                                throw new PersistentException("SQL execute error : " + sql, e);
                            }
                        }
                    }
                }
            }
            return result;
        }
    } else {
        try {
            if (input != null) {
                if (inputProps != null) {
                    for (int i = 0, imax = inputProps.size(); i < imax; i++) {
                        Object param = propertyAccess.get(input, inputProps.get(i).toString());
                        setObject(statement, i + 1, param);
                    }
                } else {
                    if (input instanceof Map) {
                        Iterator propItr = ((Map) input).keySet().iterator();
                        int i = 0;
                        while (propItr.hasNext()) {
                            Object param = propertyAccess.get(input, propItr.next().toString());
                            setObject(statement, ++i, param);
                        }
                    } else {
                        setObject(statement, 1, input);
                    }
                }
            } else {
                if (inputProps != null) {
                    for (int i = 0, imax = inputProps.size(); i < imax; i++) {
                        setObject(statement, i + 1, null);
                    }
                } else {
                    int parameterCount = 0;
                    try {
                        ParameterMetaData paramMetaData = statement.getParameterMetaData();
                        parameterCount = paramMetaData.getParameterCount();
                    } catch (SQLException e) {
                        throw new PersistentException("Illegal sql : " + sql, e);
                    } catch (IncompatibleClassChangeError e) {
                    }
                    if (parameterCount != 0) {
                        setObject(statement, 1, input);
                    }
                }
            }
        } catch (NoSuchPropertyException e) {
            throw new PersistentException("Input bean get error.", e);
        } catch (InvocationTargetException e) {
            throw new PersistentException("Input bean get error.", e.getTargetException());
        }
        if (isBatch) {
            try {
                statement.addBatch();
            } catch (SQLException e) {
                throw new PersistentException("SQL add batch error : " + sql, e);
            }
            return -1;
        } else {
            try {
                return statement.executeUpdate();
            } catch (SQLException e) {
                throw new PersistentException("SQL execute error : " + sql, e);
            }
        }
    }
}
Also used : DataSet(jp.ossc.nimbus.beans.dataset.DataSet) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) Iterator(java.util.Iterator) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) ArrayList(java.util.ArrayList) List(java.util.List) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) Property(jp.ossc.nimbus.beans.Property) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParameterMetaData(java.sql.ParameterMetaData)

Example 43 with Property

use of jp.ossc.nimbus.beans.Property in project nimbus by nimbus-org.

the class DataSetServletRequestParameterConverter method convert.

/**
 * 指定されたオブジェクトを変換する。<p>
 *
 * @param obj 変換対象のオブジェクト
 * @return 変換後のオブジェクト
 * @exception ConvertException 変換に失敗した場合
 */
public Object convert(Object obj) throws ConvertException {
    if (!(obj instanceof ServletRequest)) {
        return null;
    }
    ServletRequest request = (ServletRequest) obj;
    final Map paramMap = request.getParameterMap();
    if (paramMap == null || paramMap.size() == 0) {
        return null;
    }
    String defaultDsName = request.getParameter(dataSetParameterName);
    if ((defaultDsName == null || defaultDsName.length() == 0) && request instanceof HttpServletRequest) {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        String path = httpReq.getServletPath();
        if (httpReq.getPathInfo() != null) {
            path = path + httpReq.getPathInfo();
        }
        if (path != null) {
            int index = path.lastIndexOf('.');
            if (index != -1) {
                path = path.substring(0, index);
            }
            defaultDsName = dataSetPathPrefix + path;
        }
    }
    final Map currentDsMap = new HashMap();
    final Iterator entries = paramMap.entrySet().iterator();
    while (entries.hasNext()) {
        final Map.Entry entry = (Map.Entry) entries.next();
        final String key = (String) entry.getKey();
        final int index = key.indexOf(datasetDelimiter);
        if (index == -1 || index == key.length() - 1) {
            continue;
        }
        String dsName = null;
        if (index == 0) {
            dsName = defaultDsName;
        } else {
            dsName = key.substring(0, index);
        }
        if (dsName == null) {
            continue;
        }
        DataSet ds = (DataSet) currentDsMap.get(dsName);
        if (ds == null) {
            if (dataSetMap.containsKey(dsName)) {
                ds = ((DataSet) dataSetMap.get(dsName)).cloneSchema();
            } else if (beanFlowInvokerFactory != null && beanFlowInvokerFactory.containsFlow(dsName)) {
                final BeanFlowInvoker beanFlowInvoker = beanFlowInvokerFactory.createFlow(dsName);
                Object ret = null;
                try {
                    ret = beanFlowInvoker.invokeFlow(null);
                } catch (Exception e) {
                    throw new ConvertException("Exception occured in BeanFlow '" + dsName + "'", e);
                }
                if (!(ret instanceof DataSet)) {
                    throw new ConvertException("Result of BeanFlow '" + dsName + "' is not DataSet.");
                }
                ds = (DataSet) ret;
            } else {
                if (isIgnoreUnknownParameter) {
                    continue;
                } else {
                    throw new ConvertException("Unknown DataSet : " + dsName);
                }
            }
            currentDsMap.put(dsName, ds);
        }
        final String propStr = key.substring(index + 1);
        Property prop = (Property) propertyCache.get(propStr);
        if (prop == null) {
            try {
                prop = PropertyFactory.createProperty(propStr);
                if (isIgnoreUnknownParameter) {
                    prop.setIgnoreNullProperty(true);
                }
            } catch (IllegalArgumentException e) {
                throw new ConvertException("Parameter '" + key + "' is illegal.", e);
            }
            Property old = (Property) propertyCache.putIfAbsent(propStr, prop);
            if (old != null) {
                prop = old;
            }
        }
        final String[] vals = (String[]) entry.getValue();
        try {
            if (prop instanceof NestedProperty) {
                Property thisProp = ((NestedProperty) prop).getThisProperty();
                if (thisProp instanceof NestedProperty) {
                    Property nestedProp = ((NestedProperty) prop).getNestedProperty();
                    Property nestedProp2 = ((NestedProperty) thisProp).getNestedProperty();
                    if (nestedProp2 instanceof IndexedProperty) {
                        Property thisProp2 = ((NestedProperty) thisProp).getThisProperty();
                        Object thisObj = thisProp2.getProperty(ds);
                        if (thisObj == null) {
                            if (isIgnoreUnknownParameter) {
                                continue;
                            } else {
                                throw new ConvertException("Parameter '" + key + "' is illegal.");
                            }
                        }
                        if (thisObj instanceof RecordList) {
                            setRecordListProperty((RecordList) thisObj, nestedProp.getPropertyName(), ((IndexedProperty) nestedProp2).getIndex(), vals);
                        } else {
                            // ありえない
                            prop.setProperty(ds, vals[vals.length - 1]);
                        }
                    } else {
                        Object thisObj = thisProp.getProperty(ds);
                        if (thisObj == null) {
                            if (isIgnoreUnknownParameter) {
                                continue;
                            } else {
                                throw new ConvertException("Parameter '" + key + "' is illegal.");
                            }
                        }
                        if (thisObj instanceof RecordList) {
                            setRecordListProperty((RecordList) thisObj, nestedProp.getPropertyName(), vals);
                        } else if (thisObj instanceof Record) {
                            setRecordProperty((Record) thisObj, nestedProp.getPropertyName(), nestedProp.getPropertyType(thisObj), vals);
                        } else {
                            nestedProp.setProperty(thisObj, vals[vals.length - 1]);
                        }
                    }
                } else {
                    Object thisObj = thisProp.getProperty(ds);
                    if (thisObj == null) {
                        if (isIgnoreUnknownParameter) {
                            continue;
                        } else {
                            throw new ConvertException("Parameter '" + key + "' is illegal.");
                        }
                    }
                    Property nestedProp = ((NestedProperty) prop).getNestedProperty();
                    if (thisObj instanceof RecordList) {
                        setRecordListProperty((RecordList) thisObj, nestedProp.getPropertyName(), vals);
                    } else if (thisObj instanceof Record) {
                        setRecordProperty((Record) thisObj, nestedProp.getPropertyName(), nestedProp.getPropertyType(thisObj), vals);
                    } else {
                        nestedProp.setProperty(thisObj, vals[vals.length - 1]);
                    }
                }
            } else {
                throw new ConvertException("Parameter '" + key + "' is illegal.");
            }
        } catch (PropertySetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof ConvertException) {
                throw (ConvertException) cause;
            }
            if (isIgnoreUnknownParameter) {
                continue;
            } else {
                throw new ConvertException("Parameter '" + key + "' is illegal.", e);
            }
        } catch (NoSuchPropertyException e) {
            if (isIgnoreUnknownParameter) {
                continue;
            } else {
                throw new ConvertException("Parameter '" + key + "' is illegal.", e);
            }
        } catch (InvocationTargetException e) {
            throw new ConvertException("Parameter '" + key + "' is illegal.", e.getTargetException());
        }
    }
    if (currentDsMap.size() == 0) {
        return null;
    } else if (currentDsMap.size() == 1) {
        return currentDsMap.values().iterator().next();
    } else {
        return currentDsMap;
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) BeanFlowInvoker(jp.ossc.nimbus.service.beancontrol.interfaces.BeanFlowInvoker) HttpServletRequest(javax.servlet.http.HttpServletRequest) Iterator(java.util.Iterator) Record(jp.ossc.nimbus.beans.dataset.Record) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) Property(jp.ossc.nimbus.beans.Property) IndexedProperty(jp.ossc.nimbus.beans.IndexedProperty) NestedProperty(jp.ossc.nimbus.beans.NestedProperty) PropertySetException(jp.ossc.nimbus.beans.dataset.PropertySetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertySetException(jp.ossc.nimbus.beans.dataset.PropertySetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) IndexedProperty(jp.ossc.nimbus.beans.IndexedProperty) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 44 with Property

use of jp.ossc.nimbus.beans.Property in project nimbus by nimbus-org.

the class BeanExchangeConverter method convert.

private Object convert(Object input, Object output, boolean isClone) throws ConvertException {
    if (output == null) {
        throw new ConvertException("Output is null.");
    }
    if (isClone) {
        if (!(output instanceof Cloneable)) {
            throw new ConvertException("Output is not cloneable.");
        }
        if (output instanceof DataSet) {
            output = ((DataSet) output).cloneSchema();
        } else if (output instanceof RecordList) {
            output = ((RecordList) output).cloneSchema();
        } else if (output instanceof Record) {
            output = ((Record) output).cloneSchema();
        } else {
            try {
                output = output.getClass().getMethod("clone", (Class[]) null).invoke(output, (Object[]) null);
            } catch (NoSuchMethodException e) {
                throw new ConvertException(e);
            } catch (IllegalAccessException e) {
                throw new ConvertException(e);
            } catch (InvocationTargetException e) {
                throw new ConvertException(e);
            }
        }
    }
    if (input == null) {
        return output;
    }
    Object[] inputs = null;
    if (input instanceof Collection) {
        inputs = ((Collection) input).toArray();
    } else if (input.getClass().isArray()) {
        inputs = (Object[]) input;
    }
    if (inputs != null) {
        if (inputs.length == 0) {
            return output;
        }
        if (output instanceof RecordList) {
            RecordList list = (RecordList) output;
            for (int i = 0; i < inputs.length; i++) {
                Record record = list.createRecord();
                list.add(convert(inputs[i], record, false));
            }
            return list;
        } else if (output instanceof Collection) {
            Object[] outputs = ((Collection) output).toArray();
            if (outputs.length == 0) {
                throw new ConvertException("Size of collection is 0.");
            }
            for (int i = 0, imax = Math.min(inputs.length, outputs.length); i < imax; i++) {
                outputs[i] = convert(inputs[i], outputs[i], false);
            }
            return outputs;
        } else if (output.getClass().isArray()) {
            Object[] outputs = (Object[]) output;
            final Class componentType = output.getClass().getComponentType();
            if (outputs.length == 0) {
                if (componentType.isInterface() || componentType.isPrimitive()) {
                    throw new ConvertException("Length of array is 0.");
                }
                outputs = (Object[]) Array.newInstance(componentType, inputs.length);
                try {
                    for (int i = 0; i < outputs.length; i++) {
                        outputs[i] = componentType.newInstance();
                    }
                } catch (IllegalAccessException e) {
                    throw new ConvertException("Length of array is 0.", e);
                } catch (InstantiationException e) {
                    throw new ConvertException("Length of array is 0.", e);
                }
            }
            for (int i = 0, imax = Math.min(inputs.length, outputs.length); i < imax; i++) {
                if (outputs[i] == null) {
                    if (componentType.isInterface() || componentType.isPrimitive()) {
                        throw new ConvertException("Element of array is null.");
                    }
                    try {
                        outputs[i] = componentType.newInstance();
                    } catch (IllegalAccessException e) {
                        throw new ConvertException("Element of array is null.", e);
                    } catch (InstantiationException e) {
                        throw new ConvertException("Element of array is null.", e);
                    }
                }
                outputs[i] = convert(inputs[i], outputs[i], false);
            }
            return outputs;
        }
    }
    Map propMapping = propertyMapping;
    boolean isOutputAutoMapping = false;
    boolean isInputAutoMapping = false;
    if (propMapping == null || propMapping.size() == 0) {
        if (propMapping == null) {
            propMapping = new HashMap();
        }
        if (output instanceof Record) {
            Record record = (Record) output;
            RecordSchema schema = record.getRecordSchema();
            if (schema != null) {
                for (int i = 0, imax = schema.getPropertySize(); i < imax; i++) {
                    propMapping.put(schema.getPropertyName(i), schema.getPropertyName(i));
                }
            }
            if (propMapping.size() != 0) {
                isOutputAutoMapping = true;
            }
        } else {
            final SimpleProperty[] props = isFieldOnly(output.getClass()) ? SimpleProperty.getFieldProperties(output) : SimpleProperty.getProperties(output, !isAccessorOnly(output.getClass()));
            for (int i = 0; i < props.length; i++) {
                if (isEnabledPropertyName(output.getClass(), props[i].getPropertyName()) && props[i].isWritable(output.getClass())) {
                    propMapping.put(props[i].getPropertyName(), props[i].getPropertyName());
                }
            }
            if (propMapping.size() != 0) {
                isOutputAutoMapping = true;
            }
        }
        if (!isOutputAutoMapping) {
            if (input instanceof Record) {
                Record record = (Record) input;
                RecordSchema schema = record.getRecordSchema();
                if (schema != null) {
                    for (int i = 0, imax = schema.getPropertySize(); i < imax; i++) {
                        propMapping.put(schema.getPropertyName(i), schema.getPropertyName(i));
                    }
                }
                if (propMapping.size() != 0) {
                    isInputAutoMapping = true;
                }
            } else {
                final SimpleProperty[] props = isFieldOnly(output.getClass()) ? SimpleProperty.getFieldProperties(input) : SimpleProperty.getProperties(input, !isAccessorOnly(output.getClass()));
                for (int i = 0; i < props.length; i++) {
                    if (isEnabledPropertyName(output.getClass(), props[i].getPropertyName()) && props[i].isReadable(input.getClass())) {
                        propMapping.put(props[i].getPropertyName(), props[i].getPropertyName());
                    }
                }
                if (propMapping.size() != 0) {
                    isInputAutoMapping = true;
                }
            }
        }
        if (propMapping.size() == 0) {
            throw new ConvertException("PropertyMapping is null.");
        }
    }
    if (isMakeSchema && (output instanceof Record) && ((Record) output).getRecordSchema() == null) {
        StringBuilder buf = new StringBuilder();
        final Iterator entries = propMapping.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            String inputProp = (String) entry.getKey();
            Object outputProp = entry.getValue();
            try {
                Property prop = propertyAccess.getProperty(inputProp);
                Class propType = prop.getPropertyType(input);
                if (outputProp instanceof String) {
                    buf.append(':').append(outputProp);
                    buf.append(',').append(propType.getName()).append('\n');
                } else {
                    List outputProps = (List) outputProp;
                    for (int i = 0, imax = outputProps.size(); i < imax; i++) {
                        buf.append(':').append((String) outputProps.get(i));
                        buf.append(',').append(propType.getName()).append('\n');
                    }
                }
            } catch (IllegalArgumentException e) {
                throw new ConvertException("Input property parse error. property=" + inputProp, e);
            } catch (NoSuchPropertyException e) {
                if (isOutputAutoMapping) {
                    continue;
                }
                throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
            } catch (InvocationTargetException e) {
                throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
            }
        }
        if (buf.length() != 0) {
            try {
                ((Record) output).setSchema(buf.toString());
            } catch (PropertySchemaDefineException e) {
            // 起こらない
            }
        }
    }
    final Iterator entries = propMapping.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry entry = (Map.Entry) entries.next();
        String inputProp = (String) entry.getKey();
        Object value = null;
        try {
            value = propertyAccess.get(input, inputProp);
        } catch (IllegalArgumentException e) {
            throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
        } catch (NoSuchPropertyException e) {
            if (isOutputAutoMapping) {
                continue;
            }
            throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
        } catch (InvocationTargetException e) {
            throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
        }
        Object outputProp = entry.getValue();
        if (outputProp instanceof String) {
            try {
                propertyAccess.set(output, (String) outputProp, value);
            } catch (IllegalArgumentException e) {
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            } catch (NoSuchPropertyException e) {
                if (isInputAutoMapping) {
                    continue;
                }
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            } catch (InvocationTargetException e) {
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            }
        } else {
            List outputProps = (List) outputProp;
            try {
                for (int i = 0, imax = outputProps.size(); i < imax; i++) {
                    propertyAccess.set(output, (String) outputProps.get(i), value);
                }
            } catch (IllegalArgumentException e) {
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            } catch (NoSuchPropertyException e) {
                if (isInputAutoMapping) {
                    continue;
                }
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            } catch (InvocationTargetException e) {
                throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
            }
        }
    }
    return output;
}
Also used : DataSet(jp.ossc.nimbus.beans.dataset.DataSet) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Record(jp.ossc.nimbus.beans.dataset.Record) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) ArrayList(java.util.ArrayList) List(java.util.List) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema) SimpleProperty(jp.ossc.nimbus.beans.SimpleProperty) Property(jp.ossc.nimbus.beans.Property) PropertySchemaDefineException(jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) Collection(java.util.Collection) SimpleProperty(jp.ossc.nimbus.beans.SimpleProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with Property

use of jp.ossc.nimbus.beans.Property in project nimbus by nimbus-org.

the class HttpServletRequestTransferInterceptorService method startService.

/**
 * サービスの開始処理を行う。
 * <p>
 *
 * @exception Exception サービスの開始に失敗した場合
 */
public void startService() throws Exception {
    if (requestPropertyAndContextKeyMapping == null || requestPropertyAndContextKeyMapping.size() == 0) {
        throw new IllegalArgumentException("RequestPropertyAndContextKeyMapping must be specified.");
    }
    if (threadContextServiceName != null) {
        threadContext = (Context) ServiceManagerFactory.getServiceObject(threadContextServiceName);
    }
    Iterator entries = requestPropertyAndContextKeyMapping.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry entry = (Map.Entry) entries.next();
        Property prop = propertyAccess.getProperty((String) entry.getKey());
        if (!prop.isReadable(HttpServletRequest.class)) {
            throw new IllegalArgumentException("'" + entry.getKey() + "' cannot acquire from a request. value=null");
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Iterator(java.util.Iterator) Map(java.util.Map) Property(jp.ossc.nimbus.beans.Property)

Aggregations

Property (jp.ossc.nimbus.beans.Property)48 NoSuchPropertyException (jp.ossc.nimbus.beans.NoSuchPropertyException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Iterator (java.util.Iterator)5 Map (java.util.Map)5 HashMap (java.util.HashMap)4 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)4 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NestedProperty (jp.ossc.nimbus.beans.NestedProperty)3 SimpleProperty (jp.ossc.nimbus.beans.SimpleProperty)3 Record (jp.ossc.nimbus.beans.dataset.Record)3 PropertyEditor (java.beans.PropertyEditor)2 BufferedReader (java.io.BufferedReader)2 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1