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