Example usage for org.json JSONException getMessage

List of usage examples for org.json JSONException getMessage

Introduction

In this page you can find the example usage for org.json JSONException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.rapid.actions.Datacopy.java

@Override
public String getJavaScript(RapidRequest rapidRequest, Application application, Page page, Control control,
        JSONObject jsonDetails) {//from w  w w  .j a v  a 2s. c o m

    // the javascript we're making
    String js = "";

    // get the Rapid servlet
    RapidHttpServlet rapidServlet = rapidRequest.getRapidServlet();

    // get any copy type
    String copyType = getProperty("copyType");

    // set to replace if null (for backwards compatibility)
    if (copyType == null)
        copyType = "replace";

    // bulk copy is a special animal
    if ("bulk".equals(copyType)) {

        // check we have some data copies
        if (_dataCopies == null) {

            // data copies not found return a comment
            js = "// data copies not found for " + getId();

        } else {

            // compare against last getData call to avoid recalling
            String lastGetDataFunction = null;

            // loop them
            for (DataCopy dataCopy : _dataCopies) {

                // get the destination id
                String destinationId = dataCopy.getDestination();

                // check we got one
                if (destinationId.length() > 0) {

                    // get the get data function
                    String getDataFunction = Control.getDataJavaScript(rapidServlet.getServletContext(),
                            application, page, dataCopy.getSource(), dataCopy.getSourceField());

                    // add the getData if different from the last one
                    if (!getDataFunction.equals(lastGetDataFunction))
                        js += "var data = " + Control.getDataJavaScript(rapidServlet.getServletContext(),
                                application, page, dataCopy.getSource(), dataCopy.getSourceField()) + ";\n";

                    // remember this one
                    lastGetDataFunction = getDataFunction;

                    // split if by escaped .
                    String idParts[] = destinationId.split("\\.");
                    // if there is more than 1 part we are dealing with set properties, for now just update the destintation id
                    if (idParts.length > 1)
                        destinationId = idParts[0];

                    // first try and look for the control in the page
                    Control destinationControl = page.getControl(destinationId);
                    // assume we found it
                    boolean pageControl = true;
                    // check we got a control
                    if (destinationControl == null) {
                        // now look for the control in the application
                        destinationControl = application.getControl(rapidServlet.getServletContext(),
                                destinationId);
                        // set page control to false
                        pageControl = false;
                    }

                    // check we got one from either location
                    if (destinationControl == null) {

                        // data copies not found return a comment
                        js = "// data destination not found for " + destinationId;

                    } else {

                        // get the field
                        String destinationField = dataCopy.getDestinationField();
                        // clean up the field                        
                        if (destinationField == null)
                            destinationField = "";

                        // get any details we may have
                        String details = destinationControl.getDetailsJavaScript(application, page);

                        // if the idParts is greater then 1 this is a set property
                        if (idParts.length > 1) {

                            // if we have some details
                            if (details != null) {
                                // if this is a page control
                                if (pageControl) {
                                    // the details will already be in the page so we can use the short form
                                    details = destinationControl.getId() + "details";
                                }
                            }

                            // get the property from the second id part
                            String property = idParts[1];
                            // append the set property call
                            js += "setProperty_" + destinationControl.getType() + "_" + property + "(ev, '"
                                    + destinationControl.getId() + "', '" + destinationField + "', " + details
                                    + ", data, " + Boolean.parseBoolean(getProperty("changeEvents")) + ");\n";

                        } else {

                            // set details to empty string or clean up
                            if (details == null) {
                                details = "";
                            } else {
                                // if this is a page control
                                if (pageControl) {
                                    // the details will already be in the page so we can use the short form
                                    details = ",details:" + destinationControl.getId() + "details";
                                } else {
                                    // write the full details
                                    details = ",details:" + details;
                                }
                            }

                            // try and get the type
                            String type = dataCopy.getType();
                            // check it
                            if (type == null || "false".equals(type)) {
                                // update to empty string
                                type = "";
                            } else {
                                // update to comma-prefixed, string escaped
                                type = ",'" + type + "'";
                            }

                            // do the data copy
                            js += "Action_datacopy(ev, data, [{id:'" + destinationControl.getId() + "',type: '"
                                    + destinationControl.getType() + "',field:'" + destinationField + "'"
                                    + details + "}], " + Boolean.parseBoolean(getProperty("changeEvents"))
                                    + type + ");\n";

                        } // copy / set property check

                    } // destination control check            

                } // destination id check

            } // data copies loop

        } // data copies check

    } else {

        // get the data source
        String dataSourceId = getProperty("dataSource");

        // check there is a datasource
        if (dataSourceId == null) {

            // no data source return a comment
            js = "// no data source for action " + getId();

        } else {

            String dataSourceField = getProperty("dataSourceField");

            js = "var data = " + Control.getDataJavaScript(rapidServlet.getServletContext(), application, page,
                    dataSourceId, dataSourceField) + ";\n";

            // we're going to work with the data destinations in a json array
            JSONArray jsonDataDestinations = null;

            // assume we have no need for an outputs array
            boolean outputsArray = false;

            // try and get the data destinations from the properties into a json array, silently fail if not
            try {
                jsonDataDestinations = new JSONArray(getProperty("dataDestinations"));
            } catch (Exception e) {
            }

            if (jsonDataDestinations == null) {

                // data source destinations not found return a comment
                js = "// data source destinations not found for " + getId();

            } else {

                // prepare a string for the outputs array
                String jsOutputs = "";
                // loop the json data destination collection
                for (int i = 0; i < jsonDataDestinations.length(); i++) {

                    // try and make an output for this destination
                    try {

                        // retrieve this data destination
                        JSONObject jsonDataDesintation = jsonDataDestinations.getJSONObject(i);
                        // get the control id
                        String destinationId = jsonDataDesintation.getString("itemId");

                        // split by escaped .
                        String idParts[] = destinationId.split("\\.");
                        // if there is more than 1 part we are dealing with set properties, for now just update the destintation id
                        if (idParts.length > 1)
                            destinationId = idParts[0];

                        // first try and look for the control in the page
                        Control destinationControl = page.getControl(destinationId);
                        // assume we found it
                        boolean pageControl = true;
                        // check we got a control
                        if (destinationControl == null) {
                            // now look for the control in the application
                            destinationControl = application.getControl(rapidServlet.getServletContext(),
                                    destinationId);
                            // set page control to false
                            pageControl = false;
                        }

                        // check we got one from either location
                        if (destinationControl != null) {

                            // get the field
                            String destinationField = jsonDataDesintation.optString("field");
                            // clean up the field                        
                            if (destinationField == null)
                                destinationField = "";

                            // get any details we may have
                            String details = destinationControl.getDetailsJavaScript(application, page);

                            // if the idParts is greater then 1 this is a set property
                            if (idParts.length > 1) {

                                // if we have some details
                                if (details != null) {
                                    // if this is a page control
                                    if (pageControl) {
                                        // the details will already be in the page so we can use the short form
                                        details = destinationControl.getId() + "details";
                                    }
                                }

                                // get the property from the second id part
                                String property = idParts[1];
                                // append the set property call
                                js += "setProperty_" + destinationControl.getType() + "_" + property + "(ev,'"
                                        + destinationControl.getId() + "','" + destinationField + "'," + details
                                        + ",data);\n";

                            } else {

                                // we will need an outputs array
                                outputsArray = true;

                                // set details to empty string or clean up
                                if (details == null) {
                                    details = "";
                                } else {
                                    // if this is a page control
                                    if (pageControl) {
                                        // the details will already be in the page so we can use the short form
                                        details = ",details:" + destinationControl.getId() + "details";
                                    } else {
                                        // write the full details
                                        details = ",details:" + details;
                                    }
                                }

                                // add the properties we need as a js object it will go into the array
                                jsOutputs += "{id:'" + destinationControl.getId() + "',type: '"
                                        + destinationControl.getType() + "',field:'" + destinationField + "'"
                                        + details + "},";

                            }

                        }

                    } catch (JSONException ex) {

                        // data source destinations not found return a comment
                        js = "// error creating data output for " + getId() + " : " + ex.getMessage();

                    }
                }

                // if there was an outputs array 
                if (outputsArray) {

                    // trim the last comma
                    if (jsOutputs.length() > 0)
                        jsOutputs = jsOutputs.substring(0, jsOutputs.length() - 1);

                    // add to js as an array
                    js += "var outputs = [" + jsOutputs + "];\n";
                    // add the start of the call
                    js += "Action_datacopy(ev, data, outputs, "
                            + Boolean.parseBoolean(getProperty("changeEvents"));

                    // add the copy type to the js
                    js += ", '" + copyType + "'";
                    // check the copy type
                    if ("child".equals(copyType)) {
                        // no merge data object
                        js += ", null";
                        // look for a merge field
                        String childField = getProperty("childField");
                        // check if got we got one
                        if (childField == null) {
                            // call it child if not
                            js += ", 'child'";
                        } else {
                            // add it
                            js += ", '" + childField + "'";
                        }

                    } else if ("search".equals(copyType)) {

                        // get the search data
                        js += ", " + Control.getDataJavaScript(rapidServlet.getServletContext(), application,
                                page, getProperty("searchSource"), getProperty("searchSourceField"));
                        // look for a search field
                        String searchField = getProperty("searchField");
                        // add it if present
                        if (searchField != null)
                            js += ", '" + searchField + "'";
                        // look for a maxRows field
                        String maxRows = getProperty("maxRows");
                        // add it if present
                        if (maxRows != null)
                            js += "," + maxRows;

                    } else if ("trans".equals(copyType)) {

                        // assume the key fields are null
                        String keyFields = "null";

                        // try and fetch the key fields
                        try {
                            JSONArray jsonKeyFields = new JSONArray(getProperty("keyFields"));
                            keyFields = jsonKeyFields.toString();
                        } catch (JSONException ex) {
                            keyFields = "null /*" + ex.getMessage() + "*/";
                        }

                        // assume the ignore fields are null
                        String ignoreFields = "null";

                        // try and fetch the ignore fields, show message if issue
                        try {
                            JSONArray jsonIgnoreFields = new JSONArray(getProperty("ignoreFields"));
                            ignoreFields = jsonIgnoreFields.toString();
                        } catch (JSONException ex) {
                            ignoreFields = "null /*" + ex.getMessage() + "*/";
                        }

                        // add the details
                        js += ", null, null, {keyFields:" + keyFields + ",ignoreFields:" + ignoreFields + "}";

                    } // copy type

                    // close the copy data call
                    js += ");\n";

                } // outputs array

            } // data destinations check

        } // data source check

    } // bulk copy check

    return js;

}

From source file:org.rapidandroid.tests.ContentBootstrapTests.java

/**
 * @param forms//from w w w .  j av a  2s.  com
 * @param fieldToFormHash
 * @param allforms
 */
private void parseForms(String forms, HashMap<Integer, Vector<Field>> fieldToFormHash, Vector<Form> allforms) {
    // ok, let's get the forms
    try {
        JSONArray formarray = new JSONArray(forms);
        int arrlength = formarray.length();
        for (int i = 0; i < arrlength; i++) {
            try {
                JSONObject obj = formarray.getJSONObject(i);

                if (!obj.getString("model").equals("rapidandroid.form")) {
                    assertTrue(false);
                }

                int pk = obj.getInt("pk");
                Integer pkInt = Integer.valueOf(pk);
                JSONObject jsonfields = obj.getJSONObject("fields");

                // public Form(int id, String name, String prefix, String
                // desc, String parsetype, Field[] fields)

                //               int formcount = 0;
                Field[] fieldarr = new Field[fieldToFormHash.get(pkInt).size()];
                for (int q = 0; q < fieldarr.length; q++) {
                    fieldarr[q] = fieldToFormHash.get(pkInt).get(q);
                }
                Form newform = new Form(pk, jsonfields.getString("formname"), jsonfields.getString("prefix"),
                        jsonfields.getString("description"), fieldarr, ParserType.SIMPLEREGEX);
                allforms.add(newform);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.d("dimagi", e.getMessage());
                assertTrue(false);
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.d("testBootstrapForms.formsouter", e.getMessage());
        assertTrue(false);
    }
}

From source file:org.rapidandroid.tests.ContentBootstrapTests.java

/**
 * @param fields//from  ww w  . jav  a 2  s .  c o m
 * @param typeHash
 * @param fieldToFormHash
 */
private void parseFields(String fields, HashMap<Integer, SimpleFieldType> typeHash,
        HashMap<Integer, Vector<Field>> fieldToFormHash) {
    // ok, let's get the fields
    try {
        JSONArray fieldsarray = new JSONArray(fields);
        int arrlength = fieldsarray.length();
        for (int i = 0; i < arrlength; i++) {
            try {
                JSONObject obj = fieldsarray.getJSONObject(i);

                if (!obj.getString("model").equals("rapidandroid.field")) {
                    assertTrue(false);
                }

                int pk = obj.getInt("pk");

                JSONObject jsonfields = obj.getJSONObject("fields");
                int form_id = jsonfields.getInt("form");
                // public Field(int id, int sequence, String name, String
                // prompt, SimpleFieldType ftype) {
                Field newfield = new Field(pk, jsonfields.getInt("sequence"), jsonfields.getString("name"),
                        jsonfields.getString("prompt"),
                        typeHash.get(Integer.valueOf(jsonfields.getInt("fieldtype"))));

                // fieldHash.put(new Integer(pk), newfield);
                Integer formInt = Integer.valueOf(form_id);
                if (!fieldToFormHash.containsKey(formInt)) {
                    fieldToFormHash.put(formInt, new Vector<Field>());
                    Log.d("dimagi", "### adding a key again?!" + formInt);
                }
                fieldToFormHash.get(formInt).add(newfield);
                Log.d("dimagi", "#### Parsed field: " + newfield.getFieldId() + " [" + newfield.getName()
                        + "] newlength: " + fieldToFormHash.get(formInt).size());

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.d("dimagi", e.getMessage());
                assertTrue(false);
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.d("dimagi", e.getMessage());
        assertTrue(false);
    }
}

From source file:com.example.test38.ReadiumJSApi.java

public void openBook(Package pckg, ViewerSettings viewerSettings, OpenPageRequest openPageRequestData) {
    JSONObject openBookData = new JSONObject();
    try {//from  w w  w  . j av  a  2  s.c  om
        openBookData.put("package", pckg.toJSON());
        openBookData.put("settings", viewerSettings.toJSON());
        openBookData.put("openPageRequest", openPageRequestData.toJSON());
    } catch (JSONException e) {
        Log.e(TAG, "" + e.getMessage(), e);
    }
    loadJSOnReady("ReadiumSDK.reader.openBook(" + openBookData.toString() + ");");
}

From source file:com.example.test38.ReadiumJSApi.java

public void updateSettings(ViewerSettings viewerSettings) {
    try {//  w w  w  .  j a  v a  2s .c om
        loadJSOnReady("ReadiumSDK.reader.updateSettings(" + viewerSettings.toJSON().toString() + ");");
    } catch (JSONException e) {
        Log.e(TAG, "" + e.getMessage(), e);
    }
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public JSONObjectWrapper getJSONObject(String name) {
    if (jsonObject.has(name)) {
        try {// w ww  . ja v a 2  s . c om
            return new JSONObjectWrapper(jsonObject.getJSONObject(name));
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return null;
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public JSONArray getJSONArray(String name) {
    if (jsonObject.has(name)) {
        try {/*w  w w .j av  a2 s  .  c o  m*/
            return jsonObject.getJSONArray(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return null;
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public long getLong(String name) {
    if (jsonObject.has(name)) {
        try {/*w  ww.j a  v a  2 s  .c  om*/
            return jsonObject.getLong(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return ERROR;
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public int getInt(String name, int defaultValue) {
    if (jsonObject.has(name)) {
        try {/*from  ww  w . j  av  a2  s .  c  o  m*/
            return jsonObject.getInt(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return defaultValue;
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public double getDouble(String name) {
    if (jsonObject.has(name)) {
        try {/*from  ww w .j  av a  2 s.com*/
            return jsonObject.getDouble(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return ERROR;
}