Search in sources :

Example 66 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class BaseAttributeUpdateRequestProxy method parseCompositeAttributeValue.

protected Value parseCompositeAttributeValue(CodeListManager codeListManager, Entity parentEntity, String attributeName, Object value) {
    EntityDefinition parentEntityDefn = parentEntity.getDefinition();
    AttributeDefinition defn = (AttributeDefinition) parentEntityDefn.getChildDefinition(attributeName);
    if (defn instanceof CodeAttributeDefinition) {
        if (value instanceof String) {
            String stringVal = (String) value;
            Value result = parseCode(codeListManager, parentEntity, (CodeAttributeDefinition) defn, stringVal);
            return result;
        } else {
            throw new IllegalArgumentException("Invalid value type: expected String");
        }
    } else if (defn instanceof RangeAttributeDefinition) {
        if (value instanceof String) {
            String stringVal = (String) value;
            RangeAttributeDefinition rangeDef = (RangeAttributeDefinition) defn;
            RangeAttributeDefinition.Type type = rangeDef.getType();
            NumericRange<?> range = null;
            // todo check if unit is required here or is set later by the client
            Unit unit = null;
            switch(type) {
                case INTEGER:
                    range = IntegerRange.parseIntegerRange(stringVal, unit);
                    break;
                case REAL:
                    range = RealRange.parseRealRange(stringVal, unit);
                    break;
            }
            return range;
        } else {
            throw new IllegalArgumentException("Invalid value type: expected String");
        }
    } else {
        throw new IllegalArgumentException("Invalid AttributeDefinition: expected CodeAttributeDefinition or RangeAttributeDefinition");
    }
}
Also used : NumericRange(org.openforis.idm.model.NumericRange) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Value(org.openforis.idm.model.Value) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition) Unit(org.openforis.idm.metamodel.Unit) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition)

Example 67 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class SurveyViewGenerator method generateView.

public SurveyView generateView(final CollectSurvey survey) {
    final SurveyView surveyView = new SurveyView(survey);
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList codeList : codeLists) {
        CodeListView codeListView = new CodeListView();
        codeListView.setId(codeList.getId());
        codeListView.setName(codeList.getName());
        codeListView.setLabel(codeList.getLabel(CodeListLabel.Type.ITEM, languageCode));
        if (includeCodeListValues && !codeList.isExternal()) {
            CodeListService service = survey.getContext().getCodeListService();
            List<CodeListItem> items = service.loadRootItems(codeList);
            for (CodeListItem item : items) {
                codeListView.addItem(createCodeListItemView(item));
            }
        }
        surveyView.addCodeList(codeListView);
    }
    final Map<Integer, NodeDefView> viewById = new HashMap<Integer, NodeDefView>();
    survey.getSchema().traverse(new NodeDefinitionVisitor() {

        public void visit(NodeDefinition def) {
            int id = def.getId();
            String name = def.getName();
            String label = getLabel(def);
            NodeDefView view;
            if (def instanceof EntityDefinition) {
                view = new EntityDefView(((EntityDefinition) def).isRoot(), id, name, label, def.isMultiple());
            } else if (def instanceof CodeAttributeDefinition) {
                CodeAttributeDefinition attrDef = (CodeAttributeDefinition) def;
                int codeListId = attrDef.getList() == null ? -1 : attrDef.getList().getId();
                view = new CodeAttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef), codeListId);
            } else {
                AttributeDefinition attrDef = (AttributeDefinition) def;
                view = new AttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef));
            }
            NodeDefinition parentDef = def.getParentDefinition();
            if (parentDef == null) {
                surveyView.getSchema().addRootEntity((EntityDefView) view);
            } else {
                EntityDefView parentView = (EntityDefView) viewById.get(parentDef.getId());
                parentView.addChild(view);
            }
            viewById.put(id, view);
        }
    });
    return surveyView;
}
Also used : HashMap(java.util.HashMap) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor) CodeList(org.openforis.idm.metamodel.CodeList) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 68 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class UIColumnView method getAttributeType.

public String getAttributeType() {
    AttributeDefinition attrDef = uiObject.getAttributeDefinition();
    AttributeType attrType = AttributeType.valueOf(attrDef);
    return attrType.name();
}
Also used : AttributeType(org.openforis.collect.designer.metamodel.AttributeType) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Example 69 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class CreateRecordHandler method execute.

@Override
public List<RecordEvent> execute(CreateRecordCommand command) {
    String username = command.getUsername();
    User user = userManager.loadByUserName(username);
    CollectSurvey survey = surveyManager.getById(command.getSurveyId());
    List<EntityDefinition> rootDefs = survey.getSchema().getRootEntityDefinitions();
    EntityDefinition firstRootEntity = rootDefs.get(0);
    String firstRootEntityName = firstRootEntity.getName();
    CollectRecord record = recordManager.instantiateRecord(survey, firstRootEntityName, user, command.getFormVersion(), Step.ENTRY);
    NodeChangeSet changeSet = recordManager.initializeRecord(record);
    List<String> keyValues = command.getKeyValues();
    Iterator<String> keyValuesIt = keyValues.iterator();
    List<AttributeDefinition> keyAttributeDefinitions = firstRootEntity.getKeyAttributeDefinitions();
    Iterator<AttributeDefinition> keyDefsIt = keyAttributeDefinitions.iterator();
    while (keyDefsIt.hasNext()) {
        AttributeDefinition keyDef = keyDefsIt.next();
        String keyVal = keyValuesIt.next();
        Value keyValue = keyDef.createValue(keyVal);
        Attribute<?, Value> keyAttr = record.findNodeByPath(keyDef.getPath());
        recordUpdater.updateAttribute(keyAttr, keyValue);
    }
    recordManager.save(record);
    List<RecordEvent> events = new EventProducer().produceFor(changeSet, user.getUsername());
    for (RecordEvent recordEvent : events) {
        recordEvent.initializeRecordId(record.getId());
    }
    return events;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) User(org.openforis.collect.model.User) EventProducer(org.openforis.collect.event.EventProducer) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) RecordEvent(org.openforis.collect.event.RecordEvent) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Value(org.openforis.idm.model.Value) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 70 with AttributeDefinition

use of org.openforis.idm.metamodel.AttributeDefinition in project collect by openforis.

the class RecordSummaryDao method load.

@Transactional
public List<RecordSummary> load(EntityDefinition rootEntityDefinition, List<EntityDefinition> countable, int offset, int maxRecords, String orderByField, String filter) {
    List<AttributeDefinition> keyDefs = rootEntityDefinition.getKeyAttributeDefinitions();
    Factory jf = getJooqFactory();
    org.openforis.collect.persistence.jooq.tables.Record r = RECORD.as("r");
    SelectQuery q = jf.selectQuery();
    q.addFrom(r);
    q.addSelect(r.DATE_CREATED.as(DATE_CREATED_ALIAS), r.DATE_MODIFIED.as(DATE_MODIFIED_ALIAS), r.ERRORS, r.ID, r.LOCKED_BY_ID, r.MISSING, r.MODEL_VERSION, r.MODIFIED_BY_ID, r.ROOT_ENTITY_ID, r.SKIPPED, r.STATE, r.STEP, r.WARNINGS);
    {
        // add keys to select with an alias like KEY_ALIAS_PREFIX + ATTRIBUTE_NAME
        int position = 1;
        for (AttributeDefinition def : keyDefs) {
            String alias = KEY_ALIAS_PREFIX + def.getName();
            Field<?> field = RecordDaoUtil.getKeyField(r, def, position).as(alias);
            q.addSelect(field);
            position++;
        }
    }
    {
        // add count columns to select with an alias like COUNT_ALIAS_PREFIX + ENTITY_NAME
        int position = 1;
        for (EntityDefinition def : countable) {
            String alias = COUNT_ALIAS_PREFIX + def.getName();
            Field<?> field = RecordDaoUtil.getCountField(r, position).as(alias);
            q.addSelect(field);
            position++;
        }
    }
    // add order by condition
    Field<?> orderBy = null;
    if (orderByField != null) {
        List<Field<?>> selectFields = q.getSelect();
        for (Field<?> field : selectFields) {
            if (orderByField.equals(field.getName())) {
                orderBy = field;
                break;
            }
        }
    }
    if (orderBy != null) {
        q.addOrderBy(orderBy);
    }
    // always order by ID to avoid pagination issues
    q.addOrderBy(r.ID);
    // add limit
    q.addLimit(offset, maxRecords);
    // fetch results
    Result<Record> records = q.fetch();
    List<RecordSummary> result = mapRecordsToSummaries(records, keyDefs, countable);
    return result;
}
Also used : AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) Factory(org.jooq.impl.Factory) SelectQuery(org.jooq.SelectQuery) RecordSummary(org.openforis.collect.model.RecordSummary) Field(org.jooq.Field) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Record(org.jooq.Record) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)107 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)47 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)37 ArrayList (java.util.ArrayList)33 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)27 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)21 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)20 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)18 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)18 CollectSurvey (org.openforis.collect.model.CollectSurvey)17 CoordinateAttributeDefinition (org.openforis.idm.metamodel.CoordinateAttributeDefinition)16 KeyAttributeDefinition (org.openforis.idm.metamodel.KeyAttributeDefinition)16 TextAttributeDefinition (org.openforis.idm.metamodel.TextAttributeDefinition)12 TimeAttributeDefinition (org.openforis.idm.metamodel.TimeAttributeDefinition)12 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)11 Schema (org.openforis.idm.metamodel.Schema)10 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)9 Entity (org.openforis.idm.model.Entity)9 Value (org.openforis.idm.model.Value)9 RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)8