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.DbInt;
24  import Utilities.Year;
25  import javafx.concurrent.Task;
26  
27  import java.io.BufferedWriter;
28  import java.io.IOException;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.Paths;
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  
35  //import javax.swing.*;
36  
37  //import com.itextpdf.text.Document;
38  
39  /**
40   * Searches the text files under the given directory and counts the number of instances a given word is found
41   * in these file.
42   *
43   * @author Albert Attard
44   */
45  public class orderHistoryReportWorker extends Task<Integer> {
46      private final String csvLocation;
47      private Double prog = 0.0;
48  
49      /**
50       * Creates an instance of the worker
51       *
52       * @param csvLocation Location to save the CSV
53       */
54      public orderHistoryReportWorker(String csvLocation) {
55          this.csvLocation = csvLocation;
56  
57      }
58  
59  // --Commented out by Inspection START (7/27/16 3:02 PM):
60  //    private static void failIfInterrupted() throws InterruptedException {
61  //        if (Thread.currentThread().isInterrupted()) {
62  //            throw new InterruptedException("Interrupted while searching files");
63  //        }
64  //    }
65  // --Commented out by Inspection STOP (7/27/16 3:02 PM)
66  
67      @Override
68      protected Integer call() throws Exception {
69          Path newFile = Paths.get(csvLocation);
70          BufferedWriter writer = Files.newBufferedWriter(newFile);
71          try {
72              HashMap<String, ArrayList<String>> addressYears = new HashMap<>();
73  
74              updateMessage("Generating Report");
75  
76              ArrayList<String> years = DbInt.getUserYears();
77              writer.write("\"Address\"");
78              for (String year : years) {
79                  writer.write(",\"" + year + "\"");
80              }
81              for (String yearString : years) {
82                  Year year = new Year(yearString);
83                  for (Customer customer : year.getCustomers()) {
84                      String address = customer.getFormattedAddress();
85                      if (!addressYears.containsKey(address)) {
86  
87                          ArrayList<String> customs = new ArrayList();
88                          customs.add(yearString);
89                          addressYears.put(address, customs);
90  
91                      } else {
92                          addressYears.get(address).add(yearString);
93                      }
94                  }
95              }
96              addressYears.forEach((address, addrYears) -> {
97                  try {
98                      writer.newLine();
99                      writer.write("\"" + address + "\"");
100                     for (String year : years) {
101                         if (addrYears.contains(year)) {
102                             writer.write(",\"TRUE\"");
103                         } else {
104                             writer.write(",\"FALSE\"");
105                         }
106                     }
107 
108                 } catch (IOException e) {
109                     updateMessage("Error writing CSV file. Please try again.");
110                 }
111 
112             });
113         } catch (IOException e) {
114             updateMessage("Error writing CSV file. Please try again.");
115         } finally {
116             try {
117                 if (writer != null) { writer.close(); }
118             } catch (Exception ex) {
119                 updateMessage("Error writing CSV file. Please try again.");
120             }
121         }
122         updateMessage("Done");
123 
124 
125         // Return the number of matches found
126         return 1;
127     }
128 
129     private double getProg() {
130         return prog;
131     }
132 
133     private void setProgress(double progress) {
134         prog += progress;
135         updateProgress(progress, 100.0);
136     }
137 }