From 8ddbbc8f4ebc88a2bfcd2c71484c6b5e8a08b069 Mon Sep 17 00:00:00 2001 From: Diki Ardian <ardiandiki369@gmail.com> Date: Tue, 7 Nov 2017 17:22:24 +0700 Subject: [PATCH] add history sql query & tested --- .../informatika/ojek/webservice/History.java | 110 +++++++++++++----- .../informatika/ojek/webservice/IHistory.java | 2 +- .../ojek/webservice/Transaction.java | 19 +++ .../com/informatika/ojek/webservice/test.java | 33 ++---- 4 files changed, 114 insertions(+), 50 deletions(-) diff --git a/WebService/src/com/informatika/ojek/webservice/History.java b/WebService/src/com/informatika/ojek/webservice/History.java index 8c8d518..792415b 100644 --- a/WebService/src/com/informatika/ojek/webservice/History.java +++ b/WebService/src/com/informatika/ojek/webservice/History.java @@ -1,7 +1,10 @@ package com.informatika.ojek.webservice; import javax.jws.WebService; +import java.sql.*; +import java.util.ArrayList; import java.util.Date; +import java.util.List; @WebService(endpointInterface = "com.informatika.ojek.webservice.IHistory") public class History implements IHistory { @@ -10,18 +13,39 @@ public class History implements IHistory { boolean isTokenValid = true; //request identity service int id = 1; //request identity service if (isTokenValid) { - //QUERY getTransaction idDriver = id - int idTransaction = 1; - int idPenumpang = 1; - String lokasiAwal = "a"; - String lokasiTujuan = "b"; - int rating = 5; - String comment = "no comment"; - boolean isHide = true; - Date datePosted = new Date(2017, 7, 10); - boolean isDriver = true; - Transaction trx = new Transaction(idTransaction, id, idPenumpang, lokasiAwal, lokasiTujuan, rating, comment, isHide, datePosted); - return new Transaction[]{trx}; + List<Transaction> result = new ArrayList<>(); + try { + String query = "SELECT * FROM transaction WHERE IDDriver=" + id + ";"; + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost:3306/projek"; + Connection conn = DriverManager.getConnection(url, "root", ""); + Statement stmt = conn.createStatement(); + ResultSet rs; + rs = stmt.executeQuery(query); + while (rs.next()) { + boolean isHide; + if (rs.getInt("IsHide") == 1) { + isHide = true; + } else { + isHide = false; + } + Transaction trx = new Transaction(rs.getInt("IDTransaksi"), + rs.getInt("IDDriver"), + rs.getInt("IDPenumpang"), + rs.getString("LokasiAwal"), + rs.getString("LokasiTujuan"), + rs.getInt("Rating"), + rs.getString("Comment"), + isHide, + rs.getDate("DatePosted")); + result.add(trx); + } + conn.close(); + + } catch (IllegalAccessException | InstantiationException | SQLException | ClassNotFoundException e) { + e.printStackTrace(); + } + return result.toArray(new Transaction[0]); } else { return null; } @@ -30,20 +54,40 @@ public class History implements IHistory { @Override public Transaction[] getPenumpangHistory(String accessToken) { boolean isTokenValid = true; //request identity service - int id = 1; //request identity service + int id = 2; //request identity service if (isTokenValid) { - //QUERY getTransaction idPenumpang = id - int idTransaction = 1; - int idDriver = 1; - String lokasiAwal = "a"; - String lokasiTujuan = "b"; - int rating = 5; - String comment = "no comment"; - boolean isHide = true; - Date datePosted = new Date(2017, 7, 10); - boolean isDriver = true; - Transaction trx = new Transaction(idTransaction, idDriver, id, lokasiAwal, lokasiTujuan, rating, comment, isHide, datePosted); - return new Transaction[]{trx}; + List<Transaction> result = new ArrayList<>(); + try { + String query = "SELECT * FROM transaction WHERE IDPenumpang=" + id + ";"; + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost:3306/projek"; + Connection conn = DriverManager.getConnection(url, "root", ""); + Statement stmt = conn.createStatement(); + ResultSet rs; + rs = stmt.executeQuery(query); + while (rs.next()) { + boolean isHide; + if (rs.getInt("IsHide") == 1) { + isHide = true; + } else { + isHide = false; + } + Transaction trx = new Transaction(rs.getInt("IDTransaksi"), + rs.getInt("IDDriver"), + rs.getInt("IDPenumpang"), + rs.getString("LokasiAwal"), + rs.getString("LokasiTujuan"), + rs.getInt("Rating"), + rs.getString("Comment"), + isHide, + rs.getDate("DatePosted")); + result.add(trx); + } + conn.close(); + } catch (IllegalAccessException | InstantiationException | SQLException | ClassNotFoundException e) { + e.printStackTrace(); + } + return result.toArray(new Transaction[0]); } else { return null; } @@ -54,8 +98,20 @@ public class History implements IHistory { boolean isTokenValid = true; //request identity service int id = 1; //request identity service if (isTokenValid) { - //QUERY hideTransaction - return true; + try { + String query = "UPDATE transaction SET IsHide = 0 WHERE IDTransaksi=" + idTransaction + ";"; + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost:3306/projek"; + Connection conn = DriverManager.getConnection(url, "root", ""); + Statement stmt = conn.createStatement(); + stmt.executeUpdate(query); + conn.close(); + return true; + + } catch (IllegalAccessException | InstantiationException | SQLException | ClassNotFoundException e) { + e.printStackTrace(); + return false; + } } else { return false; } diff --git a/WebService/src/com/informatika/ojek/webservice/IHistory.java b/WebService/src/com/informatika/ojek/webservice/IHistory.java index 054fdb9..2a224db 100644 --- a/WebService/src/com/informatika/ojek/webservice/IHistory.java +++ b/WebService/src/com/informatika/ojek/webservice/IHistory.java @@ -6,7 +6,7 @@ import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface -@WebService +@WebService (name = "HistoryService", portName = "HistoryPort") @SOAPBinding(style = Style.RPC) public interface IHistory { @WebMethod public Transaction[] getDriverHistory(String accessToken); diff --git a/WebService/src/com/informatika/ojek/webservice/Transaction.java b/WebService/src/com/informatika/ojek/webservice/Transaction.java index a54d6ad..d8c02ce 100644 --- a/WebService/src/com/informatika/ojek/webservice/Transaction.java +++ b/WebService/src/com/informatika/ojek/webservice/Transaction.java @@ -25,6 +25,25 @@ public class Transaction { this.datePosted = datePosted; } + public Transaction() { + + } + + @Override + public String toString() { + return "Transaction{" + + "idTransaction=" + idTransaction + + ", idDriver=" + idDriver + + ", idPenumpang=" + idPenumpang + + ", lokasiAwal='" + lokasiAwal + '\'' + + ", lokasiTujuan='" + lokasiTujuan + '\'' + + ", rating=" + rating + + ", comment='" + comment + '\'' + + ", isHide=" + isHide + + ", datePosted=" + datePosted + + '}'; + } + public int getIdTransaction() { return idTransaction; } diff --git a/WebService/src/com/informatika/ojek/webservice/test.java b/WebService/src/com/informatika/ojek/webservice/test.java index 5cb7969..419d7e8 100644 --- a/WebService/src/com/informatika/ojek/webservice/test.java +++ b/WebService/src/com/informatika/ojek/webservice/test.java @@ -8,39 +8,28 @@ import javax.xml.ws.Service; public class test { public static void main(String[] args) { try { - String url = "http://localhost:9999/ws/profile?wsdl"; + String url = "http://localhost:9999/ws/history?wsdl"; String namespace = "http://webservice.ojek.informatika.com/"; - QName serviceQN = new QName(namespace, "ProfileService"); + QName serviceQN = new QName(namespace, "HistoryService"); Service service = Service.create(new URL(url), serviceQN); - String portName = "ProfilePort"; + String portName = "HistoryPort"; QName portQN = new QName(namespace, portName); - IProfile sample = service.getPort(portQN, IProfile.class); + IHistory sample = service.getPort(portQN, IHistory.class); -// String[] hasil = sample.getLocation("blabla"); -// System.out.println(hasil[0]); -// System.out.println(hasil[1]); -// -// if (sample.addPreferredLocation("blabla", "Cisitu")) { -// System.out.println("sukses menambahkan"); -// } else { -// System.out.println("gagal"); +// Transaction[] hasil = sample.getPenumpangHistory("blabla"); +// if (hasil != null) { +// System.out.println(hasil[0]); +// System.out.println(hasil[1]); // } -// if (sample.delPreferredLocation("blabla", "singapur")) { -// System.out.println("sukses menghapus"); -// } else { -// System.out.println("gagal"); -// } - - if (sample.updatePreferredLocation("blabla", "kucing", "Tamansari")) { - System.out.println("sukses mengupdate"); + if (sample.hideHistory("bbbbb", 1)) { + System.out.println("Sukses Hide"); } else { - System.out.println("gagal"); + System.out.println("Gagal"); } - } catch (MalformedURLException e) { e.printStackTrace(); } -- GitLab