1919import java .nio .file .LinkOption ;
2020import java .nio .file .Path ;
2121import java .nio .file .Paths ;
22- import java .sql .*;
22+ import java .sql .Connection ;
23+ import java .sql .DatabaseMetaData ;
24+ import java .sql .DriverManager ;
25+ import java .sql .ResultSet ;
26+ import java .sql .Statement ;
2327import java .text .SimpleDateFormat ;
2428import java .util .ArrayList ;
2529import java .util .Collections ;
30+ import java .util .Date ;
2631import java .util .HashMap ;
2732import java .util .Map ;
2833import java .util .concurrent .TimeUnit ;
@@ -73,11 +78,12 @@ public class MendixUtil {
7378 private static final String MENDIX_USER_NAME = "Mendix-UserName" ;
7479 private static final String DBPASSWORD = "password" ;
7580 private static final String DBUSERNAME = "username" ;
76- private static final String PGDIR = "PGDIR " ; // postgres directory
77- private static final String PGPORT = "PGPORT " ; // postgres port
78- private static final String DLDIR = "DOWNLOADDIR " ;
81+ private static final String PGDIR = "pgdir " ; // postgres directory
82+ private static final String PGPORT = "pgport " ; // postgres port
83+ private static final String DLDIR = "donwnloaddir " ;
7984 private static final String MXAPIUSER = "mxapiuser" ;
8085 private static final String BACKUPNAMING = "backupnaming" ;
86+ private static final String SIZEPREFIX = "lastdownloadsize_" ;
8187 private static final String MRUPREFIX = "mru" ;
8288 private static final String APP_ID = "AppId" ;
8389 // symmetric key for storage
@@ -208,7 +214,7 @@ private boolean tryCustomDirectory(final String directory) {
208214 * @param list SWT List of applications
209215 */
210216 public void restorePreferences (List list , String filter ) {
211- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
217+ Preferences prefs = getPreferences ( );
212218
213219 // second parameter is default value
214220 apiuser = prefs .get (MXAPIUSER , "" );
@@ -319,7 +325,7 @@ public void setUserAndKey(String apiUser, String apiKey, String userName, String
319325 this .postgresDirectory = postgresDirectory ;
320326 this .postgresPort = postgresPort ;
321327 this .downloadDirectory = downloadDirectory ;
322- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
328+ Preferences prefs = getPreferences ( );
323329
324330 prefs .put (MXAPIUSER , apiUser );
325331 prefs .put (DBUSERNAME , userName );
@@ -340,7 +346,7 @@ public void setUserAndKey(String apiUser, String apiKey, String userName, String
340346 * @param backupNaming string with tokens {environment} and {date}
341347 */
342348 public void setBackupNaming (String backupNaming ) {
343- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
349+ Preferences prefs = getPreferences ( );
344350 this .backupNaming = backupNaming ;
345351 prefs .put (BACKUPNAMING , backupNaming );
346352 }
@@ -377,10 +383,9 @@ public String getPostgresVersion() {
377383 * @param createdOn Date of the backup
378384 * @return Unique databasename
379385 */
380- public String getTargetDatabaseName (String appid , String environment , Long createdOn ) {
381- Date date = new Date (createdOn );
386+ public String getTargetDatabaseName (String appid , String environment , Date createdOn ) {
382387 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd" );
383- String strDate = formatter .format (date );
388+ String strDate = formatter .format (createdOn );
384389 String dbName = appid + backupNaming .replace ("{environment}" , environment ).replace ("{date}" , strDate );
385390 String ext = getNonExistingDatabaseExt (dbName );
386391 return dbName + ext ;
@@ -599,8 +604,9 @@ public int AppIndexByAppName(String appName) {
599604 *
600605 * @param selectionCount selected line of list
601606 * @param environment selected environment (production, acceptance, test)
607+ * return true is list has at least one item.
602608 */
603- public void GetBackupList (int selectionCount , String environment ) {
609+ public boolean GetBackupList (int selectionCount , String environment ) {
604610 if (selectionCount >= 0 ) {
605611 int appIndex = AppIndexByListIndex (selectionCount );
606612 String appid = apps .getJSONObject (appIndex ).getString (APP_ID );
@@ -621,23 +627,27 @@ public void GetBackupList(int selectionCount, String environment ) {
621627 consoleWrite (result );
622628 this .backups = new JSONArray (result );
623629 SimpleDateFormat dt1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm" );
630+ SimpleDateFormat parser = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
624631 for (int i = 0 ; i < backups .length (); i ++) {
625632 // if a backup is being created createdon is null
626633 if (!backups .getJSONObject (i ).isNull (CREATED_ON )) {
627- backuplist .add (dt1 .format (new Date (backups .getJSONObject (i ).getLong (CREATED_ON ))));
634+ backuplist .add (dt1 .format (parser . parse (backups .getJSONObject (i ).getString (CREATED_ON ))));
628635 }
629636 }
637+ return backups .length () > 0 ;
630638 } else {
631639 consoleWrite ("Error getting backuplist" );
632640 consoleWrite (result );
641+ return false ;
633642 }
634643 } catch (Exception e ) {
635644 consoleWrite ("Error getting list " + e .getMessage ());
636645 e .printStackTrace ();
637646 }
647+ } else {
648+ return false ;
638649 }
639-
640-
650+ return false ;
641651 }
642652 /**
643653 * Retrieve the backup link of the OnlyDatabase from the json and download the file into downloads
@@ -664,13 +674,13 @@ public void run()
664674 if (apps != null ) {
665675 String appid = "" ;
666676 String backupid = "" ;
667- Long createdOn = 0L ;
677+ Date createdOn = null ;
668678 try {
669679 appid = apps .getJSONObject (AppIndexByListIndex (selectedAppIndex )).getString (APP_ID );
670680 backupid = backups .getJSONObject (selectedBackupIndex ).getString (SNAPSHOT_ID );
671681 // if a backup is being created createdon is null
672682 if (!backups .getJSONObject (selectedBackupIndex ).isNull (CREATED_ON )) {
673- createdOn = backups .getJSONObject (selectedBackupIndex ).getLong (CREATED_ON );
683+ createdOn = parseJSONDate ( backups .getJSONObject (selectedBackupIndex ).getString (CREATED_ON ) );
674684 }
675685 } catch (Exception exp ) {
676686 consoleWrite ("Error getting list " + exp .getMessage ());
@@ -710,8 +720,7 @@ public void run()
710720 String dbFilename = filename ;
711721 String extractDirectory = null ;
712722 if (includeDocuments ) {
713- // todo extract dir terugkrijgen
714- // files verplaatsen naar doel documenten directory
723+ // todo get extract dir - move file to target directory
715724 extractDirectory = extractTarGz (filename );
716725 dbFilename = extractDirectory + File .separator +"db" + File .separator + "db.backup" ;
717726 }
@@ -855,10 +864,10 @@ public void run()
855864 if (apps != null ) {
856865 String appid = apps .getJSONObject (AppIndexByListIndex (selectedAppIndex )).getString (APP_ID );
857866 String backupid = backups .getJSONObject (selectedBackupIndex ).getString (SNAPSHOT_ID );
858- Long createdOn = 0l ;
867+ Date createdOn = null ;
859868 // if a backup is being created createdon is null
860869 if (!backups .getJSONObject (selectedBackupIndex ).isNull (CREATED_ON )) {
861- createdOn = backups .getJSONObject (selectedBackupIndex ).getLong (CREATED_ON );
870+ createdOn = parseJSONDate ( backups .getJSONObject (selectedBackupIndex ).getString (CREATED_ON ) );
862871 }
863872 OkHttpClient client = getClient (60 );
864873
@@ -987,7 +996,7 @@ public void ShowMessage(String message, Shell shell) {
987996 *
988997 * @param message Message to write
989998 */
990- private void consoleWrite (String message ) {
999+ void consoleWrite (String message ) {
9911000 display .asyncExec (new Runnable () {
9921001 public void run () {
9931002 if (consoleList != null && message != null ) {
@@ -1109,7 +1118,7 @@ public String downloadFile(String fileURL, String appid, String environment, Str
11091118 URL url ;
11101119 try {
11111120 url = new URL (fileURL );
1112- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
1121+ Preferences prefs = getPreferences ( );
11131122 HttpURLConnection httpConn = (HttpURLConnection ) url .openConnection ();
11141123 int responseCode = httpConn .getResponseCode ();
11151124
@@ -1163,7 +1172,7 @@ public String downloadFile(String fileURL, String appid, String environment, Str
11631172 }
11641173 }
11651174 // store for next time progress bar
1166- prefs .putLong (appid +ext , totalbytesRead );
1175+ prefs .putLong (SIZEPREFIX + appid +ext , totalbytesRead );
11671176 } catch (IOException e ) {
11681177 consoleWrite ("Error " + e .getMessage ());
11691178 e .printStackTrace ();
@@ -1288,7 +1297,7 @@ public void OpenBrowser(String path, int selectedApp) {
12881297 */
12891298
12901299 public String [] getMRUItems () {
1291- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
1300+ Preferences prefs = getPreferences ( );
12921301
12931302 ArrayList <String > mrulist = new ArrayList <String >();
12941303 for (int i = 0 ; i <MAX_MRU ; i ++) {
@@ -1302,13 +1311,21 @@ public String[] getMRUItems() {
13021311 }
13031312 return mrulist .toArray (new String [mrulist .size ()]);
13041313 }
1314+
1315+ /**
1316+ * Get the preferences storage
1317+ * @return Preferences
1318+ */
1319+ private Preferences getPreferences () {
1320+ return Preferences .userNodeForPackage (MendixUtil .class );
1321+ }
13051322 /**
13061323 * Add an app to the MRU list, max 6 items. Latest item is not put on top.
13071324 * @param appindex index of application
13081325 */
13091326
13101327 public void addMRU (int appindex ) {
1311- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
1328+ Preferences prefs = getPreferences ( );
13121329
13131330 // shift them up.
13141331 for (int i =MAX_MRU ; i >=0 ; i --) {
@@ -1321,12 +1338,30 @@ public void addMRU(int appindex) {
13211338 */
13221339
13231340 public void clearMRU () {
1324- Preferences prefs = Preferences . userNodeForPackage ( MendixUtil . class );
1341+ Preferences prefs = getPreferences ( );
13251342 for (int i = MAX_MRU ; i >=0 ; i --) {
13261343 prefs .putInt (MRUPREFIX +i , -1 );
13271344 }
1328-
13291345 }
1346+
1347+ /**
1348+ * Parse the XML date from JSON
1349+ * Returns Date
1350+ */
13301351
1352+ public java .util .Date parseJSONDate (String jsonDate ) {
1353+ if (jsonDate != null && !jsonDate .isEmpty ()) {
1354+ try {
1355+ SimpleDateFormat parser = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
1356+ return parser .parse (jsonDate );
1357+ } catch (Exception e ) {
1358+ consoleWrite ("Error parsing date " + jsonDate );
1359+ return null ;
1360+ }
1361+ } else {
1362+ return null ;
1363+ }
1364+
1365+ }
13311366
13321367}
0 commit comments