11/*******************************************************************************
22 * Copyright (C) 2009-2016 Lukas Forer and Sebastian Schönherr
3- *
3+ *
44 * This program is free software; you can redistribute it and/or modify
5- * it under the terms of the GNU General Public License as published by
5+ * it under the terms of the GNU General Public License as published by
66 * the Free Software Foundation; either version 3 of the License, or
77 * (at your option) any later version.
8- *
8+ *
99 * This program is distributed in the hope that it will be useful,
1010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1111 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212 * GNU General Public License for more details.
13- *
13+ *
1414 * You should have received a copy of the GNU General Public License
1515 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1616 ******************************************************************************/
@@ -41,50 +41,42 @@ public class DatabaseUpdater {
4141
4242 protected static final Logger log = LoggerFactory .getLogger (DatabaseUpdater .class );
4343
44- private DatabaseConnector connector ;
45-
46- private Database database ;
47-
48- private String oldVersion ;
49-
50- private String currentVersion ;
51-
52- private String filename ;
53-
54- private InputStream updateFileAsStream ;
55-
56- private boolean needUpdate = false ;
57-
58- private Map <String , IUpdateListener > listeners = new HashMap <String , IUpdateListener >();
44+ private final DatabaseConnector connector ;
45+ private final Database database ;
46+ private final String oldVersion ;
47+ private final String currentVersion ;
48+ private final String filename ;
49+ private final InputStream updateFileAsStream ;
50+ private final boolean needUpdate ;
51+ private final Map <String , IUpdateListener > listeners ;
5952
6053 public DatabaseUpdater (Database database , String filename , InputStream updateFileAsStream , String currentVersion ) {
61-
6254 this .filename = filename ;
6355 this .database = database ;
6456 this .connector = database .getConnector ();
6557 this .updateFileAsStream = updateFileAsStream ;
6658 this .currentVersion = currentVersion ;
59+ this .listeners = new HashMap <>();
6760
6861 if (isVersionTableAvailable (database )) {
69-
70- oldVersion = readVersionDB ();
62+ String oldVersion = readVersionDB ();
7163 log .info ("Read current DB version: " + oldVersion );
7264
73- // should not happen, since an entry is created when metadata table
74- // exists
65+ // Should not happen, since an entry is created when metadata table exists.
7566 if (oldVersion == null ) {
7667 oldVersion = readVersion (filename );
7768 log .info ("Read curent version from DB was not successful, read it from file: " + oldVersion );
7869 }
7970
71+ this .oldVersion = oldVersion ;
8072 } else {
8173 // check also file for backwards compatibility
82- oldVersion = readVersion (filename );
74+ this . oldVersion = readVersion (filename );
8375 log .info ("Read current version from file: " + oldVersion );
8476 }
77+
8578 log .info ("Current app version: " + currentVersion );
8679 needUpdate = (compareVersion (currentVersion , oldVersion ) > 0 );
87-
8880 }
8981
9082 public void addUpdate (String version , IUpdateListener listener ) {
@@ -125,7 +117,6 @@ public boolean updateDB() {
125117
126118 public boolean update () {
127119 if (needUpdate ) {
128-
129120 log .info ("Updating database from " + oldVersion + " to " + currentVersion + "..." );
130121
131122 try {
@@ -146,21 +137,17 @@ public boolean update() {
146137 }
147138
148139 log .info ("Updating database was successful." );
149-
150140 }
151141
152142 return true ;
153-
154143 }
155144
156145 public boolean needUpdate () {
157146 return needUpdate ;
158147 }
159148
160149 public void writeVersion (String newVersion ) {
161-
162150 try {
163-
164151 if (!isVersionTableAvailable (database )) {
165152 createVersionTable (database );
166153 }
@@ -185,31 +172,20 @@ public void writeVersion(String newVersion) {
185172 }
186173
187174 public String readVersion (String versionFile ) {
188-
189175 File file = new File (versionFile );
190176
191177 if (file .exists ()) {
192-
193178 try {
194-
195179 return readFileAsString (versionFile );
196-
197180 } catch (Exception e ) {
198-
199181 return "0.0.0" ;
200-
201182 }
202-
203183 } else {
204-
205184 return "0.0.0" ;
206-
207185 }
208-
209186 }
210187
211188 public String readVersionDB () {
212-
213189 String version = null ;
214190
215191 try {
@@ -229,11 +205,11 @@ public String readVersionDB() {
229205 // TODO Auto-generated catch block
230206 e1 .printStackTrace ();
231207 }
208+
232209 return version ;
233210 }
234211
235212 public static String readFileAsString (String filename ) throws java .io .IOException , URISyntaxException {
236-
237213 InputStream is = new FileInputStream (filename );
238214
239215 DataInputStream in = new DataInputStream (is );
@@ -246,7 +222,6 @@ public static String readFileAsString(String filename) throws java.io.IOExceptio
246222 }
247223
248224 in .close ();
249-
250225 return builder .toString ();
251226 }
252227
@@ -261,9 +236,7 @@ public String readAndPrepareSqlClasspath(InputStream filestream, String minVersi
261236 String version = null ;
262237
263238 while ((strLine = br .readLine ()) != null ) {
264-
265239 if (strLine .startsWith ("--" )) {
266-
267240 if (builder .length () > 0 ) {
268241 executeSQLFile (builder .toString (), version );
269242 builder .setLength (0 );
@@ -282,7 +255,6 @@ public String readAndPrepareSqlClasspath(InputStream filestream, String minVersi
282255 listener .beforeUpdate (database );
283256 }
284257 }
285-
286258 }
287259
288260 if (reading ) {
@@ -295,13 +267,10 @@ public String readAndPrepareSqlClasspath(InputStream filestream, String minVersi
295267 executeSQLFile (builder .toString (), version );
296268
297269 in .close ();
298-
299270 return builder .toString ();
300-
301271 }
302272
303273 public void executeSQLFile (String sqlContent , String version ) throws SQLException {
304-
305274 String cleanedSQL = sqlContent
306275 .replaceAll ("(?s)/\\ *.*?\\ */" , "" ) // remove block comments
307276 .replaceAll ("(?m)^\\ s*--.*?$" , "" ) // remove full line comments
@@ -317,27 +286,22 @@ public void executeSQLFile(String sqlContent, String version) throws SQLExceptio
317286 log .info ("DB SQL Update " + version + " finished" );
318287 writeVersion (version );
319288 }
320-
321289 }
322290
323291 public static int compareVersion (String version1 , String version2 ) {
292+ String [] parts1 = version1 .split ("-" , 2 );
293+ String [] parts2 = version2 .split ("-" , 2 );
324294
325- String parts1 [] = version1 .split ("-" , 2 );
326- String parts2 [] = version2 .split ("-" , 2 );
327-
328- String tiles1 [] = parts1 [0 ].split ("\\ ." );
329- String tiles2 [] = parts2 [0 ].split ("\\ ." );
295+ String [] tiles1 = parts1 [0 ].split ("\\ ." );
296+ String [] tiles2 = parts2 [0 ].split ("\\ ." );
330297
331298 for (int i = 0 ; i < tiles1 .length ; i ++) {
332299 int number1 = Integer .parseInt (tiles1 [i ].trim ());
333300 int number2 = Integer .parseInt (tiles2 [i ].trim ());
334301
335302 if (number1 != number2 ) {
336-
337303 return number1 > number2 ? 1 : -1 ;
338-
339304 }
340-
341305 }
342306
343307 if (parts1 .length > 1 ) {
@@ -353,7 +317,6 @@ public static int compareVersion(String version1, String version2) {
353317 }
354318
355319 return 0 ;
356-
357320 }
358321
359322 public boolean isVersionTableAvailable (Database database ) {
@@ -382,5 +345,4 @@ public void createVersionTable(Database database) {
382345 e .printStackTrace ();
383346 }
384347 }
385-
386348}
0 commit comments