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");
}
}
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;
}
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();
}
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;
}
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;
}
Aggregations