Search in sources :

Example 36 with CodeList

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

the class CSVValueFormatter method format.

public String format(AttributeDefinition defn, Value value) {
    if (value == null) {
        return "";
    } else if (value instanceof BooleanValue) {
        return ((BooleanValue) value).getValue().toString();
    } else if (value instanceof Code) {
        CodeListService codeListService = defn.getSurvey().getContext().getCodeListService();
        CodeList list = ((CodeAttributeDefinition) defn).getList();
        if (codeListService.hasQualifiableItems(list)) {
            return String.format("%s: %s", ((Code) value).getCode(), ((Code) value).getQualifier());
        } else {
            return ((Code) value).getCode();
        }
    } else if (value instanceof Coordinate) {
        return value.toString();
    } else if (value instanceof Date) {
        Date date = (Date) value;
        return String.format("%d/%d/%d", ((Date) value).getDay(), ((Date) value).getMonth(), date.getYear());
    } else if (value instanceof File) {
        return ((File) value).getFilename();
    } else if (value instanceof NumberValue) {
        Number val = ((NumberValue<?>) value).getValue();
        NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
        String formattedVal = numberFormat.format(val);
        return formattedVal;
    } else if (value instanceof NumericRange) {
        Number from = ((NumericRange<?>) value).getFrom();
        Number to = ((NumericRange<?>) value).getFrom();
        String format;
        if (value instanceof IntegerRange) {
            format = "%d-%d";
        } else {
            format = "%f-%f";
        }
        String formattedValue = String.format(Locale.ENGLISH, format, from, to);
        return formattedValue;
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof Time) {
        Time time = (Time) value;
        return String.format("%d:%d", time.getHour(), time.getMinute());
    } else
        throw new IllegalArgumentException("Unsupported attribute value type: " + value.getClass().getName());
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) CodeListService(org.openforis.idm.metamodel.CodeListService) Time(org.openforis.idm.model.Time) Code(org.openforis.idm.model.Code) Date(org.openforis.idm.model.Date) NumericRange(org.openforis.idm.model.NumericRange) CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Coordinate(org.openforis.idm.model.Coordinate) NumberValue(org.openforis.idm.model.NumberValue) TextValue(org.openforis.idm.model.TextValue) BooleanValue(org.openforis.idm.model.BooleanValue) File(org.openforis.idm.model.File) NumberFormat(java.text.NumberFormat)

Example 37 with CodeList

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

the class CodeColumnProvider method getFieldNames.

@Override
protected String[] getFieldNames() {
    List<String> result = new ArrayList<String>();
    // code field
    result.add(CodeAttributeDefinition.CODE_FIELD);
    // qualifier field
    CodeList list = attributeDefinition.getList();
    if (hasQualifiableItems(list)) {
        result.add(CodeAttributeDefinition.QUALIFIER_FIELD);
    }
    // item position field
    if (getConfig().isIncludeCodeItemPositionColumn() && !list.isExternal()) {
        result.add(ITEM_POSITION_FIELD_NAME);
    }
    // label field
    if (getConfig().isIncludeCodeItemLabelColumn() && !list.isExternal()) {
        result.add(ITEM_LABEL_FIELD_NAME);
    }
    return result.toArray(new String[result.size()]);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ArrayList(java.util.ArrayList)

Example 38 with CodeList

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

the class EnumerableEntityColumnProvider method createProviders.

private static List<ColumnProvider> createProviders(CSVDataExportParameters config, EntityDefinition defn) {
    List<ColumnProvider> providers = new ArrayList<ColumnProvider>();
    List<AttributeDefinition> keyDefs = defn.getKeyAttributeDefinitions();
    CodeAttributeDefinition keyDef = (CodeAttributeDefinition) keyDefs.get(0);
    CodeList codeList = keyDef.getList();
    SurveyContext context = defn.getSurvey().getContext();
    CodeListService codeListService = context.getCodeListService();
    List<CodeListItem> items = codeListService.loadRootItems(codeList);
    for (CodeListItem item : items) {
        String code = item.getCode();
        String keyName = keyDef.getName();
        EnumeratedCodeItemColumnProvider p = new EnumeratedCodeItemColumnProvider(config, defn, keyName, code);
        providers.add(p);
    }
    return providers;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) SurveyContext(org.openforis.idm.metamodel.SurveyContext) CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 39 with CodeList

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

the class CodeListItemDao method update.

@Override
public void update(PersistedCodeListItem item) {
    CodeList codeList = item.getCodeList();
    JooqDSLContext jf = dsl(codeList);
    jf.updateQuery(item).execute();
    if (isCacheInUse(codeList)) {
        cache.clearItemsByCodeList(codeList);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList)

Example 40 with CodeList

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

the class CodeListItemDao method deleteInvalidCodeListReferenceItems.

public void deleteInvalidCodeListReferenceItems(CollectSurvey survey) {
    // create delete where condition
    Condition whereCondition = OFC_CODE_LIST.SURVEY_ID.equal(survey.getId());
    List<Integer> codeListsIds = new ArrayList<Integer>();
    if (!survey.getCodeLists().isEmpty()) {
        // include items that belongs to detached code lists
        for (CodeList codeList : survey.getCodeLists()) {
            codeListsIds.add(codeList.getId());
        }
        whereCondition = whereCondition.and(OFC_CODE_LIST.CODE_LIST_ID.notIn(codeListsIds));
    }
    // execute delete
    JooqDSLContext jf = dsl(null);
    jf.delete(OFC_CODE_LIST).where(whereCondition).execute();
    if (isCacheInUse(survey)) {
        cache.clearItemsBySurvey(survey.getId());
    }
}
Also used : Condition(org.jooq.Condition) CodeList(org.openforis.idm.metamodel.CodeList) ArrayList(java.util.ArrayList)

Aggregations

CodeList (org.openforis.idm.metamodel.CodeList)88 CodeListItem (org.openforis.idm.metamodel.CodeListItem)24 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)20 ArrayList (java.util.ArrayList)16 CollectSurvey (org.openforis.collect.model.CollectSurvey)15 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)14 Test (org.junit.Test)10 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)10 HashMap (java.util.HashMap)7 NameValueEntry (org.openforis.collect.model.NameValueEntry)7 CodeListService (org.openforis.idm.metamodel.CodeListService)7 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)6 List (java.util.List)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 ModelVersion (org.openforis.idm.metamodel.ModelVersion)5 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)5 Code (org.openforis.idm.model.Code)5 Record (org.openforis.idm.model.Record)5 CodeAttributeDefinitionFormObject (org.openforis.collect.designer.form.CodeAttributeDefinitionFormObject)4 CodeListImportProcess (org.openforis.collect.manager.codelistimport.CodeListImportProcess)4