Search in sources :

Example 6 with NodeDefinitionVisitor

use of org.openforis.idm.metamodel.NodeDefinitionVisitor 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 7 with NodeDefinitionVisitor

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

the class NodeBuilder method detachedEntity.

public static Entity detachedEntity(Survey survey, final String name, NodeBuilder... builders) {
    NodeBuilder builder = entity(name, builders);
    final List<EntityDefinition> defs = new ArrayList<EntityDefinition>();
    survey.getSchema().traverse(new NodeDefinitionVisitor() {

        @Override
        public void visit(NodeDefinition definition) {
            if (definition.getName().equals(name)) {
                defs.add((EntityDefinition) definition);
            }
        }
    });
    return builder.createDetachedEntity(defs.get(0));
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor)

Example 8 with NodeDefinitionVisitor

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

the class SchemaUpdater method generateAlias.

public EntityDefinition generateAlias(EntityDefinition sourceDef, String sourceFilterAttributeName, EntityDefinition targetParentDef, String targetFilterAttributeName) {
    EntityDefinition aliasDef = survey.getSchema().cloneDefinition(sourceDef, targetFilterAttributeName);
    // add "Alias" suffix to labels
    for (NodeLabel nodeLabel : aliasDef.getLabels()) {
        aliasDef.setLabel(nodeLabel.getType(), nodeLabel.getLanguage(), nodeLabel.getText() + " Alias");
    }
    aliasDef.traverse(new NodeDefinitionVisitor() {

        public void visit(NodeDefinition def) {
            if (def instanceof AttributeDefinition) {
                AttributeDefinition attrDef = (AttributeDefinition) def;
                attrDef.setCalculated(false);
                attrDef.removeAllChecks();
                attrDef.removeAllAttributeDefaults();
            }
            def.setRelevantExpression(null);
            def.setRequiredExpression(null);
        }
    });
    targetParentDef.addChildDefinition(aliasDef);
    aliasDef.setVirtual(true);
    aliasDef.setGeneratorExpression(generateAliasGeneratorExpression(sourceDef, sourceFilterAttributeName, targetParentDef, targetFilterAttributeName));
    UIOptions uiOptions = survey.getUIOptions();
    // prevent layout errors
    uiOptions.setLayout(aliasDef, Layout.FORM);
    uiOptions.setHidden(aliasDef, true);
    return aliasDef;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) NodeLabel(org.openforis.idm.metamodel.NodeLabel) UIOptions(org.openforis.collect.metamodel.ui.UIOptions) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor)

Example 9 with NodeDefinitionVisitor

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

the class SurveyMigrator method migrate.

public void migrate(final CollectSurvey survey) {
    fixCodeListHierarchyLevelNames(survey);
    if (survey.getCollectVersion().compareTo(ENUMERATE_VERSION) < 0) {
        survey.getSchema().traverse(new NodeDefinitionVisitor() {

            public void visit(NodeDefinition defn) {
                if (defn instanceof EntityDefinition && defn.isMultiple()) {
                    EntityDefinition entityDefn = (EntityDefinition) defn;
                    UIConfiguration uiConfig = survey.getUIConfiguration();
                    if (uiConfig != null) {
                        UIModelObject uiModelObject = uiConfig.getModelObjectByNodeDefinitionId(entityDefn.getId());
                        CollectAnnotations annotations = survey.getAnnotations();
                        if (survey.getTarget() == SurveyTarget.COLLECT_EARTH || uiModelObject == null || uiModelObject instanceof UITable) {
                            if (defn.getMinCountExpression() != null) {
                                annotations.setAutoGenerateMinItems(entityDefn, true);
                            }
                            if (entityDefn.isEnumerable()) {
                                entityDefn.setEnumerate(true);
                            }
                        }
                    }
                }
            }
        });
    }
    survey.setCollectVersion(VERSION);
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) UIModelObject(org.openforis.collect.metamodel.ui.UIModelObject) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) UIConfiguration(org.openforis.collect.metamodel.ui.UIConfiguration) UITable(org.openforis.collect.metamodel.ui.UITable) CollectAnnotations(org.openforis.collect.metamodel.CollectAnnotations) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor)

Example 10 with NodeDefinitionVisitor

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

the class UIOptionsMigrator method verifyMigration.

private void verifyMigration(final UIConfiguration uiConfig) throws UIOptionsMigrationException {
    // 
    // StringWriter writer = new StringWriter();
    // UIConfigurationSerializer serializer = new UIConfigurationSerializer();
    // serializer.write(uiConfig, writer);
    // System.out.println(writer.toString());
    // 
    final CollectSurvey survey = uiConfig.getSurvey();
    Schema schema = survey.getSchema();
    schema.traverse(new NodeDefinitionVisitor() {

        @Override
        public void visit(NodeDefinition definition) {
            int nodeId = definition.getId();
            UIModelObject uiModelObj = uiConfig.getModelObjectByNodeDefinitionId(nodeId);
            if (uiModelObj == null) {
                throw new UIOptionsMigrationException(String.format("No UI model object found for node with id %d in %s survey with uri %s", nodeId, survey.isTemporary() ? "temporary" : "published", survey.getUri()));
            }
        }
    });
}
Also used : Schema(org.openforis.idm.metamodel.Schema) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor)

Aggregations

NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)14 NodeDefinitionVisitor (org.openforis.idm.metamodel.NodeDefinitionVisitor)14 ArrayList (java.util.ArrayList)8 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)8 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)7 Schema (org.openforis.idm.metamodel.Schema)5 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)3 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)3 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 UIConfiguration (org.openforis.collect.metamodel.ui.UIConfiguration)1 UIModelObject (org.openforis.collect.metamodel.ui.UIModelObject)1 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)1 UITable (org.openforis.collect.metamodel.ui.UITable)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 RelationalSchema (org.openforis.collect.relational.model.RelationalSchema)1 CsvWriter (org.openforis.commons.io.csv.CsvWriter)1 Task (org.openforis.concurrency.Task)1