use of org.openforis.idm.metamodel.CodeAttributeDefinition in project collect by openforis.
the class CodeListManager method loadItemByAttribute.
@SuppressWarnings("unchecked")
public <T extends CodeListItem> T loadItemByAttribute(CodeAttribute attribute) {
CodeAttributeDefinition defn = attribute.getDefinition();
CodeList list = defn.getList();
boolean persistedSurvey = list.getSurvey().getId() != null;
if (persistedSurvey && list.isExternal()) {
return (T) provider.getItem(attribute);
} else if (persistedSurvey && list.isEmpty()) {
return (T) loadPersistedItem(attribute);
} else {
return (T) getInternalCodeListItem(attribute);
}
}
use of org.openforis.idm.metamodel.CodeAttributeDefinition in project collect by openforis.
the class CollectEarthGridTemplateGenerator method validateCell.
/**
* Checks if a value can be used as the input for an attribute
* @param attributeDefinition The attribute that we want to check the value against
* @param value The value that should be checked
* @return True if the value can be used in the attribute. False otherwise (for instance trying to use a string "abc" as the input for a Number attribute
*/
private String validateCell(CollectSurvey survey, AttributeDefinition attributeDefinition, String value) {
try {
// By creating the value using hte attribute definitoon a validation is performed
Value valueCreated = attributeDefinition.createValue(value);
if (attributeDefinition.isAlwaysRequired() && StringUtils.isBlank(value)) {
return String.format("The attribute %s is marekd as \"always required\". The value in the cell is empty!", attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
}
if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LAT_COORDINATE)) {
double lat = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
if (lat < -90 || lat > 90) {
return "The latitude of a plot must be between -90 and 90 degrees!";
}
}
if (isEpsg4326SRS(survey) && attributeDefinition.getName().equals(LONG_COORDINATE)) {
double longitude = ((NumberValue<Number>) valueCreated).getValue().doubleValue();
if (longitude < -180 || longitude > 180) {
return "The latitude of a plot must be between -180 and 180 degrees!";
}
}
// Make sure that the code used in a code-attribute is actually present on the codelist
if (attributeDefinition instanceof CodeAttributeDefinition) {
CodeAttributeDefinition cad = (CodeAttributeDefinition) attributeDefinition;
// IF IT IS A STRICT CODE LIST
if (!cad.isAllowUnlisted()) {
// Check that the code exists in the codelist
// Gets the level (in case of hierarchical codelists) that the attribute refers to. If it is a flat codelist then it is alway 0
int levelIndex = cad.getLevelIndex();
CodeListService codeListService = attributeDefinition.getSurvey().getContext().getCodeListService();
List<CodeListItem> items = codeListService.loadItems(cad.getList(), levelIndex + 1);
// Check one by one in the codes of the codelist assigned to the attribute if the value is present as a code!
for (CodeListItem codeListItem : items) {
if (codeListItem.getCode().equals(value)) {
// FOUND! All good, return null
return null;
}
}
return String.format("The code with value \"%s\" is not part of the codelist used by code %s ", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
}
} else if (attributeDefinition instanceof CodeAttributeDefinition) {
}
} catch (Exception e) {
return String.format("The value \"%s\" cannot be used as a value for the attribute %s", value, attributeDefinition.getLabel(NodeLabel.Type.INSTANCE));
}
return null;
}
use of org.openforis.idm.metamodel.CodeAttributeDefinition in project collect by openforis.
the class Mondrian4SchemaGenerator method createDimensionAttributes.
private List<Attribute> createDimensionAttributes(DataTable dataTable, AttributeDefinition attrDefn) {
List<Attribute> attributes = new ArrayList<Attribute>();
if (attrDefn instanceof CodeAttributeDefinition && !((CodeAttributeDefinition) attrDefn).getList().isExternal()) {
CodeAttributeDefinition codeAttrDefn = (CodeAttributeDefinition) attrDefn;
CodeTable codeListTable = rdbSchema.getCodeListTable(codeAttrDefn);
String codeListTableName = codeListTable.getName();
Attribute attribute = new Attribute();
FieldDefinition<String> codeFieldDef = codeAttrDefn.getCodeFieldDefinition();
attribute.name = codeFieldDef.getName();
attribute.caption = getAttributeCaption(codeFieldDef);
attribute.table = codeListTableName;
attribute.keyColumn = CodeListTables.getCodeColumnName(rdbConfig, codeListTableName);
attribute.nameColumn = CodeListTables.getLabelColumnName(rdbConfig, codeListTableName);
attributes.add(attribute);
} else if (attrDefn.hasMainField()) {
attributes.addAll(createAttributesForFields(dataTable, attrDefn));
} else if (attrDefn instanceof DateAttributeDefinition || attrDefn instanceof TimeAttributeDefinition) {
List<DataColumn> dataColumns = dataTable.getDataColumns(attrDefn);
DataColumn dataColumn = dataColumns.get(0);
Attribute attribute = new Attribute();
attribute.name = attrDefn.getName();
attribute.caption = getDimensionCaption(attrDefn);
attribute.keyColumn = dataColumn.getName();
attributes.add(attribute);
attributes.addAll(createAttributesForFields(dataTable, attrDefn));
} else {
// TODO
// every field makes the Key of the Attribute ?! then nameColumn must be specified
// Attribute attribute = new Attribute();
// attribute.name = attrDefn.getName();
// attribute.caption = getDimensionCaption(attrDefn);
// Key key = new Key();
// for (FieldDefinition<?> fieldDef : attrDefn.getFieldDefinitions()) {
// DataColumn dataColumn = dataTable.getDataColumn(fieldDef);
// if (dataColumn != null) {
// Column column = new Column();
// column.name = dataColumn.getName();
// key.list().add(column);
// }
// }
// attribute.children.add(key);
// Name name = new Name();
// name.list().add(e)
// attribute.children.add(name);
// attributes.add(attribute);
}
return attributes;
}
use of org.openforis.idm.metamodel.CodeAttributeDefinition in project collect by openforis.
the class DataService method findAssignableCodeListItems.
/**
* Finds a list of code list items assignable to the specified attribute and matching the passed codes
*
* @param parentEntityId
* @param attributeName
* @param codes
* @return
*/
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attributeName, String[] codes) {
CollectRecord record = getActiveRecord();
Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attributeName);
List<CodeListItem> items = codeListManager.findValidItems(parent, def, codes);
List<CodeListItemProxy> result = new ArrayList<CodeListItemProxy>();
for (CodeListItem item : items) {
result.add(new CodeListItemProxy(item));
}
return result;
}
use of org.openforis.idm.metamodel.CodeAttributeDefinition in project collect by openforis.
the class CodeListController method loadAvailableItems.
@RequestMapping(value = "survey/{surveyId}/codelist/{codeListId}", method = GET)
@ResponseBody
public List<CodeListItemView> loadAvailableItems(@PathVariable Integer surveyId, @PathVariable Integer codeListId, @RequestParam Integer recordId, @RequestParam Integer parentEntityId, @RequestParam Integer codeAttrDefId) {
CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
CollectRecord record = recordManager.load(survey, recordId, false);
Entity parentEntity = (Entity) record.getNodeByInternalId(parentEntityId);
CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) survey.getSchema().getDefinitionById(codeAttrDefId);
List<CodeListItem> items = codeListManager.loadValidItems(parentEntity, codeAttrDef);
return toViews(items);
}
Aggregations