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 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  //import javax.swing.*;
34  
35  /**
36   * @author Patrick Magauran
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       * Creates an instance of the worker
43       *
44       * @param UGYController the mainController that started the worker
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              // 5 or less
59              if (n < 100) {
60                  // 1 or 2
61                  if (n < 10) { return 1; } else { return 2; }
62              } else {
63                  // 3 or 4 or 5
64                  if (n < 1000) { return 3; } else {
65                      // 4 or 5
66                      if (n < 10000) { return 4; } else { return 5; }
67                  }
68              }
69          } else {
70              // 6 or more
71              if (n < 10000000) {
72                  // 6 or 7
73                  if (n < 1000000) { return 6; } else { return 7; }
74              } else {
75                  // 8 to 10
76                  if (n < 100000000) { return 8; } else {
77                      // 9 or 10
78                      if (n < 1000000000) { return 9; } else { return 10; }
79                  }
80              }
81          }
82  
83      }
84  
85  /*    @Override
86      protected void process(List<String> chunks) {
87          // Updates the messages text area
88          chunks.forEach(StatusLbl::setText);
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          //year -> groups
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         ///Select all years
105         //Create a button for each year
106 /*        for (String aRet : ret) {
107             JButton b = new JButton(aRet);
108             b.addActionListener(e -> {
109                 //On button click open Utilities.Year window
110                 new YearWindow(((AbstractButton) e.getSource()).getText());
111 
112             });
113             panel_1.add(b);
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                     //UserName -> year -> UserObject
141                     //private Map<String, Map<String, User>> cachedUsers = new HashMap<>();
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         //System.out.println("Inital load took " + totalTime + "ms");
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 }