Search in sources :

Example 1 with AdminLevel

use of org.activityinfo.store.mysql.metadata.AdminLevel in project activityinfo by bedatadriven.

the class AdminEntityTable method getMapping.

@Override
public TableMapping getMapping(QueryExecutor executor, ResourceId formId) throws SQLException {
    int levelId = CuidAdapter.getLegacyIdFromCuid(formId);
    AdminLevel level = LEVEL_CACHE.getIfPresent(levelId);
    if (level == null) {
        level = AdminLevel.fetch(executor, levelId);
        LEVEL_CACHE.put(levelId, level);
    }
    FormField label = new FormField(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD));
    label.setLabel(I18N.CONSTANTS.name());
    label.setCode("name");
    label.setKey(true);
    label.setRequired(true);
    label.addSuperProperty(ResourceId.valueOf("label"));
    label.setType(TextType.SIMPLE);
    FormField code = new FormField(CuidAdapter.field(formId, CuidAdapter.CODE_FIELD));
    code.setCode("code");
    code.setLabel(I18N.CONSTANTS.codeFieldLabel());
    code.setRequired(false);
    code.setType(TextType.SIMPLE);
    FormField bounds = new FormField(CuidAdapter.field(formId, CuidAdapter.GEOMETRY_FIELD));
    bounds.setCode("boundary");
    bounds.setLabel(I18N.CONSTANTS.geography());
    bounds.setType(GeoAreaType.INSTANCE);
    FormField parent = null;
    if (level.hasParent()) {
        parent = new FormField(CuidAdapter.field(formId, CuidAdapter.ADMIN_PARENT_FIELD));
        parent.setCode("parent");
        parent.setLabel(level.getParentName());
        parent.setRequired(true);
        parent.setKey(true);
        parent.setType(ReferenceType.single(CuidAdapter.adminLevelFormClass(level.getParentId())));
        parent.addSuperProperty(ApplicationProperties.PARENT_PROPERTY);
    }
    // TODO: geometry
    TableMappingBuilder mapping = TableMappingBuilder.newMapping(formId, ADMIN_ENTITY_TABLE);
    mapping.setDatabaseId(GeodbFolder.GEODB_ID);
    mapping.setPrimaryKeyMapping(CuidAdapter.ADMIN_ENTITY_DOMAIN, "adminEntityId");
    mapping.setBaseFilter("base.AdminLevelId=" + levelId + " AND base.deleted=0");
    mapping.defaultValueOnInsert("AdminLevelId", levelId);
    mapping.setFormLabel(level.getName());
    mapping.addTextField(label, "name");
    mapping.addTextField(code, "code");
    mapping.addGeoAreaField(bounds);
    mapping.setDeleteMethod(DeleteMethod.SOFT_BY_BOOLEAN);
    mapping.setVersion(level.getVersion());
    if (parent != null) {
        mapping.add(new FieldMapping(parent, "adminEntityParentId", new ReferenceConverter(CuidAdapter.adminLevelFormClass(level.getParentId()), ADMIN_ENTITY_DOMAIN)));
    }
    return mapping.build();
}
Also used : AdminLevel(org.activityinfo.store.mysql.metadata.AdminLevel) FormField(org.activityinfo.model.form.FormField)

Aggregations

FormField (org.activityinfo.model.form.FormField)1 AdminLevel (org.activityinfo.store.mysql.metadata.AdminLevel)1