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.UsersGroupsAndYearsController;
23 import Utilities.*;
24 import javafx.concurrent.Task;
25 import javafx.scene.control.TreeItem;
26 import javafx.util.Pair;
27 import org.apache.commons.lang3.tuple.Triple;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Map;
32
33
34
35
36
37
38 public class LoadUGYWorker extends Task<Triple<TreeItem<TreeItemPair<String, Pair<String, Object>>>, Map<String, Map<String, User>>, Map<String, ArrayList<Group>>>> {
39 private final UsersGroupsAndYearsController UGYController;
40
41
42
43
44
45
46 public LoadUGYWorker(UsersGroupsAndYearsController UGYController) {
47 this.UGYController = UGYController;
48 }
49
50 private static void failIfInterrupted() throws InterruptedException {
51 if (Thread.currentThread().isInterrupted()) {
52 throw new InterruptedException("Interrupted while loading application");
53 }
54 }
55
56 private int IntegerLength(int n) {
57 if (n < 100000) {
58
59 if (n < 100) {
60
61 if (n < 10) { return 1; } else { return 2; }
62 } else {
63
64 if (n < 1000) { return 3; } else {
65
66 if (n < 10000) { return 4; } else { return 5; }
67 }
68 }
69 } else {
70
71 if (n < 10000000) {
72
73 if (n < 1000000) { return 6; } else { return 7; }
74 } else {
75
76 if (n < 100000000) { return 8; } else {
77
78 if (n < 1000000000) { return 9; } else { return 10; }
79 }
80 }
81 }
82
83 }
84
85
86
87
88
89
90
91 @Override
92 protected Triple<TreeItem<TreeItemPair<String, Pair<String, Object>>>, Map<String, Map<String, User>>, Map<String, ArrayList<Group>>> call() {
93 long startTime = System.nanoTime();
94 Map<String, Map<String, User>> cachedUsers = new HashMap<>();
95
96 Map<String, ArrayList<Group>> cachedGroups = new HashMap<>();
97 updateMessage("Loading Data");
98
99 Iterable<String> ret = DbInt.getUserYears();
100 TreeItem<TreeItemPair<String, Pair<String, Object>>> root = new TreeItem<>(new TreeItemPair("Root Node", new Pair<String, String>("RootNode", "")));
101 cachedGroups.clear();
102 cachedUsers.clear();
103
104
105
106
107
108
109
110
111
112
113
114
115 for (String curYear : ret) {
116 UsersGroupsAndYearsController.contextTreeItem tIYear = UGYController.new contextTreeItem(curYear, "Year");
117 Year year = new Year(curYear);
118 User curUser = DbInt.getUser(curYear);
119 Iterable<Group> groups = Group.getGroups(curYear);
120 for (Group group : groups) {
121 UsersGroupsAndYearsController.contextTreeItem tiGroup = UGYController.new contextTreeItem(group.getName(), new Pair<>("Group", group));
122 cachedGroups.computeIfPresent(curYear, (k, v) -> {
123 if (!v.contains(group)) {
124
125 v.add(group);
126 }
127 return v;
128 });
129 cachedGroups.computeIfAbsent(curYear, k -> {
130 ArrayList<Group> v = new ArrayList();
131 v.add(group);
132 return v;
133 });
134 Iterable<User> users = group.getUsers();
135
136 for (User user : users) {
137
138 UsersGroupsAndYearsController.contextTreeItem uManTi = UGYController.new contextTreeItem(user.getFullName() + " (" + user.getUserName() + ")", new Pair<>("User", user));
139 tiGroup.getChildren().add(uManTi);
140
141
142 cachedUsers.computeIfPresent(user.getUserName(), (k, v) -> {
143 v.put(curYear, new User(user.getUserName(), curYear, true));
144 return v;
145 });
146 cachedUsers.computeIfAbsent(user.getUserName(), k -> {
147 Map<String, User> v = new HashMap<>();
148 v.put(curYear, new User(user.getUserName(), curYear, true));
149
150 return v;
151 });
152 }
153 tIYear.getChildren().add(tiGroup);
154
155 }
156 root.getChildren().add(tIYear);
157
158
159 }
160 DbInt.getUsers().forEach(user -> {
161 cachedUsers.computeIfAbsent(user.getUserName(), k -> {
162 Map<String, User> v = new HashMap<>();
163 v.put("DB", user);
164 return v;
165 });
166 cachedUsers.computeIfPresent(user.getUserName(), (k, v) -> {
167 v.put("DB", user);
168 return v;
169 });
170 });
171 long endTime = System.nanoTime();
172 double totalTime = (endTime - startTime) / 1000000;
173
174 LogToFile.log(null, Severity.FINEST, "Inital load took " + totalTime + "ms");
175 return new Triple<TreeItem<TreeItemPair<String, Pair<String, Object>>>, Map<String, Map<String, User>>, Map<String, ArrayList<Group>>>() {
176 @Override
177 public TreeItem<TreeItemPair<String, Pair<String, Object>>> getLeft() {
178 return root;
179 }
180
181 @Override
182 public Map<String, Map<String, User>> getMiddle() {
183 return cachedUsers;
184 }
185
186 @Override
187 public Map<String, ArrayList<Group>> getRight() {
188 return cachedGroups;
189 }
190 };
191 }
192 }