Search in sources :

Example 1 with RecordSchema

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

the class SharedContextRecord method updateProperty.

/**
 * 指定された名前のプロパティに、指定された値を更新した場合の差分情報を取得する。<p>
 *
 * @param name プロパティ名
 * @param val プロパティの値
 * @param diff 差分
 * @return 差分
 * @exception PropertySetException プロパティの設定に失敗した場合
 * @exception SharedContextUpdateException 差分情報の取得に失敗した場合
 */
public SharedContextValueDifference updateProperty(String name, Object val, SharedContextValueDifference diff) throws PropertySetException, SharedContextUpdateException {
    RecordSchema recordSchema = getRecordSchema();
    if (recordSchema == null) {
        throw new PropertySetException(null, "Schema is not initialized.");
    }
    final PropertySchema propertySchema = recordSchema.getPropertySchema(name);
    if (propertySchema == null) {
        throw new PropertySetException(null, "No such property : " + name);
    }
    return updateProperty(recordSchema.getPropertyIndex(name), val, diff);
}
Also used : PropertySetException(jp.ossc.nimbus.beans.dataset.PropertySetException) PropertySchema(jp.ossc.nimbus.beans.dataset.PropertySchema) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema)

Example 2 with RecordSchema

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

the class DataSetXpathConverter method createRecord.

private void createRecord(Document document, DataSet dataSet, Object target, RecordSchema recordSchema) {
    PropertySchema[] propertySchemata = recordSchema.getPropertySchemata();
    for (int i = 0; i < propertySchemata.length; i++) {
        if (propertySchemata[i] instanceof XpathPropertySchema) {
            // PropertySchemaからXPath取得
            XpathPropertySchema xmlBindingPropertySchema = (XpathPropertySchema) propertySchemata[i];
            XPathExpression expression = xmlBindingPropertySchema.getXpathExpression();
            // XPathによりXML要素を抽出
            NodeList nodeList = null;
            try {
                nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
            } catch (XPathExpressionException e) {
                throw new ConvertException("The converter failed to evaluate a XML. ", e);
            }
            // DataSetへ変換
            int length = nodeList.getLength();
            if (target instanceof Record) {
                if (length > 0) {
                    Object nodeValue = nodeList.item(0).getNodeValue();
                    ((Record) target).setParseProperty(xmlBindingPropertySchema.getName(), nodeValue);
                }
            } else if (target instanceof RecordList) {
                RecordList targetRecordList = (RecordList) target;
                int offset = length - targetRecordList.size();
                if (offset > 0) {
                    for (int j = 0; j < offset; j++) {
                        Record record = targetRecordList.createRecord();
                        targetRecordList.addRecord(record);
                    }
                }
                for (int j = 0; j < length; j++) {
                    Object nodeValue = nodeList.item(j).getNodeValue();
                    Record record = targetRecordList.getRecord(j);
                    record.setParseProperty(xmlBindingPropertySchema.getName(), nodeValue);
                }
            }
        } else if (propertySchemata[i] instanceof RecordPropertySchema) {
            RecordPropertySchema recordPropertySchema = (RecordPropertySchema) propertySchemata[i];
            RecordSchema nestedRecordSchema = dataSet.getNestedRecordSchema(recordPropertySchema.getName());
            Record nestedRecord = dataSet.createNestedRecord(recordPropertySchema.getRecordName());
            createRecord(document, dataSet, target, nestedRecordSchema);
            ((Record) target).setProperty(recordPropertySchema.getName(), nestedRecord);
        } else if (propertySchemata[i] instanceof RecordListPropertySchema) {
            RecordListPropertySchema recordListPropertySchema = (RecordListPropertySchema) propertySchemata[i];
            RecordSchema nestedRecordSchema = dataSet.getNestedRecordListSchema(recordListPropertySchema.getRecordListName());
            RecordList nestedRecordList = dataSet.createNestedRecordList(recordListPropertySchema.getRecordListName());
            createRecord(document, dataSet, nestedRecordList, nestedRecordSchema);
            ((Record) target).setProperty(recordListPropertySchema.getName(), nestedRecordList);
        }
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XpathPropertySchema(jp.ossc.nimbus.beans.dataset.XpathPropertySchema) PropertySchema(jp.ossc.nimbus.beans.dataset.PropertySchema) RecordPropertySchema(jp.ossc.nimbus.beans.dataset.RecordPropertySchema) RecordListPropertySchema(jp.ossc.nimbus.beans.dataset.RecordListPropertySchema) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) Record(jp.ossc.nimbus.beans.dataset.Record) RecordPropertySchema(jp.ossc.nimbus.beans.dataset.RecordPropertySchema) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema) RecordListPropertySchema(jp.ossc.nimbus.beans.dataset.RecordListPropertySchema) XpathPropertySchema(jp.ossc.nimbus.beans.dataset.XpathPropertySchema)

Example 3 with RecordSchema

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

the class RecordJournalEditorService method makePropertiesFormat.

protected StringBuilder makePropertiesFormat(EditorFinder finder, Object key, Record bean, StringBuilder buf) {
    buf.append(PROPERTIES_HEADER);
    final RecordSchema schema = bean.getRecordSchema();
    final PropertySchema[] props = schema.getPropertySchemata();
    if (props == null || props.length == 0) {
        buf.append(NULL_STRING);
        return buf;
    } else {
        buf.append(getLineSeparator());
    }
    final StringBuilder subBuf = new StringBuilder();
    if (isOutputCSVProperties) {
        for (int i = 0, max = props.length; i < max; i++) {
            final String name = props[i].getName();
            if (!enabledPropertySet.isEmpty() && !enabledPropertySet.contains(name)) {
                continue;
            }
            subBuf.append(name);
            if (i != max - 1) {
                subBuf.append(CSV_SEPARATOR);
            }
        }
        subBuf.append(getLineSeparator());
        for (int i = 0, max = props.length; i < max; i++) {
            final String name = props[i].getName();
            if (!enabledPropertySet.isEmpty() && !enabledPropertySet.contains(name)) {
                continue;
            }
            if (secretPropertySet.contains(name)) {
                subBuf.append(getSecretString());
            } else {
                makeObjectFormat(finder, null, bean.getProperty(name), subBuf);
            }
            if (i != max - 1) {
                subBuf.append(CSV_SEPARATOR);
            }
        }
    } else {
        for (int i = 0, max = props.length; i < max; i++) {
            final String name = props[i].getName();
            if (!enabledPropertySet.isEmpty() && !enabledPropertySet.contains(name)) {
                continue;
            }
            subBuf.append(name);
            subBuf.append(PROPERTY_SEPARATOR);
            if (secretPropertySet.contains(name)) {
                subBuf.append(getSecretString());
            } else {
                makeObjectFormat(finder, null, bean.getProperty(name), subBuf);
            }
            if (i != max - 1) {
                subBuf.append(getLineSeparator());
            }
        }
    }
    addIndent(subBuf);
    return buf.append(subBuf);
}
Also used : PropertySchema(jp.ossc.nimbus.beans.dataset.PropertySchema) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema)

Example 4 with RecordSchema

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

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

the class SharedContextRecord method updateParseProperty.

/**
 * 指定されたインデックスのプロパティに、指定された値をパースして更新した場合の差分情報を取得する。<p>
 *
 * @param index プロパティのインデックス
 * @param val プロパティの値
 * @param diff 差分
 * @return 差分
 * @exception PropertySetException プロパティの設定に失敗した場合
 * @exception SharedContextUpdateException 差分情報の取得に失敗した場合
 */
public SharedContextValueDifference updateParseProperty(int index, Object val, SharedContextValueDifference diff) throws PropertySetException, SharedContextUpdateException {
    RecordSchema recordSchema = getRecordSchema();
    if (recordSchema == null) {
        throw new PropertySetException(null, "Schema is not initialized.");
    }
    final PropertySchema propertySchema = recordSchema.getPropertySchema(index);
    if (propertySchema == null) {
        throw new PropertySetException(null, "No such property : " + index);
    }
    return updateProperty(index, propertySchema.parse(val), diff);
}
Also used : PropertySetException(jp.ossc.nimbus.beans.dataset.PropertySetException) PropertySchema(jp.ossc.nimbus.beans.dataset.PropertySchema) RecordSchema(jp.ossc.nimbus.beans.dataset.RecordSchema)

Aggregations

RecordSchema (jp.ossc.nimbus.beans.dataset.RecordSchema)6 PropertySchema (jp.ossc.nimbus.beans.dataset.PropertySchema)5 PropertySetException (jp.ossc.nimbus.beans.dataset.PropertySetException)3 Record (jp.ossc.nimbus.beans.dataset.Record)2 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 NoSuchPropertyException (jp.ossc.nimbus.beans.NoSuchPropertyException)1 Property (jp.ossc.nimbus.beans.Property)1 SimpleProperty (jp.ossc.nimbus.beans.SimpleProperty)1 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)1 PropertySchemaDefineException (jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException)1 RecordListPropertySchema (jp.ossc.nimbus.beans.dataset.RecordListPropertySchema)1