Search in sources :

Example 16 with NoSuchPropertyException

use of jp.ossc.nimbus.beans.NoSuchPropertyException 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 17 with NoSuchPropertyException

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

the class DefaultPersistentManagerService method setStatementProperties.

private void setStatementProperties(PreparedStatement st, Map statementProps) throws PersistentException {
    if (statementProps == null || statementProps.size() == 0) {
        return;
    }
    try {
        Iterator entries = statementProps.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            propertyAccess.set(st, (String) entry.getKey(), entry.getValue());
        }
    } catch (NoSuchPropertyException e) {
        throw new PersistentException("Statement property set error.", e);
    } catch (InvocationTargetException e) {
        throw new PersistentException("Statement property set error.", e.getTargetException());
    }
}
Also used : Iterator(java.util.Iterator) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with NoSuchPropertyException

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

the class DefaultPersistentManagerService method createCursor.

public Cursor createCursor(Connection con, String sql, Object input, Object inputProps, Object outputProps, Map statementProps, Map resultSetProps) throws PersistentException {
    List inputPropList = null;
    if (inputProps != null) {
        if (inputProps.getClass().isArray()) {
            inputPropList = (List) Arrays.asList((Object[]) inputProps);
        } else if (inputProps instanceof List) {
            inputPropList = (List) inputProps;
        } else if (inputProps instanceof String) {
            inputPropList = new ArrayList();
            inputPropList.add(inputProps);
        } else {
            throw new PersistentException("No supported inputProps type." + inputProps);
        }
    } else if (input != null && input instanceof Map) {
        inputPropList = new ArrayList(((Map) input).keySet());
    }
    List outputPropList = null;
    Map outputPropMap = null;
    if (outputProps != null) {
        if (outputProps.getClass().isArray()) {
            outputPropList = (List) Arrays.asList((Object[]) outputProps);
        } else if (outputProps instanceof List) {
            outputPropList = (List) outputProps;
        } else if (outputProps instanceof Map) {
            outputPropMap = (Map) outputProps;
        } else if (outputProps instanceof String) {
            outputPropList = new ArrayList();
            outputPropList.add(outputProps);
        } else {
            throw new PersistentException("No supported outputProps type." + outputProps);
        }
    }
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        statement = con.prepareStatement(sql);
    } catch (SQLException e) {
        throw new PersistentException("Illegal sql : " + sql, e);
    }
    setStatementProperties(statement, statementProps);
    try {
        try {
            final ParameterMetaData metadata = statement.getParameterMetaData();
            if (inputPropList != null && inputPropList.size() != metadata.getParameterCount()) {
                throw new PersistentException("Illegal sql : " + sql);
            }
        } catch (IncompatibleClassChangeError e) {
        }
        if (input != null) {
            if (inputPropList != null) {
                for (int i = 0, imax = inputPropList.size(); i < imax; i++) {
                    Object param = propertyAccess.get(input, inputPropList.get(i).toString());
                    setObject(statement, i + 1, param);
                }
            } else if (input.getClass().isArray()) {
                for (int i = 0, imax = Array.getLength(input); i < imax; i++) {
                    Object param = Array.get(input, i);
                    setObject(statement, i + 1, param);
                }
            } else if (input instanceof List) {
                List list = (List) input;
                for (int i = 0, imax = list.size(); i < imax; i++) {
                    Object param = list.get(i);
                    setObject(statement, i + 1, param);
                }
            } else {
                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());
    } catch (SQLException e) {
        throw new PersistentException("The parameter is not suitable for SQL.", e);
    }
    try {
        resultSet = statement.executeQuery();
    } catch (SQLException e) {
        throw new PersistentException("SQL execute error : " + sql, e);
    }
    setResultSetProperties(resultSet, resultSetProps);
    if (outputPropList != null) {
        try {
            ResultSetMetaData metadata = resultSet.getMetaData();
            if ((outputPropList.size() != metadata.getColumnCount())) {
                throw new PersistentException("Illegal sql : " + sql);
            }
            outputPropMap = new LinkedHashMap();
            for (int i = 0, imax = outputPropList.size(); i < imax; i++) {
                outputPropMap.put(metadata.getColumnName(i + 1), outputPropList.get(i).toString());
            }
        } catch (SQLException e) {
            throw new PersistentException("The parameter is not suitable for SQL.", e);
        }
    }
    return new CursorImpl(statement, resultSet, outputPropMap);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParameterMetaData(java.sql.ParameterMetaData)

Example 19 with NoSuchPropertyException

use of jp.ossc.nimbus.beans.NoSuchPropertyException 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 20 with NoSuchPropertyException

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

the class DefaultPersistentManagerService method load.

public Object load(Connection con, String sql, Object input, Object inputProps, Object output, Object outputProps, Map statementProps, Map resultSetProps) throws PersistentException {
    List inputPropList = null;
    if (inputProps != null) {
        if (inputProps.getClass().isArray()) {
            inputPropList = (List) Arrays.asList((Object[]) inputProps);
        } else if (inputProps instanceof List) {
            inputPropList = (List) inputProps;
        } else if (inputProps instanceof String) {
            inputPropList = new ArrayList();
            inputPropList.add(inputProps);
        } else {
            throw new PersistentException("No supported inputProps type." + inputProps);
        }
    } else if (input != null && input instanceof Map) {
        inputPropList = new ArrayList(((Map) input).keySet());
    }
    List outputPropList = null;
    Map outputPropMap = null;
    if (outputProps != null) {
        if (outputProps.getClass().isArray()) {
            outputPropList = (List) Arrays.asList((Object[]) outputProps);
        } else if (outputProps instanceof List) {
            outputPropList = (List) outputProps;
        } else if (outputProps instanceof Map) {
            outputPropMap = (Map) outputProps;
        } else if (outputProps instanceof String) {
            outputPropList = new ArrayList();
            outputPropList.add(outputProps);
        } else {
            throw new PersistentException("No supported outputProps type." + outputProps);
        }
    }
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        try {
            statement = con.prepareStatement(sql);
        } catch (SQLException e) {
            throw new PersistentException("Illegal sql : " + sql, e);
        }
        setStatementProperties(statement, statementProps);
        try {
            try {
                final ParameterMetaData metadata = statement.getParameterMetaData();
                if (inputPropList != null && inputPropList.size() != metadata.getParameterCount()) {
                    throw new PersistentException("Illegal sql : " + sql);
                }
            } catch (IncompatibleClassChangeError e) {
            }
            if (input != null) {
                if (inputPropList != null) {
                    for (int i = 0, imax = inputPropList.size(); i < imax; i++) {
                        Object param = propertyAccess.get(input, inputPropList.get(i).toString());
                        setObject(statement, i + 1, param);
                    }
                } else if (input.getClass().isArray()) {
                    for (int i = 0, imax = Array.getLength(input); i < imax; i++) {
                        Object param = Array.get(input, i);
                        setObject(statement, i + 1, param);
                    }
                } else if (input instanceof List) {
                    List list = (List) input;
                    for (int i = 0, imax = list.size(); i < imax; i++) {
                        Object param = list.get(i);
                        setObject(statement, i + 1, param);
                    }
                } else {
                    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());
        } catch (SQLException e) {
            throw new PersistentException("The parameter is not suitable for SQL.", e);
        }
        try {
            resultSet = statement.executeQuery();
        } catch (SQLException e) {
            throw new PersistentException("SQL execute error : " + sql, e);
        }
        setResultSetProperties(resultSet, resultSetProps);
        if (outputPropList != null) {
            try {
                ResultSetMetaData metadata = resultSet.getMetaData();
                if ((outputPropList.size() != metadata.getColumnCount())) {
                    throw new PersistentException("Illegal sql : " + sql);
                }
                outputPropMap = new LinkedHashMap();
                for (int i = 0, imax = outputPropList.size(); i < imax; i++) {
                    outputPropMap.put(metadata.getColumnName(i + 1), outputPropList.get(i).toString());
                }
            } catch (SQLException e) {
                throw new PersistentException("The parameter is not suitable for SQL.", e);
            }
        }
        return fillOutput(resultSet, output, outputPropMap, false);
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
            statement = null;
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) NoSuchPropertyException(jp.ossc.nimbus.beans.NoSuchPropertyException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ParameterMetaData(java.sql.ParameterMetaData)

Aggregations

NoSuchPropertyException (jp.ossc.nimbus.beans.NoSuchPropertyException)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 Map (java.util.Map)13 HashMap (java.util.HashMap)11 Iterator (java.util.Iterator)11 Property (jp.ossc.nimbus.beans.Property)8 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)7 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 ArrayList (java.util.ArrayList)5 NestedProperty (jp.ossc.nimbus.beans.NestedProperty)5 SQLException (java.sql.SQLException)4 SimpleProperty (jp.ossc.nimbus.beans.SimpleProperty)4 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)4 Record (jp.ossc.nimbus.beans.dataset.Record)4 ParameterMetaData (java.sql.ParameterMetaData)3 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 IndexedProperty (jp.ossc.nimbus.beans.IndexedProperty)3 PropertyEditor (java.beans.PropertyEditor)2