1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41
42
43
44
45 public class orderHistoryReportWorker extends Task<Integer> {
46 private final String csvLocation;
47 private Double prog = 0.0;
48
49
50
51
52
53
54 public orderHistoryReportWorker(String csvLocation) {
55 this.csvLocation = csvLocation;
56
57 }
58
59
60
61
62
63
64
65
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
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 }