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.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  //import javax.swing.*;
31  
32  /**
33   * @author Patrick Magauran
34   */
35  public class LoadMainWorker extends Task<TreeItem<TreeItemPair<String, Pair<String, Object>>>> {
36      private final MainController mainController;
37  
38      /**
39       * Creates an instance of the worker
40       *
41       * @param mainController the mainController that started the worker
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              // 5 or less
56              if (n < 100) {
57                  // 1 or 2
58                  if (n < 10) { return 1; } else { return 2; }
59              } else {
60                  // 3 or 4 or 5
61                  if (n < 1000) { return 3; } else {
62                      // 4 or 5
63                      if (n < 10000) { return 4; } else { return 5; }
64                  }
65              }
66          } else {
67              // 6 or more
68              if (n < 10000000) {
69                  // 6 or 7
70                  if (n < 1000000) { return 6; } else { return 7; }
71              } else {
72                  // 8 to 10
73                  if (n < 100000000) { return 8; } else {
74                      // 9 or 10
75                      if (n < 1000000000) { return 9; } else { return 10; }
76                  }
77              }
78          }
79  
80      }
81  
82  /*    @Override
83      protected void process(List<String> chunks) {
84          // Updates the messages text area
85          chunks.forEach(StatusLbl::setText);
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         ///Select all years
106         //Create a button for each year
107 /*        for (String aRet : ret) {
108             JButton b = new JButton(aRet);
109             b.addActionListener(e -> {
110                 //On button click open Utilities.Year window
111                 new YearWindow(((AbstractButton) e.getSource()).getText());
112 
113             });
114             panel_1.add(b);
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         //System.out.println("Inital load took " + totalTime + "ms");
142         LogToFile.log(null, Severity.FINEST, "Inital load took " + totalTime + "ms");
143         return root;
144     }
145 }