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 Controllers.MainController;
23 import Utilities.*;
24 import javafx.concurrent.Task;
25 import javafx.scene.control.TreeItem;
26 import javafx.util.Pair;
27
28 import java.util.Objects;
29
30
31
32
33
34
35 public class LoadMainWorker extends Task<TreeItem<TreeItemPair<String, Pair<String, Object>>>> {
36 private final MainController mainController;
37
38
39
40
41
42
43 public LoadMainWorker(MainController mainController) {
44 this.mainController = mainController;
45 }
46
47 private static void failIfInterrupted() throws InterruptedException {
48 if (Thread.currentThread().isInterrupted()) {
49 throw new InterruptedException("Interrupted while loading application");
50 }
51 }
52
53 private int IntegerLength(int n) {
54 if (n < 100000) {
55
56 if (n < 100) {
57
58 if (n < 10) { return 1; } else { return 2; }
59 } else {
60
61 if (n < 1000) { return 3; } else {
62
63 if (n < 10000) { return 4; } else { return 5; }
64 }
65 }
66 } else {
67
68 if (n < 10000000) {
69
70 if (n < 1000000) { return 6; } else { return 7; }
71 } else {
72
73 if (n < 100000000) { return 8; } else {
74
75 if (n < 1000000000) { return 9; } else { return 10; }
76 }
77 }
78 }
79
80 }
81
82
83
84
85
86
87
88 @Override
89 protected TreeItem<TreeItemPair<String, Pair<String, Object>>> call() {
90 long startTime = System.nanoTime();
91
92 updateMessage("Loading Data");
93
94 Iterable<String> ret = DbInt.getUserYears();
95 TreeItem<TreeItemPair<String, Pair<String, Object>>> root = new TreeItem<>(new TreeItemPair("Root Node", new Pair<String, String>("RootNode", "")));
96 MainController.contextTreeItem userRoot = mainController.new contextTreeItem("Groups/Users", new Pair<String, String>("RootNode", ""));
97 root.getChildren().add(mainController.new contextTreeItem("Reports", "Window"));
98 root.getChildren().add(mainController.new contextTreeItem("View Map", "Window"));
99 root.getChildren().add(mainController.new contextTreeItem("Settings", "Window"));
100 if (DbInt.isAdmin()) {
101 root.getChildren().add(mainController.new contextTreeItem("Users Groups & Years", "Window"));
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116 for (String curYear : ret) {
117 MainController.contextTreeItem tIYear = mainController.new contextTreeItem(curYear, "Year");
118 Year year = new Year(curYear);
119 User curUser = DbInt.getUser(curYear);
120 Iterable<String> uManage = curUser.getuManage();
121 for (String uMan : uManage) {
122 MainController.contextTreeItem uManTi = mainController.new contextTreeItem(uMan, new Pair<>("UserCustomerView", curYear));
123
124 Iterable<Customer> customers = year.getCustomers(uMan);
125 for (Customer customer : customers) {
126 uManTi.getChildren().add(mainController.new contextTreeItem(customer.getName(), new Pair<String, Customer>("Customer", customer)));
127 }
128 uManTi.getChildren().add(mainController.new contextTreeItem("Add Customer", new Pair<String, Pair<String, String>>("Window", new Pair<String, String>(curYear, uMan))));
129 if (Objects.equals(uMan, curUser.getUserName())) {
130 tIYear.getChildren().addAll(uManTi.getChildren());
131 } else {
132 tIYear.getChildren().add(uManTi);
133 }
134 }
135 root.getChildren().add(tIYear);
136
137
138 }
139 long endTime = System.nanoTime();
140 double totalTime = (endTime - startTime) / 1000000;
141
142 LogToFile.log(null, Severity.FINEST, "Inital load took " + totalTime + "ms");
143 return root;
144 }
145 }