Search in sources :

Example 11 with RecordList

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

the class DataSetXpathConverter method convertToObject.

/**
 * 指定された{@link DataSet}サブクラスのオブジェクトへ変換する。
 * @param inputStream 入力ストリーム
 * @param returnObject 変換対象の{@link DataSet}サブクラス
 * @return 指定したデータセットサブクラスに変換されたオブジェクト
 * @throws ConvertException 変換に失敗した場合
 */
public Object convertToObject(InputStream inputStream, Object returnObject) throws ConvertException {
    DataSet result = null;
    // 出力DataSet生成
    if (returnObject != null) {
        if (DataSet.class.isAssignableFrom(returnObject.getClass())) {
            result = ((DataSet) returnObject).cloneDataSet();
        } else {
            throw new ConvertException("A return object is not a sub-class of DataSet.");
        }
    } else {
        throw new ConvertException("A return object is not specified.");
    }
    Document document = parseXml(inputStream);
    validateXml(document);
    // Header要素抽出
    Iterator headers = result.getHeaderMap().values().iterator();
    while (headers.hasNext()) {
        Header header = (Header) headers.next();
        createRecord(document, result, header, header.getRecordSchema());
    }
    // RecordList要素抽出
    Iterator recordLists = result.getRecordListMap().values().iterator();
    while (recordLists.hasNext()) {
        RecordList recordList = (RecordList) recordLists.next();
        createRecord(document, result, recordList, recordList.getRecordSchema());
    }
    return result;
}
Also used : Header(jp.ossc.nimbus.beans.dataset.Header) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) Iterator(java.util.Iterator) Document(org.w3c.dom.Document)

Example 12 with RecordList

use of jp.ossc.nimbus.beans.dataset.RecordList 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 13 with RecordList

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

the class DataSetXpathConverterTest method testConvertToObject.

public void testConvertToObject() {
    DataSet inputDataSet = new DataSetXPathConverterTestDataSet();
    // 初期データ設定
    Header inputHeader = inputDataSet.getHeader();
    inputHeader.setProperty(DataSetXPathConverterTestDataSet.PROPERTY0, "PROP0");
    inputHeader.setParseProperty(DataSetXPathConverterTestDataSet.PROPERTY1, "PROP1");
    BindingStreamConverter converter = new DataSetXpathConverter();
    DataSetXPathConverterTestDataSet dataSet = (DataSetXPathConverterTestDataSet) converter.convertToObject(createTestData(), inputDataSet);
    Header header = dataSet.getHeader();
    RecordList recordList = dataSet.getRecordList();
    // 初期データ取得
    Assert.assertEquals("PROP0", header.get(DataSetXPathConverterTestDataSet.PROPERTY0));
    // 属性取得
    Assert.assertEquals("ATTR", header.get(DataSetXPathConverterTestDataSet.PROPERTY1));
    // 属性値による指定ノード取得
    Assert.assertEquals("テキスト2-3", header.get(DataSetXPathConverterTestDataSet.PROPERTY2));
    // ノードインデックスによる指定ノード取得
    Assert.assertEquals("ATTR1", header.get(DataSetXPathConverterTestDataSet.PROPERTY3));
    // 子ノード取得
    for (int i = 0; i < recordList.size(); i++) {
        Record record = (Record) recordList.get(i);
        Assert.assertEquals("テキスト" + (i + 1) + "-1", record.get(DataSetXPathConverterTestDataSet.PROPERTY4));
        Assert.assertEquals("テキスト" + (i + 1) + "-2", record.get(DataSetXPathConverterTestDataSet.PROPERTY5));
        Assert.assertEquals("テキスト" + (i + 1) + "-3", record.get(DataSetXPathConverterTestDataSet.PROPERTY6));
    }
}
Also used : Header(jp.ossc.nimbus.beans.dataset.Header) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) Record(jp.ossc.nimbus.beans.dataset.Record)

Example 14 with RecordList

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

the class DatabaseAuthenticateStoreService method activate.

public Object activate(HttpServletRequest request, Object authenticatedKey) throws AuthenticateStoreException {
    if (selectQueryOnFindUser == null) {
        return null;
    }
    Connection con = null;
    try {
        con = connectionFactory.getConnection();
    } catch (ConnectionFactoryException e) {
        throw new AuthenticateStoreException(e);
    }
    try {
        Object authenticatedInfo = null;
        if (selectQueryOnFindUser != null) {
            if (authenticatedInfoClass != null) {
                List list = (List) persistentManager.loadQuery(con, selectQueryOnFindUser, createInput(request, null, authenticatedKey), authenticatedInfoClass);
                if (list.size() == 0) {
                    return null;
                }
                authenticatedInfo = list.get(0);
            } else {
                if (authenticatedInfoTemplate instanceof DataSet) {
                    authenticatedInfo = ((DataSet) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof RecordList) {
                    authenticatedInfo = ((RecordList) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof Record) {
                    authenticatedInfo = ((Record) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof Cloneable) {
                    try {
                        authenticatedInfo = authenticatedInfoTemplate.getClass().getMethod("clone", (Class[]) null).invoke(authenticatedInfoTemplate, (Object[]) null);
                    } catch (NoSuchMethodException e) {
                        throw new AuthenticateStoreException(e);
                    } catch (IllegalAccessException e) {
                        throw new AuthenticateStoreException(e);
                    } catch (InvocationTargetException e) {
                        throw new AuthenticateStoreException(e);
                    }
                }
                authenticatedInfo = persistentManager.loadQuery(con, selectQueryOnFindUser, createInput(request, null, authenticatedKey), authenticatedInfo);
            }
        }
        if (authenticatedInfo != null && updateQueryOnActivate != null) {
            if (request.getSession(false) == null) {
                request.getSession(true);
            }
            persistentManager.persistQuery(con, updateQueryOnActivate, createInput(request, null, authenticatedInfo));
        }
        return authenticatedInfo;
    } catch (PersistentException e) {
        throw new AuthenticateStoreException(e);
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
        }
    }
}
Also used : PersistentException(jp.ossc.nimbus.service.connection.PersistentException) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) List(java.util.List) Record(jp.ossc.nimbus.beans.dataset.Record)

Example 15 with RecordList

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

the class RecordListJournalEditorService method processBlock.

protected boolean processBlock(EditorFinder finder, Object key, Object value, StringBuilder buf) {
    final RecordList bean = (RecordList) value;
    boolean isMake = false;
    if (isOutputRecordListName()) {
        makeRecordListNameFormat(finder, key, bean, buf);
        isMake = true;
    }
    if (isOutputRecordSchema()) {
        if (isMake) {
            buf.append(getLineSeparator());
        }
        makeRecordSchemaFormat(finder, key, bean, buf);
        isMake = true;
    }
    if (isMake) {
        buf.append(getLineSeparator());
    }
    makeRecordsFormat(finder, key, bean, buf);
    isMake = true;
    return isMake;
}
Also used : RecordList(jp.ossc.nimbus.beans.dataset.RecordList)

Aggregations

RecordList (jp.ossc.nimbus.beans.dataset.RecordList)15 Record (jp.ossc.nimbus.beans.dataset.Record)10 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Iterator (java.util.Iterator)7 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 NoSuchPropertyException (jp.ossc.nimbus.beans.NoSuchPropertyException)5 SQLException (java.sql.SQLException)4 Property (jp.ossc.nimbus.beans.Property)4 ArrayList (java.util.ArrayList)3 NestedProperty (jp.ossc.nimbus.beans.NestedProperty)3 Header (jp.ossc.nimbus.beans.dataset.Header)3 Connection (java.sql.Connection)2 LinkedHashMap (java.util.LinkedHashMap)2 RecordSchema (jp.ossc.nimbus.beans.dataset.RecordSchema)2 RecordSet (jp.ossc.nimbus.recset.RecordSet)2 ConnectionFactoryException (jp.ossc.nimbus.service.connection.ConnectionFactoryException)2 PersistentException (jp.ossc.nimbus.service.connection.PersistentException)2