Search in sources :

Example 16 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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 17 with GfJsonObject

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

the class DataCommandFunction method select_SelectResults.

private void select_SelectResults(SelectResults selectResults, Object principal, List<SelectResultRow> list, AtomicInteger nestedObjectCount) throws GfJsonException {
    for (Object object : selectResults) {
        // Post processing
        object = securityService.postProcess(principal, null, null, object, false);
        if (object instanceof Struct) {
            StructImpl impl = (StructImpl) object;
            GfJsonObject jsonStruct = getJSONForStruct(impl, nestedObjectCount);
            if (logger.isDebugEnabled()) {
                logger.debug("SelectResults : Adding select json string : {}", jsonStruct);
            }
            list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_STRUCT_RESULT, jsonStruct.toString()));
        } else if (JsonUtil.isPrimitiveOrWrapper(object.getClass())) {
            if (logger.isDebugEnabled()) {
                logger.debug("SelectResults : Adding select primitive : {}", object);
            }
            list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_PRIMITIVE, object));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("SelectResults : Bean Results class is {}", object.getClass());
            }
            String str = toJson(object);
            GfJsonObject jsonBean;
            try {
                jsonBean = new GfJsonObject(str);
            } catch (GfJsonException e) {
                logger.error(e.getMessage(), e);
                jsonBean = new GfJsonObject();
                try {
                    jsonBean.put("msg", e.getMessage());
                } catch (GfJsonException e1) {
                    logger.warn("Ignored GfJsonException:", e1);
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("SelectResults : Adding bean json string : {}", jsonBean);
            }
            list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString()));
        }
    }
}
Also used : StructImpl(org.apache.geode.cache.query.internal.StructImpl) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) SelectResultRow(org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Struct(org.apache.geode.cache.query.Struct)

Example 18 with GfJsonObject

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

the class CommandResult method buildSection.

private void buildSection(Table table, RowGroup parentRowGroup, GfJsonObject section, int depth) throws GfJsonException {
    Iterator<String> keys = section.keys();
    RowGroup rowGroup = null;
    if (parentRowGroup != null) {
        rowGroup = parentRowGroup;
    } else {
        rowGroup = table.newRowGroup();
    }
    addHeaderInRowGroup(rowGroup, section);
    while (keys.hasNext()) {
        String key = keys.next();
        Object object = section.get(key);
        // System.out.println(key +" : " + object);
        if (key.startsWith(CompositeResultData.TABLE_DATA_ACCESSOR)) {
            GfJsonObject tableObject = section.getJSONObject(key);
            addHeaderInTable(table, tableObject);
            RowGroup rowGroupForTable = table.newRowGroup();
            buildTable(rowGroupForTable, tableObject.getJSONObject(ResultData.RESULT_CONTENT));
            addFooterInTable(table, tableObject);
        } else if (key.startsWith(CompositeResultData.SECTION_DATA_ACCESSOR)) {
            GfJsonObject subSection = section.getJSONObject(key);
            buildSection(table, rowGroup, subSection, depth + 1);
        } else if (key.equals(CompositeResultData.SEPARATOR)) {
            String separatorString = section.getString(key);
            rowGroup.newRowSeparator(separatorString.charAt(0), true);
        } else if (key.equals(ResultData.RESULT_HEADER) || key.equals(ResultData.RESULT_FOOTER)) {
            // skip header & footer
            continue;
        } else {
            Row newRow = rowGroup.newRow();
            String prefix = "";
            for (int i = 0; i < depth; i++) {
                prefix += " . ";
            }
            String[] value = getValuesSeparatedByLines(object);
            if (value.length == 1) {
                newRow.newLeftCol(prefix + key).newLeftCol(value[0]);
            } else {
                if (value.length != 0) {
                    // possible when object == CliConstants.LINE_SEPARATOR
                    newRow.newLeftCol(prefix + key).newLeftCol(value[0]);
                    for (int i = 1; i < value.length; i++) {
                        newRow = rowGroup.newRow();
                        newRow.setColumnSeparator("   ");
                        newRow.newLeftCol("").newLeftCol(value[i]);
                    }
                } else {
                    newRow.newLeftCol(prefix + key).newLeftCol("");
                }
            }
        // System.out.println(key+" : "+object);
        }
    }
    addFooterInRowGroup(rowGroup, section);
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Example 19 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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 20 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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

GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)34 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)23 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)9 JSONObject (org.json.JSONObject)5 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Row (org.apache.geode.management.internal.cli.result.TableBuilder.Row)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SelectResultRow (org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow)2 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)2 RowGroup (org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup)2 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1