Search in sources :

Example 6 with GfJsonArray

use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.

the class JsonUtil method getList.

public static List<CliJsonSerializable> getList(GfJsonObject jsonObject, String byName) {
    List<CliJsonSerializable> cliJsonSerializables = Collections.emptyList();
    try {
        GfJsonArray cliJsonSerializableArray = jsonObject.getJSONArray(byName);
        int size = cliJsonSerializableArray.size();
        if (size > 0) {
            cliJsonSerializables = new ArrayList<CliJsonSerializable>();
        }
        for (int i = 0; i < size; i++) {
            GfJsonObject cliJsonSerializableState = cliJsonSerializableArray.getJSONObject(i);
            int jsId = cliJsonSerializableState.getInt(CliJsonSerializable.JSID);
            CliJsonSerializable cliJsonSerializable = CliJsonSerializableFactory.getCliJsonSerializable(jsId);
            cliJsonSerializable.fromJson(cliJsonSerializableState);
            cliJsonSerializables.add(cliJsonSerializable);
        }
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    return cliJsonSerializables;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) CliJsonSerializable(org.apache.geode.management.internal.cli.result.CliJsonSerializable)

Example 7 with GfJsonArray

use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.

the class JsonUtil method getSet.

public static Set<CliJsonSerializable> getSet(GfJsonObject jsonObject, String byName) {
    Set<CliJsonSerializable> cliJsonSerializables = Collections.emptySet();
    try {
        GfJsonArray cliJsonSerializableArray = jsonObject.getJSONArray(byName);
        int size = cliJsonSerializableArray.size();
        if (size > 0) {
            cliJsonSerializables = new HashSet<CliJsonSerializable>();
        }
        for (int i = 0; i < size; i++) {
            GfJsonObject cliJsonSerializableState = cliJsonSerializableArray.getJSONObject(i);
            int jsId = cliJsonSerializableState.getInt(CliJsonSerializable.JSID);
            CliJsonSerializable cliJsonSerializable = CliJsonSerializableFactory.getCliJsonSerializable(jsId);
            cliJsonSerializable.fromJson(cliJsonSerializableState);
            cliJsonSerializables.add(cliJsonSerializable);
        }
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    return cliJsonSerializables;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) CliJsonSerializable(org.apache.geode.management.internal.cli.result.CliJsonSerializable)

Example 8 with GfJsonArray

use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.

the class ObjectResultData method getAllObjects.

public List<CliJsonSerializable> getAllObjects() {
    List<CliJsonSerializable> list = new ArrayList<CliJsonSerializable>();
    try {
        GfJsonArray rootJsonArray = contentObject.getJSONArray(OBJECTS_ACCESSOR);
        int size = rootJsonArray.size();
        GfJsonObject jsonObject = null;
        CliJsonSerializable cliJsonSerializable = null;
        for (int i = 0; i < size; i++) {
            jsonObject = rootJsonArray.getJSONObject(i);
            cliJsonSerializable = CliJsonSerializableFactory.getCliJsonSerializable(jsonObject.getInt(CliJsonSerializable.JSID));
            cliJsonSerializable.fromJson(jsonObject);
            list.add(cliJsonSerializable);
        }
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    return list;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 9 with GfJsonArray

use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.

the class CommandResult method buildTable.

/**
   * @param rowGroup
   * @param content
   * @throws GfJsonException
   */
private void buildTable(RowGroup rowGroup, GfJsonObject content) throws GfJsonException {
    GfJsonArray columnNames = content.names();
    int numOfColumns = columnNames.size();
    Row headerRow = rowGroup.newRow();
    rowGroup.setColumnSeparator(" | ");
    rowGroup.newRowSeparator('-', false);
    // build Table Header first
    for (int i = 0; i < numOfColumns; i++) {
        Object object = columnNames.get(i);
        if (AbstractResultData.BYTE_DATA_ACCESSOR.equals(object)) {
            // skip file data if any //TODO - make response format better
            continue;
        }
        headerRow.newCenterCol((String) object);
    }
    // Build remaining rows by extracting data column-wise from JSON object
    Row[] dataRows = null;
    for (int i = 0; i < numOfColumns; i++) {
        Object object = columnNames.get(i);
        if (AbstractResultData.BYTE_DATA_ACCESSOR.equals(object)) {
            // skip file data if any //TODO - make response format better
            continue;
        }
        GfJsonArray accumulatedData = content.getJSONArray((String) object);
        dataRows = buildRows(rowGroup, dataRows, accumulatedData);
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Example 10 with GfJsonArray

use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.

the class CommandResult method buildObjectSection.

private void buildObjectSection(Table table, RowGroup parentRowGroup, GfJsonObject object, int depth) throws GfJsonException {
    Iterator<String> keys = object.keys();
    RowGroup rowGroup = null;
    if (parentRowGroup != null) {
        rowGroup = parentRowGroup;
    } else {
        rowGroup = table.newRowGroup();
    }
    GfJsonArray nestedCollection = null;
    GfJsonObject nestedObject = null;
    GfJsonObject fieldDisplayNames = object.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
    List<String> fieldsToSkipOnUI = null;
    if (object.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
        GfJsonArray jsonArray = object.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
        ;
        fieldsToSkipOnUI = new ArrayList<String>();
        for (int i = 0; i < jsonArray.size(); i++) {
            fieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
        }
    }
    while (keys.hasNext()) {
        String key = keys.next();
        if (CliJsonSerializable.FIELDS_TO_SKIP.contains(key) || (fieldsToSkipOnUI != null && fieldsToSkipOnUI.contains(key))) {
            continue;
        }
        try {
            nestedCollection = object.getJSONArray(key);
        } catch (GfJsonException e) {
        /* next check if it's a nested object */
        }
        Object field = null;
        if (nestedCollection == null) {
            field = object.get(key);
            if (!isPrimitiveOrStringOrWrapper(field)) {
                nestedObject = object.getJSONObject(key);
            }
        }
        if (nestedCollection != null && isPrimitiveOrStringOrWrapperArray(nestedCollection)) {
            String str = nestedCollection.toString();
            field = str.substring(1, str.length() - 1);
            nestedCollection = null;
        }
        Row newRow = rowGroup.newRow();
        String prefix = "";
        /* if (nestedCollection != null) */
        {
            for (int i = 0; i < depth; i++) {
                prefix += " . ";
            }
        }
        String fieldNameToDisplay = fieldDisplayNames.getString(key);
        if (nestedCollection == null) {
            newRow.newLeftCol(prefix + fieldNameToDisplay);
        }
        if (nestedCollection != null) {
            Map<String, Integer> columnsMap = new HashMap<String, Integer>();
            GfJsonArray rowsArray = nestedCollection;
            RowGroup newRowGroup = table.newRowGroup();
            newRowGroup.setColumnSeparator(" | ");
            newRowGroup.newBlankRow();
            newRowGroup.newRow().newLeftCol(fieldNameToDisplay);
            Row headerRow = newRowGroup.newRow();
            int numOfRows = rowsArray.size();
            List<String> tableFieldsToSkipOnUI = null;
            for (int j = 0; j < numOfRows; j++) {
                GfJsonObject content = rowsArray.getJSONObject(j);
                if (content.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
                    GfJsonArray jsonArray = content.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
                    tableFieldsToSkipOnUI = new ArrayList<String>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        tableFieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
                    }
                }
                GfJsonArray columnNames = content.names();
                int numOfColumns = columnNames.size();
                if (headerRow.isEmpty()) {
                    GfJsonObject innerFieldDisplayNames = content.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
                    for (int i = 0; i < numOfColumns; i++) {
                        Object columnName = columnNames.get(i);
                        if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
                            // skip file data if any //TODO - make response format better
                            continue;
                        }
                        headerRow.newCenterCol(innerFieldDisplayNames.getString((String) columnName));
                        columnsMap.put((String) columnName, i);
                    }
                    newRowGroup.newRowSeparator('-', false);
                }
                newRow = newRowGroup.newRow();
                for (int i = 0; i < numOfColumns; i++) {
                    Object columnName = columnNames.get(i);
                    if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
                        // skip file data if any //TODO - make response format better
                        continue;
                    }
                    newRow.newLeftCol(String.valueOf(content.get((String) columnName)));
                }
            }
        } else if (nestedObject != null) {
            buildObjectSection(table, rowGroup, nestedObject, depth + 1);
        } else {
            // Row newRow = rowGroup.newRow();
            // String prefix = "";
            // for (int i = 0; i < depth; i++) {
            // prefix += " . ";
            // }
            // newRow.newLeftCol(prefix+fieldDisplayNames.getString(key)).newLeftCol(field);
            Object value = field;
            /*
         * if (isPrimitiveOrStringOrWrapperArray(value)) { value = Arrays.toString((String[])value);
         * }
         */
            newRow.newLeftCol(value);
        }
        nestedCollection = null;
        nestedObject = null;
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) HashMap(java.util.HashMap) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Aggregations

GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)17 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)14 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)10 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)4 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 ArrayList (java.util.ArrayList)3 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)3 SectionResultData (org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)3 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 Row (org.apache.geode.management.internal.cli.result.TableBuilder.Row)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DataFormatException (java.util.zip.DataFormatException)1 DeflaterInflaterData (org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData)1