Search in sources :

Example 1 with DataInconsistencyException

use of org.openforis.collect.persistence.DataInconsistencyException in project collect by openforis.

the class UIOptionsBinder method unmarshal.

@Override
public UIOptions unmarshal(Survey survey, String type, String body) {
    XmlPullParser parser = null;
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        Reader reader = new StringReader(body);
        parser.setInput(reader);
        UIOptions uiOptions = new UIOptions((CollectSurvey) survey);
        UITabSet tabSet = unmarshalTabSet(parser, uiOptions);
        while (tabSet != null) {
            uiOptions.addTabSet(tabSet);
            tabSet = unmarshalTabSet(parser, uiOptions);
        }
        return uiOptions;
    } catch (Exception e) {
        throw new DataInconsistencyException(e.getMessage(), e);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) UIOptions(org.openforis.collect.metamodel.ui.UIOptions) UITabSet(org.openforis.collect.metamodel.ui.UITabSet) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) DataInconsistencyException(org.openforis.collect.persistence.DataInconsistencyException) IOException(java.io.IOException) DataInconsistencyException(org.openforis.collect.persistence.DataInconsistencyException) XmlParseException(org.openforis.idm.metamodel.xml.XmlParseException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 2 with DataInconsistencyException

use of org.openforis.collect.persistence.DataInconsistencyException in project collect by openforis.

the class DataLoader method processRow.

private void processRow(CollectRecord record, Record row) throws DataInconsistencyException {
    Integer id = row.getValueAsInteger(DATA.ID);
    Integer parentId = row.getValueAsInteger(DATA.PARENT_ID);
    Integer defnId = row.getValueAsInteger(DATA.DEFINITION_ID);
    Node<?> o;
    if (parentId == null) {
        // Process root entity
        o = record.getRootEntity();
        Integer rootEntityDefnId = o.getDefinition().getId();
        if (!rootEntityDefnId.equals(defnId)) {
            throw new DataInconsistencyException(DATA.DEFINITION_ID + " " + defnId + " does not match " + RECORD.ROOT_ENTITY_ID + " " + rootEntityDefnId);
        }
    } else {
        // Process other objects
        Node<? extends NodeDefinition> parent = objectsById.get(parentId);
        if (parent == null) {
            throw new DataInconsistencyException("Parent " + parentId + " not yet loaded");
        }
        if (!(parent instanceof Entity)) {
            throw new DataInconsistencyException("Parent " + parentId + " not an entity");
        }
        NodeDefinition defn = record.getSurvey().getSchema().getById(defnId);
        if (defn == null) {
            throw new DataInconsistencyException("Unknown schema definition " + DATA.DEFINITION_ID);
        }
        NodeMapper mapper = NodeMapper.getInstance(defn.getClass());
        Node<?> o1 = mapper.addNode(defn, row, (Entity) parent);
        if (defn instanceof AttributeDefinition) {
            String remarks = row.getValue(DATA.REMARKS);
            String s = row.getValueAsString(DATA.SYMBOL);
            Character symbol = null;
            if (s != null && s.length() == 1) {
                symbol = s.charAt(0);
            }
            Attribute<?, ?> attribute = (Attribute<?, ?>) o1;
            attribute.setRemarks(remarks);
            attribute.setSymbol(symbol);
        }
        o = o1;
    }
    objectsById.put(id, o);
}
Also used : Entity(org.openforis.idm.model.Entity) Attribute(org.openforis.idm.model.Attribute) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) DataInconsistencyException(org.openforis.collect.persistence.DataInconsistencyException)

Aggregations

DataInconsistencyException (org.openforis.collect.persistence.DataInconsistencyException)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)1 UITabSet (org.openforis.collect.metamodel.ui.UITabSet)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)1 XmlParseException (org.openforis.idm.metamodel.xml.XmlParseException)1 Attribute (org.openforis.idm.model.Attribute)1 Entity (org.openforis.idm.model.Entity)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)1