Example usage for com.google.gwt.http.client RequestBuilder sendRequest

List of usage examples for com.google.gwt.http.client RequestBuilder sendRequest

Introduction

In this page you can find the example usage for com.google.gwt.http.client RequestBuilder sendRequest.

Prototype

public Request sendRequest(String requestData, RequestCallback callback) throws RequestException 

Source Link

Document

Sends an HTTP request based on the current builder configuration with the specified data and callback.

Usage

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

protected void addUser() {
    String requestURL = "org.freemedsoftware.api.UserInterface.add";
    HashMap<String, String> hm = new HashMap<String, String>();
    if (userId != null) {
        hm.put("id", userId.toString());
        requestURL = "org.freemedsoftware.api.UserInterface.mod";
    }/* ww  w .j  a  va2 s  .co m*/

    hm.put("username", tbUsername.getText());
    if (tbPassword.isEnabled())
        hm.put("userpassword", tbPassword.getText());

    hm.put("userfname", tbUserFirstName.getText());
    hm.put("userlname", tbUserLastName.getText());
    hm.put("usermname", tbUserMiddleName.getText());
    hm.put("usertitle", tbUserTitle.getWidgetValue());
    hm.put("userdescrip", tbDescription.getText());
    String usertype = lbUserType.getValue(lbUserType.getSelectedIndex());
    hm.put("usertype", usertype);
    if (usertype == "phy") {
        hm.put("userrealphy", lbActualPhysician.getValue().toString());
    }

    String userfac = "";
    Iterator<Integer> itr = facilitiesCheckBoxesMap.keySet().iterator();
    while (itr.hasNext()) {
        Integer id = itr.next();
        CheckBox checkBox = facilitiesCheckBoxesMap.get(id);
        if (checkBox.getValue())
            userfac = userfac + id.toString() + ",";
    }
    hm.put("userfac", userfac);
    String useracl = "";
    itr = aclSelectedGroupsIdsList.iterator();
    while (itr.hasNext()) {
        useracl = useracl + itr.next().toString();
        if (itr.hasNext())
            useracl = useracl + ",";
    }
    hm.put("useracl", useracl);

    List paramsList = new ArrayList();
    paramsList.add(JsonUtil.jsonify(hm));

    if (customizePermissionsTable.isVisible()) {
        calculateBlockedAndAllowedACLSections();
        if (blockedPermissionsMap.size() > 0)
            paramsList.add(JsonUtil.jsonify(blockedPermissionsMap));
        else
            paramsList.add("");
        if (allowedPermissionsMap.size() > 0)
            paramsList.add(JsonUtil.jsonify(allowedPermissionsMap));
        else
            paramsList.add("");
    }
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Do nothing.
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        String[] params = (String[]) paramsList.toArray(new String[0]);
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest(requestURL, params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    Util.showErrorMsg("UserManagementScreen", _("Failed to add user."));
                }

                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        Integer r = (Integer) JsonUtil.shoehornJson(JSONParser.parseStrict(response.getText()),
                                "Integer");
                        if (r != null) {
                            Util.showInfoMsg("UserManagementScreen", _("Successfully added user."));
                            retrieveAllUsers();
                            clearForm();
                        } else {
                            Boolean b = (Boolean) JsonUtil
                                    .shoehornJson(JSONParser.parseStrict(response.getText()), "Boolean");
                            if (b) {
                                Util.showInfoMsg("UserManagementScreen", _("Successfully modified user."));
                                retrieveAllUsers();
                                clearForm();
                            }
                        }
                    } else {
                        Util.showErrorMsg("UserManagementScreen", _("Failed to add user."));
                    }
                }
            });
        } catch (RequestException e) {
            Util.showErrorMsg("UserManagementScreen", _("Failed to add user."));
        }
    } else {
        // TODO: Create GWT-RPC stuff here
    }

}

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

protected void deleteUser() {
    if (true) {//ww w .  jav  a  2s .  com
        if (Util.getProgramMode() == ProgramMode.STUBBED) {
            // Do nothing.
        } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
            String[] params = { JsonUtil.jsonify(userId) };
            RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                    URL.encode(Util.getJsonRequest("org.freemedsoftware.api.UserInterface.del", params)));
            try {
                builder.sendRequest(null, new RequestCallback() {
                    public void onError(Request request, Throwable ex) {
                        Util.showErrorMsg("UserManagementScreen", _("Failed to delete user."));
                    }

                    public void onResponseReceived(Request request, Response response) {
                        if (200 == response.getStatusCode()) {
                            Boolean flag = (Boolean) JsonUtil
                                    .shoehornJson(JSONParser.parseStrict(response.getText()), "Boolean");
                            if (flag) {
                                Util.showInfoMsg("UserManagementScreen", _("Successfully deleted user."));
                                retrieveAllUsers();
                                clearForm();
                            }
                        } else {
                            Util.showErrorMsg("UserManagementScreen", _("Failed to delete user."));
                        }
                    }
                });
            } catch (RequestException e) {
                Util.showErrorMsg("UserManagementScreen", _("Failed to delete user."));
            }
        } else {
            // TODO: Create GWT-RPC stuff here
        }

    }
}

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

public void retrieveAllUsers() {
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Do nothing
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // Use JSON-RPC to retrieve the data
        String[] params = {};//  w w  w .  jav  a  2s  . com

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest("org.freemedsoftware.api.UserInterface.GetRecords", params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    GWT.log(request.toString(), ex);
                }

                @SuppressWarnings("unchecked")
                public void onResponseReceived(Request request, Response response) {
                    if (response.getStatusCode() == 200) {
                        HashMap<String, String>[] data = (HashMap<String, String>[]) JsonUtil.shoehornJson(
                                JSONParser.parseStrict(response.getText()), "HashMap<String,String>[]");
                        if (data != null) {
                            wUsers.clearData();
                            wUsers.loadData(data);
                        }
                    }
                }
            });
        } catch (RequestException e) {
            // nothing here right now
        }
    } else if (Util.getProgramMode() == ProgramMode.NORMAL) {
        // Use GWT-RPC to retrieve the data
        // TODO: Create that stuff
    }

}

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

public void getACLGroups() {
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Do nothing
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // Use JSON-RPC to retrieve the data
        String[] params = {};//from w w w  .j a v a  2 s.  com

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest("org.freemedsoftware.module.ACL.userGroups", params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    GWT.log(request.toString(), ex);
                }

                public void onResponseReceived(Request request, Response response) {
                    if (response.getStatusCode() == 200) {
                        String[][] data = (String[][]) JsonUtil
                                .shoehornJson(JSONParser.parseStrict(response.getText()), "String[][]");
                        if (data != null) {
                            addACLGroup(data);
                        }
                    }
                }
            });
        } catch (RequestException e) {
            // nothing here right now
        }
    } else if (Util.getProgramMode() == ProgramMode.NORMAL) {
        // Use GWT-RPC to retrieve the data
        // TODO: Create that stuff
    }

}

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

public void getUserDetails(final Integer userId) {
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Do nothing
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // Use JSON-RPC to retrieve the data
        String[] params = { JsonUtil.jsonify(userId) };

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest("org.freemedsoftware.api.UserInterface.GetRecord", params)));
        try {/*  ww w.j av a2s . c  o  m*/
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    GWT.log(request.toString(), ex);
                }

                @SuppressWarnings("unchecked")
                public void onResponseReceived(Request request, Response response) {
                    if (response.getStatusCode() == 200) {
                        HashMap<String, String> data = (HashMap<String, String>) JsonUtil.shoehornJson(
                                JSONParser.parseStrict(response.getText()), "HashMap<String,String>");
                        if (data != null) {
                            tbUsername.setText(data.get("username"));
                            tbDescription.setText(data.get("userdescrip"));
                            tbUserFirstName.setText(data.get("userfname"));
                            tbUserLastName.setText(data.get("userlname"));
                            tbUserMiddleName.setText(data.get("usermname"));
                            tbUserTitle.setWidgetValue(data.get("usertitle"));
                            lbUserType.setWidgetValue(data.get("usertype"));
                            if (data.get("usertype") != null && data.get("usertype").equalsIgnoreCase("phy")) {
                                lbActualPhysician.setValue(Integer.parseInt(data.get("userrealphy")));
                                lbActualPhysician.setVisible(true);
                            }

                            if (data.get("userfac") != null && data.get("userfac").length() > 0) {
                                String[] userFacilities = data.get("userfac").split(",");
                                for (int i = 0; i < userFacilities.length; i++) {
                                    Integer id = Integer.parseInt(userFacilities[i]);
                                    CheckBox checkBox = facilitiesCheckBoxesMap.get(id);
                                    if (checkBox != null)
                                        checkBox.setValue(true);
                                }
                            }

                            addUserButton.setText(_("Modify User"));
                            changePasswordLink.setVisible(true);
                            tbPassword.setEnabled(false);
                            tbPasswordverify.setEnabled(false);
                            deleteUserButton.setVisible(true);
                            copyButton.setVisible(true);
                        }
                    }
                }
            });
        } catch (RequestException e) {
            // nothing here right now
        }
    } else if (Util.getProgramMode() == ProgramMode.NORMAL) {
        // Use GWT-RPC to retrieve the data
        // TODO: Create that stuff
    }

}

From source file:org.freemedsoftware.gwt.client.screen.UserManagementScreen.java

License:Open Source License

public void getUserGroup(final Integer userId) {
    aclSelectedGroupsIdsList.clear();/*w  ww . j  ava  2  s.c o  m*/
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Do nothing
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // Use JSON-RPC to retrieve the data
        String[] params = { JsonUtil.jsonify(userId) };

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest("org.freemedsoftware.module.ACL.GetUserGroups", params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    GWT.log(request.toString(), ex);
                }

                public void onResponseReceived(Request request, Response response) {
                    if (response.getStatusCode() == 200) {
                        String[] data = (String[]) JsonUtil
                                .shoehornJson(JSONParser.parseStrict(response.getText()), "String[]");
                        if (data != null) {
                            Integer groupId = null;
                            for (int i = 0; i < data.length; i++) {
                                groupId = Integer.parseInt(data[i]);
                                CheckBox checkBox = aclGroupsCheckBoxesMap.get(groupId);
                                checkBox.setValue(true);
                                aclSelectedGroupsIdsList.add(groupId);
                            }
                        }
                    }
                }
            });
        } catch (RequestException e) {
            // nothing here right now
        }
    } else if (Util.getProgramMode() == ProgramMode.NORMAL) {
        // Use GWT-RPC to retrieve the data
        // TODO: Create that stuff
    }

}

From source file:org.freemedsoftware.gwt.client.SystemNotifications.java

License:Open Source License

/**
 * Asynchronously poll for new system notifications
 *//*from w ww .  j  a  v  a 2 s . c  o  m*/
public boolean poll() {
    JsonUtil.debug("SystemNotifications.poll() called");

    // if (mutexStatus) {
    // JsonUtil.debug("mutexStatus indicates run in progress.");
    // return false;
    // }

    mutexStatus = true;
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // Nothing. Do nothing.
        mutexStatus = false;
        return true;
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // JSON-RPC
        String[] params = { Long.toString(mutexTimestamp) };
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(Util
                .getJsonRequest("org.freemedsoftware.module.SystemNotifications.GetFromTimestamp", params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    Util.showErrorMsg("SystemNotifications", _("Failed to get system notifications."));
                }

                @SuppressWarnings("unchecked")
                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        if (response.getText().compareToIgnoreCase("false") != 0) {
                            if (response.getText().trim().equalsIgnoreCase(AppConstants.INVALID_SESSION)) {
                                Util.logout(new Command() {

                                    @Override
                                    public void execute() {
                                        stop();
                                    }

                                });
                                mutexStatus = false;
                                return;
                            }
                            HashMap<String, String>[] r = (HashMap<String, String>[]) JsonUtil.shoehornJson(
                                    JSONParser.parseStrict(response.getText()), "HashMap<String,String>[]");
                            if (r != null) {
                                if (r.length > 0) {
                                    // Update our working timestamp
                                    try {
                                        mutexTimestamp = Long.parseLong(r[0].get("timestamp"));
                                        JsonUtil.debug("SystemNotifications(): Received timestamp "
                                                + r[0].get("timestamp"));
                                        JsonUtil.debug(
                                                "SystemNotifications(): Parsed timestamp as " + mutexTimestamp);
                                    } catch (Exception ex) {
                                        JsonUtil.debug(
                                                "SystemNotifications(): Exception parsing mutex timestamp"
                                                        + r[0].get("timestamp"));
                                    }
                                    for (int iter = 0; iter < r.length; iter++) {
                                        handleNotification(r[iter]);
                                    }
                                }
                            }
                        } else {
                            JsonUtil.debug("SystemNotifications(): Received dummy response from JSON backend");
                        }
                    } else {
                        Util.showErrorMsg("SystemNotifications", _("Failed to get system notifications."));
                    }

                    // Release mutex
                    mutexStatus = false;
                }
            });
        } catch (RequestException e) {
            Util.showErrorMsg("SystemNotifications", _("Failed to get system notifications."));
        }
    } else {
        // GWT-RPC
    }

    return true;
}

From source file:org.freemedsoftware.gwt.client.Util.java

License:Open Source License

public static void login(String username, String password, String location, final CustomCommand whenDone,
        final CustomCommand whenFail) {
    List<String> paramList = new ArrayList<String>();
    paramList.add(username);/*from   ww w . j  av a 2  s  .  c o  m*/
    paramList.add(password);
    if (location != null)
        paramList.add(location);
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
            URL.encode(Util.getJsonRequest("org.freemedsoftware.public.Login.Validate",
                    (String[]) paramList.toArray(new String[0]))));
    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable ex) {
                Window.alert(ex.toString());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    String res = (String) JsonUtil.shoehornJson(JSONParser.parseStrict(response.getText()),
                            "String");
                    if (res == null)
                        res = response.getText().trim();
                    if (res.compareToIgnoreCase("true") == 0) {
                        whenDone.execute(null);
                    } else if (res.compareToIgnoreCase("false") == 0) {
                        whenFail.execute(AppConstants.INVALID_USER);
                    } else if (res.compareToIgnoreCase(AppConstants.NOT_IN_FACILITY) == 0)
                        whenFail.execute(AppConstants.NOT_IN_FACILITY);
                } else {
                    whenFail.execute(AppConstants.INVALID_RESPONSE);
                }
            }
        });
    } catch (RequestException e) {
        whenFail.execute(AppConstants.INVALID_RESPONSE);
    }
}

From source file:org.freemedsoftware.gwt.client.Util.java

License:Open Source License

/**
 * Logout of the system and pop up a login dialog.
 * //from   w  w  w.  ja v  a2 s  .  c  o m
 * @param state
 */
public static void logout(final Command onSuccess) {
    if (Util.getProgramMode() == ProgramMode.STUBBED) {

    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        String[] params = {};
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(Util.getJsonRequest("org.freemedsoftware.public.Login.Logout", params)));
        try {
            builder.sendRequest(null, new RequestCallback() {
                public void onError(com.google.gwt.http.client.Request request, Throwable ex) {
                    GWT.log("Exception", ex);
                    Window.alert(_("Failed to log out."));
                }

                public void onResponseReceived(com.google.gwt.http.client.Request request,
                        com.google.gwt.http.client.Response response) {
                    if (200 == response.getStatusCode()) {
                        // closeAllTabs();
                        if (onSuccess != null)
                            onSuccess.execute();
                        CurrentState.getMainScreen().setVisible(false);
                        UIObject.setVisible(RootPanel.get("loginScreenOuter").getElement(), true);
                        CurrentState.getFreemedInterface().getLoginDialog().center();
                        CurrentState.getFreemedInterface().getLoginDialog().setFocusToPasswordField();
                    } else {
                        Window.alert(_("Failed to log out."));
                    }
                }
            });
        } catch (RequestException e) {
            GWT.log("Exception", e);
            Window.alert("Failed to log out.");
        }

    } else {
        try {
            LoginAsync service = (LoginAsync) Util.getProxy("org.freemedsoftware.gwt.client.Public.Login");
            service.Logout(new AsyncCallback<Void>() {
                public void onSuccess(Void r) {
                    CurrentState.getMainScreen().hide();
                    UIObject.setVisible(RootPanel.get("loginScreenOuter").getElement(), true);
                    CurrentState.getFreemedInterface().getLoginDialog().center();
                }

                public void onFailure(Throwable t) {
                    Window.alert("Failed to log out.");
                }
            });
        } catch (Exception e) {
            Window.alert("Could not create proxy for Login");
        }
    }
}

From source file:org.freemedsoftware.gwt.client.Util.java

License:Open Source License

/**
 * Calls server method//from  www  .j  a va 2 s.c o m
 * 
 * @param package - package name
 * 
 * @param module
 *            - module name
 * 
 * @param method
 *            - method name
 * 
 * @param paramsList
 *            - list of parameters of any type or multi-type
 * 
 * @param requestCallback
 *            - calls its onError & jsonifiedData function on getting
 *            response from server
 * 
 * @param responseType
 *            - type of response e.g
 *            Integer,HashMap<String,String>,String[],String[][] etc
 */
@SuppressWarnings("rawtypes")
private static void callServerMethod(final String packageName, final String className, final String method,
        final List paramsList, final CustomRequestCallback requestCallback, final String responseType,
        Method requestMethod) {
    if (Util.getProgramMode() == ProgramMode.STUBBED) {
        // TODO: STUBBED
    } else if (Util.getProgramMode() == ProgramMode.JSONRPC) {
        // JSON-RPC
        String params = "";
        List<String> paramsStr = new ArrayList<String>();
        if (paramsList != null) {
            int i = 0;
            Iterator iterator = paramsList.iterator();
            while (iterator.hasNext()) {
                Object object = iterator.next();
                if (requestMethod == RequestBuilder.GET)
                    paramsStr.add(JsonUtil.jsonify(object));
                else {
                    if (params.length() > 0)
                        params += "&";
                    params += "param" + i++ + "=" + JsonUtil.jsonify(object);
                }
            }
        }

        String methodURL = packageName + "." + className + "." + method;

        RequestBuilder builder = null;
        if (requestMethod == RequestBuilder.POST) {
            builder = new RequestBuilder(requestMethod, URL.encode(Util.getJsonRequest(methodURL)));
            builder.setHeader("Content-type", "application/x-www-form-urlencoded");
            builder.setHeader("Content-length", params.length() + "");
            builder.setHeader("Connection", "close");
        } else {
            builder = new RequestBuilder(requestMethod,
                    URL.encode(Util.getJsonRequest(methodURL, paramsStr.toArray(new String[0]))));
        }
        try {
            builder.sendRequest(params, new RequestCallback() {
                public void onError(Request request, Throwable ex) {
                    if (requestCallback != null)
                        requestCallback.onError();
                }

                public void onResponseReceived(Request request, Response response) {
                    if (requestCallback != null) {
                        if (200 == response.getStatusCode()) {
                            Object result = JsonUtil.shoehornJson(JSONParser.parseStrict(response.getText()),
                                    responseType);
                            requestCallback.jsonifiedData(result);
                        } else
                            requestCallback.onError();
                    }
                }
            });
        } catch (RequestException e) {
        }
    } else {
        // GWT-RPC
    }
}