SalesModel.SalesWS.java Source code

Java tutorial

Introduction

Here is the source code for SalesModel.SalesWS.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SalesModel;

import connection.DbConnectionManager;
import connection.UrlConnectionManager;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Asus X550ZE
 */
@WebService(serviceName = "SalesWS")
public class SalesWS {

    /**
       * Web service operation
       * @param user_id
       * @return hasSales
       */
    @WebMethod(operationName = "hasSales")
    public boolean hasSales(@WebParam(name = "username") String username) {
        boolean hasSales = false;
        Connection conn = DbConnectionManager.getConnection();
        try {
            String query = "SELECT * FROM purchase WHERE purchase.seller = '" + username + "'";
            Statement stmt = conn.createStatement();
            ResultSet rslt = stmt.executeQuery(query);
            hasSales = rslt.next();

        } catch (SQLException ex) {
            System.out.println("Insert to database failed: An Exception has occurred! " + ex);
        }
        return hasSales;
    }

    /**
     * Web service operation
     * @param access_token
     * @param user_id
     */
    @WebMethod(operationName = "getSales")
    public ArrayList<String> getSales(@WebParam(name = "access_token") String access_token)
            throws IOException, ParseException {
        ArrayList<String> answers = new ArrayList<String>();
        Connection conn = DbConnectionManager.getConnection();
        String targetIS = "ValidateToken";
        String urlParameters = "access_token=" + access_token;
        HttpURLConnection urlConn = UrlConnectionManager.doReqPost(targetIS, urlParameters);
        String resp = UrlConnectionManager.getResponse(urlConn);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(resp);
        String statusResp = (String) obj.get("status");
        String username = (String) obj.get("username");
        switch (statusResp) {
        case "valid":
            if (hasSales(username)) {
                try {
                    String query = "SELECT * FROM purchase WHERE purchase.seller = '" + username
                            + "' ORDER BY purchase.purchase_id DESC ";
                    Statement stmt = conn.createStatement();
                    ResultSet rslt = stmt.executeQuery(query);
                    while (rslt.next()) {
                        int product_id = rslt.getInt("purchase_id");
                        String product_name = rslt.getString("product_name");
                        int product_price = rslt.getInt("product_price");
                        String seller = rslt.getString("seller");
                        String buyer = rslt.getString("buyer");
                        String image = rslt.getString("image");
                        int quantity = rslt.getInt("quantity");
                        String consignee = rslt.getString("consignee");
                        String fulladdress = rslt.getString("fulladdress");
                        int postalcode = rslt.getInt("postalcode");
                        String phonenumber = rslt.getString("newphonenumber");
                        String creditcard = rslt.getString("creditcard");
                        String verification = rslt.getString("verification");
                        Date dateBoughtF = rslt.getDate("datebought");
                        SimpleDateFormat simpledatefo = new SimpleDateFormat("dd/MM/yyyy");
                        Time timeBoughtF = rslt.getTime("timebought");
                        SimpleDateFormat simpletimefo = new SimpleDateFormat("dd/MM/yyyy");
                        String datebought = rslt.getString("datebought");
                        String timebought = rslt.getString("timebought");
                        int totalprice = quantity * product_price;

                        String answer = "<li>" + "<span id='date'>" + "<b>" + datebought + "</b>" + "<br/>" + "at "
                                + timebought + "<hr></hr>" + "</span>" + "<div class='item-list-product'>"
                                + "<div style='position:absolute'>" + "<a href='" + image
                                + "'><img class='img-item' src='" + image + "'></img></a>" + "</div>"
                                + "<div id='product-info' style='width:250px'>" + "<span><b>" + product_name
                                + "</b></span><br/>" + "<span>IDR " + totalprice + "</span> <br/>" + "<span>"
                                + quantity + "pcs</span><br/>" + "<span>@IDR " + product_price + "</span>"
                                + "</div>" + "<div id='eddel' style='font-size:12px;left:380px;width:220px'>"
                                + "<span>Delivery to <b>" + consignee + "</b> </span> <br/>" + "<span>"
                                + fulladdress + "</span> <br/>" + "<span>" + postalcode + "</span><br/>" + "<span>"
                                + phonenumber + "</span>" + "</div>" + "</div>"
                                + "<span style=\"margin-left:120px;font-size:12px\">bought by <b>" + buyer
                                + "</b></span>" + "</li>" + "<br>";
                        answers.add(answer);
                    }
                } catch (SQLException ex) {
                    System.out.println("Insert to database failed: An Exception has occurred! " + ex);
                }
            } else {
                String answer = "<b>You have not sold any product.<b>";
                answers.add(answer);
            }
            break;
        case "non-valid":
            String answer = "2";
            answers.add(answer);
            break;
        default:
            answer = "3";
            answers.add(answer);
            break;
        }
        return answers;
    }

}