Java Code Examples for com.nextgis.maplib.datasource.Field#getType()

The following examples show how to use com.nextgis.maplib.datasource.Field#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextEdit.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void init(Field field,
                 Bundle savedState,
                 Cursor featureCursor){
    ControlHelper.setClearAction(this);

    mFieldName = field.getName();
    String text = "";

    if (ControlHelper.hasKey(savedState, mFieldName))
        text = savedState.getString(ControlHelper.getSavedStateKey(mFieldName));
    else if (null != featureCursor) {
        int column = featureCursor.getColumnIndex(mFieldName);
        if (column >= 0)
            text = featureCursor.getString(column);
    }

    setText(text);

    switch (field.getType()) {

        case GeoConstants.FTString:
            break;

        case GeoConstants.FTInteger:
            setSingleLine(true);
            setInputType(InputType.TYPE_CLASS_NUMBER);
            break;

        case GeoConstants.FTReal:
            setSingleLine(true);
            setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            break;
    }
}
 
Example 2
Source File: TextEdit.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void init(JSONObject element,
                 List<Field> fields,
                 Bundle savedState,
                 Cursor featureCursor,
                 SharedPreferences preferences,
                 Map<String, Map<String, String>> translations) throws JSONException{
    ControlHelper.setClearAction(this);

    JSONObject attributes = element.getJSONObject(JSON_ATTRIBUTES_KEY);
    mFieldName = attributes.getString(JSON_FIELD_NAME_KEY);
    mIsShowLast = ControlHelper.isSaveLastValue(attributes);
    boolean enabled = ControlHelper.isEnabled(fields, mFieldName);

    int column = -1;
    String value = null;
    if (ControlHelper.hasKey(savedState, mFieldName))
        value = savedState.getString(ControlHelper.getSavedStateKey(mFieldName));
    else if (null != featureCursor) { // feature exists
        column = featureCursor.getColumnIndex(mFieldName);
        if (column >= 0)
            value = featureCursor.getString(column);
    } else {    // new feature
        if (!attributes.isNull(JSON_TEXT_KEY))
            value = attributes.getString(JSON_TEXT_KEY);

        if (mIsShowLast)
            value = preferences.getString(mFieldName, value);
    }

    boolean useLogin = attributes.optBoolean(USE_LOGIN);
    String accountName = element.optString(SyncStateContract.Columns.ACCOUNT_NAME);
    if (useLogin && !TextUtils.isEmpty(accountName)) {
        enabled = false;
        Activity activity = ControlHelper.getActivity(getContext());
        if (activity != null) {
            GISApplication app = (GISApplication) activity.getApplication();
            Account account = app.getAccount(accountName);
            value = app.getAccountLogin(account);
        }
    }

    boolean ngidLogin = attributes.optBoolean(NGID_LOGIN);
    if (ngidLogin) {
        enabled = false;
        if (column == -1) {
            String fName = element.optString(PREF_FIRST_NAME);
            String lName = element.optString(PREF_LAST_NAME);
            String uName = element.optString(PREF_USERNAME);
            value = formUserName(fName, lName, uName);
        }
    }

    setEnabled(enabled);
    setText(value);

    //let's create control
    int maxLines = attributes.getInt(JSON_MAX_STRING_COUNT_KEY);
    if (maxLines < 2) {
        setSingleLine(true);
    } else {
        setMaxLines(maxLines);
    }

    int fieldType = NOT_FOUND;
    for (Field field : fields) {
        if (field.getName().equals(mFieldName)) {
            fieldType = field.getType();
            break;
        }
    }

    boolean onlyFigures = attributes.getBoolean(JSON_ONLY_FIGURES_KEY);
    if (onlyFigures) {
        //check field type
        switch (fieldType) {
            default:
            case GeoConstants.FTInteger:
                setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);
                break;

            case GeoConstants.FTReal:
                setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
        }
    }
}
 
Example 3
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String cursorToJson(Cursor cursor)
        throws JSONException, IOException
{
    JSONObject rootObject = new JSONObject();
    if (0 != (mSyncType & Constants.SYNC_ATTRIBUTES)) {
        JSONObject valueObject = new JSONObject();
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            String name = cursor.getColumnName(i);
            if (name.equals(Constants.FIELD_ID) || name.equals(Constants.FIELD_GEOM)) {
                continue;
            }

            Field field = mFields.get(cursor.getColumnName(i));
            if (null == field) {
                continue;
            }

            switch (field.getType()) {
                case GeoConstants.FTReal:
                    valueObject.put(name, cursor.getFloat(i));
                    break;
                case GeoConstants.FTInteger:
                    valueObject.put(name, cursor.getInt(i));
                    break;
                case GeoConstants.FTString:
                    String stringVal = cursor.getString(i);
                    if (null != stringVal && !stringVal.equals("null")) {
                        valueObject.put(name, stringVal);
                    }
                    break;
                case GeoConstants.FTDateTime:
                    TimeZone timeZoneDT = TimeZone.getDefault();
                    timeZoneDT.setRawOffset(0); // set to UTC
                    Calendar calendarDT = Calendar.getInstance(timeZoneDT);
                    calendarDT.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonDateTime = new JSONObject();
                    jsonDateTime.put("year", calendarDT.get(Calendar.YEAR));
                    jsonDateTime.put("month", calendarDT.get(Calendar.MONTH) + 1);
                    jsonDateTime.put("day", calendarDT.get(Calendar.DAY_OF_MONTH));
                    jsonDateTime.put("hour", calendarDT.get(Calendar.HOUR_OF_DAY));
                    jsonDateTime.put("minute", calendarDT.get(Calendar.MINUTE));
                    jsonDateTime.put("second", calendarDT.get(Calendar.SECOND));
                    valueObject.put(name, jsonDateTime);
                    break;
                case GeoConstants.FTDate:
                    TimeZone timeZoneD = TimeZone.getDefault();
                    timeZoneD.setRawOffset(0); // set to UTC
                    Calendar calendarD = Calendar.getInstance(timeZoneD);
                    calendarD.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonDate = new JSONObject();
                    jsonDate.put("year", calendarD.get(Calendar.YEAR));
                    jsonDate.put("month", calendarD.get(Calendar.MONTH) + 1);
                    jsonDate.put("day", calendarD.get(Calendar.DAY_OF_MONTH));
                    valueObject.put(name, jsonDate);
                    break;
                case GeoConstants.FTTime:
                    TimeZone timeZoneT = TimeZone.getDefault();
                    timeZoneT.setRawOffset(0); // set to UTC
                    Calendar calendarT = Calendar.getInstance(timeZoneT);
                    calendarT.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonTime = new JSONObject();
                    jsonTime.put("hour", calendarT.get(Calendar.HOUR_OF_DAY));
                    jsonTime.put("minute", calendarT.get(Calendar.MINUTE));
                    jsonTime.put("second", calendarT.get(Calendar.SECOND));
                    valueObject.put(name, jsonTime);
                    break;
                default:
                    break;
            }
        }
        rootObject.put(NGWUtil.NGWKEY_FIELDS, valueObject);
    }

    if (0 != (mSyncType & Constants.SYNC_GEOMETRY)) {
        //may be found geometry in cache by id is faster
        GeoGeometry geometry = GeoGeometryFactory.fromBlob(
                cursor.getBlob(cursor.getColumnIndex(Constants.FIELD_GEOM)));

        geometry.setCRS(GeoConstants.CRS_WEB_MERCATOR);
        if (mCRS != GeoConstants.CRS_WEB_MERCATOR)
            geometry.project(mCRS);

        rootObject.put(NGWUtil.NGWKEY_GEOM, geometry.toWKT(true));
        //rootObject.put("id", cursor.getLong(cursor.getColumnIndex(FIELD_ID)));
    }

    return rootObject.toString();
}
 
Example 4
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void createField(Field field)
        throws SQLiteException
{
    if (null == mFields || mFields.isEmpty()) //the db table is not yet created
    {
        return;
    }
    if (mFields.containsKey(field.getName())) {
        return;
    }

    if (!LayerUtil.isFieldNameValid(field.getName())) {
        return;
    } else {
        field.setName(LayerUtil.normalizeFieldName(field.getName()));
    }

    mFields.put(field.getName(), field);

    String fieldCreate = "ALTER TABLE " + mPath.getName() + " ADD COLUMN '" + field.getName() + "'";

    switch (field.getType()) {
        case FTString:
            fieldCreate += " TEXT";
            break;
        case FTInteger:
            fieldCreate += " INTEGER";
            break;
        case FTReal:
            fieldCreate += " REAL";
            break;
        case FTDateTime:
        case FTDate:
        case FTTime:
            fieldCreate += " TIMESTAMP";
            break;
    }

    MapContentProviderHelper map = (MapContentProviderHelper) MapBase.getInstance();
    SQLiteDatabase db = map.getDatabase(false);
    db.execSQL(fieldCreate);
}
 
Example 5
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void readNGWFeatureFields(Feature feature, JsonReader reader, List<Field> fields)
        throws IOException, IllegalStateException, NumberFormatException
{
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (reader.peek() == JsonToken.NULL) {
            reader.skipValue();
        } else {
            boolean bAdded = false;
            for (Field field : fields) {
                if (field.getName().equals(name) || field.getName().equals(LayerUtil.normalizeFieldName(name))) {
                    switch (field.getType()) {
                        case GeoConstants.FTReal:
                            feature.setFieldValue(field.getName(), reader.nextDouble());
                            bAdded = true;
                            break;
                        case GeoConstants.FTInteger:
                            feature.setFieldValue(field.getName(), reader.nextInt());
                            bAdded = true;
                            break;
                        case GeoConstants.FTString:
                            feature.setFieldValue(field.getName(), reader.nextString());
                            bAdded = true;
                            break;
                        case GeoConstants.FTDate:
                        case GeoConstants.FTTime:
                        case GeoConstants.FTDateTime:
                            readNGWDate(feature, reader, field.getName());
                            bAdded = true;
                            break;
                        default:
                            break;
                    }
                    break;
                }
            }
            if (!bAdded) {
                reader.skipValue();
            }
        }
    }
    reader.endObject();
}