LayoutItemPortalDeepCloneTest: Test something to avoid warnings.
[online-glom:gwt-glom.git] / src / test / java / org / glom / web / server / SelfHoster.java
1 /*
2  * Copyright (C) 2012 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.net.ServerSocket;
30 import java.sql.Connection;
31 import java.sql.DriverManager;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Properties;
39
40 import org.apache.commons.lang3.StringUtils;
41 import org.glom.web.server.libglom.Document;
42 import org.glom.web.shared.DataItem;
43 import org.glom.web.shared.libglom.Field;
44 import org.jooq.InsertResultStep;
45 import org.jooq.InsertSetStep;
46 import org.jooq.Record;
47 import org.jooq.SQLDialect;
48 import org.jooq.Table;
49 import org.jooq.exception.DataAccessException;
50 import org.jooq.impl.Factory;
51
52 import com.google.common.io.Files;
53 import com.ibm.icu.text.NumberFormat;
54
55 /**
56  * @author Murray Cumming <murrayc@murrayc.com>
57  * 
58  */
59 public class SelfHoster {
60         // private String tempFilepathDir = "";
61         private boolean selfHostingActive = false;
62         private Document document = null;
63         private String username = "";
64         private String password = "";
65
66         SelfHoster(final Document document) {
67                 this.document = document;
68         }
69
70         private static final int PORT_POSTGRESQL_SELF_HOSTED_START = 5433;
71         private static final int PORT_POSTGRESQL_SELF_HOSTED_END = 5500;
72
73         private static final String DEFAULT_CONFIG_PG_HBA_LOCAL_8p4 = "# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD\n"
74                         + "\n"
75                         + "# local is for Unix domain socket connections only\n"
76                         + "# trust allows connection from the current PC without a password:\n"
77                         + "local   all         all                               trust\n"
78                         + "local   all         all                               ident\n"
79                         + "local   all         all                               md5\n"
80                         + "\n"
81                         + "# TCP connections from the same computer, with a password:\n"
82                         + "host    all         all         127.0.0.1    255.255.255.255    md5\n"
83                         + "# IPv6 local connections:\n"
84                         + "host    all         all         ::1/128               md5\n";
85
86         private static final String DEFAULT_CONFIG_PG_IDENT = "";
87         private static final String FILENAME_DATA = "data";
88
89         public boolean createAndSelfHostFromExample(final Document.HostingMode hostingMode) {
90
91                 if (!createAndSelfHostNewEmpty(hostingMode)) {
92                         // std::cerr << G_STRFUNC << ": test_create_and_selfhost_new_empty() failed." << std::endl;
93                         return false;
94                 }
95
96                 final boolean recreated = recreateDatabaseFromDocument(); /* TODO: Progress callback */
97                 if (!recreated) {
98                         if (!cleanup()) {
99                                 return false;
100                         }
101                 }
102
103                 return recreated;
104         }
105
106         /**
107          * @param document
108          * @param
109          * @param subDirectoryPath
110          * @return
111          */
112         private boolean createAndSelfHostNewEmpty(final Document.HostingMode hostingMode) {
113                 if (hostingMode != Document.HostingMode.HOSTING_MODE_POSTGRES_SELF) {
114                         // TODO: std::cerr << G_STRFUNC << ": This test function does not support the specified hosting_mode: " <<
115                         // hosting_mode << std::endl;
116                         return false;
117                 }
118
119                 // Save a copy, specifying the path to file in a directory:
120                 // For instance, /tmp/testglom/testglom.glom");
121                 final String tempFilename = "testglom";
122                 final File tempFolder = Files.createTempDir();
123                 final File tempDir = new File(tempFolder, tempFilename);
124
125                 final String tempDirPath = tempDir.getPath();
126                 final String tempFilePath = tempDirPath + File.separator + tempFilename;
127                 final File file = new File(tempFilePath);
128
129                 // Make sure that the file does not exist yet:
130                 {
131                         tempDir.delete();
132                 }
133
134                 // Save the example as a real file:
135                 document.setFileURI(file.getPath());
136
137                 document.setHostingMode(hostingMode);
138                 document.setIsExampleFile(false);
139                 final boolean saved = document.save();
140                 if (!saved) {
141                         System.out.println("createAndSelfHostNewEmpty(): Document.save() failed.");
142                         return false; // TODO: Delete the directory.
143                 }
144
145                 // We must specify a default username and password:
146                 final String user = "glom_default_developer_user";
147                 final String password = "glom_default_developer_password";
148
149                 // Create the self-hosting files:
150                 if (!initialize(user, password)) {
151                         System.out.println("createAndSelfHostNewEmpty(): initialize failed.");
152                         // TODO: Delete directory.
153                 }
154
155                 // Check that it really created some files:
156                 if (!tempDir.exists()) {
157                         System.out.println("createAndSelfHostNewEmpty(): tempDir does not exist.");
158                         // TODO: Delete directory.
159                 }
160
161                 return selfHost(user, password);
162         }
163
164         /**
165          * @param document
166          * @param user
167          * @param password
168          * @return
169          */
170         private boolean selfHost(final String user, final String password) {
171                 // TODO: m_network_shared = network_shared;
172
173                 if (getSelfHostingActive()) {
174                         // TODO: std::cerr << G_STRFUNC << ": Already started." << std::endl;
175                         return false; // STARTUPERROR_NONE; //Just do it once.
176                 }
177
178                 final String dbDirData = getSelfHostingDataPath(false);
179                 if (StringUtils.isEmpty(dbDirData) || !fileExists(dbDirData)) {
180                         /*
181                          * final String dbDirBackup = dbDir + File.separator + FILENAME_BACKUP;
182                          * 
183                          * if(fileExists(dbDirBackup)) { //TODO: std::cerr << G_STRFUNC <<
184                          * ": There is no data, but there is backup data." << std::endl; //Let the caller convert the backup to real
185                          * data and then try again: return false; // STARTUPERROR_FAILED_NO_DATA_HAS_BACKUP_DATA; } else {
186                          */
187                         // TODO: std::cerr << "ConnectionPool::create_self_hosting(): The data sub-directory could not be found." <<
188                         // dbdir_data_uri << std::endl;
189                         return false; // STARTUPERROR_FAILED_NO_DATA;
190                         // }
191                 }
192
193                 final int availablePort = discoverFirstFreePort(PORT_POSTGRESQL_SELF_HOSTED_START,
194                                 PORT_POSTGRESQL_SELF_HOSTED_END);
195                 // std::cout << "debug: " << G_STRFUNC << ":() : debug: Available port for self-hosting: " << available_port <<
196                 // std::endl;
197                 if (availablePort == 0) {
198                         // TODO: Use a return enum or exception so we can tell the user about this:
199                         // TODO: std::cerr << G_STRFUNC << ": No port was available between " << PORT_POSTGRESQL_SELF_HOSTED_START
200                         // << " and " << PORT_POSTGRESQL_SELF_HOSTED_END << std::endl;
201                         return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
202                 }
203
204                 final NumberFormat format = NumberFormat.getInstance(Locale.US);
205                 format.setGroupingUsed(false); // TODO: Does this change it system-wide?
206                 final String portAsText = format.format(availablePort);
207
208                 // -D specifies the data directory.
209                 // -c config_file= specifies the configuration file
210                 // -k specifies a directory to use for the socket. This must be writable by us.
211                 // Make sure to use double quotes for the executable path, because the
212                 // CreateProcess() API used on Windows does not support single quotes.
213                 final String dbDir = getSelfHostingPath("", false);
214                 final String dbDirConfig = getSelfHostingPath("config", false);
215                 final String dbDirHba = dbDirConfig + File.separator + "pg_hba.conf";
216                 final String dbDirIdent = dbDirConfig + File.separator + "pg_ident.conf";
217                 final String dbDirPid = getSelfHostingPath("pid", false);
218
219                 // Note that postgres returns this error if we split the arguments more,
220                 // for instance splitting -D and dbDirData into separate strings:
221                 // too many command-line arguments (first is "(null)")
222                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
223                 // though that does not happen with the normal command line.
224                 // However, we must have a space after -k.
225                 // Also, the c hba_file=path argument must be split after -c, or postgres will get a " hba_file" configuration
226                 // parameter instead of "hba_file".
227                 final String commandPathStart = getPathToPostgresExecutable("postgres");
228                 if (StringUtils.isEmpty(commandPathStart)) {
229                         System.out.println("selfHost(): getPathToPostgresExecutable(postgres) failed.");
230                         return false;
231                 }
232                 final ProcessBuilder commandPostgresStart = new ProcessBuilder(commandPathStart, "-D" + shellQuote(dbDirData),
233                                 "-p", portAsText, "-i", // Equivalent to -h "*", which in turn is equivalent
234                                                                                 // to
235                                 // listen_addresses in postgresql.conf. Listen to all IP addresses,
236                                 // so any client can connect (with a username+password)
237                                 "-c", "hba_file=" + shellQuote(dbDirHba), "-c", "ident_file=" + shellQuote(dbDirIdent), "-k"
238                                                 + shellQuote(dbDir), "--external_pid_file=" + shellQuote(dbDirPid));
239                 // std::cout << G_STRFUNC << ": debug: " << command_postgres_start << std::endl;
240
241                 // Make sure to use double quotes for the executable path, because the
242                 // CreateProcess() API used on Windows does not support single quotes.
243                 //
244                 // Note that postgres returns this error if we split the arguments more,
245                 // for instance splitting -D and dbDirData into separate strings:
246                 // too many command-line arguments (first is "(null)")
247                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
248                 // though that does not happen with the normal command line.
249                 final String commandPathCheck = getPathToPostgresExecutable("pg_ctl");
250                 if (StringUtils.isEmpty(commandPathCheck)) {
251                         System.out.println("selfHost(): getPathToPostgresExecutable(pg_ctl) failed.");
252                         return false;
253                 }
254                 final ProcessBuilder commandCheckPostgresHasStarted = new ProcessBuilder(commandPathCheck, "status", "-D"
255                                 + shellQuote(dbDirData));
256
257                 // For postgres 8.1, this is "postmaster is running".
258                 // For postgres 8.2, this is "server is running".
259                 // This is a big hack that we should avoid. murrayc.
260                 //
261                 // pg_ctl actually seems to return a 0 result code for "is running" and a 1 for not running, at least with
262                 // Postgres 8.2,
263                 // so maybe we can avoid this in future.
264                 // Please do test it with your postgres version, using "echo $?" to see the result code of the last command.
265                 final String secondCommandSuccessText = "is running"; // TODO: This is not a stable API. Also, watch out for
266                                                                                                                                 // localisation.
267
268                 // The first command does not return, but the second command can check whether it succeeded:
269                 // TODO: Progress
270                 final boolean result = executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(commandPostgresStart,
271                                 commandCheckPostgresHasStarted, secondCommandSuccessText);
272                 if (!result) {
273                         System.out.println("selfHost(): Error while attempting to self-host a database.");
274                         return false; // STARTUPERROR_FAILED_UNKNOWN_REASON;
275                 }
276
277                 // Remember the port for later:
278                 document.setConnectionPort(availablePort);
279
280                 // Check that we can really connect:
281
282                 // pg_ctl sometimes reports success before it is really ready to let us connect,
283                 // so in this case we can just keep trying until it works, for a while:
284                 for (int i = 0; i < 10; i++) {
285
286                         try {
287                                 Thread.sleep(1000);
288                         } catch (InterruptedException e) {
289                                 // TODO Auto-generated catch block
290                                 e.printStackTrace();
291                         }
292
293                         final String dbName = document.getConnectionDatabase();
294                         document.setConnectionDatabase(""); // We have not created the database yet.
295                         final Connection connection = createConnection();
296                         document.setConnectionDatabase(dbName);
297                         if (connection != null) {
298                                 return true; // STARTUPERROR_NONE;
299                         }
300
301                         System.out
302                                         .println("selfHost(): Waiting and retrying the connection due to suspected too-early success of pg_ctl. retries="
303                                                         + i);
304                 }
305
306                 System.out.println("selfHost(): Test connection failed after multiple retries.");
307                 return false;
308         }
309
310         /**
311          * @param dbDirData
312          * @return
313          */
314         private String shellQuote(final String str) {
315                 // TODO: If we add the quotes then they seem to be used as part of the path, though that is not a problem with
316                 // the normal command line.
317                 return str;
318
319                 // TODO: Escape.
320                 // return "'" + str + "'";
321         }
322
323         private String getSelfHostingPath(final String subpath, final boolean create) {
324                 final String dbDir = document.getSelfHostedDirectoryPath();
325                 if (StringUtils.isEmpty(subpath)) {
326                         return dbDir;
327                 }
328
329                 final String dbDirData = dbDir + File.separator + subpath;
330                 final File file = new File(dbDirData);
331
332                 // Return the path regardless of whether it exists:
333                 if (!create) {
334                         return dbDirData;
335                 }
336
337                 if (!file.exists()) {
338                         try {
339                                 Files.createParentDirs(file);
340                         } catch (final IOException e) {
341                                 // TODO Auto-generated catch block
342                                 e.printStackTrace();
343                                 return "";
344                         }
345
346                         if (!file.mkdir()) {
347                                 return "";
348                         }
349                 }
350
351                 return dbDirData;
352         }
353
354         private String getSelfHostingDataPath(final boolean create) {
355                 return getSelfHostingPath(FILENAME_DATA, create);
356         }
357
358         private boolean executeCommandLineAndWait(final ProcessBuilder command) {
359
360                 command.redirectErrorStream(true);
361
362                 // Run the first command, and wait for it to return:
363                 Process process = null;
364                 try {
365                         process = command.start();
366                 } catch (final IOException e) {
367                         // TODO Auto-generated catch block
368                         e.printStackTrace();
369                         return false;
370                 }
371
372                 final InputStream stderr = process.getInputStream();
373                 final InputStreamReader isr = new InputStreamReader(stderr);
374                 final BufferedReader br = new BufferedReader(isr);
375                 String output = "";
376                 String line;
377                 /*
378                  * try { //TODO: readLine() can hang, waiting for an end of line that never comes. while ((line = br.readLine())
379                  * != null) { output += line + "\n"; } } catch (final IOException e1) { e1.printStackTrace(); return false; }
380                  */
381
382                 int result = 0;
383                 try {
384                         result = process.waitFor();
385                 } catch (final InterruptedException e) {
386                         // TODO Auto-generated catch block
387                         e.printStackTrace();
388                         return false;
389                 }
390
391                 if (result != 0) {
392                         System.out.println("executeCommandLineAndWait(): Command failed: " + command.command().toString());
393                         System.out.print("  Output: " + output);
394                         return false;
395                 }
396
397                 return true;
398         }
399
400         private boolean executeCommandLineAndWaitUntilSecondCommandReturnsSuccess(final ProcessBuilder command,
401                         final ProcessBuilder commandSecond, final String secondCommandSuccessText) {
402                 command.redirectErrorStream(true);
403
404                 // Run the first command, and do not wait for it to return:
405                 Process process = null;
406                 try {
407                         process = command.start();
408                 } catch (final IOException e) {
409                         // TODO Auto-generated catch block
410                         e.printStackTrace();
411                         return false;
412                 }
413
414                 final InputStream stderr = process.getInputStream();
415                 final InputStreamReader isr = new InputStreamReader(stderr);
416                 final BufferedReader br = new BufferedReader(isr);
417
418                 /*
419                  * We do not wait, because this (postgres, for instance), does not return: final int result = process.waitFor();
420                  * if (result != 0) { // TODO: Warn. return false; }
421                  */
422
423                 // Now run the second command, usually to verify that the first command has really done its work:
424                 // We run this repeatedly until it succeeds, to show that the first command has finished.
425                 boolean result = false;
426                 while (true) {
427                         result = executeCommandLineAndWait(commandSecond);
428                         if (result) {
429                                 System.out.println("executeCommandLineAndWait(): second command succeeded.");
430                                 return true;
431                         } else {
432                                 try {
433                                         Thread.sleep(1000);
434                                 } catch (InterruptedException e) {
435                                         // TODO Auto-generated catch block
436                                         e.printStackTrace();
437                                         return false;
438                                 }
439
440                                 System.out.println("executeCommandLineAndWait(): Trying the second command again.");
441                         }
442                 }
443
444                 // Try to get the output:
445                 /*
446                  * if (!result) { String output = ""; /* String line; try { // TODO: readLine() can hang, waiting for an end of
447                  * line that never comes. while ((line = br.readLine()) != null) { output += line + "\n";
448                  * System.out.println(line); } } catch (final IOException e1) { // TODO Auto-generated catch block
449                  * e1.printStackTrace(); return false; }
450                  */
451
452                 // System.out.println("  Output of first command: " + output);
453                 // System.out.println("  first command: " + command.command().toString());
454                 // System.out.println("  second command: " + commandSecond.command().toString());
455                 // }
456         }
457
458         /**
459          * @param string
460          * @return
461          */
462         private static String getPathToPostgresExecutable(final String string) {
463                 final List<String> dirPaths = new ArrayList<String>();
464                 dirPaths.add("/usr/bin");
465                 dirPaths.add("/usr/lib/postgresql/9.1/bin");
466                 dirPaths.add("/usr/lib/postgresql/9.0/bin");
467                 dirPaths.add("/usr/lib/postgresql/8.4/bin");
468
469                 for (String dir : dirPaths) {
470                         final String path = dir + File.separator + string;
471                         if (fileExistsAndIsExecutable(path)) {
472                                 return path;
473                         }
474                 }
475
476                 return "";
477         }
478
479         /**
480          * @param path
481          * @return
482          */
483         private static boolean fileExistsAndIsExecutable(String path) {
484                 final File file = new File(path);
485                 if (!file.exists()) {
486                         return false;
487                 }
488
489                 if (!file.canExecute()) {
490                         return false;
491                 }
492
493                 return true;
494         }
495
496         /**
497          * @param start
498          * @param end
499          * @return
500          */
501         private static int discoverFirstFreePort(final int start, final int end) {
502                 for (int port = start; port <= end; ++port) {
503                         try {
504                                 final ServerSocket socket = new ServerSocket(port);
505
506                                 // If the instantiation succeeded then the port was free:
507                                 return socket.getLocalPort(); // This must equal port.
508                         } catch (final IOException ex) {
509                                 continue; // try next port
510                         }
511                 }
512
513                 return 0;
514         }
515
516         /**
517          * @param dbDir
518          * @return
519          */
520         private static boolean fileExists(final String filePath) {
521                 final File file = new File(filePath);
522                 return file.exists();
523         }
524
525         /**
526          * @return
527          */
528         private boolean getSelfHostingActive() {
529                 return selfHostingActive;
530         }
531
532         /**
533          * @param cpds
534          * @return
535          */
536         private boolean initialize(final String initialUsername, final String initialPassword) {
537                 if (!initializeConfFiles()) {
538                         System.out.println("initialize(): initializeConfFiles() failed.");
539                         return false;
540                 }
541
542                 // initdb creates a new postgres database cluster:
543
544                 // Get file:// URI for the tmp/ directory:
545                 File filePwFile = null;
546                 try {
547                         filePwFile = File.createTempFile("glom_initdb_pwfile", "");
548                 } catch (final IOException e) {
549                         // TODO Auto-generated catch block
550                         e.printStackTrace();
551                 }
552                 final String tempPwFile = filePwFile.getPath();
553
554                 final boolean pwfileCreationSucceeded = createTextFile(tempPwFile, initialPassword);
555                 if (!pwfileCreationSucceeded) {
556                         System.out.println("initialize(): createTextFile() failed.");
557                         return false;
558                 }
559
560                 // Make sure to use double quotes for the executable path, because the
561                 // CreateProcess() API used on Windows does not support single quotes.
562                 final String dbDirData = getSelfHostingDataPath(false /* create */);
563
564                 // Note that initdb returns this error if we split the arguments more,
565                 // for instance splitting -D and dbDirData into separate strings:
566                 // too many command-line arguments (first is "(null)")
567                 // TODO: If we quote tempPwFile then initdb says that it cannot find it.
568                 // Note: If we use "-D " instead of "-D" then the initdb seems to make the space part of the filepath,
569                 // though that does not happen with the normal command line.
570                 boolean result = false;
571                 final String commandPath = getPathToPostgresExecutable("initdb");
572                 if (StringUtils.isEmpty(commandPath)) {
573                         System.out.println("initialize(): getPathToPostgresExecutable(initdb) failed.");
574                 } else {
575                         final ProcessBuilder commandInitdb = new ProcessBuilder(commandPath, "-D" + shellQuote(dbDirData), "-U",
576                                         initialUsername, "--pwfile=" + tempPwFile);
577
578                         // Note that --pwfile takes the password from the first line of a file. It's an alternative to supplying it
579                         // when
580                         // prompted on stdin.
581                         result = executeCommandLineAndWait(commandInitdb);
582                 }
583
584                 // Of course, we don't want this to stay around. It would be a security risk.
585                 final File fileTempPwFile = new File(tempPwFile);
586                 if (!fileTempPwFile.delete()) {
587                         System.out.println("initialize(): Failed to delete the password file.");
588                 }
589
590                 if (!result) {
591                         System.out.println("initialize(): Error while attempting to create self-hosting database.");
592                         return false;
593                 }
594
595                 // Save the username and password for later;
596                 this.username = initialUsername;
597                 this.password = initialPassword;
598
599                 return result; // ? INITERROR_NONE : INITERROR_COULD_NOT_START_SERVER;
600
601         }
602
603         private boolean initializeConfFiles() {
604                 final String dataDirPath = document.getSelfHostedDirectoryPath();
605
606                 final String dbDirConfig = dataDirPath + File.separator + "config";
607                 // String defaultConfContents = "";
608
609                 // Choose the configuration contents based on the postgresql version
610                 // and whether we want to be network-shared:
611                 // final float postgresqlVersion = 9.0f; //TODO: get_postgresql_utils_version_as_number(slot_progress);
612                 // final boolean networkShared = true;
613                 // std::cout << "DEBUG: postgresql_version=" << postgresql_version << std::endl;
614
615                 // TODO: Support the other configurations, as in libglom.
616                 final String defaultConfContents = DEFAULT_CONFIG_PG_HBA_LOCAL_8p4;
617
618                 // std::cout << "DEBUG: default_conf_contents=" << default_conf_contents << std::endl;
619
620                 final boolean hbaConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_hba.conf",
621                                 defaultConfContents);
622                 if (!hbaConfCreationSucceeded) {
623                         System.out.println("initialize(): createTextFile() failed.");
624                         return false;
625                 }
626
627                 final boolean identConfCreationSucceeded = createTextFile(dbDirConfig + File.separator + "pg_ident.conf",
628                                 DEFAULT_CONFIG_PG_IDENT);
629                 if (!identConfCreationSucceeded) {
630                         System.out.println("initialize(): createTextFile() failed.");
631                         return false;
632                 }
633
634                 return true;
635         }
636
637         /**
638          * @param path
639          * @param contents
640          * @return
641          */
642         private static boolean createTextFile(final String path, final String contents) {
643                 final File file = new File(path);
644                 final File parent = file.getParentFile();
645                 if (parent == null) {
646                         System.out.println("initialize(): getParentFile() failed.");
647                         return false;
648                 }
649
650                 parent.mkdirs();
651                 try {
652                         file.createNewFile();
653                 } catch (final IOException e) {
654                         // TODO Auto-generated catch block
655                         e.printStackTrace();
656                         return false;
657                 }
658
659                 FileOutputStream output = null;
660                 try {
661                         output = new FileOutputStream(file);
662                 } catch (final FileNotFoundException e) {
663                         // TODO Auto-generated catch block
664                         e.printStackTrace();
665                         return false;
666                 }
667
668                 try {
669                         output.write(contents.getBytes());
670                 } catch (final IOException e) {
671                         // TODO Auto-generated catch block
672                         e.printStackTrace();
673                         return false;
674                 }
675
676                 return true;
677         }
678
679         /**
680          * @param document
681          * @return
682          */
683         private boolean recreateDatabaseFromDocument() {
684                 // Check whether the database exists already.
685                 final String dbName = document.getConnectionDatabase();
686                 if (StringUtils.isEmpty(dbName)) {
687                         return false;
688                 }
689
690                 document.setConnectionDatabase(dbName);
691                 Connection connection = createConnection();
692                 if (connection != null) {
693                         // Connection to the database succeeded, so the database
694                         // exists already.
695                         try {
696                                 connection.close();
697                         } catch (final SQLException e) {
698                                 // TODO Auto-generated catch block
699                                 e.printStackTrace();
700                         }
701                         return false;
702                 }
703
704                 // Create the database:
705                 progress();
706                 document.setConnectionDatabase("");
707
708                 connection = createConnection();
709                 if (connection == null) {
710                         System.out.println("recreatedDatabase(): createConnection() failed, before creating the database.");
711                         return false;
712                 }
713
714                 final boolean dbCreated = createDatabase(connection, dbName);
715
716                 if (!dbCreated) {
717                         return false;
718                 }
719
720                 progress();
721
722                 // Check that we can connect:
723                 try {
724                         connection.close();
725                 } catch (final SQLException e) {
726                         // TODO Auto-generated catch block
727                         e.printStackTrace();
728                 }
729                 connection = null;
730
731                 document.setConnectionDatabase(dbName);
732                 connection = createConnection();
733                 if (connection == null) {
734                         System.out.println("recreatedDatabase(): createConnection() failed, after creating the database.");
735                         return false;
736                 }
737
738                 progress();
739
740                 // Create each table:
741                 final List<String> tables = document.getTableNames();
742                 for (final String tableName : tables) {
743
744                         // Create SQL to describe all fields in this table:
745                         final List<Field> fields = document.getTableFields(tableName);
746
747                         progress();
748                         final boolean tableCreationSucceeded = createTable(connection, document, tableName, fields);
749                         progress();
750                         if (!tableCreationSucceeded) {
751                                 // TODO: std::cerr << G_STRFUNC << ": CREATE TABLE failed with the newly-created database." <<
752                                 // std::endl;
753                                 return false;
754                         }
755                 }
756
757                 // Note that create_database() has already called add_standard_tables() and add_standard_groups(document).
758
759                 // Add groups from the document:
760                 progress();
761                 if (!addGroupsFromDocument(document)) {
762                         // TODO: std::cerr << G_STRFUNC << ": add_groups_from_document() failed." << std::endl;
763                         return false;
764                 }
765
766                 // Set table privileges, using the groups we just added:
767                 progress();
768                 if (!setTablePrivilegesGroupsFromDocument(document)) {
769                         // TODO: std::cerr << G_STRFUNC << ": set_table_privileges_groups_from_document() failed." << std::endl;
770                         return false;
771                 }
772
773                 for (final String tableName : tables) {
774                         // Add any example data to the table:
775                         progress();
776
777                         // try
778                         // {
779                         progress();
780                         final boolean tableInsertSucceeded = insertExampleData(connection, document, tableName);
781
782                         if (!tableInsertSucceeded) {
783                                 // TODO: std::cerr << G_STRFUNC << ": INSERT of example data failed with the newly-created database." <<
784                                 // std::endl;
785                                 return false;
786                         }
787                         // }
788                         // catch(final std::exception& ex)
789                         // {
790                         // std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
791                         // HandleError(ex);
792                         // }
793
794                 } // for(tables)
795
796                 return true; // All tables created successfully.
797         }
798
799         /**
800          * @return
801          * @throws SQLException
802          */
803         private Connection createConnection() {
804                 final Properties connectionProps = new Properties();
805                 connectionProps.put("user", this.username);
806                 connectionProps.put("password", this.password);
807
808                 String jdbcURL = "jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort();
809                 String db = document.getConnectionDatabase();
810                 if (StringUtils.isEmpty(db)) {
811                         // Use the default PostgreSQL database, because ComboPooledDataSource.connect() fails otherwise.
812                         db = "template1";
813                 }
814                 jdbcURL += "/" + db; // TODO: Quote the database name?
815
816                 Connection conn = null;
817                 try {
818                         conn = DriverManager.getConnection(jdbcURL + "/", connectionProps);
819                 } catch (final SQLException e) {
820                         // e.printStackTrace();
821                         return null;
822                 }
823
824                 return conn;
825         }
826
827         /**
828          *
829          */
830         private void progress() {
831                 // TODO Auto-generated method stub
832
833         }
834
835         /**
836          * @param document
837          * @param tableName
838          * @return
839          */
840         private boolean insertExampleData(final Connection connection, final Document document, final String tableName) {
841
842                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
843                 final Table<Record> table = Factory.tableByName(tableName);
844
845                 final List<Map<String, DataItem>> exampleRows = document.getExampleRows(tableName);
846                 for (final Map<String, DataItem> row : exampleRows) {
847                         InsertSetStep<Record> insertStep = factory.insertInto(table);
848
849                         for (final Entry<String, DataItem> entry : row.entrySet()) {
850                                 final String fieldName = entry.getKey();
851                                 final DataItem value = entry.getValue();
852                                 if (value == null) {
853                                         continue;
854                                 }
855
856                                 final Field field = document.getField(tableName, fieldName);
857                                 if (field == null) {
858                                         continue;
859                                 }
860
861                                 final org.jooq.Field<Object> jooqField = Factory.fieldByName(field.getName());
862                                 if (jooqField == null) {
863                                         continue;
864                                 }
865
866                                 final Object fieldValue = value.getValue(field.getGlomType());
867                                 insertStep = insertStep.set(jooqField, fieldValue);
868                         }
869
870                         if (!(insertStep instanceof InsertResultStep<?>)) {
871                                 continue;
872                         }
873
874                         // We suppress the warning because we _do_ check the cast above.
875                         @SuppressWarnings("unchecked")
876                         final InsertResultStep<Record> insertResultStep = (InsertResultStep<Record>) insertStep;
877
878                         try {
879                                 insertResultStep.fetchOne();
880                         } catch (final DataAccessException e) {
881                                 // e.printStackTrace();
882                                 return false;
883                         }
884                         // TODO: Check that it worked.
885                 }
886
887                 return true;
888         }
889
890         /**
891          * @param document2
892          * @return
893          */
894         private boolean setTablePrivilegesGroupsFromDocument(final Document document2) {
895                 // TODO Auto-generated method stub
896                 return true;
897         }
898
899         /**
900          * @param document2
901          * @return
902          */
903         private boolean addGroupsFromDocument(final Document document2) {
904                 // TODO Auto-generated method stub
905                 return true;
906         }
907
908         /**
909          * @param document
910          * @param tableName
911          * @param fields
912          * @return
913          */
914         private boolean createTable(final Connection connection, final Document document, final String tableName,
915                         final List<Field> fields) {
916                 boolean tableCreationSucceeded = false;
917
918                 /*
919                  * TODO: //Create the standard field too: //(We don't actually use this yet) if(std::find_if(fields.begin(),
920                  * fields.end(), predicate_FieldHasName<Field>(GLOM_STANDARD_FIELD_LOCK)) == fields.end()) { sharedptr<Field>
921                  * field = sharedptr<Field>::create(); field->set_name(GLOM_STANDARD_FIELD_LOCK);
922                  * field->set_glom_type(Field::TYPE_TEXT); fields.push_back(field); }
923                  */
924
925                 // Create SQL to describe all fields in this table:
926                 String sqlFields = "";
927                 for (final Field field : fields) {
928                         // Create SQL to describe this field:
929                         String sqlFieldDescription = escapeSqlId(field.getName()) + " " + field.getSqlType();
930
931                         if (field.getPrimaryKey()) {
932                                 sqlFieldDescription += " NOT NULL  PRIMARY KEY";
933                         }
934
935                         // Append it:
936                         if (!StringUtils.isEmpty(sqlFields)) {
937                                 sqlFields += ", ";
938                         }
939
940                         sqlFields += sqlFieldDescription;
941                 }
942
943                 if (StringUtils.isEmpty(sqlFields)) {
944                         // TODO: std::cerr << G_STRFUNC << ": sql_fields is empty." << std::endl;
945                 }
946
947                 // Actually create the table
948                 final String query = "CREATE TABLE " + escapeSqlId(tableName) + " (" + sqlFields + ");";
949                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
950                 factory.execute(query);
951                 tableCreationSucceeded = true;
952                 if (!tableCreationSucceeded) {
953                         System.out.println("recreatedDatabase(): CREATE TABLE() failed.");
954                 }
955
956                 return tableCreationSucceeded;
957         }
958
959         /**
960          * @param name
961          * @return
962          */
963         private String escapeSqlId(final String name) {
964                 // TODO:
965                 return "\"" + name + "\"";
966         }
967
968         /**
969          * @return
970          */
971         private static boolean createDatabase(final Connection connection, final String databaseName) {
972
973                 final String query = "CREATE DATABASE \"" + databaseName + "\""; // TODO: Escaping.
974                 final Factory factory = new Factory(connection, SQLDialect.POSTGRES);
975
976                 factory.execute(query);
977
978                 return true;
979         }
980
981         /**
982          *
983          */
984         public boolean cleanup() {
985                 boolean result = true;
986
987                 // Stop the server:
988                 if ((document != null) && (document.getConnectionPort() != 0)) {
989                         final String dbDirData = getSelfHostingDataPath(false);
990
991                         // -D specifies the data directory.
992                         // -c config_file= specifies the configuration file
993                         // -k specifies a directory to use for the socket. This must be writable by us.
994                         // We use "-m fast" instead of the default "-m smart" because that waits for clients to disconnect (and
995                         // sometimes never succeeds).
996                         // TODO: Warn about connected clients on other computers? Warn those other users?
997                         // Make sure to use double quotes for the executable path, because the
998                         // CreateProcess() API used on Windows does not support single quotes.
999                         final String commandPath = getPathToPostgresExecutable("pg_ctl");
1000                         if (StringUtils.isEmpty(commandPath)) {
1001                                 System.out.println("cleanup(): getPathToPostgresExecutable(pg_ctl) failed.");
1002                         } else {
1003                                 final ProcessBuilder commandPostgresStop = new ProcessBuilder(commandPath,
1004                                                 "-D" + shellQuote(dbDirData), "stop", "-m", "fast");
1005                                 result = executeCommandLineAndWait(commandPostgresStop);
1006                                 if (!result) {
1007                                         System.out.println("cleanup(): Failed to stop the PostgreSQL server.");
1008                                 }
1009                         }
1010
1011                         document.setConnectionPort(0);
1012                 }
1013
1014                 // Delete the files:
1015                 final String selfhostingPath = getSelfHostingPath("", false);
1016                 final File fileSelfHosting = new File(selfhostingPath);
1017                 fileSelfHosting.delete();
1018
1019                 final String docPath = document.getFileURI();
1020                 final File fileDoc = new File(docPath);
1021                 fileDoc.delete();
1022
1023                 return result;
1024         }
1025 }