private void pesquisar(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String dispatcher;
    String tipoDePesquisa = request.getParameter("tipoDePesquisa");
    String campoDePesquisa = request.getParameter("campoDePesquisa");

    Data dt = new Data();

    ClienteBusiness bancoCliente =
        (ClienteBusiness) request.getServletContext().getAttribute("bancoCliente");

    ArrayList<Cliente> resultado = new ArrayList<Cliente>();
    Cliente resultadoEspecifico = null;

    switch (tipoDePesquisa.toLowerCase()) {
      case "nome":
        resultado = bancoCliente.pesquisarNome(campoDePesquisa);
        dispatcher = "cliente-pesquisar.jsp";
        break;
      case "sexo":
        resultado = bancoCliente.pesquisarSexo(campoDePesquisa);
        dispatcher = "cliente-pesquisar.jsp";
        break;
      case "nascimento":
        resultado = bancoCliente.pesquisarNascimento(dt.returnDateInvertido(campoDePesquisa));
        dispatcher = "cliente-pesquisar.jsp";
        break;
      case "rg":
        resultadoEspecifico = bancoCliente.pesquisarRg(campoDePesquisa);
        if (resultadoEspecifico != null) dispatcher = "cliente-detalhes.jsp";
        else dispatcher = "cliente-pesquisar.jsp";
        break;
      case "cpf":
        resultadoEspecifico = bancoCliente.pesquisarCpf(campoDePesquisa);
        if (resultadoEspecifico != null) dispatcher = "cliente-detalhes.jsp";
        else dispatcher = "cliente-pesquisar.jsp";
        break;
      case "email":
        resultado = bancoCliente.pesquisarEmail(campoDePesquisa);
        dispatcher = "cliente-pesquisar.jsp";
        break;
      case "telefone":
        resultado = bancoCliente.pesquisarTelefone(campoDePesquisa);
        dispatcher = "cliente-pesquisar.jsp";
        break;
      default:
        dispatcher = "cliente.jsp";
        response.sendRedirect(dispatcher);
        break;
    }
    if ((tipoDePesquisa.equalsIgnoreCase("Rg") || tipoDePesquisa.equalsIgnoreCase("Cpf"))
        && resultadoEspecifico != null) {
      request.setAttribute("clienteSelecionado", resultadoEspecifico);
    } else {
      request.setAttribute("resultadoPesquisa", resultado);
      request.setAttribute("listouCliente", Boolean.TRUE);
    }
    request.getRequestDispatcher(dispatcher).forward(request, response);
  }
  private void cadastrar(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String dispatcher = "cliente-detalhes.jsp";
    ;

    String nome = request.getParameter("clienteNome");
    String sexo = request.getParameter("clienteSexo");
    String nascimento = request.getParameter("clienteNascimento");
    String rg = request.getParameter("clienteRG");
    String cpf = request.getParameter("clienteCPF");
    String email = request.getParameter("clienteEmail");
    String telefone = request.getParameter("clienteDDD") + request.getParameter("clienteTelefone");

    Data dt = new Data();

    Cliente novo =
        new Cliente(nome, rg, cpf, sexo, dt.returnDateInvertido(nascimento), email, telefone);

    ClienteBusiness bancoCliente =
        (ClienteBusiness) request.getServletContext().getAttribute("bancoCliente");

    bancoCliente.adicionar(novo);

    request.getServletContext().setAttribute("bancoCliente", bancoCliente);
    request.setAttribute("clienteSelecionado", novo);
    request.setAttribute("cadastrado", Boolean.TRUE);

    request.getRequestDispatcher(dispatcher).forward(request, response);
  }
Example #3
0
 @Override
 public void actionPerformed(ActionEvent e) {
   // TODO Auto-generated method stub
   if (e.getSource() == ui.getMenuItem("getData")) {
     Data.getData();
     ui.change();
   }
 }
Example #4
0
 /**
  * construct an array of Data object from a JSONArray. only used in directly parsing data from
  * server.
  *
  * @param clazz
  * @param jsonArr
  * @return
  */
 public static <E extends Data> E[] from_json_arr(Class<E> clazz, JSONArray jsonArr) {
   @SuppressWarnings("unchecked")
   final E[] rtn = (E[]) Array.newInstance(clazz, jsonArr.length());
   for (int i = 0; i < jsonArr.length(); i++) {
     JSONObject iJson;
     try {
       iJson = jsonArr.getJSONObject(i);
     } catch (JSONException e) {
       Log.w(TAG, "bad json array");
       continue;
     }
     rtn[i] = (E) Data.from_json(iJson);
   }
   return rtn;
 }
Example #5
0
  /**
   * construct a Data Object from a JSON returned by server. only used in directly parsing data from
   * server.
   *
   * @param jObject the JSON returned by server
   * @return the parsed Data object. returns null if parsing fails.
   */
  public static Data from_json(JSONObject jObject) {
    final String TAG = "Data.from_json";
    String className = "";
    Data rtn = null;

    // get class name
    try {
      className = jObject.getString("_className");
    } catch (JSONException e) {
      Log.d(TAG, "_className not found");
      return null;
    }

    // get class from class name
    Class<?> theClass = null;
    try {
      theClass = Class.forName(Data.PACKAGE_PATH + "." + className);
    } catch (ClassNotFoundException e) {
      Log.d(TAG, "class not found");
      return null;
    }

    // construct object
    Object _rtn = null;
    try {
      _rtn = theClass.newInstance();
    } catch (Exception e) {
      Log.d(TAG, "error creating instance");
      return null;
    }

    // verify it is Data and assign _thisPtr
    if (!(_rtn instanceof Data)) {
      Log.d(TAG, "class not an instance of Data");
      return null;
    }

    rtn = (Data) _rtn;
    try {
      rtn._thisPtr = jObject.getString("_thisPtr");
    } catch (JSONException e2) {

    }

    // traverse JSON fields
    Iterator<?> keys = jObject.keys();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      if (key.equals("_className")) continue;
      if (key.equals("_thisPtr")) continue;

      // get field from rtn Data
      Field field = null;
      try {
        field = theClass.getDeclaredField(key);
      } catch (NoSuchFieldException e1) {
        continue;
      } catch (SecurityException e1) {
        Log.d(TAG, "security exception");
        continue;
      }

      // get value from JSON
      Object value = null;
      try {
        value = jObject.get(key);
      } catch (JSONException e) {
        e.printStackTrace();
        continue;
      }

      // set value to rtn Data
      try {
        field.set(rtn, Data.parse_json_value(value));

      } catch (Exception e) {
        Log.e(TAG, "error setting field: " + key);
        Log.e(TAG, "message: " + e.getMessage());
        continue;
      }
    }

    return rtn;
  }