View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package Workers;
21  
22  import Utilities.Customer;
23  import Utilities.Geolocation;
24  import Utilities.Order;
25  import Utilities.formattedProductProps;
26  import javafx.concurrent.Task;
27  import javafx.scene.control.TableView;
28  
29  import java.math.BigDecimal;
30  
31  //import javax.swing.*;
32  
33  /**
34   * @author Patrick Magauran
35   */
36  public class AddCustomerWorker extends Task<Customer> {
37      private final Integer ID;
38  
39      private final String Address;
40      private final String Town;
41      private final String State;
42      private final String year;
43      private final TableView<formattedProductProps> ProductTable;
44      private final String Name;
45      private final String ZipCode;
46      private final String Phone;
47      private final String Email;
48      private final String DonationsT;
49      private final String NameEditCustomer;
50      private final String uName;
51      private final BigDecimal Paid;
52      private final Boolean Delivered;
53  
54      /**
55       * Creates an instance of the worker
56       * old/new is relavant for editing. Db uses name to find customer
57       *
58       * @param id               The ID of the Utilities.Customer
59       * @param name             (old) name of the customer
60       * @param zipCode          ZIpcode
61       * @param phone            Phone #
62       * @param email            Email address
63       * @param donationsT       Total Donations
64       * @param nameEditCustomer (new) name of customer
65       * @param paid             Did they pay
66       * @param delivered        Was it deleivered
67       */
68      public AddCustomerWorker(Integer id, String Address, String Town, String State, String year, TableView ProductTable, String name, String zipCode, String phone, String email, String donationsT, String nameEditCustomer, String paid, Boolean delivered, String uName) {
69          ID = id;
70          this.Address = Address;
71          this.Town = Town;
72          this.State = State;
73          this.year = year;
74          this.ProductTable = ProductTable;
75          Name = name;
76          ZipCode = zipCode;
77          Phone = phone;
78          Email = email;
79          DonationsT = donationsT;
80          NameEditCustomer = nameEditCustomer;
81          Paid = new BigDecimal(paid);
82          Delivered = delivered;
83          this.uName = uName;
84      }
85  
86      private static void failIfInterrupted() throws InterruptedException {
87          if (Thread.currentThread().isInterrupted()) {
88              throw new InterruptedException("Interrupted while Adding Order");
89          }
90      }
91  
92      private int IntegerLength(int n) {
93          if (n < 100000) {
94              // 5 or less
95              if (n < 100) {
96                  // 1 or 2
97                  if (n < 10) { return 1; } else { return 2; }
98              } else {
99                  // 3 or 4 or 5
100                 if (n < 1000) { return 3; } else {
101                     // 4 or 5
102                     if (n < 10000) { return 4; } else { return 5; }
103                 }
104             }
105         } else {
106             // 6 or more
107             if (n < 10000000) {
108                 // 6 or 7
109                 if (n < 1000000) { return 6; } else { return 7; }
110             } else {
111                 // 8 to 10
112                 if (n < 100000000) { return 8; } else {
113                     // 9 or 10
114                     if (n < 1000000000) { return 9; } else { return 10; }
115                 }
116             }
117         }
118 
119     }
120 
121 /*    @Override
122     protected void process(List<String> chunks) {
123         // Updates the messages text area
124         chunks.forEach(StatusLbl::setText);
125     }*/
126 
127     @Override
128     protected Customer call() throws Exception {
129         String address = String.format("%s, %s, %s, %s", Address, Town, State, ZipCode);//Formats address
130         updateMessage("Analyzing Address");
131         Utilities.Coords coords;
132         coords = Geolocation.GetCoords(address);
133 
134 
135         int numRows = ProductTable.getItems().size();
136 
137         double lat = coords.getLat();
138         double lon = coords.getLon();
139         AddCustomerWorker.failIfInterrupted();
140         updateProgress(0, 100);
141         Customer customer = new Customer(ID, NameEditCustomer, year, Address, Town, State, ZipCode, lat, lon, Phone, Paid, Delivered, Email,
142                 Name, new BigDecimal(DonationsT), uName);
143         customer.progressProperty().addListener(((observableValue, oldProgress, newProgress) -> {
144             updateProgress(newProgress.doubleValue(), 100);
145         }));
146         customer.messageProperty().addListener(((observableValue, oldMessage, newMessage) -> {
147             updateMessage(newMessage);
148         }));
149         Integer custID = customer.updateValues(() -> AddCustomerWorker.failIfInterrupted());
150         Order order = new Order(ProductTable.getItems(), year, custID, Paid, Delivered, uName);
151         order.progressProperty().addListener(((observableValue, oldProgress, newProgress) -> {
152             updateProgress(newProgress.doubleValue(), 100);
153         }));
154         order.messageProperty().addListener(((observableValue, oldMessage, newMessage) -> {
155             updateMessage(newMessage);
156         }));
157         String Id = order.updateOrder(() -> AddCustomerWorker.failIfInterrupted());
158 
159         updateProgress(100, 100);
160 
161 
162         updateMessage("Done");
163 
164 
165 
166         // Return the number of matches found
167         return customer;
168     }
169 }