Example usage for com.google.gwt.json.client JSONValue isArray

List of usage examples for com.google.gwt.json.client JSONValue isArray

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isArray.

Prototype

public JSONArray isArray() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONArray.

Usage

From source file:org.sakaiproject.gradebook.gwt.client.gxt.view.panel.StatisticsPanel.java

License:Educational Community License

private void getGradeItemStatisticsChartData(String assignmentId, String sectionId) {

    // First we check the cache if we have the data already
    String cacheKey = assignmentId + sectionId;

    if (dataTableCache.containsKey(cacheKey)) {

        // Cache hit
        dataTable = dataTableCache.get(cacheKey);
        statisticsChartPanel.setDataTable(dataTable);
        statisticsChartPanel.show();//from   ww  w. j  a v  a 2 s . com
    } else {

        statisticsChartPanel.showUserFeedback(null);

        // Data is not in cache yet
        Gradebook gbModel = Registry.get(AppConstants.CURRENT);

        RestBuilder builder = RestBuilder.getInstance(Method.GET, GWT.getModuleBaseURL(),
                AppConstants.REST_FRAGMENT, AppConstants.STATISTICS_FRAGMENT, AppConstants.INSTRUCTOR_FRAGMENT,
                gbModel.getGradebookUid(), gbModel.getGradebookId().toString(), assignmentId, sectionId);

        // Keeping track of the assignmentId so that we can add the dataTable to
        // the cache once the call returns
        selectedAssignmentId = assignmentId;
        selectedSectionId = sectionId;

        builder.sendRequest(200, 400, null, new RestCallback() {

            public void onError(Request request, Throwable caught) {

                statisticsChartPanel.hideUserFeedback(null);
                Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                        i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
            }

            public void onFailure(Request request, Throwable exception) {

                statisticsChartPanel.hideUserFeedback(null);
                Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                        i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
            }

            public void onSuccess(Request request, Response response) {

                // Getting two dimensional INT array
                // GRBK-939 
                // deprecated method can cause security problems as it evals. 

                /*
                 * GRBK-939 According to google this will toss exceptions if the input
                 * is null or empty, so we'll guard against that and send a notify if
                 * we have this problem... 
                 * 
                 */
                String jsonText = response.getText();

                if (jsonText != null && !"".equals(jsonText)) {

                    JSONValue jsonValue = JSONParser.parseStrict(jsonText);
                    JSONArray jsonArray = jsonValue.isArray();

                    JSONArray positiveFrequencies = jsonArray.get(AppConstants.POSITIVE_NUMBER).isArray();

                    dataTable = statisticsChartPanel.createDataTable();
                    dataTable.addColumn(ColumnType.STRING, i18n.statisticsChartLabelDistribution());
                    dataTable.addColumn(ColumnType.NUMBER, i18n.statisticsChartLabelFrequency());
                    dataTable.addRows(positiveFrequencies.size());

                    String[] xAxisRangeLables = statisticsChartPanel.getXAxisRangeLabels();

                    for (int i = 0; i < positiveFrequencies.size(); i++) {

                        // Set label
                        dataTable.setValue(i, 0, xAxisRangeLables[i]);

                        // Set value
                        double positiveValue = positiveFrequencies.get(i).isNumber().doubleValue();
                        dataTable.setValue(i, 1, positiveValue);
                    }

                    // adding the dataTable to the cache
                    dataTableCache.put(selectedAssignmentId + selectedSectionId, dataTable);
                    statisticsChartPanel.show();
                } else {
                    Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                            i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
                }

                statisticsChartPanel.hideUserFeedback(null);
            }
        });
    }
}

From source file:org.sakaiproject.gradebook.gwt.client.gxt.view.panel.StudentPanel.java

License:Educational Community License

private void getGradeItemStatisticsChartData(String assignmentId) {

    // First we check the cache if we have the data already
    String cacheKey = assignmentId;

    if (dataTableCache.containsKey(cacheKey)) {

        // Cache hit
        dataTable = dataTableCache.get(cacheKey);
        statisticsChartPanel.setDataTable(dataTable);
        statisticsChartPanel.show();/*from w  ww. j a  v  a  2s .c  o m*/
    } else {

        statisticsChartPanel.showUserFeedback(null);

        // Data is not in cache yet
        Gradebook gbModel = Registry.get(AppConstants.CURRENT);

        RestBuilder builder = RestBuilder.getInstance(Method.GET, GWT.getModuleBaseURL(),
                AppConstants.REST_FRAGMENT, AppConstants.STATISTICS_FRAGMENT, AppConstants.STUDENT_FRAGMENT,
                gbModel.getGradebookUid(), gbModel.getGradebookId().toString(), assignmentId);

        // Keeping track of the assignmentId so that we can add the dataTable to
        // the cache once the call returns
        selectedAssignmentId = assignmentId;

        builder.sendRequest(200, 400, null, new RestCallback() {

            public void onError(Request request, Throwable caught) {

                statisticsChartPanel.hideUserFeedback(null);
                Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                        i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
            }

            public void onFailure(Request request, Throwable exception) {

                statisticsChartPanel.hideUserFeedback(null);
                Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                        i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
            }

            public void onSuccess(Request request, Response response) {

                String jsonText = response.getText();
                if (jsonText != null && !"".equals(jsonText)) {

                    JSONValue jsonValue = JSONParser.parseStrict(jsonText);
                    JSONArray jsonArray = jsonValue.isArray();

                    JSONArray positiveFrequencies = jsonArray.get(AppConstants.POSITIVE_NUMBER).isArray();

                    dataTable = statisticsChartPanel.createDataTable();
                    dataTable.addColumn(ColumnType.STRING, i18n.statisticsChartLabelDistribution());
                    dataTable.addColumn(ColumnType.NUMBER, i18n.statisticsChartLabelFrequency());
                    dataTable.addRows(positiveFrequencies.size());

                    String[] xAxisRangeLabels = statisticsChartPanel.getXAxisRangeLabels();

                    for (int i = 0; i < positiveFrequencies.size(); i++) {

                        // Set label
                        dataTable.setValue(i, 0, xAxisRangeLabels[i]);

                        // Set value
                        double positiveValue = positiveFrequencies.get(i).isNumber().doubleValue();
                        dataTable.setValue(i, 1, positiveValue);
                    }

                    // adding the dataTable to the cache
                    dataTableCache.put(selectedAssignmentId, dataTable);
                    statisticsChartPanel.show();
                } else {
                    Dispatcher.forwardEvent(GradebookEvents.Notification.getEventType(), new NotificationEvent(
                            i18n.statisticsDataErrorTitle(), i18n.statisticsDataErrorMsg(), true));
                }

                statisticsChartPanel.hideUserFeedback(null);
            }
        });
    }
}

From source file:org.spiffyui.client.JSONUtil.java

License:Apache License

/**
 * Get a JSONArray from the JSON object or null if it doesn't exist or
 * isn't a JSONArray//from w w w .j av a  2 s . c  om
 * 
 * @param obj    the object with the value
 * @param key    the key for the object
 * 
 * @return the value or an empty array if it could not be decoded
 */
public static JSONArray getJSONArray(JSONObject obj, String key) {
    JSONValue v = obj.get(key);
    if (v != null) {
        JSONArray a = v.isArray();
        if (a != null) {
            return a;
        }
    }

    return new JSONArray();
}

From source file:org.spiffyui.spiffyforms.client.User.java

License:Apache License

/**
 * Get the list of users from the server
 * /*from  w  ww  .j av a2s. c o  m*/
 * @param callback the callback for getting the list of users
 */
public static void getUsers(final RESTObjectCallBack<User[]> callback) {
    RESTility.callREST("api/users", new RESTCallback() {
        @Override
        public void onSuccess(JSONValue val) {
            JSONArray usersArray = val.isArray();
            ArrayList<User> users = new ArrayList<User>();

            for (int i = 0; i < usersArray.size(); i++) {
                if (usersArray.get(i).isNull() != null) {
                    continue;
                }

                User u = new User();

                u.setFirstName(JSONUtil.getStringValue(usersArray.get(i).isObject(), "firstName"));
                u.setLastName(JSONUtil.getStringValue(usersArray.get(i).isObject(), "lastName"));
                u.setPassword(JSONUtil.getStringValue(usersArray.get(i).isObject(), "password"));
                u.setUserId(JSONUtil.getStringValue(usersArray.get(i).isObject(), "userID"));
                u.setEmail(JSONUtil.getStringValue(usersArray.get(i).isObject(), "email"));
                u.setUserDesc(JSONUtil.getStringValue(usersArray.get(i).isObject(), "desc"));
                u.setGender(JSONUtil.getStringValue(usersArray.get(i).isObject(), "gender"));
                u.setBirthday(JSONUtil.getDateValue(usersArray.get(i).isObject(), "birthday"));
                u.m_isNew = false;

                users.add(u);
            }

            callback.success(users.toArray(new User[users.size()]));

        }

        @Override
        public void onError(int statusCode, String errorResponse) {
            callback.error(errorResponse);
        }

        @Override
        public void onError(RESTException e) {
            callback.error(e);
        }

    });
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

private BaseTreeModel buildGroupResultFromJsonRepresentation(JsonRepresentation representation)
        throws IOException {
    BaseTreeModel rootModel = new BaseTreeModel();
    List<String> matchFieldList = new ArrayList<String>();
    rootModel.set(StagingConstant.MATCH_FIELD_LIST, matchFieldList);
    JSONObject jsonObject = representation.getJsonObject();
    if (jsonObject != null) {
        JSONValue rootValue = jsonObject.get(StagingConstant.MATCH_ROOT_NAME);
        if (rootValue != null && rootValue.isArray() != null) {
            JSONArray array = rootValue.isArray();
            for (int i = 0; i < array.size(); i++) {
                JSONValue groupValue = array.get(i);
                buildGroupNode(groupValue, rootModel, i);
            }/*from w ww.  jav a2s .c o m*/
        }
    }
    return rootModel;
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

private void buildGroupNode(JSONValue value, final BaseTreeModel parent, int index) {
    JSONArray groupArray = getJSONArray(value, StagingConstant.MATCH_GROUP_NAME);
    if (groupArray != null) {
        final BaseTreeModel group = new BaseTreeModel();
        group.set(StagingConstant.MATCH_DATA, buildTreeModelFromJsonRepresentation(value).getChild(0));
        group.setParent(parent);// w w  w.  ja v  a2  s  .  c  o  m
        JSONValue resultValue = groupArray.get(0);
        JSONArray resultArray = getJSONArray(resultValue, "result"); //$NON-NLS-1$
        if (resultArray != null) {
            group.set(StagingConstant.MATCH_IS_GROUP, true);
            group.set(StagingConstant.MATCH_GROUP_NAME, StagingConstant.MATCH_GROUP_NAME + (index + 1));
            JSONValue relatedIdArrayValue = getJSONValue(resultArray, StagingConstant.MATCH_RELATED_IDS);
            if (relatedIdArrayValue != null && relatedIdArrayValue.isArray() != null) {
                JSONArray relatedIdArray = relatedIdArrayValue.isArray();
                List<String> idList = new ArrayList<String>();
                for (int i = 0; i < relatedIdArray.size(); i++) {
                    idList.add(getStringValue(relatedIdArray.get(i)));
                }
                group.set(StagingConstant.MATCH_RELATED_IDS, idList);
                String groupId = getStringValue(getJSONValue(resultArray, StagingConstant.MATCH_GROUP_ID));
                group.set(StagingConstant.MATCH_GROUP_ID, groupId);
                group.set(StagingConstant.MATCH_GROUP_CONFIDENCE,
                        getScoreValue(getJSONValue(resultArray, StagingConstant.MATCH_GROUP_CONFIDENCE)));
                group.set(StagingConstant.MATCH_GROUP_GID, groupId);
                group.set(StagingConstant.MATCH_GROUP_SZIE, relatedIdArray.size());
                JSONValue valueArrayValue = getJSONValue(resultArray, "values"); //$NON-NLS-1$
                if (valueArrayValue != null && valueArrayValue.isArray() != null) {
                    JSONArray valueArray = valueArrayValue.isArray();
                    List<String> matchFieldList = parent.get(StagingConstant.MATCH_FIELD_LIST);
                    for (int i = 0; i < valueArray.size(); i++) {
                        JSONArray childArray = getJSONArray(valueArray.get(i), StagingConstant.MATCH_VALUE);
                        if (childArray != null) {
                            String fieldName = getStringValue(
                                    getJSONValue(childArray, StagingConstant.MATCH_FIELD));
                            if (!matchFieldList.contains(fieldName)) {
                                matchFieldList.add(fieldName);
                            }
                            String fieldValue = getStringValue(
                                    getJSONValue(childArray, StagingConstant.MATCH_VALUE));
                            group.set(fieldName, fieldValue);
                        }
                    }
                }
            }
        }
        JSONValue detailsValue = getJSONValue(groupArray, StagingConstant.MATCH_DETAILS);
        if (detailsValue != null && detailsValue.isArray() != null) {
            JSONArray detailsArray = detailsValue.isArray();
            if (detailsArray != null) {
                for (int i = 0; i < detailsArray.size(); i++) {
                    JSONValue detailValue = detailsArray.get(i);
                    buildDetailNode(detailValue, group);
                }
            }
        }
        parent.add(group);
    }
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

private void buildDetailNode(JSONValue value, BaseTreeModel parent) {
    final JSONArray detailArray = getJSONArray(value, "detail"); //$NON-NLS-1$
    if (detailArray != null) {
        List<String> relatedIdList = parent.get(StagingConstant.MATCH_RELATED_IDS);
        String id = getStringValue(getJSONValue(detailArray, StagingConstant.MATCH_GROUP_ID));
        if (relatedIdList.contains(id)) {
            final BaseTreeModel detail = new BaseTreeModel();
            detail.setParent(parent);/*from   w  w w  .j a  v a 2s .co m*/
            detail.set(StagingConstant.MATCH_IS_GROUP, false);
            detail.set(StagingConstant.MATCH_GROUP_NAME, ""); //$NON-NLS-1$
            detail.set(StagingConstant.MATCH_GROUP_ID, id);
            JSONValue valuesValue = getJSONValue(detailArray, "values"); //$NON-NLS-1$
            if (valuesValue != null && valuesValue.isArray() != null) {
                JSONArray valuesArray = valuesValue.isArray();
                for (int i = 0; i < valuesArray.size(); i++) {
                    JSONArray valueArray = getJSONArray(valuesArray.get(i), "value"); //$NON-NLS-1$
                    if (valueArray != null) {
                        String fieldName = getStringValue(
                                getJSONValue(valueArray, StagingConstant.MATCH_FIELD));
                        String fieldValue = getStringValue(getJSONValue(valueArray, "value")); //$NON-NLS-1$
                        detail.set(fieldName, fieldValue);
                    }
                }
            }

            for (int i = 0; i < detailArray.size(); i++) {
                JSONValue detailValue = detailArray.get(i);
                JSONArray matchArray = getJSONArray(detailValue, "match"); //$NON-NLS-1$
                if (matchArray != null) {
                    if ("true".equals(getStringValue(getJSONValue(matchArray, "is_match")))) { //$NON-NLS-1$ //$NON-NLS-2$
                        JSONValue scoreValue = getJSONValue(matchArray, StagingConstant.MATCH_SCORE);
                        detail.set(StagingConstant.MATCH_SCORE, getScoreValue(scoreValue));
                        detail.set(StagingConstant.MATCH_EXACT_SCORE, getStringValue(scoreValue));
                        JSONValue fieldScoresValue = getJSONValue(matchArray, "field_scores"); //$NON-NLS-1$
                        if (fieldScoresValue != null && fieldScoresValue.isArray() != null) {
                            JSONArray fieldScoresArray = fieldScoresValue.isArray();
                            StringBuilder scoreAttributeBuilder = new StringBuilder();
                            StringBuilder exactScoreAttributeBuilder = new StringBuilder();
                            for (int j = 0; j < fieldScoresArray.size(); j++) {
                                JSONArray fieldScoreArray = getJSONArray(fieldScoresArray.get(j),
                                        StagingConstant.MATCH_FIELD_SCORE);
                                if (fieldScoreArray != null) {
                                    String fieldName = getStringValue(
                                            getJSONValue(fieldScoreArray, StagingConstant.MATCH_FIELD));
                                    scoreAttributeBuilder.append(fieldName);
                                    exactScoreAttributeBuilder.append(fieldName);
                                    scoreAttributeBuilder.append(StagingConstant.VALUE_SEPARATOR);
                                    exactScoreAttributeBuilder.append(StagingConstant.VALUE_SEPARATOR);
                                    JSONValue fieldScoreValue = getJSONValue(fieldScoreArray,
                                            StagingConstant.MATCH_VALUE);
                                    scoreAttributeBuilder.append(getScoreValue(fieldScoreValue));
                                    exactScoreAttributeBuilder.append(getStringValue(fieldScoreValue));
                                    scoreAttributeBuilder.append(","); //$NON-NLS-1$
                                    exactScoreAttributeBuilder.append(","); //$NON-NLS-1$
                                }
                            }
                            String fieldScoreValue = scoreAttributeBuilder.length() > 0 ? scoreAttributeBuilder
                                    .toString().substring(0, scoreAttributeBuilder.toString().length() - 1)
                                    : ""; //$NON-NLS-1$
                            detail.set(StagingConstant.MATCH_FIELD_SCORE, fieldScoreValue);
                            String fieldExactScoreValue = exactScoreAttributeBuilder.length() > 0
                                    ? exactScoreAttributeBuilder.toString().substring(0,
                                            exactScoreAttributeBuilder.toString().length() - 1)
                                    : ""; //$NON-NLS-1$
                            detail.set(StagingConstant.MATCH_EXACT_FIELD_SCORE, fieldExactScoreValue);
                        }
                    }
                }
            }
            parent.add(detail);
        }
    }
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

public void retriveNode(JSONValue jsonValue, BaseTreeModel parent) {
    if (jsonValue.isArray() != null) {
        JSONArray array = jsonValue.isArray();
        for (int i = 0; i < array.size(); i++) {
            JSONValue value = array.get(i);
            String content = getStringValue(value);
            if (content != null) {
                addChildNode(content, parent);
            }/*from w  w  w. j a  v a  2s  .  co m*/
            retriveNode(value, parent);
        }
    } else if (jsonValue.isObject() != null) {
        JSONObject object = jsonValue.isObject();
        Set<String> keySet = object.keySet();
        for (String key : keySet) {
            JSONValue value = object.get(key);
            String content = getStringValue(value);
            if (content != null) {
                addChildNode(key + StagingConstant.VALUE_SEPARATOR + content, parent);
            } else {
                BaseTreeModel treeModel = addChildNode(key, parent);
                retriveNode(value, treeModel);
            }
        }
    }
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

public JSONArray getJSONArray(JSONValue value, String key) {
    JSONArray childArray = null;/*from   ww  w  .j  av a2  s  .  c  o  m*/
    if (value != null && value.isObject() != null) {
        JSONObject object = value.isObject();
        JSONValue childValue = object.get(key);
        if (childValue != null && childValue.isArray() != null) {
            childArray = childValue.isArray();
        }
    }
    return childArray;
}

From source file:org.talend.mdm.webapp.welcomeportal.client.rest.StatisticsRestServiceHandler.java

License:Open Source License

/**
 * Get entity count summary for a container
 *///from   w  w  w  .ja va  2s.c  o  m
public void getContainerDataStats(String dataContainer, ConfigModel configModel,
        final SessionAwareAsyncCallback<JSONArray> callback) {
    if (dataContainer == null) {
        throw new IllegalArgumentException("Data container required"); //$NON-NLS-1$
    }
    client.init(Method.GET,
            restServiceUrl + "/data/" + dataContainer + "?top=" + configModel.getSettingValue()); //$NON-NLS-1$ //$NON-NLS-2$
    client.setCallback(new ResourceSessionAwareCallbackHandler() {

        @Override
        public void doProcess(Request request, Response response) throws Exception {

            JsonRepresentation jsonRepresentation = RestServiceHelper
                    .getJsonRepresentationFromResponse(response);
            if (jsonRepresentation != null) {
                JSONValue jsonValue = jsonRepresentation.getJsonObject().get("data"); //$NON-NLS-1$
                callback.onSuccess(jsonValue.isArray());
            }

        }
    });
    client.request(MediaType.APPLICATION_JSON);
}