Search in sources :

Example 1 with FieldTypeClass

use of org.activityinfo.model.type.FieldTypeClass in project activityinfo by bedatadriven.

the class DatabaseSetupSteps method createFieldForColumn.

/**
 * Creates form field from table column. Returns column type (field type).
 * @param form form name
 * @param dataTable table
 * @param columnIndex column index
 * @return column type (field type)
 * @throws Exception
 */
private Optional<FieldTypeClass> createFieldForColumn(String form, DataTable dataTable, int columnIndex) throws Exception {
    String label = dataTable.getGherkinRows().get(0).getCells().get(columnIndex);
    if (isPredefinedField(label)) {
        return Optional.absent();
    }
    String type = driver.resolveFieldTypeName(dataTable.getGherkinRows().get(1).getCells().get(columnIndex));
    FieldTypeClass typeClass = TypeRegistry.get().getTypeClass(type);
    // Find set of distinct values
    Set<String> values = new HashSet<>();
    for (int row = 2; row < dataTable.getGherkinRows().size(); ++row) {
        String cellValue = dataTable.getGherkinRows().get(row).getCells().get(columnIndex);
        if (typeClass == EnumType.TYPE_CLASS) {
            values.addAll(asList(cellValue.split("\\s*,\\s*")));
        } else {
            values.add(cellValue);
        }
    }
    if (typeClass == EnumType.TYPE_CLASS) {
        I_have_created_a_enumerated_field_with_options(label, Lists.newArrayList(values));
    } else {
        driver.setup().createField(property("form", form), property("name", label), property("type", typeClass.getId()));
    }
    return Optional.of(typeClass);
}
Also used : FieldTypeClass(org.activityinfo.model.type.FieldTypeClass)

Example 2 with FieldTypeClass

use of org.activityinfo.model.type.FieldTypeClass in project activityinfo by bedatadriven.

the class ColumnActionSelector method updateTypeStyles.

public void updateTypeStyles(FieldTypeClass sourceType) {
    for (Map.Entry<ColumnAction, RadioButton> entry : buttons.entrySet()) {
        final ColumnAction columnAction = entry.getKey();
        if (columnAction instanceof MapExistingAction) {
            final ImportTarget target = ((MapExistingAction) columnAction).getTarget();
            final FieldTypeClass targetType = target.getFormField().getType().getTypeClass();
            final RadioButton button = entry.getValue();
            button.removeStyleName(ColumnMappingStyles.INSTANCE.typeNotMatched());
            button.removeStyleName(ColumnMappingStyles.INSTANCE.typeMatched());
            if (targetType == sourceType || (sourceType == FieldTypeClass.FREE_TEXT && targetType == FieldTypeClass.REFERENCE)) {
                button.addStyleName(ColumnMappingStyles.INSTANCE.typeMatched());
            } else {
                button.addStyleName(ColumnMappingStyles.INSTANCE.typeNotMatched());
            }
        }
    }
}
Also used : ColumnAction(org.activityinfo.ui.client.component.importDialog.model.ColumnAction) ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) MapExistingAction(org.activityinfo.ui.client.component.importDialog.model.MapExistingAction) RadioButton(org.activityinfo.ui.client.widget.RadioButton) Map(java.util.Map)

Example 3 with FieldTypeClass

use of org.activityinfo.model.type.FieldTypeClass in project activityinfo by bedatadriven.

the class ColumnTypeGuesser method calculateTypeScores.

private void calculateTypeScores() {
    for (String value : columnValues) {
        final Map<FieldTypeClass, Integer> copyMap = Maps.newHashMap(typeMap);
        // we don't need to iterate over string types because input is always string
        copyMap.remove(FieldTypeClass.FREE_TEXT);
        copyMap.remove(FieldTypeClass.NARRATIVE);
        boolean hasMatch = false;
        for (Map.Entry<FieldTypeClass, Integer> entry : copyMap.entrySet()) {
            try {
                if (isBoolean(value) && entry.getKey().equals(BooleanType.TYPE_CLASS)) {
                    increaseValue(entry.getKey());
                    hasMatch = true;
                    break;
                }
                final FieldValueParser stringConverter = converterFactory.createStringConverter(entry.getKey().createType());
                final Object convertedValue = stringConverter.convert(value);
                // analyze converted value
                if (convertedValue != null) {
                    increaseValue(entry.getKey());
                    hasMatch = true;
                    break;
                }
            } catch (Exception e) {
            // ignore
            }
        }
        // if no match then we fallback to string type
        if (!hasMatch) {
            final int length = value.length();
            if (length < FormFieldType.FREE_TEXT_LENGTH) {
                increaseValue(FieldTypeClass.FREE_TEXT);
            } else {
                increaseValue(FieldTypeClass.NARRATIVE);
            }
        }
    }
}
Also used : FieldValueParser(org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) Map(java.util.Map)

Example 4 with FieldTypeClass

use of org.activityinfo.model.type.FieldTypeClass in project activityinfo by bedatadriven.

the class TargetIndicatorView method setValidatorForCellBeforeEdit.

private void setValidatorForCellBeforeEdit(TargetValueDTO targetValueDTO, int column) {
    TextField field = new TextField<String>();
    field.setAllowBlank(true);
    IndicatorDTO indicatorById = presenter.getIndicatorById(targetValueDTO.getIndicatorId());
    FieldTypeClass type = indicatorById.getType();
    if (type == FieldTypeClass.QUANTITY) {
        field = new NumberField();
        ((NumberField) field).setFormat(IndicatorNumberFormat.INSTANCE);
        field.setAllowBlank(true);
    }
    tree.getColumnModel().getColumn(column).setEditor(new CellEditor(field));
}
Also used : TextField(com.extjs.gxt.ui.client.widget.form.TextField) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) NumberField(com.extjs.gxt.ui.client.widget.form.NumberField)

Example 5 with FieldTypeClass

use of org.activityinfo.model.type.FieldTypeClass in project activityinfo by bedatadriven.

the class OldGetSitesHandler method joinIndicatorValues.

private Promise<Void> joinIndicatorValues(final GetSites command, SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap, final Map<Integer, SiteDTO> periodMap) {
    final Promise<Void> complete = new Promise<>();
    Log.trace("Starting joinIndicatorValues()");
    SqlQuery query = SqlQuery.select().appendColumn("P.SiteId", "SiteId").appendColumn("V.IndicatorId", "SourceIndicatorId").appendColumn("I.ActivityId", "SourceActivityId").appendColumn("D.IndicatorId", "DestIndicatorId").appendColumn("D.ActivityId", "DestActivityId").appendColumn("I.Type").appendColumn("I.Expression").appendColumn("V.Value").appendColumn("V.TextValue").appendColumn("V.DateValue").appendColumn("P.ReportingPeriodId", "PeriodId").from(Tables.REPORTING_PERIOD, "P").innerJoin(Tables.INDICATOR_VALUE, "V").on("P.ReportingPeriodId = V.ReportingPeriodId").innerJoin(Tables.INDICATOR, "I").on("I.IndicatorId = V.IndicatorId").leftJoin(Tables.INDICATOR_LINK, "L").on("L.SourceIndicatorId=I.IndicatorId").leftJoin(Tables.INDICATOR, "D").on("L.DestinationIndicatorId=D.IndicatorId").whereTrue("I.dateDeleted IS NULL");
    if (weAreFetchingAllSitesForAnActivityAndThereAreNoLinkedSites(command, siteMap)) {
        query.where("I.ActivityId").in(command.getFilter().getRestrictions(DimensionType.Activity));
    } else {
        query.where("P.SiteId").in(siteMap.keySet());
    }
    query.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Received results for join indicators");
            for (final SqlResultSetRow row : results.getRows()) {
                FieldTypeClass indicatorType = FormFieldType.valueOf(row.getString("Type"));
                String expression = row.getString("Expression");
                boolean isCalculatedIndicator = !Strings.isNullOrEmpty(expression);
                Object indicatorValue = null;
                if (isCalculatedIndicator) {
                // ignore -> see joinCalculatedIndicatorValues
                } else {
                    // if indicator is no calculated then assign value directly
                    if (indicatorType == FieldTypeClass.QUANTITY) {
                        if (!row.isNull("Value")) {
                            indicatorValue = row.getDouble("Value");
                        }
                    } else if (indicatorType == FieldTypeClass.FREE_TEXT || indicatorType == FieldTypeClass.NARRATIVE || indicatorType == ReferenceType.TYPE_CLASS || indicatorType == AttachmentType.TYPE_CLASS) {
                        if (!row.isNull("TextValue")) {
                            indicatorValue = row.getString("TextValue");
                        }
                    } else if (indicatorType == FieldTypeClass.LOCAL_DATE) {
                        indicatorValue = row.getDate("DateValue");
                    } else if (indicatorType == FieldTypeClass.BOOLEAN) {
                        if (!row.isNull("BooleanValue")) {
                            indicatorValue = row.getBoolean("BooleanValue");
                        }
                    }
                }
                int sourceActivityId = row.getInt("SourceActivityId");
                if (command.isFetchAllReportingPeriods()) {
                    SiteDTO site = periodMap.get(row.getInt("PeriodId"));
                    if (site != null) {
                        site.setIndicatorValue(row.getInt("SourceIndicatorId"), indicatorValue);
                    }
                } else {
                    for (SiteDTO site : siteMap.get(row.getInt("SiteId"))) {
                        if (sourceActivityId == site.getActivityId()) {
                            int indicatorId = row.getInt("SourceIndicatorId");
                            site.setIndicatorValue(indicatorId, indicatorValue);
                        } else if (!row.isNull("DestActivityId")) {
                            int destActivityId = row.getInt("DestActivityId");
                            if (site.getActivityId() == destActivityId) {
                                int indicatorId = row.getInt("DestIndicatorId");
                                site.setIndicatorValue(indicatorId, indicatorValue);
                            }
                        }
                    }
                }
            }
            Log.trace("Done populating dtos for join indicators");
            // after normal indicators are evaluated try to calculate indicators with expression
            joinCalculatedIndicatorValues(complete, tx, siteMap);
        }
    });
    return complete;
}
Also used : SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) Promise(org.activityinfo.promise.Promise) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback)

Aggregations

FieldTypeClass (org.activityinfo.model.type.FieldTypeClass)8 Map (java.util.Map)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)1 NumberField (com.extjs.gxt.ui.client.widget.form.NumberField)1 TextField (com.extjs.gxt.ui.client.widget.form.TextField)1 Promise (org.activityinfo.promise.Promise)1 ColumnAction (org.activityinfo.ui.client.component.importDialog.model.ColumnAction)1 MapExistingAction (org.activityinfo.ui.client.component.importDialog.model.MapExistingAction)1 ImportTarget (org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget)1 FieldValueParser (org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser)1 RadioButton (org.activityinfo.ui.client.widget.RadioButton)1