1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Utilities;
21
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
38
39 public static String getConfigLocation() {
40 return ConfigLocation;
41 }
42
43
44
45
46 public static void setConfigLocation(String configLocation) {
47 ConfigLocation = configLocation;
48 }
49
50
51
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
62 prop.load(input);
63
64
65
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
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
93 }
94
95
96
97
98 public static String getDbLoc() {
99 return getProp("databaseLocation");
100 }
101
102
103
104
105 public static String getPrefix() {
106 return getProp("databasePrefix");
107 }
108
109
110
111
112 public static String getSSL() {
113 return getProp("SSL");
114 }
115
116
117
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
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
136
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
149 prop.load(input);
150
151
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
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
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
206 prop.setProperty("databaseLocation", "");
207 prop.setProperty("SSL", "TRUE");
208 prop.setProperty("databasePrefix", "ABOS-Test-");
209
210
211
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
225
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 }