Search in sources :

Example 1 with ActivityField

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

the class SiteHistoryReader method parseChanges.

private Map<ResourceId, FieldValue> parseChanges(JsonValue jsonObject) {
    Map<ResourceId, ResourceId> attributeToFieldMap = new HashMap<>();
    for (ActivityField activityField : activity.getAttributeAndIndicatorFields()) {
        if (activityField.getFormField().getType() instanceof EnumType) {
            EnumType type = (EnumType) activityField.getFormField().getType();
            for (EnumItem enumItem : type.getValues()) {
                attributeToFieldMap.put(enumItem.getId(), activityField.getResourceId());
            }
        }
    }
    Map<ResourceId, FieldValue> valueMap = new HashMap<>();
    Multimap<ResourceId, ResourceId> attributeValueMap = HashMultimap.create();
    for (Map.Entry<String, JsonValue> jsonEntry : jsonObject.entrySet()) {
        String fieldName = jsonEntry.getKey();
        if (fieldName.equals("comments")) {
            valueMap.put(fieldId(CuidAdapter.COMMENT_FIELD), NarrativeValue.valueOf(parseString(jsonEntry.getValue())));
        } else if (fieldName.equals("date1")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("date2")) {
            valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
        } else if (fieldName.equals("partnerId")) {
            valueMap.put(fieldId(CuidAdapter.PARTNER_FIELD), parseRef(jsonEntry.getValue(), activity.getPartnerFormClassId(), CuidAdapter.PARTNER_DOMAIN));
        } else if (fieldName.equals("projectId")) {
            valueMap.put(fieldId(CuidAdapter.PROJECT_FIELD), parseRef(jsonEntry.getValue(), activity.getProjectFormClassId(), CuidAdapter.PROJECT_DOMAIN));
        } else if (fieldName.equals("locationId")) {
            valueMap.put(fieldId(CuidAdapter.LOCATION_FIELD), parseRef(jsonEntry.getValue(), activity.getLocationFormClassId(), CuidAdapter.LOCATION_DOMAIN));
        } else if (fieldName.startsWith("I")) {
            int mIndex = fieldName.indexOf("M");
            if (mIndex == -1) {
                int indicatorId = Integer.parseInt(fieldName.substring(1));
                ResourceId fieldId = CuidAdapter.indicatorField(indicatorId);
                valueMap.put(fieldId, parseQuantity(jsonEntry.getValue()));
            } else {
                // old history
                valueMap.put(ResourceId.valueOf(fieldName), parseQuantity(jsonEntry.getValue()));
            }
        } else if (fieldName.startsWith("ATTRIB")) {
            if (parseBoolean(jsonEntry.getValue())) {
                int attributeId = Integer.parseInt(fieldName.substring("ATTRIB".length()));
                ResourceId attributeCuid = CuidAdapter.attributeId(attributeId);
                ResourceId fieldId = attributeToFieldMap.get(attributeCuid);
                if (fieldId != null) {
                    attributeValueMap.put(fieldId, attributeCuid);
                }
            }
        }
    }
    for (ResourceId fieldId : attributeValueMap.keySet()) {
        valueMap.put(fieldId, new EnumValue(attributeValueMap.get(fieldId)));
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) FieldValue(org.activityinfo.model.type.FieldValue) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ActivityField

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

the class AttributeValueTableUpdater method update.

public void update(ResourceId fieldId, FieldValue value) {
    Preconditions.checkArgument(fieldId.getDomain() == CuidAdapter.ATTRIBUTE_GROUP_FIELD_DOMAIN);
    int attributeGroupId = CuidAdapter.getLegacyIdFromCuid(fieldId);
    ActivityField field = activity.getAttributeGroupField(attributeGroupId);
    EnumType enumType = (EnumType) field.getFormField().getType();
    for (EnumItem enumItem : enumType.getValues()) {
        attributesToClear.add(CuidAdapter.getLegacyIdFromCuid(enumItem.getId()));
    }
    add(value);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 3 with ActivityField

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

the class ActivityTableMappingBuilder method reportingPeriod.

public static ActivityTableMappingBuilder reportingPeriod(Activity activity) {
    ActivityTableMappingBuilder mapping = new ActivityTableMappingBuilder();
    mapping.activity = activity;
    mapping.baseTable = "reportingperiod";
    mapping.baseFromClause = "reportingperiod base";
    mapping.baseFilter = "base.deleted=0 AND base.activityId=" + activity.getId();
    mapping.classId = CuidAdapter.reportingPeriodFormClass(activity.getId());
    mapping.formClass = new FormClass(mapping.classId);
    mapping.formClass.setLabel(activity.getName() + " Monthly Reports");
    mapping.formClass.setDatabaseId(activity.getDatabaseId());
    mapping.primaryKeyMapping = new PrimaryKeyMapping(CuidAdapter.MONTHLY_REPORT, "reportingPeriodId");
    mapping.addSiteField();
    mapping.addDateFields();
    for (ActivityField indicatorField : activity.getIndicatorFields()) {
        mapping.addIndicatorOrAttributeField(indicatorField);
    }
    sortFormClassFields(mapping.formClass, activity.getFieldsOrder());
    return mapping;
}
Also used : FormClass(org.activityinfo.model.form.FormClass) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField)

Example 4 with ActivityField

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

the class ActivityTableMappingBuilder method site.

public static ActivityTableMappingBuilder site(Activity activity) {
    if (activity.getSerializedFormClass() != null) {
        return newForm(activity);
    }
    ActivityTableMappingBuilder mapping = new ActivityTableMappingBuilder();
    mapping.activity = activity;
    mapping.baseTable = "site";
    mapping.baseFromClause = "site base";
    mapping.baseFilter = "base.deleted=0     AND base.activityId=" + activity.getId();
    mapping.classId = CuidAdapter.activityFormClass(activity.getId());
    mapping.formClass = new FormClass(mapping.classId);
    mapping.formClass.setSchemaVersion(activity.getActivityVersion().getSchemaVersion());
    mapping.formClass.setLabel(activity.getName());
    mapping.formClass.setDatabaseId(activity.getDatabaseId());
    mapping.primaryKeyMapping = new PrimaryKeyMapping(CuidAdapter.SITE_DOMAIN, "siteId");
    if (activity.getReportingFrequency() == Activity.REPORT_ONCE) {
        mapping.addDateFields();
    }
    mapping.addPartnerField();
    mapping.addProjectField();
    if (activity.hasLocationType()) {
        mapping.addLocationField();
    }
    for (ActivityField field : activity.getAttributeAndIndicatorFields()) {
        mapping.addIndicatorOrAttributeField(field);
    }
    mapping.addComments();
    sortFormClassFields(mapping.formClass, activity.getFieldsOrder());
    return mapping;
}
Also used : FormClass(org.activityinfo.model.form.FormClass) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField)

Example 5 with ActivityField

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

the class PivotAdapter method executeTargetValuesQuery.

private void executeTargetValuesQuery(Integer databaseId) {
    if (filter.isRestricted(DimensionType.AttributeGroup) || filter.isRestricted(DimensionType.AdminLevel) || filter.isRestricted(DimensionType.Site) || filter.isRestricted(DimensionType.Location)) {
        // No results, exit now
        return;
    }
    ResourceId targetFormClassId = CuidAdapter.cuid(CuidAdapter.TARGET_FORM_CLASS_DOMAIN, databaseId);
    QueryModel queryModel = new QueryModel(targetFormClassId);
    QueryFilter queryFilter = new QueryFilter(filter, attributeFilters, LOGGER);
    queryModel.setFilter(queryFilter.composeTargetFilter());
    final Collection<Activity> activities = databases.get(databaseId);
    // Add all indicators we're querying for
    for (Activity activity : activities) {
        for (ActivityField indicator : selectedIndicators(activity)) {
            queryModel.selectField(targetFieldExpr(indicator)).as(alias(indicator));
        }
    }
    for (DimBinding dimension : groupBy) {
        for (ColumnModel columnModel : dimension.getTargetColumnQuery(targetFormClassId)) {
            queryModel.addColumn(columnModel);
        }
    }
    enqueueQuery(queryModel, new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            for (Activity activity : activities) {
                for (ActivityField indicator : selectedIndicators(activity)) {
                    ColumnView measureView = columnSet.getColumnView(alias(indicator));
                    DimensionCategory indicatorCategory = null;
                    if (indicatorDimension.isPresent()) {
                        indicatorCategory = indicatorDimension.get().category(indicator);
                    }
                    for (int i = 0; i < columnSet.getNumRows(); i++) {
                        double value = measureView.getDouble(i);
                        if (!Double.isNaN(value) && !Double.isInfinite(value)) {
                            Map<Dimension, DimensionCategory> key = new HashMap<>();
                            for (DimBinding dim : groupBy) {
                                DimensionCategory category = dim.extractTargetCategory(activity, columnSet, i);
                                if (category != null) {
                                    key.put(dim.getModel(), category);
                                }
                            }
                            if (indicatorCategory != null) {
                                key.put(indicatorDimension.get().getModel(), indicatorCategory);
                            }
                            Accumulator bucket = bucketForKey(key, indicator.getAggregation());
                            bucket.addValue(value);
                        }
                    }
                }
            }
            return null;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) ColumnView(org.activityinfo.model.query.ColumnView) Activity(org.activityinfo.store.mysql.metadata.Activity) LinkedActivity(org.activityinfo.store.mysql.metadata.LinkedActivity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) QueryFilter(org.activityinfo.server.command.QueryFilter) ResourceId(org.activityinfo.model.resource.ResourceId) ColumnModel(org.activityinfo.model.query.ColumnModel) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) Nullable(javax.annotation.Nullable)

Aggregations

ActivityField (org.activityinfo.store.mysql.metadata.ActivityField)8 ResourceId (org.activityinfo.model.resource.ResourceId)3 Nullable (javax.annotation.Nullable)2 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)2 FormClass (org.activityinfo.model.form.FormClass)2 ColumnModel (org.activityinfo.model.query.ColumnModel)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 EnumType (org.activityinfo.model.type.enumerated.EnumType)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonValue (org.activityinfo.json.JsonValue)1 FormField (org.activityinfo.model.form.FormField)1 FormTree (org.activityinfo.model.formTree.FormTree)1 FieldType (org.activityinfo.model.type.FieldType)1 FieldValue (org.activityinfo.model.type.FieldValue)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1