From b2567f25f14a244eb234bee5be2188a34d2386df Mon Sep 17 00:00:00 2001 From: gopi17701 <41270090+gopi17701@users.noreply.github.com> Date: Sat, 29 Sep 2018 21:25:24 -0400 Subject: [PATCH] Add files via upload --- .../src/main/java/com/astute/dao/DAO.java | 5 +- .../src/main/java/com/astute/dao/SqlDAO.java | 80 +++++++++++++++---- .../astute/exceptions/AstuteException.java | 2 +- .../java/com/astute/model/PaymentType.java | 29 +++++++ .../src/main/java/com/astute/model/User.java | 30 +++++-- .../requests/InvoicePaymentRequest.java | 5 +- .../com/astute/resources/AuthResource.java | 7 +- .../astute/resources/ChangeOrderResource.java | 6 +- .../astute/resources/CustomerResource.java | 4 + .../resources/InvoicePaymentResource.java | 9 ++- .../com/astute/resources/InvoiceResource.java | 8 ++ .../java/com/astute/service/AuthService.java | 19 +++-- .../astute/service/InvoicePaymentService.java | 4 + .../com/astute/service/InvoiceService.java | 5 ++ 14 files changed, 173 insertions(+), 40 deletions(-) create mode 100644 AstuteSystem/src/main/java/com/astute/model/PaymentType.java diff --git a/AstuteSystem/src/main/java/com/astute/dao/DAO.java b/AstuteSystem/src/main/java/com/astute/dao/DAO.java index 9a1c6a2..7c21e00 100644 --- a/AstuteSystem/src/main/java/com/astute/dao/DAO.java +++ b/AstuteSystem/src/main/java/com/astute/dao/DAO.java @@ -132,6 +132,8 @@ public abstract class DAO { public abstract String dupliateInvoice(String InvoiceNumber) throws AstuteException; + public abstract List getSubmittedInvoiceNumbers() throws AstuteException; + public abstract List getCustomers(String customerId) throws AstuteException; public abstract Customer getCustomer(String poNumber) throws AstuteException; @@ -154,7 +156,7 @@ public abstract class DAO { public abstract String getSessionUsername(String sessionId) throws AstuteException; - public abstract String login(String username, String password) throws AstuteException; + public abstract User login(String username, String password) throws AstuteException; public abstract List getChangeOrders(String poNum) throws AstuteException; @@ -168,4 +170,5 @@ public abstract class DAO { public abstract void createInvoicePayment(String invoiceNum, int invoicePaymentTypeId, Double paymentAmount, Date paymentDate) throws AstuteException; + public abstract List getPaymentTypes() throws AstuteException; } \ No newline at end of file diff --git a/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java b/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java index 1f6e265..500d789 100644 --- a/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java +++ b/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java @@ -618,6 +618,28 @@ public class SqlDAO extends DAO { throw new AstuteException(DB_ERROR,e.getMessage()); } } + + public List getSubmittedInvoiceNumbers() throws AstuteException { + String sql = "select inv_no from invoice where inv_status = 2"; + String invoiceNumber = null; + Invoice invoice = null; + List invoices = new ArrayList(); + try { + Statement stmt = conn.createStatement(); + ResultSet resultSet = stmt.executeQuery(sql); + + while (resultSet.next()) { + invoiceNumber = resultSet.getString(1); + invoice = new Invoice(invoiceNumber,null,null,0,null,null,null,0); + invoices.add(invoice); + } + } catch (SQLException e) { + e.printStackTrace(); + throw new AstuteException(DB_ERROR,e.getMessage()); + } + System.out.println("Returning invoices " + invoices.size()); + return invoices; + } /* =============================== Customer Methods =============================================== */ @@ -760,7 +782,7 @@ public class SqlDAO extends DAO { } public User getUser(String username) throws AstuteException { - String sql = "select user_id, username, password from user where username='" + username + "'"; + String sql = "select user_id, username, password, CONCAT(first_name, ' ', last_name) as name from user where username='" + username + "'"; try { Statement stmt = conn.createStatement(); @@ -768,7 +790,11 @@ public class SqlDAO extends DAO { User user = null; if(resultSet.next()) { - user = new User(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3)); + user = new User(resultSet.getInt(1), + resultSet.getString(2), + resultSet.getString(3), + resultSet.getString(4),null + ); } // conn.close(); @@ -894,7 +920,7 @@ public class SqlDAO extends DAO { try { List invoicePayments = new ArrayList(); Statement stmt = conn.createStatement(); - String sql = "SELECT invoice_payment_id, invoice_payment_type, get_payment_type(invoice_payment_type), invoice_amount, payment_date FROM invoice_payment "; + String sql = "SELECT invoice_payment_id, invoice_payment_type, get_payment_type(invoice_payment_type), invoice_amount, payment_date, inv_no FROM invoice_payment "; if (invoiceNum != null) { sql += " WHERE inv_no = '" + invoiceNum + "'"; } @@ -905,7 +931,8 @@ public class SqlDAO extends DAO { String invoicePaymentType = rs.getString(3); Double paymentAmount = rs.getDouble(4); Date paymentDate = rs.getDate(5); - InvoicePayment invoicePayment = new InvoicePayment(invoiceNum, invoicePaymentId, invoicePaymentTypeId, invoicePaymentType, paymentDate, paymentAmount); + String invNo = rs.getString(6); + InvoicePayment invoicePayment = new InvoicePayment(invNo, invoicePaymentId, invoicePaymentTypeId, invoicePaymentType, paymentDate, paymentAmount); invoicePayments.add(invoicePayment); } return invoicePayments; @@ -922,14 +949,14 @@ public class SqlDAO extends DAO { String whereClause = ""; if (invoiceNum == null || invoiceNum.isEmpty() || invoicePaymentId <=0 ) { throw new AstuteException(DB_ERROR,"Invoice Number can't be null and Invoice Payment Id should be a positive number! "); - } else { - whereClause = " WHERE UPPER(inv_no) ='" + invoiceNum.toUpperCase() + "' and invoice_amount = " + paymentAmount; } - updateClause = updateClause + " payment_amount = " + paymentAmount ; - if (paymentDate != null) { - updateClause = "payment_date " + ", paymentDate = STR_TO_DATE(" + paymentDate + ", '%Y-%m-%d')"; - } + + updateClause = updateClause + " invoice_amount = " + paymentAmount ; +// if (paymentDate != null) { + updateClause = updateClause + ", payment_date = STR_TO_DATE('" + paymentDate + "', '%Y-%m-%d')"; +// } + whereClause = " WHERE UPPER(inv_no) ='" + invoiceNum.toUpperCase() + "' and invoice_payment_id = " + invoicePaymentId; sql = sql + updateClause + whereClause; System.out.println(sql); @@ -944,9 +971,10 @@ public class SqlDAO extends DAO { public void createInvoicePayment(String invoiceNum, int invoicePaymentTypeId, Double paymentAmount, Date paymentDate) throws AstuteException{ try { - String dateString = "STR_TO_DATE(" + paymentDate + ", '%Y-%m-%d')"; - String sql = "insert into invoice_payment (inv_no, invoice_payment_type, invoice_amount, payment_date) values ('" + invoiceNum + "', " + paymentAmount + ", " + dateString + "')"; + String dateString = "STR_TO_DATE('" + paymentDate + "', '%Y-%m-%d')"; + String sql = "insert into invoice_payment (inv_no, invoice_payment_type, invoice_amount, payment_date) values ('" + invoiceNum + "', " + invoicePaymentTypeId + ", " + paymentAmount + ", " + dateString + ")"; Statement stmt = conn.createStatement(); + System.out.println(sql); stmt.execute(sql); } catch (SQLException e) { e.printStackTrace(); @@ -954,12 +982,30 @@ public class SqlDAO extends DAO { } } + public List getPaymentTypes() throws AstuteException { + try { + List paymentTypes = new ArrayList(); + Statement stmt = conn.createStatement(); + String sql = "SELECT payment_type_id, payment_type_name FROM payment_type ORDER BY 1 "; + ResultSet rs = stmt.executeQuery(sql); + while (rs.next()) { + int paymentTypeId = rs.getInt(1); + String paymentTypeName = rs.getString(2); + PaymentType paymentType = new PaymentType(paymentTypeId, paymentTypeName); + paymentTypes.add(paymentType); + } + return paymentTypes; + } catch (SQLException e) { + e.printStackTrace(); + throw new AstuteException(DB_ERROR,e.getMessage()); + } + }; /* =============================== Utility Methods =============================================== */ - public String login(String username, String password) throws AstuteException{ - User user = dao.getUser(username); + public User login(String username, String password) throws AstuteException{ + User user = getUser(username); boolean check = false; if (password.equals(user.getPassword())) { check = true; @@ -975,9 +1021,9 @@ public class SqlDAO extends DAO { //create session String sessionId = UUID.randomUUID().toString().replaceAll("-", ""); - dao.createSession(user.getUserId(), sessionId); - - return sessionId; + createSession(user.getUserId(), sessionId); + user.setSessionId(sessionId); + return user; }else{ return null; //"Username or password was not correct"; } diff --git a/AstuteSystem/src/main/java/com/astute/exceptions/AstuteException.java b/AstuteSystem/src/main/java/com/astute/exceptions/AstuteException.java index 04fe77b..008d9c8 100644 --- a/AstuteSystem/src/main/java/com/astute/exceptions/AstuteException.java +++ b/AstuteSystem/src/main/java/com/astute/exceptions/AstuteException.java @@ -9,7 +9,7 @@ public class AstuteException extends Exception { public static final int CLIENT_ERROR = 400; public static final int SERVER_ERROR = 500; public static final int DB_ERROR = 600; - + public static final int AUTH_ERROR = 700; private int code; diff --git a/AstuteSystem/src/main/java/com/astute/model/PaymentType.java b/AstuteSystem/src/main/java/com/astute/model/PaymentType.java new file mode 100644 index 0000000..5252b2a --- /dev/null +++ b/AstuteSystem/src/main/java/com/astute/model/PaymentType.java @@ -0,0 +1,29 @@ +package com.astute.model; + +import java.sql.Date; + +public class PaymentType { + int paymentTypeId; + String paymentTypeName; + + public PaymentType(int paymentTypeId, String paymentTypeName) { + this.paymentTypeId = paymentTypeId; + this.paymentTypeName = paymentTypeName; + } + + public int getPaymentTypeId() { + return paymentTypeId; + } + + public void setPaymentTypeId(int paymentTypeId) { + this.paymentTypeId = paymentTypeId; + } + + public String getPaymentTypeName() { + return paymentTypeName; + } + + public void setPaymentTypeName(String paymentTypeName) { + this.paymentTypeName = paymentTypeName; + } +} diff --git a/AstuteSystem/src/main/java/com/astute/model/User.java b/AstuteSystem/src/main/java/com/astute/model/User.java index b9ceb75..84be803 100644 --- a/AstuteSystem/src/main/java/com/astute/model/User.java +++ b/AstuteSystem/src/main/java/com/astute/model/User.java @@ -4,11 +4,31 @@ public class User { int userId; String username; String password; + String name; + String sessionId; - public User(int userId, String username, String password) { + public User(int userId, String username, String password, String name, String sessionId) { this.userId = userId; this.username = username; this.password = password; + this.name = name; + + } + + public String getSessionId() { + return sessionId; + } + + public void setSessionId(String sessionId) { + this.sessionId = sessionId; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; } public int getUserId() { @@ -27,11 +47,11 @@ public class User { this.username = username; } - public String getPassword() { - return password; + public String getName() { + return name; } - public void setPassword(String password) { - this.password = password; + public void setName(String name) { + this.name = name; } } diff --git a/AstuteSystem/src/main/java/com/astute/requests/InvoicePaymentRequest.java b/AstuteSystem/src/main/java/com/astute/requests/InvoicePaymentRequest.java index 729e04a..e011eec 100644 --- a/AstuteSystem/src/main/java/com/astute/requests/InvoicePaymentRequest.java +++ b/AstuteSystem/src/main/java/com/astute/requests/InvoicePaymentRequest.java @@ -1,6 +1,5 @@ package com.astute.requests; -import java.sql.Date; public class InvoicePaymentRequest { String invoiceNum; @@ -19,6 +18,10 @@ public class InvoicePaymentRequest { this.invoiceAmount = invoiceAmount; } + public InvoicePaymentRequest() { + + } + public String getInvoiceNum() { return invoiceNum; } diff --git a/AstuteSystem/src/main/java/com/astute/resources/AuthResource.java b/AstuteSystem/src/main/java/com/astute/resources/AuthResource.java index 09a1ed3..83d9604 100644 --- a/AstuteSystem/src/main/java/com/astute/resources/AuthResource.java +++ b/AstuteSystem/src/main/java/com/astute/resources/AuthResource.java @@ -1,6 +1,7 @@ package com.astute.resources; import com.astute.exceptions.AstuteException; +import com.astute.model.User; import com.astute.requests.LoginRequest; import com.astute.response.ApiResponse; import com.astute.service.AuthService; @@ -25,9 +26,9 @@ public class AuthResource { @POST public Response login(LoginRequest request) throws AstuteException { - String sessionId = service.login(request.getUsername(), request.getPassword()); - if (sessionId != null) { - return new ApiResponse(sessionId).toResponse(); + User user = service.login(request.getUsername(), request.getPassword()); + if (user != null) { + return new ApiResponse(user).toResponse(); } else { return new ApiResponse(ApiResponse.ACCESS_DENIED).toResponse(); } diff --git a/AstuteSystem/src/main/java/com/astute/resources/ChangeOrderResource.java b/AstuteSystem/src/main/java/com/astute/resources/ChangeOrderResource.java index 6af94f0..c480a1b 100644 --- a/AstuteSystem/src/main/java/com/astute/resources/ChangeOrderResource.java +++ b/AstuteSystem/src/main/java/com/astute/resources/ChangeOrderResource.java @@ -3,6 +3,7 @@ package com.astute.resources; import com.astute.exceptions.AstuteException; import com.astute.requests.ChangeOrderRequest; import com.astute.response.ApiResponse; +import com.astute.service.AuthService; import com.astute.service.ChangeOrderService; import javax.ws.rs.*; @@ -16,13 +17,16 @@ import java.sql.SQLException; public class ChangeOrderResource { private com.astute.service.ChangeOrderService service = new ChangeOrderService(); + private com.astute.service.AuthService authService = new AuthService(); + public ChangeOrderResource() { } @GET - public Response getChangeOrders(@QueryParam("poNum") String poNum) + public Response getChangeOrders(@QueryParam("poNum") String poNum, @QueryParam("sessionId") String sessionId) throws AstuteException { + authService.getUser(sessionId); return new ApiResponse(service.getChangeOrders(poNum)).toResponse(); } diff --git a/AstuteSystem/src/main/java/com/astute/resources/CustomerResource.java b/AstuteSystem/src/main/java/com/astute/resources/CustomerResource.java index fcff7f7..9381c7f 100644 --- a/AstuteSystem/src/main/java/com/astute/resources/CustomerResource.java +++ b/AstuteSystem/src/main/java/com/astute/resources/CustomerResource.java @@ -3,6 +3,7 @@ package com.astute.resources; import com.astute.exceptions.AstuteException; import com.astute.requests.CustomerRequest; import com.astute.response.ApiResponse; +import com.astute.service.AuthService; import com.astute.service.CustomerService; import javax.ws.rs.*; @@ -16,6 +17,7 @@ import java.sql.SQLException; public class CustomerResource { private com.astute.service.CustomerService service = new CustomerService(); + private com.astute.service.AuthService authService = new AuthService(); public CustomerResource() { } @@ -23,6 +25,8 @@ public class CustomerResource { @GET public Response getCustomers(@QueryParam("customerId") String customerId) throws AstuteException { + // TODO , @QueryParam("sessionId") String sessionId +// authService.getUser(sessionId); return new ApiResponse(service.getCustomers(customerId)).toResponse(); } diff --git a/AstuteSystem/src/main/java/com/astute/resources/InvoicePaymentResource.java b/AstuteSystem/src/main/java/com/astute/resources/InvoicePaymentResource.java index 4c5d386..8414f31 100644 --- a/AstuteSystem/src/main/java/com/astute/resources/InvoicePaymentResource.java +++ b/AstuteSystem/src/main/java/com/astute/resources/InvoicePaymentResource.java @@ -46,7 +46,14 @@ public class InvoicePaymentResource { String dateStr = request.getPaymentDate(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = new java.sql.Date(df.parse(dateStr).getTime()); - service.createInvoicePayment(request.getInvoiceNum(), request.getInvoicePaymentId(), request.getInvoiceAmount(),date); + service.createInvoicePayment(request.getInvoiceNum(), request.getPaymentTypeId(), request.getInvoiceAmount(),date); return new ApiResponse(ApiResponse.UPDATE_ACCESS_SUCESS).toResponse(); } + + @GET + @Path("/paymentTypes") + public Response getInvoicePaymentTypes() throws AstuteException { + return new ApiResponse(service.getPaymentTypes()).toResponse(); + } + } diff --git a/AstuteSystem/src/main/java/com/astute/resources/InvoiceResource.java b/AstuteSystem/src/main/java/com/astute/resources/InvoiceResource.java index 542a67d..a92de46 100644 --- a/AstuteSystem/src/main/java/com/astute/resources/InvoiceResource.java +++ b/AstuteSystem/src/main/java/com/astute/resources/InvoiceResource.java @@ -122,4 +122,12 @@ public class InvoiceResource { public Response duplicateInvoice(@PathParam("InvoiceNumber") String InvoiceNumber) throws AstuteException { return new ApiResponse(service.dupliateInvoice(InvoiceNumber)).toResponse(); } + + @Path("/submitted") + @GET + public Response getSubmittedInvoiceNumbers() throws AstuteException { + System.out.println("In getSubmittedInvoiceNumbers"); + return new ApiResponse(service.getSubmittedInvoiceNumbers()).toResponse(); + } + } diff --git a/AstuteSystem/src/main/java/com/astute/service/AuthService.java b/AstuteSystem/src/main/java/com/astute/service/AuthService.java index c3926c9..4aa3e8c 100644 --- a/AstuteSystem/src/main/java/com/astute/service/AuthService.java +++ b/AstuteSystem/src/main/java/com/astute/service/AuthService.java @@ -2,30 +2,29 @@ package com.astute.service; import com.astute.exceptions.AstuteException; +import com.astute.model.User; import java.sql.SQLException; import static com.astute.dao.DAO.getDao; +import static com.astute.exceptions.AstuteException.AUTH_ERROR; public class AuthService extends Service{ public AuthService(){ super(); } - public String login(String username, String password) + public User login(String username, String password) throws AstuteException { return getDao().login(username,password); } - public void updateCustomer( String customerId, String customerName, String billToDept, String add1, String add2, String city, String state, int zip, int ziplast4, String email, String phone, String fax) - throws AstuteException { - getDao().updateCustomer(customerId, customerName,billToDept, add1, add2, city, state, zip, ziplast4, email, phone, fax); - } - - - public void createCustomer(String customerId, String customerName, String billToDept, String add1, String add2, String city, String state, int zip, int ziplast4, String email, String phone, String fax) - throws AstuteException { - getDao().createCustomer(customerId, customerName,billToDept, add1, add2, city, state, zip, ziplast4, email, phone, fax); + public String getUser(String sessionId) throws AstuteException { + User user = getDao().getUser(sessionId); + if (user == null) { + throw new AstuteException(AUTH_ERROR, "Authentication Error. Please login first!"); + } + return user.getUsername(); } } diff --git a/AstuteSystem/src/main/java/com/astute/service/InvoicePaymentService.java b/AstuteSystem/src/main/java/com/astute/service/InvoicePaymentService.java index b91874c..cbe0dbe 100644 --- a/AstuteSystem/src/main/java/com/astute/service/InvoicePaymentService.java +++ b/AstuteSystem/src/main/java/com/astute/service/InvoicePaymentService.java @@ -2,6 +2,7 @@ package com.astute.service; import com.astute.exceptions.AstuteException; import com.astute.model.InvoicePayment; +import com.astute.model.PaymentType; import java.sql.Date; import java.util.List; @@ -29,4 +30,7 @@ public class InvoicePaymentService extends Service{ getDao().createInvoicePayment(invoiceNum, invoicePaymentTypeId, paymentAmount, paymentDate); } + public List getPaymentTypes() throws AstuteException { + return getDao().getPaymentTypes(); + } } diff --git a/AstuteSystem/src/main/java/com/astute/service/InvoiceService.java b/AstuteSystem/src/main/java/com/astute/service/InvoiceService.java index 907bc58..58dff1d 100644 --- a/AstuteSystem/src/main/java/com/astute/service/InvoiceService.java +++ b/AstuteSystem/src/main/java/com/astute/service/InvoiceService.java @@ -81,4 +81,9 @@ public class InvoiceService extends Service{ public List getPaymentStatuses() throws AstuteException { return getDao().getPaymentStatuses(); } + + public List getSubmittedInvoiceNumbers() throws AstuteException { + return getDao().getSubmittedInvoiceNumbers(); + } + }