Search in sources :

Example 11 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class MySqlCatalogTest method singleSite.

@Test
public void singleSite() {
    FormStorage siteStorage = catalog.getForm(CuidAdapter.activityFormClass(1)).get();
    FormRecord siteRecord = siteStorage.get(CuidAdapter.cuid(CuidAdapter.SITE_DOMAIN, 1)).get();
    FormInstance site = FormInstance.toFormInstance(siteStorage.getFormClass(), siteRecord);
    EnumValue cause = (EnumValue) site.get(CuidAdapter.attributeGroupField(1));
    EnumValue kitContents = (EnumValue) site.get(CuidAdapter.attributeGroupField(2));
    assertThat(cause, nullValue());
    assertThat(kitContents.getResourceIds(), contains(CuidAdapter.attributeId(3), CuidAdapter.attributeField(4)));
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) Test(org.junit.Test)

Example 12 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue 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 13 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class AttributeBuffer method next.

@Override
public void next() {
    EnumValue value;
    int cardinality = bitSet.cardinality();
    if (cardinality == 1) {
        // Single item is set, use our cached FieldValues
        int index = bitSet.nextSetBit(0);
        value = singletonValues.get(index);
    } else if (cardinality == 0) {
        // No items set, null value
        value = null;
    } else {
        // Multiple items set, construct a new EnumValue
        Set<ResourceId> set = new HashSet<>();
        for (int i = 0; i < itemIds.size(); i++) {
            if (bitSet.get(i)) {
                set.add(itemIds.get(i));
            }
        }
        value = new EnumValue(set);
    }
    for (CursorObserver<FieldValue> observer : observers) {
        observer.onNext(value);
    }
    bitSet.clear();
}
Also used : ResultSet(java.sql.ResultSet) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue)

Example 14 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class AttributeValueTableUpdater method add.

public void add(FieldValue value) {
    EnumValue enumValue = (EnumValue) value;
    if (enumValue != null) {
        for (ResourceId resourceId : enumValue.getResourceIds()) {
            Preconditions.checkArgument(resourceId.getDomain() == CuidAdapter.ATTRIBUTE_DOMAIN);
            int attributeId = CuidAdapter.getLegacyIdFromCuid(resourceId);
            attributesToSet.add(attributeId);
            attributesToClear.remove(attributeId);
        }
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) EnumValue(org.activityinfo.model.type.enumerated.EnumValue)

Example 15 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class AttributeConverter method toParameters.

@Override
public Collection<?> toParameters(FieldValue value) {
    EnumValue enumValue = (EnumValue) value;
    ResourceId enumItemId = enumValue.getValueId();
    Preconditions.checkArgument(enumItemId.getDomain() == CuidAdapter.ATTRIBUTE_DOMAIN);
    return Collections.singleton(CuidAdapter.getLegacyIdFromCuid(enumItemId));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) EnumValue(org.activityinfo.model.type.enumerated.EnumValue)

Aggregations

EnumValue (org.activityinfo.model.type.enumerated.EnumValue)28 Test (org.junit.Test)13 ResourceId (org.activityinfo.model.resource.ResourceId)11 FormInstance (org.activityinfo.model.form.FormInstance)6 RecordRef (org.activityinfo.model.type.RecordRef)6 LocalDate (org.activityinfo.model.type.time.LocalDate)6 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)5 FieldValue (org.activityinfo.model.type.FieldValue)4 EnumType (org.activityinfo.model.type.enumerated.EnumType)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)4 FormClass (org.activityinfo.model.form.FormClass)3 Quantity (org.activityinfo.model.type.number.Quantity)3 CheckBox (com.sencha.gxt.widget.core.client.form.CheckBox)2 FormRecord (org.activityinfo.model.form.FormRecord)2 ReferenceValue (org.activityinfo.model.type.ReferenceValue)2 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1