Search in sources :

Example 31 with CodeList

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

the class SurveyManager method updateTemporaryModel.

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public CollectSurvey updateTemporaryModel(File surveyFile, boolean validate, UserGroup userGroup) throws SurveyValidationException, SurveyStoreException {
    CollectSurvey parsedSurvey;
    try {
        parsedSurvey = unmarshalSurvey(surveyFile, validate, false);
    } catch (IdmlParseException e) {
        throw new SurveyImportException(e);
    }
    String uri = parsedSurvey.getUri();
    SurveySummary oldTemporarySurvey = loadTemporarySummaryByUri(uri);
    if (oldTemporarySurvey == null) {
        throw new IllegalArgumentException("Survey to update not found: " + uri);
    } else {
        int oldSurveyId = oldTemporarySurvey.getId();
        parsedSurvey.setId(oldSurveyId);
        parsedSurvey.setName(oldTemporarySurvey.getName());
        parsedSurvey.setPublishedId(oldTemporarySurvey.getPublishedId());
        parsedSurvey.setTemporary(true);
        parsedSurvey.setUserGroup(userGroup);
        // clean code list items
        for (CodeList codeList : parsedSurvey.getCodeLists()) {
            codeList.removeAllItems();
        }
        codeListManager.deleteAllItemsBySurvey(oldSurveyId, true);
        save(parsedSurvey);
        // import code list items
        try {
            codeListManager.importCodeLists(parsedSurvey, surveyFile);
        } catch (CodeListImportException e) {
            throw new SurveyImportException(e);
        }
    }
    return parsedSurvey;
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeListImportException(org.openforis.collect.io.exception.CodeListImportException) SurveySummary(org.openforis.collect.model.SurveySummary) CollectSurvey(org.openforis.collect.model.CollectSurvey) SurveyImportException(org.openforis.collect.persistence.SurveyImportException) IdmlParseException(org.openforis.idm.metamodel.xml.IdmlParseException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 32 with CodeList

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

the class SurveyValidator method validateCodeLists.

private List<SurveyValidationResult> validateCodeLists(CollectSurvey survey, ValidationParameters validationParameters) {
    List<SurveyValidationResult> results = new ArrayList<SurveyValidationResult>();
    for (CodeList list : survey.getCodeLists()) {
        if (!survey.isPredefinedCodeList(list)) {
            if (validationParameters.warnOnUnusedCodeLists && !codeListManager.isInUse(list)) {
                // unused code list not allowed
                SurveyValidationResult validationResult = new SurveyValidationResult(Flag.WARNING, String.format(CODE_LIST_PATH_FORMAT, list.getName()), "survey.validation.error.unused_code_list");
                results.add(validationResult);
            } else if (validationParameters.warnOnEmptyCodeLists && !list.isExternal() && codeListManager.isEmpty(list)) {
                // empty code list not allowed
                SurveyValidationResult validationResult = new SurveyValidationResult(Flag.WARNING, String.format(CODE_LIST_PATH_FORMAT, list.getName()), "survey.validation.error.empty_code_list");
                results.add(validationResult);
            }
        }
    }
    return results;
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ArrayList(java.util.ArrayList)

Example 33 with CodeList

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

the class RecordUpdater method addEmptyEnumeratedEntities.

private void addEmptyEnumeratedEntities(Entity parentEntity, EntityDefinition enumerableEntityDefn) {
    Record record = parentEntity.getRecord();
    ModelVersion version = record.getVersion();
    CodeAttributeDefinition enumeratingCodeDefn = enumerableEntityDefn.getEnumeratingKeyCodeAttribute(version);
    if (enumeratingCodeDefn != null) {
        CodeList list = enumeratingCodeDefn.getList();
        Survey survey = record.getSurvey();
        CodeListService codeListService = survey.getContext().getCodeListService();
        List<CodeListItem> items = codeListService.loadRootItems(list);
        int i = 0;
        for (CodeListItem item : items) {
            if (version == null || version.isApplicable(item)) {
                String code = item.getCode();
                Entity enumeratedEntity = parentEntity.getEnumeratedEntity(enumerableEntityDefn, enumeratingCodeDefn, code);
                if (enumeratedEntity == null) {
                    Entity addedEntity = performEntityAdd(parentEntity, enumerableEntityDefn, i);
                    addEmptyNodes(addedEntity);
                    // set the value of the key CodeAttribute
                    CodeAttribute addedCode = (CodeAttribute) addedEntity.getChild(enumeratingCodeDefn, 0);
                    addedCode.setValue(new Code(code));
                    addedCode.updateSummaryInfo();
                } else if (enumeratedEntity.getIndex() != i) {
                    parentEntity.move(enumerableEntityDefn, enumeratedEntity.getIndex(), i);
                }
                i++;
            }
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) CodeListService(org.openforis.idm.metamodel.CodeListService) Code(org.openforis.idm.model.Code) CodeList(org.openforis.idm.metamodel.CodeList) Survey(org.openforis.idm.metamodel.Survey) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 34 with CodeList

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

the class UICodeField method getListId.

public Integer getListId() {
    CodeAttributeDefinition attrDef = (CodeAttributeDefinition) getAttributeDefinition();
    CodeList list = attrDef.getList();
    return list == null ? null : list.getId();
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 35 with CodeList

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

the class CodeAttributeProxy method isExternalCodeList.

protected boolean isExternalCodeList() {
    CodeAttributeDefinition defn = codeAttribute.getDefinition();
    CodeList list = defn.getList();
    return list.isExternal();
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

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