示例#1
0
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    /** port */
    Gson gson = new Gson();
    String json = null;
    JsonParser jsonParser = new JsonParser();

    if (request.getParameter("action").equals("add")) {
      BarangBean barang =
          new BarangBean(
              0,
              Integer.parseInt(request.getParameter("category")),
              request.getParameter("name"),
              null,
              Integer.parseInt(request.getParameter("price")),
              request.getParameter("description"),
              Integer.parseInt(request.getParameter("amount")));
      int id = 0;
      URL filename = null;
      InsertBarangProxy insbp = new InsertBarangProxy();
      insbp.insertBarang(
          String.valueOf(barang.getId_category()),
          barang.getName(),
          barang.getPicture(),
          String.valueOf(barang.getPrice()),
          barang.getDescription(),
          String.valueOf(request.getParameter("amount")));
      try {
        String temp =
            "http://coba-soap.ap01.aws.af.cm/BS/barang/select?nama="
                + barang.getName()
                + "&idKat="
                + barang.getId_category()
                + "&keterangan="
                + barang.getDescription()
                + "&harga="
                + barang.getPrice()
                + "&jumlah="
                + barang.getTotal_item()
                + "&gambar="
                + barang.getPicture();
        temp = temp.replace(" ", "%20");
        System.out.println(temp);
        json = WebServicesKit.readUrl(temp);
        JsonArray barangArray = jsonParser.parse(json).getAsJsonArray();
        ArrayList<Barang> barangList = new ArrayList<Barang>();
        for (JsonElement barangss : barangArray) {
          Barang barangObj = gson.fromJson(barangss, Barang.class);
          barangList.add(barangObj);
        }
        if (barangList.get(0) != null) {
          id = (int) barangList.get(0).getId();
          filename = getServletContext().getResource("/img/barang/1.jpg");
        }

      } catch (Exception e) {
        e.printStackTrace();
      }

      /** port */

      /** old */
      // Gson gson = new Gson();
      // String json = null;
      // JsonParser jsonParser = new JsonParser();
      // DbConnection dbConnection = new DbConnection();
      // Connection connection = dbConnection.mySqlConnection();
      // if (request.getParameter("action").equals("add")) {
      // Barang barang = new Barang(0, Integer.parseInt(request
      // .getParameter("category")), request.getParameter("name"),
      // null, Integer.parseInt(request.getParameter("price")),
      // request.getParameter("description"),
      // Integer.parseInt(request.getParameter("amount")));
      //
      // System.out.println("SR0");
      // try {
      // json = WebServicesKit
      // .readUrl("http://coba-soap.ap01.aws.af.cm/BS/barang/insert?idKat="
      // + barang.getId_category()
      // + "&nama="
      // + barang.getName()
      // + "&gambar="
      // + barang.getPicture()
      // + "&harga="
      // + barang.getPrice()
      // + "&keterangan="
      // + barang.getDescription()
      // + "&jumlah="
      // + barang.getTotal_item());
      // filename = getServletContext().getResource("/img/barang/1.jpg");
      // } catch (Exception e1) {
      // // TODO Auto-generated catch block
      // e1.printStackTrace();
      // }
      // int id = jsonParser.parse(json).getAsInt();
      /** old */

      /* upload */
      System.out.println(filename.toString() + "1");
      Part filePart = request.getPart("photo");
      System.out.println(filename.toString() + "2");
      String fileName = getFileName(filePart);
      System.out.println(filename.toString() + "3");

      String[] sp = fileName.toString().split("\\.");
      sp[1] = sp[1].toLowerCase();
      OutputStream out =
          new FileOutputStream(
              new File(request.getRealPath("") + "\\img\\barang\\" + id + "." + sp[1]));
      InputStream filecontent = filePart.getInputStream();

      int read = 0;
      final byte[] bytes = new byte[1024];

      while ((read = filecontent.read(bytes)) != -1) {
        out.write(bytes, 0, read);
      }
      if (out != null) {
        out.close();
      }
      if (filecontent != null) {
        filecontent.close();
      }
      /* end of upload */
      try {
        json =
            WebServicesKit.readUrl(
                "http://coba-soap.ap01.aws.af.cm/BS/barang/update?id="
                    + id
                    + "&gambar="
                    + id
                    + "."
                    + sp[1]);
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      int success = jsonParser.parse(json).getAsInt();
      if (success > 0) {
        response.sendRedirect("admin?action=main&result=success");
      } else {
        response.sendRedirect("admin?action=main&result=unsuccess");
      }
    } else if (request.getParameter("action").equals("update")) {
      System.out.println(request.getParameter("description"));
      Barang barang =
          new Barang(
              Integer.parseInt(request.getParameter("id")),
              Integer.parseInt(request.getParameter("category")),
              request.getParameter("name"),
              null,
              Integer.parseInt(request.getParameter("price")),
              request.getParameter("description"),
              Integer.parseInt(request.getParameter("amount")));
      try {
        json =
            WebServicesKit.readUrl(
                "http://coba-soap.ap01.aws.af.cm/BS/barang/update?id="
                    + barang.getId()
                    + "&idKat="
                    + barang.getId_category()
                    + "&nama="
                    + barang.getName()
                    + "&harga="
                    + barang.getPrice()
                    + "&keterangan="
                    + barang.getDescription()
                    + "&jumlah="
                    + barang.getTotal_item());
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      int success = jsonParser.parse(json).getAsInt();
      PrintWriter out = response.getWriter();
      if (success < 1) {
        out.print("not");
      } else {
        out.print("success");
      }
    } else if (request.getParameter("action").equals("delete")) {
      try {
        json =
            WebServicesKit.readUrl(
                "http://coba-soap.ap01.aws.af.cm/BS/barang/delete?id="
                    + request.getParameter("id"));
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    } else if (request.getParameter("action").equals("pic")) {
      String id = request.getParameter("id");
      String old = null;
      try {
        json = WebServicesKit.readUrl("http://coba-soap.ap01.aws.af.cm/BS/barang/select?id=" + id);
        JsonArray barangArray = jsonParser.parse(json).getAsJsonArray();
        ArrayList<Barang> barangs = new ArrayList<Barang>();
        for (JsonElement barang : barangArray) {
          Barang barangObj = gson.fromJson(barang, Barang.class);
          barangs.add(barangObj);
          old = barangObj.getPicture();
        }
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      System.out.println(old);
      Part filePart = request.getPart("photo");
      String fileName = getFileName(filePart);
      String[] sp = fileName.toString().split("\\.");
      sp[1] = sp[1].toLowerCase();
      if (old != "null") {
        old = request.getRealPath("") + "\\img\\barang\\" + old;
      } else {
        old = request.getRealPath("") + "\\img\\barang\\" + id + "." + sp[1];
      }
      // OutputStream out = new FileOutputStream(new File(UPLOAD_DIRECTORY
      // + File.separator + id + "." + sp[1]));
      OutputStream out2 = new FileOutputStream(new File(old));
      InputStream filecontent = filePart.getInputStream();

      int read = 0;
      final byte[] bytes = new byte[1024];

      while ((read = filecontent.read(bytes)) != -1) {
        // out.write(bytes, 0, read);
        out2.write(bytes, 0, read);
      }
      /*
       * if (out != null) { out.close(); }
       */
      if (out2 != null) {
        out2.close();
      }
      if (filecontent != null) {
        filecontent.close();
      }
      /* end of upload */
      try {
        json =
            WebServicesKit.readUrl(
                "http://coba-soap.ap01.aws.af.cm/BS/barang/update?id="
                    + id
                    + "&gambar="
                    + id
                    + "."
                    + sp[1]);
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      int success = jsonParser.parse(json).getAsInt();
      response.sendRedirect("close.jsp");
    }
  }
示例#2
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    HttpSession session = request.getSession(true);
    RequestDispatcher dispatcher = null;

    if (session.getAttribute("role") == null) {
      response.sendRedirect("register");
    } else if (session.getAttribute("role").equals("1")) {
      DbConnection dbConnection = new DbConnection();
      if (request.getParameter("action").equals("edit")) {
        String category = request.getParameter("category");

        /** port */
        try {
          String json =
              WebServicesKit.readUrl(
                  "http://coba-soap.ap01.aws.af.cm/CategoryService/categoryservice/categories");
          Gson gson = new Gson();
          JsonParser jsonParser = new JsonParser();
          JsonArray categoryArray = jsonParser.parse(json).getAsJsonArray();
          ArrayList<KategoriBean> kategoris = new ArrayList<KategoriBean>();
          for (JsonElement categ : categoryArray) {
            KategoriBean kategObj = gson.fromJson(categ, KategoriBean.class);
            System.out.println("debug admin barang=>" + kategObj.getName());
            kategoris.add(kategObj);
          }

          /** port */

          /** old */
          // String selectCQuery = "SELECT * FROM kategori";
          // try {
          // Statement cstmt = connection.createStatement();
          // ResultSet rsc = cstmt.executeQuery(selectCQuery);
          // ArrayList<KategoriBean> kategoris = new
          // ArrayList<KategoriBean>();
          // while (rsc.next()) {
          // kategoris.add(new KategoriBean(rsc.getInt("id"),
          // rsc.getString("nama")));
          // }
          /** old */
          try {
            json =
                WebServicesKit.readUrl(
                    "http://coba-soap.ap01.aws.af.cm/BS/barang/select?idkat=" + category);
          } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
          JsonArray barangArray = jsonParser.parse(json).getAsJsonArray();
          ArrayList<Barang> barangs = new ArrayList<Barang>();
          for (JsonElement barang : barangArray) {
            Barang barangObj = gson.fromJson(barang, Barang.class);
            barangs.add(barangObj);
          }
          request.setAttribute("kategoris", kategoris);
          request.setAttribute("barangs", barangs);
        } catch (SQLException e) {
          e.printStackTrace();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        dispatcher = getServletContext().getRequestDispatcher("/adminedit.jsp");
      } else if (request.getParameter("action").equals("add")) {
        dispatcher = getServletContext().getRequestDispatcher("/adminadd.jsp");
      } else if (request.getParameter("action").equals("main")) {
        dispatcher = getServletContext().getRequestDispatcher("/adminmain.jsp");
      } else if (request.getParameter("action").equals("pic")) {
        request.setAttribute("id", request.getParameter("id"));
        dispatcher = getServletContext().getRequestDispatcher("/adminpic.jsp");
      }
      dispatcher.forward(request, response);
    } else if (session.getAttribute("role").equals("0")) {
      response.sendRedirect("Index");
    }
  }
示例#3
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    HttpSession session = request.getSession();

    if (session.getAttribute("username") == null) {
      response.sendRedirect("../register");
    } else {
      DbConnection dbConnection = new DbConnection();
      Connection connection = dbConnection.mySqlConnection();

      // String query = "SELECT * FROM barang_user WHERE id_user="******"user_id") + " AND status=0";

      try {
        // ResultSet rs =
        // connection.createStatement().executeQuery(query);

        json =
            ServiceParser.readUrl(
                ServiceParser.BASE_URL
                    + "BarangUserService/baranguserService/statuszero?id="
                    + session.getAttribute("user_id"));

        List<BarangUserBean> allResults =
            ServiceParser.parseJsonToGenericlist(json, BarangUserBean.class); // new
        // ArrayList<BarangUserBean>();

        // ArrayList<BarangUserBean> allResults = new
        // ArrayList<BarangUserBean>();

        // while (rs.next()) {
        // BarangUserBean barangUser = new BarangUserBean(
        // Integer.valueOf(rs.getString("id")),
        // Integer.valueOf(rs.getString("id_barang")),
        // Integer.valueOf(rs.getString("id_user")),
        // Integer.valueOf(rs.getString("status")),
        // Integer.valueOf(rs.getString("jumlah_barang")),
        // rs.getString("deskripsi_tambahan"));
        // allResults.add(barangUser);
        // }
        json = WebServicesKit.readUrl("http://coba-soap.ap01.aws.af.cm/BS/barang/select");
        JsonArray barangArray = jsonParser.parse(json).getAsJsonArray();
        ArrayList<Barang> barangList = new ArrayList<Barang>();
        for (JsonElement barang : barangArray) {
          Barang barangObj = gson.fromJson(barang, Barang.class);
          barangList.add(barangObj);
        }
        request.setAttribute("items", barangList);

        request.setAttribute("user_items", allResults);

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/barang/cart.jsp");
        dispatcher.forward(request, response);

      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }