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 Utilities;/*
21    Created by patrick on 4/16/15.
22   */
23  
24  import Launchers.Settings;
25  import javafx.scene.control.Alert;
26  import javafx.scene.control.ButtonBar;
27  import javafx.scene.control.ButtonType;
28  
29  import java.io.*;
30  import java.util.Optional;
31  import java.util.Properties;
32  
33  public class Config {
34      private static String ConfigLocation = "./ABOSConfig.properties";
35  
36      /**
37       * @return the configurationFile location
38       */
39      public static String getConfigLocation() {
40          return ConfigLocation;
41      }
42  
43      /**
44       * @param configLocation The location of the configuration file
45       */
46      public static void setConfigLocation(String configLocation) {
47          ConfigLocation = configLocation;
48      }
49  
50      /**
51       * @return If the configuration file specified exists
52       */
53      public static boolean doesConfExist() {
54          @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") Properties prop = new Properties();
55          InputStream input = null;
56          boolean loc = false;
57          try {
58  
59              input = new FileInputStream(ConfigLocation);
60  
61              // load a properties file
62              prop.load(input);
63  
64              // get the property value and print it out
65              //loc = prop.getProperty("databaseLocation");
66  
67  
68          } catch (FileNotFoundException ignored) {
69              return false;
70          } catch (IOException ex) {
71              if ("LGconfig.properties (No such file or directory)".equals(ex.getMessage())) {
72                  return false;
73              } else {
74                  LogToFile.log(ex, Severity.SEVERE, "Error reading config file state, ensure the software has access to the directory.");
75              }
76              //System.out.print(ex.getMessage());
77          } finally {
78              if (input != null) {
79                  try {
80                      input.close();
81                      loc = true;
82                  } catch (IOException e) {
83                      LogToFile.log(e, Severity.WARNING, "");
84                  }
85  
86              }
87  
88          }
89  
90  
91          return loc;
92          //return true;
93      }
94  
95      /**
96       * @return The database URL
97       */
98      public static String getDbLoc() {
99          return getProp("databaseLocation");
100     }
101 
102     /**
103      * @return The prefix for the database
104      */
105     public static String getPrefix() {
106         return getProp("databasePrefix");
107     }
108 
109     /**
110      * @return The SSL choice
111      */
112     public static String getSSL() {
113         return getProp("SSL");
114     }
115 
116     /**
117      * @return The program version
118      */
119     public static Version getProgramVersion() {
120         final Properties properties = new Properties();
121         try {
122             properties.load(Config.class.getClass().getResourceAsStream("/CompileProps.properties"));
123 
124             // read each line and write to System.out
125             return new Version(properties.getProperty("Version"));
126 
127 
128         } catch (IOException e) {
129             LogToFile.log(e, Severity.SEVERE, "Unable to determine program version. Please attempt a re-install of the software.");
130             return new Version("");
131         }
132     }
133 
134     /**
135      * @param property The propertiy to retrieve
136      * @return The value of Property
137      */
138     @Nonnull
139     public static String getProp(String property) {
140         if (doesConfExist()) {
141             Properties prop = new Properties();
142             InputStream input = null;
143             String loc = "";
144             try {
145 
146                 input = new FileInputStream(ConfigLocation);
147 
148                 // load a properties file
149                 prop.load(input);
150 
151                 // get the property value and print it out
152                 loc = prop.getProperty(property, "");
153 
154             } catch (IOException ex) {
155                 LogToFile.log(ex, Severity.SEVERE, "Error reading config file, ensure the software has access to the directory.");
156 
157                 //System.out.print(ex.getMessage());
158             } finally {
159                 if (input != null) {
160                     try {
161                         input.close();
162                     } catch (IOException e) {
163                         LogToFile.log(e, Severity.WARNING, "");
164 
165                     }
166                 }
167             }
168 
169 
170             return loc;
171         } else {
172             createConfigFile();
173             Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
174             alert.setTitle("Set Configuration?");
175             alert.setHeaderText("Software needs to be configured.");
176             alert.setContentText("A configuration file has been created. Would you like to edit it?");
177 
178             ButtonType buttonTypeOne = new ButtonType("Edit");
179             ButtonType buttonTypeTwo = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
180 
181             alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo);
182 
183             Optional<ButtonType> result = alert.showAndWait();
184 
185             if (result.get() == buttonTypeOne) {
186                 new Settings();
187                 return getProp(property);
188 
189             } else {
190                 return "";
191             }
192         }
193     }
194 
195     /**
196      * Creates a default configuration file
197      */
198     public static void createConfigFile() {
199         Properties prop = new Properties();
200         OutputStream output = null;
201 
202         try {
203             output = new FileOutputStream(ConfigLocation);
204 
205             //Add DB setting
206             prop.setProperty("databaseLocation", "");
207             prop.setProperty("SSL", "TRUE");
208             prop.setProperty("databasePrefix", "ABOS-Test-");
209 
210 
211             //AddCustomer
212             {
213                 prop.setProperty("CustomerName", "");
214                 prop.setProperty("CustomerAddress", "");
215                 prop.setProperty("CustomerZipCode", "");
216                 prop.setProperty("CustomerTown", "");
217                 prop.setProperty("CustomerState", "");
218                 prop.setProperty("CustomerPhone", "");
219                 prop.setProperty("CustomerEmail", "");
220                 prop.setProperty("CustomerPaid", "");
221                 prop.setProperty("CustomerDelivered", "");
222                 prop.setProperty("CustomerDonation", "");
223             }
224             //Maps
225             //Launchers.Reports
226             {
227                 prop.setProperty("ReportType", "");
228                 prop.setProperty("ScoutName", "");
229                 prop.setProperty("ScoutAddress", "");
230                 prop.setProperty("ScoutZip", "");
231                 prop.setProperty("ScoutTown", "");
232                 prop.setProperty("ScoutState", "");
233                 prop.setProperty("ScoutPhone", "");
234 
235                 prop.setProperty("ScoutRank", "");
236                 prop.setProperty("logoLoc", "");
237                 prop.setProperty("pdfLoc", "");
238 
239             }
240             prop.store(output, null);
241 
242         } catch (IOException io) {
243             LogToFile.log(io, Severity.SEVERE, "Error writing settings file. Please try again.");
244         } finally {
245             if (output != null) {
246                 try {
247                     output.close();
248                 } catch (IOException e) {
249                     LogToFile.log(e, Severity.SEVERE, "Error closing settings file. Please try again.");
250                 }
251             }
252 
253         }
254     }
255 }