• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 17208175973

25 Aug 2025 12:00PM UTC coverage: 63.742% (+0.07%) from 63.671%
17208175973

push

github

ibacher
TRUNK-6395: Saner scheme for copying properties from the installation script (#5260)

0 of 2 new or added lines in 1 file covered. (0.0%)

697 existing lines in 13 files now uncovered.

22147 of 34745 relevant lines covered (63.74%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/web/src/main/java/org/openmrs/web/filter/initialization/InitializationFilter.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 * 
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.web.filter.initialization;
11

12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.PrintWriter;
18
import java.net.URI;
19
import java.nio.charset.StandardCharsets;
20
import java.sql.Connection;
21
import java.sql.DriverManager;
22
import java.sql.SQLException;
23
import java.sql.Statement;
24
import java.util.ArrayList;
25
import java.util.Base64;
26
import java.util.Base64.Encoder;
27
import java.util.HashMap;
28
import java.util.HashSet;
29
import java.util.List;
30
import java.util.Locale;
31
import java.util.Map;
32
import java.util.Properties;
33
import java.util.Random;
34
import java.util.Set;
35
import java.util.zip.ZipInputStream;
36
import javax.servlet.FilterChain;
37
import javax.servlet.FilterConfig;
38
import javax.servlet.ServletException;
39
import javax.servlet.ServletRequest;
40
import javax.servlet.ServletResponse;
41
import javax.servlet.http.HttpServletRequest;
42
import javax.servlet.http.HttpServletResponse;
43

44
import liquibase.changelog.ChangeSet;
45
import org.apache.commons.io.IOUtils;
46
import org.openmrs.ImplementationId;
47
import org.openmrs.api.APIAuthenticationException;
48
import org.openmrs.api.PasswordException;
49
import org.openmrs.api.UserService;
50
import org.openmrs.api.context.Context;
51
import org.openmrs.api.context.ContextAuthenticationException;
52
import org.openmrs.api.context.UsernamePasswordCredentials;
53
import org.openmrs.liquibase.ChangeLogDetective;
54
import org.openmrs.liquibase.ChangeLogVersionFinder;
55
import org.openmrs.module.MandatoryModuleException;
56
import org.openmrs.module.OpenmrsCoreModuleException;
57
import org.openmrs.module.web.WebModuleUtil;
58
import org.openmrs.util.DatabaseUpdateException;
59
import org.openmrs.util.DatabaseUpdater;
60
import org.openmrs.liquibase.ChangeSetExecutorCallback;
61
import org.openmrs.util.DatabaseUpdaterLiquibaseProvider;
62
import org.openmrs.util.DatabaseUtil;
63
import org.openmrs.util.InputRequiredException;
64
import org.openmrs.util.OpenmrsConstants;
65
import org.openmrs.util.OpenmrsUtil;
66
import org.openmrs.util.PrivilegeConstants;
67
import org.openmrs.util.Security;
68
import org.openmrs.web.Listener;
69
import org.openmrs.web.WebConstants;
70
import org.openmrs.web.WebDaemon;
71
import org.openmrs.web.filter.StartupFilter;
72
import org.openmrs.web.filter.update.UpdateFilter;
73
import org.openmrs.web.filter.util.CustomResourceLoader;
74
import org.openmrs.web.filter.util.ErrorMessageConstants;
75
import org.openmrs.web.filter.util.FilterUtil;
76
import org.slf4j.LoggerFactory;
77
import org.springframework.util.StringUtils;
78
import org.springframework.web.context.ContextLoader;
79

80
/**
81
 * This is the first filter that is processed. It is only active when starting OpenMRS for the very
82
 * first time. It will redirect all requests to the {@link WebConstants#SETUP_PAGE_URL} if the
83
 * {@link Listener} wasn't able to find any runtime properties
84
 */
UNCOV
85
public class InitializationFilter extends StartupFilter {
×
86
        
UNCOV
87
        private static final org.slf4j.Logger log = LoggerFactory.getLogger(InitializationFilter.class);
×
88
        
89
        private static final String DATABASE_POSTGRESQL = "postgresql";
90
        
91
        private static final String DATABASE_MYSQL = "mysql";
92
        
93
        private static final String DATABASE_SQLSERVER = "sqlserver";
94
        
95
        private static final String DATABASE_H2 = "h2";
96
        
97
        private static final String LIQUIBASE_DEMO_DATA = "liquibase-demo-data.xml";
98
        
99
        /**
100
         * The very first page of wizard, that asks user for select his preferred language
101
         */
102
        private static final String CHOOSE_LANG = "chooselang.vm";
103
        
104
        /**
105
         * The second page of the wizard that asks for simple or advanced installation.
106
         */
107
        private static final String INSTALL_METHOD = "installmethod.vm";
108
        
109
        /**
110
         * The simple installation setup page.
111
         */
112
        private static final String SIMPLE_SETUP = "simplesetup.vm";
113
        
114
        /**
115
         * The first page of the advanced installation of the wizard that asks for a current or past
116
         * database
117
         */
118
        private static final String DATABASE_SETUP = "databasesetup.vm";
119
        
120
        /**
121
         * The page from where the user specifies the url to a remote system, username and password
122
         */
123
        private static final String TESTING_REMOTE_DETAILS_SETUP = "remotedetails.vm";
124
        
125
        /**
126
         * The velocity macro page to redirect to if an error occurs or on initial startup
127
         */
128
        private static final String DEFAULT_PAGE = CHOOSE_LANG;
129
        
130
        /**
131
         * This page asks whether database tables/demo data should be inserted and what the
132
         * username/password that will be put into the runtime properties is
133
         */
134
        private static final String DATABASE_TABLES_AND_USER = "databasetablesanduser.vm";
135
        
136
        /**
137
         * This page lets the user define the admin user
138
         */
139
        private static final String ADMIN_USER_SETUP = "adminusersetup.vm";
140
        
141
        /**
142
         * This page lets the user pick an implementation id
143
         */
144
        private static final String IMPLEMENTATION_ID_SETUP = "implementationidsetup.vm";
145
        
146
        /**
147
         * This page asks for settings that will be put into the runtime properties files
148
         */
149
        private static final String OTHER_RUNTIME_PROPS = "otherruntimeproperties.vm";
150
        
151
        /**
152
         * A page that tells the user that everything is collected and will now be processed
153
         */
154
        private static final String WIZARD_COMPLETE = "wizardcomplete.vm";
155
        
156
        /**
157
         * A page that lists off what is happening while it is going on. This page has ajax that callst he
158
         * {@value #PROGRESS_VM_AJAXREQUEST} page
159
         */
160
        private static final String PROGRESS_VM = "progress.vm";
161
        
162
        /**
163
         * This url is called by javascript to get the status of the install
164
         */
165
        private static final String PROGRESS_VM_AJAXREQUEST = "progress.vm.ajaxRequest";
166
        
167
        public static final String RELEASE_TESTING_MODULE_PATH = "/module/releasetestinghelper/";
168
        
169
        /**
170
         * The model object that holds all the properties that the rendered templates use. All attributes on
171
         * this object are made available to all templates via reflection in the
172
         * {@link org.openmrs.web.filter.StartupFilter#renderTemplate(String, Map, HttpServletResponse)} method.
173
         */
UNCOV
174
        private InitializationWizardModel wizardModel = null;
×
175
        
176
        private InitializationCompletion initJob;
177
        
178
        /**
179
         * Variable set to true as soon as the installation begins and set to false when the process ends
180
         * This thread should only be accesses through the synchronized method.
181
         */
182
        private static boolean isInstallationStarted = false;
×
183
        
184
        // the actual driver loaded by the DatabaseUpdater class
185
        private String loadedDriverString;
186
        
187
        /**
188
         * Variable set at the end of the wizard when spring is being restarted
189
         */
190
        private static boolean initializationComplete = false;
×
191
        
192
        protected synchronized void setInitializationComplete(boolean initializationComplete) {
193
                InitializationFilter.initializationComplete = initializationComplete;
×
UNCOV
194
        }
×
195
        
196
        /**
197
         * Called by {@link #doFilter(ServletRequest, ServletResponse, FilterChain)} on GET requests
198
         *
199
         * @param httpRequest
200
         * @param httpResponse
201
         */
202
        @Override
203
        protected void doGet(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
204
                throws IOException, ServletException {
205
                loadInstallationScriptIfPresent();
×
206
                
207
                // we need to save current user language in references map since it will be used when template
208
                // will be rendered
209
                if (httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE) == null) {
×
UNCOV
210
                        checkLocaleAttributesForFirstTime(httpRequest);
×
211
                }
212
                
213
                Map<String, Object> referenceMap = new HashMap<>();
×
214
                String page = httpRequest.getParameter("page");
×
215
                
216
                referenceMap.put(FilterUtil.LOCALE_ATTRIBUTE, httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE));
×
217
                
218
                httpResponse.setHeader("Cache-Control", "no-cache");
×
219
                
220
                // if any body has already started installation and this is not an ajax request for the progress
221
                if (isInstallationStarted() && !PROGRESS_VM_AJAXREQUEST.equals(page)) {
×
UNCOV
222
                        referenceMap.put("isInstallationStarted", true);
×
UNCOV
223
                        httpResponse.setContentType("text/html");
×
224
                        renderTemplate(PROGRESS_VM, referenceMap, httpResponse);
×
225
                } else if (PROGRESS_VM_AJAXREQUEST.equals(page)) {
×
226
                        httpResponse.setContentType("text/json");
×
227
                        Map<String, Object> result = new HashMap<>();
×
228
                        if (initJob != null) {
×
229
                                result.put("hasErrors", initJob.hasErrors());
×
230
                                if (initJob.hasErrors()) {
×
231
                                        result.put("errorPage", initJob.getErrorPage());
×
232
                                        errors.putAll(initJob.getErrors());
×
233
                                }
234
                                
UNCOV
235
                                result.put("initializationComplete", isInitializationComplete());
×
UNCOV
236
                                result.put("message", initJob.getMessage());
×
UNCOV
237
                                result.put("actionCounter", initJob.getStepsComplete());
×
238
                                if (!isInitializationComplete()) {
×
UNCOV
239
                                        result.put("executingTask", initJob.getExecutingTask());
×
240
                                        result.put("executedTasks", initJob.getExecutedTasks());
×
241
                                        result.put("completedPercentage", initJob.getCompletedPercentage());
×
242
                                }
243
                                
UNCOV
244
                                addLogLinesToResponse(result);
×
245
                        }
246
                        
247
                        PrintWriter writer = httpResponse.getWriter();
×
UNCOV
248
                        writer.write(toJSONString(result));
×
249
                        writer.close();
×
250
                } else if (InitializationWizardModel.INSTALL_METHOD_AUTO.equals(wizardModel.installMethod)
×
UNCOV
251
                        || httpRequest.getServletPath().equals("/" + AUTO_RUN_OPENMRS)) {
×
252
                        autoRunOpenMRS(httpRequest);
×
253
                        referenceMap.put("isInstallationStarted", true);
×
254
                        httpResponse.setContentType("text/html");
×
255
                        renderTemplate(PROGRESS_VM, referenceMap, httpResponse);
×
UNCOV
256
                } else if (page == null) {
×
UNCOV
257
                        httpResponse.setContentType("text/html");// if any body has already started installation
×
258
                        
259
                        //If someone came straight here without setting the hidden page input,
260
                        // then we need to clear out all the passwords
UNCOV
261
                        clearPasswords();
×
262
                        
263
                        renderTemplate(DEFAULT_PAGE, referenceMap, httpResponse);
×
UNCOV
264
                } else if (INSTALL_METHOD.equals(page)) {
×
265
                        // get props and render the second page
266
                        File runtimeProperties = getRuntimePropertiesFile();
×
267
                        
268
                        if (!runtimeProperties.exists()) {
×
269
                                try {
UNCOV
270
                                        runtimeProperties.createNewFile();
×
271
                                        // reset the error objects in case of refresh
UNCOV
272
                                        wizardModel.canCreate = true;
×
UNCOV
273
                                        wizardModel.cannotCreateErrorMessage = "";
×
274
                                }
UNCOV
275
                                catch (IOException io) {
×
UNCOV
276
                                        wizardModel.canCreate = false;
×
UNCOV
277
                                        wizardModel.cannotCreateErrorMessage = io.getMessage();
×
278
                                }
×
279
                                
280
                                // check this before deleting the file again
281
                                wizardModel.canWrite = runtimeProperties.canWrite();
×
282
                                
283
                                // delete the file again after testing the create/write
284
                                // so that if the user stops the webapp before finishing
285
                                // this wizard, they can still get back into it
UNCOV
286
                                runtimeProperties.delete();
×
287
                                
288
                        } else {
289
                                wizardModel.canWrite = runtimeProperties.canWrite();
×
290
                                
291
                                wizardModel.databaseConnection = Context.getRuntimeProperties().getProperty("connection.url",
×
292
                                        wizardModel.databaseConnection);
293
                                
UNCOV
294
                                wizardModel.currentDatabaseUsername = Context.getRuntimeProperties().getProperty("connection.username",
×
295
                                        wizardModel.currentDatabaseUsername);
296
                                
UNCOV
297
                                wizardModel.currentDatabasePassword = Context.getRuntimeProperties().getProperty("connection.password",
×
298
                                        wizardModel.currentDatabasePassword);
299
                        }
300
                        
UNCOV
301
                        wizardModel.runtimePropertiesPath = runtimeProperties.getAbsolutePath();
×
302
                        
303
                        // do step one of the wizard
304
                        httpResponse.setContentType("text/html");
×
UNCOV
305
                        renderTemplate(INSTALL_METHOD, referenceMap, httpResponse);
×
306
                }
307
        }
×
308
        
309
        private void loadInstallationScriptIfPresent() {
UNCOV
310
                Properties script = getInstallationScript();
×
UNCOV
311
                if (!script.isEmpty()) {
×
312
                        wizardModel.installMethod = script.getProperty("install_method", wizardModel.installMethod);
×
313
                        
314
                        wizardModel.databaseConnection = script.getProperty("connection.url", wizardModel.databaseConnection);
×
UNCOV
315
                        wizardModel.databaseDriver = script.getProperty("connection.driver_class", wizardModel.databaseDriver);
×
316
                        wizardModel.currentDatabaseUsername = script.getProperty("connection.username",
×
317
                                wizardModel.currentDatabaseUsername);
UNCOV
318
                        wizardModel.currentDatabasePassword = script.getProperty("connection.password",
×
319
                                wizardModel.currentDatabasePassword);
320
                        
321
                        String hasCurrentOpenmrsDatabase = script.getProperty("has_current_openmrs_database");
×
UNCOV
322
                        if (hasCurrentOpenmrsDatabase != null) {
×
UNCOV
323
                                wizardModel.hasCurrentOpenmrsDatabase = Boolean.parseBoolean(hasCurrentOpenmrsDatabase);
×
324
                        }
325
                        wizardModel.createDatabaseUsername = script.getProperty("create_database_username",
×
326
                                wizardModel.createDatabaseUsername);
UNCOV
327
                        wizardModel.createDatabasePassword = script.getProperty("create_database_password",
×
328
                                wizardModel.createDatabasePassword);
329
                        
330
                        String createTables = script.getProperty("create_tables");
×
331
                        if (createTables != null) {
×
UNCOV
332
                                wizardModel.createTables = Boolean.parseBoolean(createTables);
×
333
                        }
334
                        
UNCOV
335
                        String createDatabaseUser = script.getProperty("create_database_user");
×
336
                        if (createDatabaseUser != null) {
×
UNCOV
337
                                wizardModel.createDatabaseUser = Boolean.parseBoolean(createDatabaseUser);
×
338
                        }
339
                        wizardModel.createUserUsername = script.getProperty("create_user_username", wizardModel.createUserUsername);
×
340
                        wizardModel.createUserPassword = script.getProperty("create_user_password", wizardModel.createUserPassword);
×
341
                        
342
                        String addDemoData = script.getProperty("add_demo_data");
×
343
                        if (addDemoData != null) {
×
344
                                wizardModel.addDemoData = Boolean.parseBoolean(addDemoData);
×
345
                        }
346
                        
UNCOV
347
                        String moduleWebAdmin = script.getProperty("module_web_admin");
×
UNCOV
348
                        if (moduleWebAdmin != null) {
×
UNCOV
349
                                wizardModel.moduleWebAdmin = Boolean.parseBoolean(moduleWebAdmin);
×
350
                        }
351
                        
UNCOV
352
                        String autoUpdateDatabase = script.getProperty("auto_update_database");
×
UNCOV
353
                        if (autoUpdateDatabase != null) {
×
UNCOV
354
                                wizardModel.autoUpdateDatabase = Boolean.parseBoolean(autoUpdateDatabase);
×
355
                        }
356
                        
UNCOV
357
                        wizardModel.adminUserPassword = script.getProperty("admin_user_password", wizardModel.adminUserPassword);
×
358
                        
359
                        for (Map.Entry<Object, Object> entry : script.entrySet()) {
×
NEW
360
                                if (entry.getKey() instanceof String && ((String) entry.getKey()).startsWith("property.")) {
×
NEW
361
                                        wizardModel.additionalPropertiesFromInstallationScript.put(((String) entry.getKey()).substring(9), entry.getValue());
×
362
                                }
UNCOV
363
                        }
×
364
                }
365
        }
×
366
        
367
        private void clearPasswords() {
368
                wizardModel.databaseRootPassword = "";
×
369
                wizardModel.createDatabasePassword = "";
×
UNCOV
370
                wizardModel.createUserPassword = "";
×
371
                wizardModel.currentDatabasePassword = "";
×
UNCOV
372
                wizardModel.remotePassword = "";
×
373
        }
×
374
        
375
        /**
376
         * Called by {@link #doFilter(ServletRequest, ServletResponse, FilterChain)} on POST requests
377
         *
378
         * @param httpRequest
379
         * @param httpResponse
380
         */
381
        @Override
382
        protected void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
383
                throws IOException, ServletException {
384
                String page = httpRequest.getParameter("page");
×
UNCOV
385
                Map<String, Object> referenceMap = new HashMap<>();
×
386
                // we need to save current user language in references map since it will be used when template
387
                // will be rendered
UNCOV
388
                if (httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE) != null) {
×
UNCOV
389
                        referenceMap.put(FilterUtil.LOCALE_ATTRIBUTE,
×
UNCOV
390
                                httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE));
×
391
                }
392
                
393
                // if any body has already started installation
UNCOV
394
                if (isInstallationStarted()) {
×
395
                        referenceMap.put("isInstallationStarted", true);
×
UNCOV
396
                        httpResponse.setContentType("text/html");
×
UNCOV
397
                        renderTemplate(PROGRESS_VM, referenceMap, httpResponse);
×
398
                        return;
×
399
                }
UNCOV
400
                if (DEFAULT_PAGE.equals(page)) {
×
401
                        // get props and render the first page
UNCOV
402
                        File runtimeProperties = getRuntimePropertiesFile();
×
UNCOV
403
                        if (!runtimeProperties.exists()) {
×
404
                                try {
405
                                        runtimeProperties.createNewFile();
×
406
                                        // reset the error objects in case of refresh
407
                                        wizardModel.canCreate = true;
×
408
                                        wizardModel.cannotCreateErrorMessage = "";
×
409
                                }
410
                                catch (IOException io) {
×
UNCOV
411
                                        wizardModel.canCreate = false;
×
412
                                        wizardModel.cannotCreateErrorMessage = io.getMessage();
×
UNCOV
413
                                }
×
414
                                // check this before deleting the file again
415
                                wizardModel.canWrite = runtimeProperties.canWrite();
×
416
                                
417
                                // delete the file again after testing the create/write
418
                                // so that if the user stops the webapp before finishing
419
                                // this wizard, they can still get back into it
420
                                runtimeProperties.delete();
×
421
                        } else {
422
                                wizardModel.canWrite = runtimeProperties.canWrite();
×
423
                                
424
                                wizardModel.databaseConnection = Context.getRuntimeProperties().getProperty("connection.url",
×
425
                                        wizardModel.databaseConnection);
426
                                
427
                                wizardModel.currentDatabaseUsername = Context.getRuntimeProperties().getProperty("connection.username",
×
428
                                        wizardModel.currentDatabaseUsername);
429
                                
430
                                wizardModel.currentDatabasePassword = Context.getRuntimeProperties().getProperty("connection.password",
×
431
                                        wizardModel.currentDatabasePassword);
432
                        }
433
                        
434
                        wizardModel.runtimePropertiesPath = runtimeProperties.getAbsolutePath();
×
435
                        
436
                        checkLocaleAttributes(httpRequest);
×
UNCOV
437
                        referenceMap.put(FilterUtil.LOCALE_ATTRIBUTE,
×
438
                                httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE));
×
439
                        log.info("Locale stored in session is " + httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE));
×
440
                        
441
                        httpResponse.setContentType("text/html");
×
442
                        // otherwise do step one of the wizard
443
                        renderTemplate(INSTALL_METHOD, referenceMap, httpResponse);
×
UNCOV
444
                } else if (INSTALL_METHOD.equals(page)) {
×
UNCOV
445
                        if (goBack(httpRequest)) {
×
446
                                referenceMap.put(FilterUtil.REMEMBER_ATTRIBUTE,
×
UNCOV
447
                                        httpRequest.getSession().getAttribute(FilterUtil.REMEMBER_ATTRIBUTE) != null);
×
UNCOV
448
                                referenceMap.put(FilterUtil.LOCALE_ATTRIBUTE,
×
449
                                        httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE));
×
UNCOV
450
                                renderTemplate(CHOOSE_LANG, referenceMap, httpResponse);
×
451
                                return;
×
452
                        }
UNCOV
453
                        wizardModel.installMethod = httpRequest.getParameter("install_method");
×
454
                        if (InitializationWizardModel.INSTALL_METHOD_SIMPLE.equals(wizardModel.installMethod)) {
×
455
                                page = SIMPLE_SETUP;
×
UNCOV
456
                        } else if (InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod)) {
×
UNCOV
457
                                page = TESTING_REMOTE_DETAILS_SETUP;
×
458
                                wizardModel.currentStepNumber = 1;
×
459
                                wizardModel.numberOfSteps = skipDatabaseSetupPage() ? 1 : 3;
×
460
                        } else {
461
                                page = DATABASE_SETUP;
×
462
                                wizardModel.currentStepNumber = 1;
×
UNCOV
463
                                wizardModel.numberOfSteps = 5;
×
464
                        }
UNCOV
465
                        renderTemplate(page, referenceMap, httpResponse);
×
466
                } // simple method
467
                else if (SIMPLE_SETUP.equals(page)) {
×
UNCOV
468
                        if (goBack(httpRequest)) {
×
469
                                renderTemplate(INSTALL_METHOD, referenceMap, httpResponse);
×
UNCOV
470
                                return;
×
471
                        }
UNCOV
472
                        wizardModel.databaseConnection = httpRequest.getParameter("database_connection");
×
473
                        ;
474
                        
UNCOV
475
                        wizardModel.createDatabaseUsername = Context.getRuntimeProperties().getProperty("connection.username",
×
476
                                wizardModel.createDatabaseUsername);
477
                        
478
                        wizardModel.createUserUsername = wizardModel.createDatabaseUsername;
×
479
                        
480
                        wizardModel.databaseRootPassword = httpRequest.getParameter("database_root_password");
×
481
                        checkForEmptyValue(wizardModel.databaseRootPassword, errors, ErrorMessageConstants.ERROR_DB_PSDW_REQ);
×
482
                        
483
                        wizardModel.hasCurrentOpenmrsDatabase = false;
×
484
                        wizardModel.createTables = true;
×
485
                        // default wizardModel.databaseName is openmrs
486
                        // default wizardModel.createDatabaseUsername is root
UNCOV
487
                        wizardModel.createDatabasePassword = wizardModel.databaseRootPassword;
×
488
                        wizardModel.addDemoData = "yes".equals(httpRequest.getParameter("add_demo_data"));
×
489
                        
490
                        wizardModel.hasCurrentDatabaseUser = false;
×
491
                        wizardModel.createDatabaseUser = true;
×
492
                        // default wizardModel.createUserUsername is root
UNCOV
493
                        wizardModel.createUserPassword = wizardModel.databaseRootPassword;
×
494
                        
UNCOV
495
                        wizardModel.moduleWebAdmin = true;
×
496
                        wizardModel.autoUpdateDatabase = false;
×
497
                        
UNCOV
498
                        wizardModel.adminUserPassword = InitializationWizardModel.ADMIN_DEFAULT_PASSWORD;
×
499
                        
500
                        createSimpleSetup(httpRequest.getParameter("database_root_password"), httpRequest.getParameter("add_demo_data"));
×
501
                        
502
                        try {
503
                                loadedDriverString = DatabaseUtil.loadDatabaseDriver(wizardModel.databaseConnection,
×
504
                                        wizardModel.databaseDriver);
505
                        }
506
                        catch (ClassNotFoundException e) {
×
507
                                errors.put(ErrorMessageConstants.ERROR_DB_DRIVER_CLASS_REQ, null);
×
508
                                renderTemplate(page, referenceMap, httpResponse);
×
509
                                return;
×
UNCOV
510
                        }
×
511
                        
UNCOV
512
                        if (errors.isEmpty()) {
×
UNCOV
513
                                page = WIZARD_COMPLETE;
×
514
                        }
UNCOV
515
                        renderTemplate(page, referenceMap, httpResponse);
×
516
                } // step one
517
                else if (DATABASE_SETUP.equals(page)) {
×
518
                        if (goBack(httpRequest)) {
×
519
                                wizardModel.currentStepNumber -= 1;
×
UNCOV
520
                                if (InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod)) {
×
UNCOV
521
                                        renderTemplate(TESTING_REMOTE_DETAILS_SETUP, referenceMap, httpResponse);
×
522
                                } else {
UNCOV
523
                                        renderTemplate(INSTALL_METHOD, referenceMap, httpResponse);
×
524
                                }
UNCOV
525
                                return;
×
526
                        }
527
                        
528
                        wizardModel.databaseConnection = httpRequest.getParameter("database_connection");
×
529
                        checkForEmptyValue(wizardModel.databaseConnection, errors, ErrorMessageConstants.ERROR_DB_CONN_REQ);
×
530
                        
UNCOV
531
                        wizardModel.databaseDriver = httpRequest.getParameter("database_driver");
×
532
                        checkForEmptyValue(wizardModel.databaseConnection, errors, ErrorMessageConstants.ERROR_DB_DRIVER_REQ);
×
533
                        
534
                        loadedDriverString = loadDriver(wizardModel.databaseConnection, wizardModel.databaseDriver);
×
535
                        if (!StringUtils.hasText(loadedDriverString)) {
×
UNCOV
536
                                errors.put(ErrorMessageConstants.ERROR_DB_DRIVER_CLASS_REQ, null);
×
UNCOV
537
                                renderTemplate(page, referenceMap, httpResponse);
×
538
                                return;
×
539
                        }
540
                        
541
                        //TODO make each bit of page logic a (unit testable) method
542
                        
543
                        // asked the user for their desired database name
544
                        
UNCOV
545
                        if ("yes".equals(httpRequest.getParameter("current_openmrs_database"))) {
×
UNCOV
546
                                wizardModel.databaseName = httpRequest.getParameter("openmrs_current_database_name");
×
UNCOV
547
                                checkForEmptyValue(wizardModel.databaseName, errors, ErrorMessageConstants.ERROR_DB_CURR_NAME_REQ);
×
548
                                wizardModel.hasCurrentOpenmrsDatabase = true;
×
549
                                // TODO check to see if this is an active database
550
                                
551
                        } else {
552
                                // mark this wizard as a "to create database" (done at the end)
553
                                wizardModel.hasCurrentOpenmrsDatabase = false;
×
554
                                
555
                                wizardModel.createTables = true;
×
556
                                
UNCOV
557
                                wizardModel.databaseName = httpRequest.getParameter("openmrs_new_database_name");
×
UNCOV
558
                                checkForEmptyValue(wizardModel.databaseName, errors, ErrorMessageConstants.ERROR_DB_NEW_NAME_REQ);
×
559
                                // TODO create database now to check if its possible?
560
                                
UNCOV
561
                                wizardModel.createDatabaseUsername = httpRequest.getParameter("create_database_username");
×
UNCOV
562
                                checkForEmptyValue(wizardModel.createDatabaseUsername, errors, ErrorMessageConstants.ERROR_DB_USER_NAME_REQ);
×
563
                                wizardModel.createDatabasePassword = httpRequest.getParameter("create_database_password");
×
UNCOV
564
                                checkForEmptyValue(wizardModel.createDatabasePassword, errors, ErrorMessageConstants.ERROR_DB_USER_PSWD_REQ);
×
565
                        }
566
                        
567
                        if (errors.isEmpty()) {
×
UNCOV
568
                                page = DATABASE_TABLES_AND_USER;
×
569
                                
570
                                if (InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod)) {
×
UNCOV
571
                                        wizardModel.currentStepNumber = 3;
×
572
                                } else {
573
                                        wizardModel.currentStepNumber = 2;
×
574
                                }
575
                        }
576
                        
UNCOV
577
                        renderTemplate(page, referenceMap, httpResponse);
×
578
                        
579
                } // step two
580
                else if (DATABASE_TABLES_AND_USER.equals(page)) {
×
581
                        
UNCOV
582
                        if (goBack(httpRequest)) {
×
UNCOV
583
                                wizardModel.currentStepNumber -= 1;
×
584
                                renderTemplate(DATABASE_SETUP, referenceMap, httpResponse);
×
585
                                return;
×
586
                        }
587
                        
UNCOV
588
                        if (wizardModel.hasCurrentOpenmrsDatabase) {
×
589
                                wizardModel.createTables = "yes".equals(httpRequest.getParameter("create_tables"));
×
590
                        }
591
                        
UNCOV
592
                        wizardModel.addDemoData = "yes".equals(httpRequest.getParameter("add_demo_data"));
×
593
                        
594
                        if ("yes".equals(httpRequest.getParameter("current_database_user"))) {
×
595
                                wizardModel.currentDatabaseUsername = httpRequest.getParameter("current_database_username");
×
UNCOV
596
                                checkForEmptyValue(wizardModel.currentDatabaseUsername, errors,
×
597
                                        ErrorMessageConstants.ERROR_DB_CUR_USER_NAME_REQ);
598
                                wizardModel.currentDatabasePassword = httpRequest.getParameter("current_database_password");
×
599
                                checkForEmptyValue(wizardModel.currentDatabasePassword, errors,
×
600
                                        ErrorMessageConstants.ERROR_DB_CUR_USER_PSWD_REQ);
601
                                wizardModel.hasCurrentDatabaseUser = true;
×
602
                                wizardModel.createDatabaseUser = false;
×
603
                        } else {
604
                                wizardModel.hasCurrentDatabaseUser = false;
×
UNCOV
605
                                wizardModel.createDatabaseUser = true;
×
606
                                // asked for the root mysql username/password
607
                                wizardModel.createUserUsername = httpRequest.getParameter("create_user_username");
×
UNCOV
608
                                checkForEmptyValue(wizardModel.createUserUsername, errors, ErrorMessageConstants.ERROR_DB_USER_NAME_REQ);
×
UNCOV
609
                                wizardModel.createUserPassword = httpRequest.getParameter("create_user_password");
×
610
                                checkForEmptyValue(wizardModel.createUserPassword, errors, ErrorMessageConstants.ERROR_DB_USER_PSWD_REQ);
×
611
                        }
612
                        
613
                        if (errors.isEmpty()) { // go to next page
×
614
                                page = InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod) ? WIZARD_COMPLETE
×
615
                                        : OTHER_RUNTIME_PROPS;
616
                        }
617
                        
618
                        renderTemplate(page, referenceMap, httpResponse);
×
619
                } // step three
UNCOV
620
                else if (OTHER_RUNTIME_PROPS.equals(page)) {
×
621
                        
622
                        if (goBack(httpRequest)) {
×
623
                                renderTemplate(DATABASE_TABLES_AND_USER, referenceMap, httpResponse);
×
624
                                return;
×
625
                        }
626
                        
UNCOV
627
                        wizardModel.moduleWebAdmin = "yes".equals(httpRequest.getParameter("module_web_admin"));
×
628
                        wizardModel.autoUpdateDatabase = "yes".equals(httpRequest.getParameter("auto_update_database"));
×
629
                        
630
                        if (wizardModel.createTables) { // go to next page if they are creating tables
×
631
                                page = ADMIN_USER_SETUP;
×
632
                        } else { // skip a page
UNCOV
633
                                page = IMPLEMENTATION_ID_SETUP;
×
634
                        }
635
                        
UNCOV
636
                        renderTemplate(page, referenceMap, httpResponse);
×
637
                        
638
                } // optional step four
639
                else if (ADMIN_USER_SETUP.equals(page)) {
×
640
                        
641
                        if (goBack(httpRequest)) {
×
UNCOV
642
                                renderTemplate(OTHER_RUNTIME_PROPS, referenceMap, httpResponse);
×
643
                                return;
×
644
                        }
645
                        
UNCOV
646
                        wizardModel.adminUserPassword = httpRequest.getParameter("new_admin_password");
×
647
                        String adminUserConfirm = httpRequest.getParameter("new_admin_password_confirm");
×
648
                        
649
                        // throw back to admin user if passwords don't match
650
                        if (!wizardModel.adminUserPassword.equals(adminUserConfirm)) {
×
UNCOV
651
                                errors.put(ErrorMessageConstants.ERROR_DB_ADM_PSWDS_MATCH, null);
×
652
                                renderTemplate(ADMIN_USER_SETUP, referenceMap, httpResponse);
×
653
                                return;
×
654
                        }
655
                        
656
                        // throw back if the user didn't put in a password
UNCOV
657
                        if ("".equals(wizardModel.adminUserPassword)) {
×
658
                                errors.put(ErrorMessageConstants.ERROR_DB_ADM_PSDW_EMPTY, null);
×
UNCOV
659
                                renderTemplate(ADMIN_USER_SETUP, referenceMap, httpResponse);
×
UNCOV
660
                                return;
×
661
                        }
662
                        
663
                        try {
664
                                OpenmrsUtil.validatePassword("admin", wizardModel.adminUserPassword, "admin");
×
665
                        }
UNCOV
666
                        catch (PasswordException p) {
×
667
                                errors.put(ErrorMessageConstants.ERROR_DB_ADM_PSDW_WEAK, null);
×
668
                                renderTemplate(ADMIN_USER_SETUP, referenceMap, httpResponse);
×
669
                                return;
×
670
                        }
×
671
                        
UNCOV
672
                        if (errors.isEmpty()) { // go to next page
×
673
                                page = IMPLEMENTATION_ID_SETUP;
×
674
                        }
675
                        
UNCOV
676
                        renderTemplate(page, referenceMap, httpResponse);
×
677
                        
678
                } // optional step five
×
UNCOV
679
                else if (IMPLEMENTATION_ID_SETUP.equals(page)) {
×
680
                        
UNCOV
681
                        if (goBack(httpRequest)) {
×
682
                                if (wizardModel.createTables) {
×
683
                                        renderTemplate(ADMIN_USER_SETUP, referenceMap, httpResponse);
×
684
                                } else {
685
                                        renderTemplate(OTHER_RUNTIME_PROPS, referenceMap, httpResponse);
×
686
                                }
UNCOV
687
                                return;
×
688
                        }
689
                        
UNCOV
690
                        wizardModel.implementationIdName = httpRequest.getParameter("implementation_name");
×
691
                        wizardModel.implementationId = httpRequest.getParameter("implementation_id");
×
UNCOV
692
                        wizardModel.implementationIdPassPhrase = httpRequest.getParameter("pass_phrase");
×
693
                        wizardModel.implementationIdDescription = httpRequest.getParameter("description");
×
694
                        
695
                        // throw back if the user-specified ID is invalid (contains ^ or |).
UNCOV
696
                        if (wizardModel.implementationId.indexOf('^') != -1 || wizardModel.implementationId.indexOf('|') != -1) {
×
697
                                errors.put(ErrorMessageConstants.ERROR_DB_IMPL_ID_REQ, null);
×
698
                                renderTemplate(IMPLEMENTATION_ID_SETUP, referenceMap, httpResponse);
×
699
                                return;
×
700
                        }
701
                        
702
                        if (errors.isEmpty()) { // go to next page
×
UNCOV
703
                                page = WIZARD_COMPLETE;
×
704
                        }
705
                        
706
                        renderTemplate(page, referenceMap, httpResponse);
×
707
                } else if (WIZARD_COMPLETE.equals(page)) {
×
708
                        
709
                        if (goBack(httpRequest)) {
×
710
                                
711
                                if (InitializationWizardModel.INSTALL_METHOD_SIMPLE.equals(wizardModel.installMethod)) {
×
712
                                        page = SIMPLE_SETUP;
×
UNCOV
713
                                } else if (InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod)) {
×
UNCOV
714
                                        if (skipDatabaseSetupPage()) {
×
715
                                                page = TESTING_REMOTE_DETAILS_SETUP;
×
716
                                        } else {
UNCOV
717
                                                page = DATABASE_TABLES_AND_USER;
×
718
                                        }
719
                                } else {
UNCOV
720
                                        page = IMPLEMENTATION_ID_SETUP;
×
721
                                }
UNCOV
722
                                renderTemplate(page, referenceMap, httpResponse);
×
723
                                return;
×
724
                        }
725
                        
726
                        wizardModel.tasksToExecute = new ArrayList<>();
×
727
                        createDatabaseTask();
×
728
                        if (InitializationWizardModel.INSTALL_METHOD_TESTING.equals(wizardModel.installMethod)) {
×
729
                                wizardModel.importTestData = true;
×
730
                                wizardModel.createTables = false;
×
UNCOV
731
                                wizardModel.addDemoData = false;
×
732
                                //if we have a runtime properties file
733
                                if (skipDatabaseSetupPage()) {
×
734
                                        wizardModel.hasCurrentOpenmrsDatabase = false;
×
735
                                        wizardModel.hasCurrentDatabaseUser = true;
×
UNCOV
736
                                        wizardModel.createDatabaseUser = false;
×
737
                                        Properties props = OpenmrsUtil.getRuntimeProperties(WebConstants.WEBAPP_NAME);
×
UNCOV
738
                                        wizardModel.currentDatabaseUsername = props.getProperty("connection.username");
×
739
                                        wizardModel.currentDatabasePassword = props.getProperty("connection.password");
×
740
                                        wizardModel.createDatabaseUsername = wizardModel.currentDatabaseUsername;
×
UNCOV
741
                                        wizardModel.createDatabasePassword = wizardModel.currentDatabasePassword;
×
742
                                }
743
                                
744
                                wizardModel.tasksToExecute.add(WizardTask.IMPORT_TEST_DATA);
×
745
                                wizardModel.tasksToExecute.add(WizardTask.ADD_MODULES);
×
746
                        } else {
747
                                createTablesTask();
×
UNCOV
748
                                createDemoDataTask();
×
749
                        }
750
                        wizardModel.tasksToExecute.add(WizardTask.UPDATE_TO_LATEST);
×
751
                        
UNCOV
752
                        referenceMap.put("tasksToExecute", wizardModel.tasksToExecute);
×
UNCOV
753
                        startInstallation();
×
754
                        renderTemplate(PROGRESS_VM, referenceMap, httpResponse);
×
755
                } else if (TESTING_REMOTE_DETAILS_SETUP.equals(page)) {
×
756
                        if (goBack(httpRequest)) {
×
757
                                wizardModel.currentStepNumber -= 1;
×
758
                                renderTemplate(INSTALL_METHOD, referenceMap, httpResponse);
×
759
                                return;
×
760
                        }
761
                        
UNCOV
762
                        wizardModel.remoteUrl = httpRequest.getParameter("remoteUrl");
×
763
                        checkForEmptyValue(wizardModel.remoteUrl, errors, "install.testing.remote.url.required");
×
764
                        if (errors.isEmpty()) {
×
765
                                //Check if the remote system is running
766
                                if (TestInstallUtil.testConnection(wizardModel.remoteUrl)) {
×
767
                                        //Check if the test module is installed by connecting to its setting page
768
                                        if (TestInstallUtil
×
769
                                                .testConnection(wizardModel.remoteUrl.concat(RELEASE_TESTING_MODULE_PATH + "settings.htm"))) {
×
770
                                                
771
                                                wizardModel.remoteUsername = httpRequest.getParameter("username");
×
UNCOV
772
                                                wizardModel.remotePassword = httpRequest.getParameter("password");
×
UNCOV
773
                                                checkForEmptyValue(wizardModel.remoteUsername, errors, "install.testing.username.required");
×
774
                                                checkForEmptyValue(wizardModel.remotePassword, errors, "install.testing.password.required");
×
775
                                                
776
                                                if (errors.isEmpty()) {
×
777
                                                        //check if the username and password are valid
778
                                                        try {
UNCOV
779
                                                                TestInstallUtil.getResourceInputStream(
×
780
                                                                        wizardModel.remoteUrl + RELEASE_TESTING_MODULE_PATH + "verifycredentials.htm",
781
                                                                        wizardModel.remoteUsername, wizardModel.remotePassword);
782
                                                        }
783
                                                        catch (APIAuthenticationException e) {
×
UNCOV
784
                                                                log.debug("Error generated: ", e);
×
UNCOV
785
                                                                page = TESTING_REMOTE_DETAILS_SETUP;
×
786
                                                                errors.put(ErrorMessageConstants.UPDATE_ERROR_UNABLE_AUTHENTICATE, null);
×
UNCOV
787
                                                                renderTemplate(page, referenceMap, httpResponse);
×
UNCOV
788
                                                                return;
×
789
                                                        }
×
790
                                                        
791
                                                        //If we have a runtime properties file, get the database setup details from it
UNCOV
792
                                                        if (skipDatabaseSetupPage()) {
×
793
                                                                Properties props = OpenmrsUtil.getRuntimeProperties(WebConstants.WEBAPP_NAME);
×
UNCOV
794
                                                                wizardModel.databaseConnection = props.getProperty("connection.url");
×
795
                                                                loadedDriverString = loadDriver(wizardModel.databaseConnection, wizardModel.databaseDriver);
×
UNCOV
796
                                                                if (!StringUtils.hasText(loadedDriverString)) {
×
UNCOV
797
                                                                        page = TESTING_REMOTE_DETAILS_SETUP;
×
UNCOV
798
                                                                        errors.put(ErrorMessageConstants.ERROR_DB_DRIVER_CLASS_REQ, null);
×
799
                                                                        renderTemplate(page, referenceMap, httpResponse);
×
800
                                                                        return;
×
801
                                                                }
802
                                                                
UNCOV
803
                                                                wizardModel.databaseName = InitializationWizardModel.DEFAULT_DATABASE_NAME;
×
804
                                                                page = WIZARD_COMPLETE;
×
UNCOV
805
                                                        } else {
×
UNCOV
806
                                                                page = DATABASE_SETUP;
×
807
                                                                wizardModel.currentStepNumber = 2;
×
808
                                                        }
UNCOV
809
                                                        msgs.put("install.testing.testingModuleFound", null);
×
810
                                                } else {
UNCOV
811
                                                        renderTemplate(page, referenceMap, httpResponse);
×
UNCOV
812
                                                        return;
×
813
                                                }
814
                                        } else {
815
                                                errors.put("install.testing.noTestingModule", null);
×
816
                                        }
817
                                } else {
UNCOV
818
                                        errors.put("install.testing.invalidProductionUrl", new Object[] { wizardModel.remoteUrl });
×
819
                                }
820
                        }
821
                        
UNCOV
822
                        renderTemplate(page, referenceMap, httpResponse);
×
823
                }
824
        }
×
825
        
826
        private void startInstallation() {
827
                //if no one has run any installation
UNCOV
828
                if (!isInstallationStarted()) {
×
829
                        initJob = new InitializationCompletion();
×
830
                        setInstallationStarted(true);
×
UNCOV
831
                        initJob.start();
×
832
                }
833
        }
×
834
        
835
        private void createDemoDataTask() {
836
                if (wizardModel.addDemoData) {
×
UNCOV
837
                        wizardModel.tasksToExecute.add(WizardTask.ADD_DEMO_DATA);
×
838
                }
839
        }
×
840
        
841
        private void createTablesTask() {
842
                if (wizardModel.createTables) {
×
UNCOV
843
                        wizardModel.tasksToExecute.add(WizardTask.CREATE_TABLES);
×
UNCOV
844
                        wizardModel.tasksToExecute.add(WizardTask.ADD_CORE_DATA);
×
845
                }
846
        }
×
847
        
848
        private void createDatabaseTask() {
849
                if (!wizardModel.hasCurrentOpenmrsDatabase) {
×
UNCOV
850
                        wizardModel.tasksToExecute.add(WizardTask.CREATE_SCHEMA);
×
851
                }
UNCOV
852
                if (wizardModel.createDatabaseUser) {
×
853
                        wizardModel.tasksToExecute.add(WizardTask.CREATE_DB_USER);
×
854
                }
UNCOV
855
        }
×
856
        
857
        private void createSimpleSetup(String databaseRootPassword, String addDemoData) {
UNCOV
858
                setDatabaseNameIfInTestMode();
×
UNCOV
859
                wizardModel.databaseConnection = Context.getRuntimeProperties().getProperty("connection.url",
×
860
                        wizardModel.databaseConnection);
861
                
UNCOV
862
                wizardModel.createDatabaseUsername = Context.getRuntimeProperties().getProperty("connection.username",
×
863
                        wizardModel.createDatabaseUsername);
864
                
UNCOV
865
                wizardModel.createUserUsername = wizardModel.createDatabaseUsername;
×
866
                
867
                wizardModel.databaseRootPassword = databaseRootPassword;
×
UNCOV
868
                checkForEmptyValue(wizardModel.databaseRootPassword, errors, ErrorMessageConstants.ERROR_DB_PSDW_REQ);
×
869
                
870
                wizardModel.hasCurrentOpenmrsDatabase = false;
×
871
                wizardModel.createTables = true;
×
872
                // default wizardModel.databaseName is openmrs
873
                // default wizardModel.createDatabaseUsername is root
874
                wizardModel.createDatabasePassword = wizardModel.databaseRootPassword;
×
UNCOV
875
                wizardModel.addDemoData = "yes".equals(addDemoData);
×
876
                
877
                wizardModel.hasCurrentDatabaseUser = false;
×
UNCOV
878
                wizardModel.createDatabaseUser = true;
×
879
                // default wizardModel.createUserUsername is root
UNCOV
880
                wizardModel.createUserPassword = wizardModel.databaseRootPassword;
×
881
                
882
                wizardModel.moduleWebAdmin = true;
×
883
                wizardModel.autoUpdateDatabase = false;
×
884
                
885
                wizardModel.adminUserPassword = InitializationWizardModel.ADMIN_DEFAULT_PASSWORD;
×
886
        }
×
887
        
888
        private void setDatabaseNameIfInTestMode() {
889
                if (OpenmrsUtil.isTestMode()) {
×
890
                        wizardModel.databaseName = OpenmrsUtil.getOpenMRSVersionInTestMode();
×
891
                }
UNCOV
892
        }
×
893
        
894
        private void autoRunOpenMRS(HttpServletRequest httpRequest) {
UNCOV
895
                File runtimeProperties = getRuntimePropertiesFile();
×
UNCOV
896
                wizardModel.runtimePropertiesPath = runtimeProperties.getAbsolutePath();
×
897
                
UNCOV
898
                if (!InitializationWizardModel.INSTALL_METHOD_AUTO.equals(wizardModel.installMethod)) {
×
UNCOV
899
                        if (httpRequest.getParameter("database_user_name") != null) {
×
UNCOV
900
                                wizardModel.createDatabaseUsername = httpRequest.getParameter("database_user_name");
×
901
                        }
902
                        
UNCOV
903
                        createSimpleSetup(httpRequest.getParameter("database_root_password"), "yes");
×
904
                }
905
                
UNCOV
906
                checkLocaleAttributes(httpRequest);
×
907
                try {
908
                        loadedDriverString = DatabaseUtil.loadDatabaseDriver(wizardModel.databaseConnection, wizardModel.databaseDriver);
×
909
                }
910
                catch (ClassNotFoundException e) {
×
UNCOV
911
                        errors.put(ErrorMessageConstants.ERROR_DB_DRIVER_CLASS_REQ, null);
×
UNCOV
912
                        return;
×
UNCOV
913
                }
×
914
                wizardModel.tasksToExecute = new ArrayList<>();
×
915
                createDatabaseTask();
×
916
                createTablesTask();
×
UNCOV
917
                createDemoDataTask();
×
918
                wizardModel.tasksToExecute.add(WizardTask.UPDATE_TO_LATEST);
×
919
                startInstallation();
×
920
        }
×
921
        
922
        /**
923
         * This method should be called after the user has left wizard's first page (i.e. choose language).
924
         * It checks if user has changed any of locale related parameters and makes appropriate corrections
925
         * with filter's model or/and with locale attribute inside user's session.
926
         *
927
         * @param httpRequest the http request object
928
         */
929
        private void checkLocaleAttributes(HttpServletRequest httpRequest) {
UNCOV
930
                String localeParameter = httpRequest.getParameter(FilterUtil.LOCALE_ATTRIBUTE);
×
UNCOV
931
                Boolean rememberLocale = false;
×
932
                // we need to check if user wants that system will remember his selection of language
UNCOV
933
                if (httpRequest.getParameter(FilterUtil.REMEMBER_ATTRIBUTE) != null) {
×
UNCOV
934
                        rememberLocale = true;
×
935
                }
UNCOV
936
                if (localeParameter != null) {
×
UNCOV
937
                        String storedLocale = null;
×
938
                        if (httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE) != null) {
×
939
                                storedLocale = httpRequest.getSession().getAttribute(FilterUtil.LOCALE_ATTRIBUTE).toString();
×
940
                        }
941
                        // if user has changed locale parameter to new one
942
                        // or chooses it parameter at first page loading
UNCOV
943
                        if (storedLocale == null || !storedLocale.equals(localeParameter)) {
×
944
                                log.info("Stored locale parameter to session " + localeParameter);
×
UNCOV
945
                                httpRequest.getSession().setAttribute(FilterUtil.LOCALE_ATTRIBUTE, localeParameter);
×
946
                        }
UNCOV
947
                        if (rememberLocale) {
×
UNCOV
948
                                httpRequest.getSession().setAttribute(FilterUtil.LOCALE_ATTRIBUTE, localeParameter);
×
UNCOV
949
                                httpRequest.getSession().setAttribute(FilterUtil.REMEMBER_ATTRIBUTE, true);
×
UNCOV
950
                                wizardModel.localeToSave = localeParameter;
×
951
                        } else {
952
                                // we need to reset it if it was set before
UNCOV
953
                                httpRequest.getSession().setAttribute(FilterUtil.REMEMBER_ATTRIBUTE, null);
×
UNCOV
954
                                wizardModel.localeToSave = null;
×
955
                        }
956
                }
UNCOV
957
        }
×
958
        
959
        /**
960
         * It sets locale parameter for current session when user is making first GET http request to
961
         * application. It retrieves user locale from request object and checks if this locale is supported
962
         * by application. If not, it uses {@link Locale#ENGLISH} by default
963
         *
964
         * @param httpRequest the http request object
965
         */
966
        public void checkLocaleAttributesForFirstTime(HttpServletRequest httpRequest) {
967
                Locale locale = httpRequest.getLocale();
×
UNCOV
968
                if (CustomResourceLoader.getInstance(httpRequest).getAvailablelocales().contains(locale)) {
×
UNCOV
969
                        httpRequest.getSession().setAttribute(FilterUtil.LOCALE_ATTRIBUTE, locale.toString());
×
970
                } else {
971
                        httpRequest.getSession().setAttribute(FilterUtil.LOCALE_ATTRIBUTE, Locale.ENGLISH.toString());
×
972
                }
UNCOV
973
        }
×
974
        
975
        /**
976
         * Verify the database connection works.
977
         *
978
         * @param connectionUsername
979
         * @param connectionPassword
980
         * @param databaseConnectionFinalUrl
981
         * @return true/false whether it was verified or not
982
         */
983
        private boolean verifyConnection(String connectionUsername, String connectionPassword,
984
                String databaseConnectionFinalUrl) {
985
                try {
986
                        // verify connection
987
                        //Set Database Driver using driver String
UNCOV
988
                        Class.forName(loadedDriverString).newInstance();
×
UNCOV
989
                        try (Connection ignored = DriverManager.getConnection(databaseConnectionFinalUrl, connectionUsername, connectionPassword)) {
×
990
                                return true;
×
991
                        }
992
                }
UNCOV
993
                catch (Exception e) {
×
UNCOV
994
                        errors.put("User account " + connectionUsername + " does not work. " + e.getMessage()
×
995
                                        + " See the error log for more details",
996
                                null); // TODO internationalize this
997
                        log.warn("Error while checking the connection user account", e);
×
998
                        return false;
×
999
                }
1000
        }
1001
        
1002
        /**
1003
         * Convenience method to load the runtime properties file.
1004
         *
1005
         * @return the runtime properties file.
1006
         */
1007
        private File getRuntimePropertiesFile() {
1008
                File file;
1009
                
UNCOV
1010
                String pathName = OpenmrsUtil.getRuntimePropertiesFilePathName(WebConstants.WEBAPP_NAME);
×
UNCOV
1011
                if (pathName != null) {
×
UNCOV
1012
                        file = new File(pathName);
×
1013
                } else {
UNCOV
1014
                        file = new File(OpenmrsUtil.getApplicationDataDirectory(), getRuntimePropertiesFileName());
×
1015
                }
1016
                
UNCOV
1017
                log.debug("Using file: " + file.getAbsolutePath());
×
1018
                
UNCOV
1019
                return file;
×
1020
        }
1021
        
1022
        private String getRuntimePropertiesFileName() {
UNCOV
1023
                String fileName = OpenmrsUtil.getRuntimePropertiesFileNameInTestMode();
×
UNCOV
1024
                if (fileName == null) {
×
UNCOV
1025
                        fileName = WebConstants.WEBAPP_NAME + "-runtime.properties";
×
1026
                }
1027
                return fileName;
×
1028
        }
1029
        
1030
        /**
1031
         * @see org.openmrs.web.filter.StartupFilter#getTemplatePrefix()
1032
         */
1033
        @Override
1034
        protected String getTemplatePrefix() {
UNCOV
1035
                return "org/openmrs/web/filter/initialization/";
×
1036
        }
1037
        
1038
        /**
1039
         * @see org.openmrs.web.filter.StartupFilter#getUpdateFilterModel()
1040
         */
1041
        @Override
1042
        protected Object getUpdateFilterModel() {
1043
                return wizardModel;
×
1044
        }
1045
        
1046
        /**
1047
         * @see org.openmrs.web.filter.StartupFilter#skipFilter(HttpServletRequest)
1048
         */
1049
        @Override
1050
        public boolean skipFilter(HttpServletRequest httpRequest) {
1051
                // If progress.vm makes an ajax request even immediately after initialization has completed
1052
                // let the request pass in order to let progress.vm load the start page of OpenMRS
1053
                // (otherwise progress.vm is displayed "forever")
UNCOV
1054
                return !PROGRESS_VM_AJAXREQUEST.equals(httpRequest.getParameter("page")) && !initializationRequired();
×
1055
        }
1056
        
1057
        /**
1058
         * Public method that returns true if database+runtime properties initialization is required
1059
         *
1060
         * @return true if this initialization wizard needs to run
1061
         */
1062
        public static boolean initializationRequired() {
UNCOV
1063
                return !isInitializationComplete();
×
1064
        }
1065
        
1066
        /**
1067
         * @param isInstallationStarted the value to set
1068
         */
1069
        protected static synchronized void setInstallationStarted(boolean isInstallationStarted) {
UNCOV
1070
                InitializationFilter.isInstallationStarted = isInstallationStarted;
×
UNCOV
1071
        }
×
1072
        
1073
        /**
1074
         * @return true if installation has been started
1075
         */
1076
        protected static boolean isInstallationStarted() {
1077
                return isInstallationStarted;
×
1078
        }
1079
        
1080
        /**
1081
         * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
1082
         */
1083
        @Override
1084
        public void init(FilterConfig filterConfig) throws ServletException {
1085
                super.init(filterConfig);
×
UNCOV
1086
                wizardModel = new InitializationWizardModel();
×
UNCOV
1087
                DatabaseDetective databaseDetective = new DatabaseDetective();
×
1088
                //set whether need to do initialization work
1089
                if (databaseDetective.isDatabaseEmpty(OpenmrsUtil.getRuntimeProperties(WebConstants.WEBAPP_NAME))) {
×
1090
                        //if runtime-properties file doesn't exist, have to do initialization work
UNCOV
1091
                        setInitializationComplete(false);
×
1092
                } else {
1093
                        //if database is not empty, then let UpdaterFilter to judge whether need database update
1094
                        setInitializationComplete(true);
×
1095
                }
1096
        }
×
1097
        
1098
        private void importTestDataSet(InputStream in, String connectionUrl, String connectionUsername,
1099
                String connectionPassword) throws IOException {
1100
                File tempFile = null;
×
1101
                FileOutputStream fileOut = null;
×
1102
                try {
1103
                        ZipInputStream zipIn = new ZipInputStream(in);
×
1104
                        zipIn.getNextEntry();
×
1105
                        
UNCOV
1106
                        tempFile = File.createTempFile("testDataSet", "dump");
×
1107
                        fileOut = new FileOutputStream(tempFile);
×
1108
                        
UNCOV
1109
                        IOUtils.copy(zipIn, fileOut);
×
1110
                        
UNCOV
1111
                        fileOut.close();
×
UNCOV
1112
                        zipIn.close();
×
1113
                        
1114
                        //Cater for the stand-alone connection url with has :mxj:
UNCOV
1115
                        if (connectionUrl.contains(":mxj:")) {
×
UNCOV
1116
                                connectionUrl = connectionUrl.replace(":mxj:", ":");
×
1117
                        }
1118
                        
UNCOV
1119
                        URI uri = URI.create(connectionUrl.substring(5)); //remove 'jdbc:' prefix to conform to the URI format
×
UNCOV
1120
                        String host = uri.getHost();
×
UNCOV
1121
                        int port = uri.getPort();
×
1122
                        
UNCOV
1123
                        TestInstallUtil.addTestData(host, port, wizardModel.databaseName, connectionUsername, connectionPassword,
×
1124
                                tempFile.getAbsolutePath());
×
1125
                }
1126
                finally {
1127
                        IOUtils.closeQuietly(in);
×
UNCOV
1128
                        IOUtils.closeQuietly(fileOut);
×
1129
                        
1130
                        if (tempFile != null) {
×
1131
                                tempFile.delete();
×
1132
                        }
1133
                }
1134
        }
×
1135
        
1136
        private boolean isCurrentDatabase(String database) {
UNCOV
1137
                return wizardModel.databaseConnection.contains(database);
×
1138
        }
1139
        
1140
        /**
1141
         * @param silent if this statement fails do not display stack trace or record an error in the wizard
1142
         *            object.
1143
         * @param user username to connect with
1144
         * @param pw password to connect with
1145
         * @param sql String containing sql and question marks
1146
         * @param args the strings to fill into the question marks in the given sql
1147
         * @return result of executeUpdate or -1 for error
1148
         */
1149
        private int executeStatement(boolean silent, String user, String pw, String sql, String... args) {
1150
                
1151
                Connection connection = null;
×
UNCOV
1152
                Statement statement = null;
×
1153
                try {
UNCOV
1154
                        String replacedSql = sql;
×
1155
                        
1156
                        // TODO how to get the driver for the other dbs...
1157
                        if (isCurrentDatabase(DATABASE_MYSQL)) {
×
UNCOV
1158
                                Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
×
UNCOV
1159
                        } else if (isCurrentDatabase(DATABASE_POSTGRESQL)) {
×
1160
                                Class.forName("org.postgresql.Driver").newInstance();
×
1161
                                replacedSql = replacedSql.replaceAll("`", "\"");
×
1162
                        } else {
1163
                                replacedSql = replacedSql.replaceAll("`", "\"");
×
1164
                        }
1165
                        
1166
                        String tempDatabaseConnection;
1167
                        if (sql.contains("create database")) {
×
1168
                                tempDatabaseConnection = wizardModel.databaseConnection.replace("@DBNAME@",
×
1169
                                        ""); // make this dbname agnostic so we can create the db
1170
                        } else {
UNCOV
1171
                                tempDatabaseConnection = wizardModel.databaseConnection.replace("@DBNAME@", wizardModel.databaseName);
×
1172
                        }
1173
                        
UNCOV
1174
                        connection = DriverManager.getConnection(tempDatabaseConnection, user, pw);
×
1175
                        
1176
                        for (String arg : args) {
×
1177
                                arg = arg.replace(";", "&#094"); // to prevent any sql injection
×
1178
                                replacedSql = replacedSql.replaceFirst("\\?", arg);
×
1179
                        }
1180
                        
1181
                        // run the sql statement
1182
                        statement = connection.createStatement();
×
1183
                        
UNCOV
1184
                        return statement.executeUpdate(replacedSql);
×
1185
                        
1186
                }
1187
                catch (SQLException sqlex) {
×
UNCOV
1188
                        if (!silent) {
×
1189
                                // log and add error
1190
                                log.warn("error executing sql: " + sql, sqlex);
×
UNCOV
1191
                                errors.put("Error executing sql: " + sql + " - " + sqlex.getMessage(), null);
×
1192
                        }
1193
                }
UNCOV
1194
                catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
×
UNCOV
1195
                        log.error("Error generated", e);
×
1196
                }
1197
                finally {
1198
                        try {
UNCOV
1199
                                if (statement != null) {
×
1200
                                        statement.close();
×
1201
                                }
1202
                        }
UNCOV
1203
                        catch (SQLException e) {
×
UNCOV
1204
                                log.warn("Error while closing statement");
×
UNCOV
1205
                        }
×
1206
                        try {
1207
                                
UNCOV
1208
                                if (connection != null) {
×
UNCOV
1209
                                        connection.close();
×
1210
                                }
1211
                        }
UNCOV
1212
                        catch (Exception e) {
×
1213
                                log.warn("Error while closing connection", e);
×
1214
                        }
×
1215
                }
1216
                
1217
                return -1;
×
1218
        }
1219
        
1220
        /**
1221
         * Convenience variable to know if this wizard has completed successfully and that this wizard does
1222
         * not need to be executed again
1223
         *
1224
         * @return true if this has been run already
1225
         */
1226
        private static synchronized boolean isInitializationComplete() {
UNCOV
1227
                return initializationComplete;
×
1228
        }
1229
        
1230
        /**
1231
         * Check if the given value is null or a zero-length String
1232
         *
1233
         * @param value the string to check
1234
         * @param errors the list of errors to append the errorMessage to if value is empty
1235
         * @param errorMessageCode the string with code of error message translation to append if value is
1236
         *            empty
1237
         * @return true if the value is non-empty
1238
         */
1239
        private boolean checkForEmptyValue(String value, Map<String, Object[]> errors, String errorMessageCode) {
UNCOV
1240
                if (!StringUtils.isEmpty(value)) {
×
UNCOV
1241
                        return true;
×
1242
                }
UNCOV
1243
                errors.put(errorMessageCode, null);
×
UNCOV
1244
                return false;
×
1245
        }
1246
        
1247
        /**
1248
         * Separate thread that will run through all tasks to complete the initialization. The database is
1249
         * created, user's created, etc here
1250
         */
1251
        private class InitializationCompletion {
1252
                
1253
                private Thread thread;
1254
                
1255
                private int steps = 0;
×
1256
                
UNCOV
1257
                private String message = "";
×
1258
                
1259
                private Map<String, Object[]> errors = new HashMap<>();
×
1260
                
UNCOV
1261
                private String errorPage = null;
×
1262
                
UNCOV
1263
                private boolean erroneous = false;
×
1264
                
UNCOV
1265
                private int completedPercentage = 0;
×
1266
                
1267
                private WizardTask executingTask;
1268
                
1269
                private List<WizardTask> executedTasks = new ArrayList<>();
×
1270
                
1271
                public synchronized void reportError(String error, String errorPage, Object... params) {
UNCOV
1272
                        errors.put(error, params);
×
1273
                        this.errorPage = errorPage;
×
UNCOV
1274
                        erroneous = true;
×
1275
                }
×
1276
                
1277
                public synchronized boolean hasErrors() {
1278
                        return erroneous;
×
1279
                }
1280
                
1281
                public synchronized String getErrorPage() {
1282
                        return errorPage;
×
1283
                }
1284
                
1285
                public synchronized Map<String, Object[]> getErrors() {
UNCOV
1286
                        return errors;
×
1287
                }
1288
                
1289
                /**
1290
                 * Start the completion stage. This fires up the thread to do all the work.
1291
                 */
1292
                public void start() {
1293
                        setStepsComplete(0);
×
1294
                        setInitializationComplete(false);
×
1295
                        thread.start();
×
UNCOV
1296
                }
×
1297
                
1298
                public void waitForCompletion() {
1299
                        try {
UNCOV
1300
                                thread.join();
×
1301
                        }
UNCOV
1302
                        catch (InterruptedException e) {
×
UNCOV
1303
                                log.error("Error generated", e);
×
UNCOV
1304
                        }
×
UNCOV
1305
                }
×
1306
                
1307
                protected synchronized void setStepsComplete(int steps) {
1308
                        this.steps = steps;
×
UNCOV
1309
                }
×
1310
                
1311
                protected synchronized int getStepsComplete() {
UNCOV
1312
                        return steps;
×
1313
                }
1314
                
1315
                public synchronized String getMessage() {
1316
                        return message;
×
1317
                }
1318
                
1319
                public synchronized void setMessage(String message) {
UNCOV
1320
                        this.message = message;
×
UNCOV
1321
                        setStepsComplete(getStepsComplete() + 1);
×
UNCOV
1322
                }
×
1323
                
1324
                /**
1325
                 * @return the executingTask
1326
                 */
1327
                protected synchronized WizardTask getExecutingTask() {
UNCOV
1328
                        return executingTask;
×
1329
                }
1330
                
1331
                /**
1332
                 * @return the completedPercentage
1333
                 */
1334
                protected synchronized int getCompletedPercentage() {
UNCOV
1335
                        return completedPercentage;
×
1336
                }
1337
                
1338
                /**
1339
                 * @param completedPercentage the completedPercentage to set
1340
                 */
1341
                protected synchronized void setCompletedPercentage(int completedPercentage) {
UNCOV
1342
                        this.completedPercentage = completedPercentage;
×
UNCOV
1343
                }
×
1344
                
1345
                /**
1346
                 * Adds a task that has been completed to the list of executed tasks
1347
                 *
1348
                 * @param task
1349
                 */
1350
                protected synchronized void addExecutedTask(WizardTask task) {
UNCOV
1351
                        this.executedTasks.add(task);
×
UNCOV
1352
                }
×
1353
                
1354
                /**
1355
                 * @param executingTask the executingTask to set
1356
                 */
1357
                protected synchronized void setExecutingTask(WizardTask executingTask) {
1358
                        this.executingTask = executingTask;
×
UNCOV
1359
                }
×
1360
                
1361
                /**
1362
                 * @return the executedTasks
1363
                 */
1364
                protected synchronized List<WizardTask> getExecutedTasks() {
1365
                        return this.executedTasks;
×
1366
                }
1367
                
1368
                /**
1369
                 * This class does all the work of creating the desired database, user, updates, etc
1370
                 */
UNCOV
1371
                public InitializationCompletion() {
×
1372
                        Runnable r = new Runnable() {
×
1373
                                
1374
                                /**
1375
                                 * TODO split this up into multiple testable methods
1376
                                 *
1377
                                 * @see java.lang.Runnable#run()
1378
                                 */
1379
                                @Override
1380
                                public void run() {
1381
                                        try {
1382
                                                String connectionUsername;
1383
                                                StringBuilder connectionPassword = new StringBuilder();
×
1384
                                                ChangeLogDetective changeLogDetective = new ChangeLogDetective();
×
1385
                                                ChangeLogVersionFinder changeLogVersionFinder = new ChangeLogVersionFinder();
×
1386
                                                
1387
                                                if (!wizardModel.hasCurrentOpenmrsDatabase) {
×
UNCOV
1388
                                                        setMessage("Create database");
×
UNCOV
1389
                                                        setExecutingTask(WizardTask.CREATE_SCHEMA);
×
1390
                                                        // connect via jdbc and create a database
1391
                                                        String sql;
UNCOV
1392
                                                        if (isCurrentDatabase(DATABASE_MYSQL)) {
×
1393
                                                                sql = "create database if not exists `?` default character set utf8";
×
1394
                                                        } else if (isCurrentDatabase(DATABASE_POSTGRESQL)) {
×
1395
                                                                sql = "create database `?` encoding 'utf8'";
×
1396
                                                        } else if (isCurrentDatabase(DATABASE_H2)) {
×
1397
                                                                sql = null;
×
1398
                                                        } else {
UNCOV
1399
                                                                sql = "create database `?`";
×
1400
                                                        }
1401
                                                        
1402
                                                        int result;
UNCOV
1403
                                                        if (sql != null) {
×
UNCOV
1404
                                                                result = executeStatement(false, wizardModel.createDatabaseUsername,
×
1405
                                                                        wizardModel.createDatabasePassword, sql, wizardModel.databaseName);
×
1406
                                                        } else {
1407
                                                                result = 1;
×
1408
                                                        }
1409
                                                        // throw the user back to the main screen if this error occurs
UNCOV
1410
                                                        if (result < 0) {
×
1411
                                                                reportError(ErrorMessageConstants.ERROR_DB_CREATE_NEW, DEFAULT_PAGE);
×
UNCOV
1412
                                                                return;
×
1413
                                                        } else {
1414
                                                                wizardModel.workLog.add("Created database " + wizardModel.databaseName);
×
1415
                                                        }
1416
                                                        
1417
                                                        addExecutedTask(WizardTask.CREATE_SCHEMA);
×
1418
                                                }
1419
                                                
1420
                                                if (wizardModel.createDatabaseUser) {
×
1421
                                                        setMessage("Create database user");
×
1422
                                                        setExecutingTask(WizardTask.CREATE_DB_USER);
×
1423
                                                        connectionUsername = wizardModel.databaseName + "_user";
×
1424
                                                        if (connectionUsername.length() > 16) {
×
UNCOV
1425
                                                                connectionUsername = wizardModel.databaseName.substring(0, 11)
×
1426
                                                                        + "_user"; // trim off enough to leave space for _user at the end
1427
                                                        }
1428
                                                        
UNCOV
1429
                                                        connectionPassword.append("");
×
1430
                                                        // generate random password from this subset of alphabet
1431
                                                        // intentionally left out these characters: ufsb$() to prevent certain words forming randomly
1432
                                                        String chars = "acdeghijklmnopqrtvwxyzACDEGHIJKLMNOPQRTVWXYZ0123456789.|~@#^&";
×
1433
                                                        Random r = new Random();
×
UNCOV
1434
                                                        StringBuilder randomStr = new StringBuilder("");
×
UNCOV
1435
                                                        for (int x = 0; x < 12; x++) {
×
1436
                                                                randomStr.append(chars.charAt(r.nextInt(chars.length())));
×
1437
                                                        }
1438
                                                        connectionPassword.append(randomStr);
×
1439
                                                        
1440
                                                        // connect via jdbc with root user and create an openmrs user
1441
                                                        String host = "'%'";
×
1442
                                                        if (wizardModel.databaseConnection.contains("localhost")
×
UNCOV
1443
                                                                || wizardModel.databaseConnection.contains("127.0.0.1")) {
×
UNCOV
1444
                                                                host = "'localhost'";
×
1445
                                                        }
1446
                                                        
1447
                                                        String sql = "";
×
1448
                                                        if (isCurrentDatabase(DATABASE_MYSQL)) {
×
1449
                                                                sql = "drop user '?'@" + host;
×
1450
                                                        } else if (isCurrentDatabase(DATABASE_POSTGRESQL)) {
×
1451
                                                                sql = "drop user `?`";
×
1452
                                                        }
1453
                                                        
1454
                                                        executeStatement(true, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
×
1455
                                                                connectionUsername);
1456
                                                        
UNCOV
1457
                                                        if (isCurrentDatabase(DATABASE_MYSQL)) {
×
1458
                                                                sql = "create user '?'@" + host + " identified by '?'";
×
1459
                                                        } else if (isCurrentDatabase(DATABASE_POSTGRESQL)) {
×
1460
                                                                sql = "create user `?` with password '?'";
×
1461
                                                        }
1462
                                                        
1463
                                                        if (-1 != executeStatement(false, wizardModel.createUserUsername, wizardModel.createUserPassword,
×
UNCOV
1464
                                                                sql, connectionUsername, connectionPassword.toString())) {
×
UNCOV
1465
                                                                wizardModel.workLog.add("Created user " + connectionUsername);
×
1466
                                                        } else {
1467
                                                                // if error occurs stop
1468
                                                                reportError(ErrorMessageConstants.ERROR_DB_CREATE_DB_USER, DEFAULT_PAGE);
×
1469
                                                                return;
×
1470
                                                        }
1471
                                                        
1472
                                                        // grant the roles
1473
                                                        int result = 1;
×
1474
                                                        if (isCurrentDatabase(DATABASE_MYSQL)) {
×
UNCOV
1475
                                                                sql = "GRANT ALL ON `?`.* TO '?'@" + host;
×
1476
                                                                result = executeStatement(false, wizardModel.createUserUsername,
×
1477
                                                                        wizardModel.createUserPassword, sql, wizardModel.databaseName, connectionUsername);
×
UNCOV
1478
                                                        } else if (isCurrentDatabase(DATABASE_POSTGRESQL)) {
×
UNCOV
1479
                                                                sql = "ALTER USER `?` WITH SUPERUSER";
×
1480
                                                                result = executeStatement(false, wizardModel.createUserUsername,
×
UNCOV
1481
                                                                        wizardModel.createUserPassword, sql, connectionUsername);
×
1482
                                                        }
1483
                                                        
1484
                                                        // throw the user back to the main screen if this error occurs
1485
                                                        if (result < 0) {
×
UNCOV
1486
                                                                reportError(ErrorMessageConstants.ERROR_DB_GRANT_PRIV, DEFAULT_PAGE);
×
UNCOV
1487
                                                                return;
×
1488
                                                        } else {
1489
                                                                wizardModel.workLog.add("Granted user " + connectionUsername + " all privileges to database "
×
UNCOV
1490
                                                                        + wizardModel.databaseName);
×
1491
                                                        }
1492
                                                        
1493
                                                        addExecutedTask(WizardTask.CREATE_DB_USER);
×
1494
                                                } else {
×
1495
                                                        connectionUsername = wizardModel.currentDatabaseUsername;
×
UNCOV
1496
                                                        connectionPassword.setLength(0);
×
1497
                                                        connectionPassword.append(wizardModel.currentDatabasePassword);
×
1498
                                                }
1499
                                                
1500
                                                String finalDatabaseConnectionString = wizardModel.databaseConnection.replace("@DBNAME@",
×
1501
                                                        wizardModel.databaseName);
×
1502
                                                
1503
                                                finalDatabaseConnectionString = finalDatabaseConnectionString.replace("@APPLICATIONDATADIR@",
×
1504
                                                        OpenmrsUtil.getApplicationDataDirectory().replace("\\", "/"));
×
1505
                                                
1506
                                                // verify that the database connection works
1507
                                                if (!verifyConnection(connectionUsername, connectionPassword.toString(),
×
1508
                                                        finalDatabaseConnectionString)) {
1509
                                                        setMessage("Verify that the database connection works");
×
1510
                                                        // redirect to setup page if we got an error
1511
                                                        reportError("Unable to connect to database", DEFAULT_PAGE);
×
1512
                                                        return;
×
1513
                                                }
1514
                                                
1515
                                                // save the properties for startup purposes
1516
                                                Properties runtimeProperties = new Properties();
×
1517
                                                
UNCOV
1518
                                                runtimeProperties.put("connection.url", finalDatabaseConnectionString);
×
UNCOV
1519
                                                runtimeProperties.put("connection.username", connectionUsername);
×
UNCOV
1520
                                                runtimeProperties.put("connection.password", connectionPassword.toString());
×
UNCOV
1521
                                                if (StringUtils.hasText(wizardModel.databaseDriver)) {
×
UNCOV
1522
                                                        runtimeProperties.put("connection.driver_class", wizardModel.databaseDriver);
×
1523
                                                }
1524
                                                if (finalDatabaseConnectionString.contains(DATABASE_POSTGRESQL)) {
×
UNCOV
1525
                                                        runtimeProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
×
1526
                                                }
UNCOV
1527
                                                if (finalDatabaseConnectionString.contains(DATABASE_SQLSERVER)) {
×
1528
                                                        runtimeProperties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
×
1529
                                                }
1530
                                                if (finalDatabaseConnectionString.contains(DATABASE_H2)) {
×
UNCOV
1531
                                                        runtimeProperties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
×
1532
                                                }
UNCOV
1533
                                                runtimeProperties.put("module.allow_web_admin", "" + wizardModel.moduleWebAdmin);
×
UNCOV
1534
                                                runtimeProperties.put("auto_update_database", "" + wizardModel.autoUpdateDatabase);
×
UNCOV
1535
                                                final Encoder base64 = Base64.getEncoder();
×
UNCOV
1536
                                                runtimeProperties.put(OpenmrsConstants.ENCRYPTION_VECTOR_RUNTIME_PROPERTY,
×
1537
                                                        new String(base64.encode(Security.generateNewInitVector()), StandardCharsets.UTF_8));
×
1538
                                                runtimeProperties.put(OpenmrsConstants.ENCRYPTION_KEY_RUNTIME_PROPERTY,
×
1539
                                                        new String(base64.encode(Security.generateNewSecretKey()), StandardCharsets.UTF_8));
×
1540
                                                
1541
                                                runtimeProperties.putAll(wizardModel.additionalPropertiesFromInstallationScript);
×
1542
                                                
1543
                                                Properties properties = Context.getRuntimeProperties();
×
UNCOV
1544
                                                properties.putAll(runtimeProperties);
×
UNCOV
1545
                                                runtimeProperties = properties;
×
UNCOV
1546
                                                Context.setRuntimeProperties(runtimeProperties);
×
1547
                                                
1548
                                                /**
1549
                                                 * A callback class that prints out info about liquibase changesets
1550
                                                 */
1551
                                                class PrintingChangeSetExecutorCallback implements ChangeSetExecutorCallback {
1552
                                                        
1553
                                                        private int i = 1;
×
1554
                                                        
1555
                                                        private String message;
1556
                                                        
UNCOV
1557
                                                        public PrintingChangeSetExecutorCallback(String message) {
×
1558
                                                                this.message = message;
×
UNCOV
1559
                                                        }
×
1560
                                                        
1561
                                                        /**
1562
                                                         * @see ChangeSetExecutorCallback#executing(liquibase.changelog.ChangeSet, int)
1563
                                                         */
1564
                                                        @Override
1565
                                                        public void executing(ChangeSet changeSet, int numChangeSetsToRun) {
1566
                                                                setMessage(message + " (" + i++ + "/" + numChangeSetsToRun + "): Author: "
×
UNCOV
1567
                                                                        + changeSet.getAuthor() + " Comments: " + changeSet.getComments() + " Description: "
×
1568
                                                                        + changeSet.getDescription());
×
UNCOV
1569
                                                                float numChangeSetsToRunFloat = (float) numChangeSetsToRun;
×
1570
                                                                float j = (float) i;
×
UNCOV
1571
                                                                setCompletedPercentage(Math.round(j * 100 / numChangeSetsToRunFloat));
×
1572
                                                        }
×
1573
                                                        
1574
                                                }
1575
                                                
1576
                                                if (wizardModel.createTables) {
×
1577
                                                        // use liquibase to create core data + tables
1578
                                                        try {
1579
                                                                String liquibaseSchemaFileName = changeLogVersionFinder.getLatestSchemaSnapshotFilename()
×
1580
                                                                        .get();
×
UNCOV
1581
                                                                String liquibaseCoreDataFileName = changeLogVersionFinder.getLatestCoreDataSnapshotFilename()
×
UNCOV
1582
                                                                        .get();
×
1583
                                                                
UNCOV
1584
                                                                setMessage("Executing " + liquibaseSchemaFileName);
×
1585
                                                                setExecutingTask(WizardTask.CREATE_TABLES);
×
1586
                                                                
1587
                                                                log.debug("executing Liquibase file '{}' ", liquibaseSchemaFileName);
×
1588
                                                                
UNCOV
1589
                                                                DatabaseUpdater.executeChangelog(liquibaseSchemaFileName,
×
1590
                                                                        new PrintingChangeSetExecutorCallback("OpenMRS schema file"));
1591
                                                                addExecutedTask(WizardTask.CREATE_TABLES);
×
1592
                                                                
1593
                                                                //reset for this task
1594
                                                                setCompletedPercentage(0);
×
1595
                                                                setExecutingTask(WizardTask.ADD_CORE_DATA);
×
1596
                                                                
1597
                                                                log.debug("executing Liquibase file '{}' ", liquibaseCoreDataFileName);
×
1598
                                                                
1599
                                                                DatabaseUpdater.executeChangelog(liquibaseCoreDataFileName,
×
1600
                                                                        new PrintingChangeSetExecutorCallback("OpenMRS core data file"));
UNCOV
1601
                                                                wizardModel.workLog.add("Created database tables and added core data");
×
1602
                                                                addExecutedTask(WizardTask.ADD_CORE_DATA);
×
1603
                                                                
1604
                                                        }
UNCOV
1605
                                                        catch (Exception e) {
×
1606
                                                                reportError(ErrorMessageConstants.ERROR_DB_CREATE_TABLES_OR_ADD_DEMO_DATA, DEFAULT_PAGE,
×
1607
                                                                        e.getMessage());
×
1608
                                                                log.warn("Error while trying to create tables and demo data", e);
×
UNCOV
1609
                                                        }
×
1610
                                                }
1611
                                                
1612
                                                if (wizardModel.importTestData) {
×
1613
                                                        try {
1614
                                                                setMessage("Importing test data");
×
UNCOV
1615
                                                                setExecutingTask(WizardTask.IMPORT_TEST_DATA);
×
1616
                                                                setCompletedPercentage(0);
×
1617
                                                                
1618
                                                                try {
UNCOV
1619
                                                                        InputStream inData = TestInstallUtil.getResourceInputStream(
×
1620
                                                                                wizardModel.remoteUrl + RELEASE_TESTING_MODULE_PATH + "generateTestDataSet.form",
×
1621
                                                                                wizardModel.remoteUsername, wizardModel.remotePassword);
×
1622
                                                                        
UNCOV
1623
                                                                        setCompletedPercentage(40);
×
1624
                                                                        setMessage("Loading imported test data...");
×
1625
                                                                        importTestDataSet(inData, finalDatabaseConnectionString, connectionUsername,
×
UNCOV
1626
                                                                                connectionPassword.toString());
×
1627
                                                                        wizardModel.workLog.add("Imported test data");
×
1628
                                                                        addExecutedTask(WizardTask.IMPORT_TEST_DATA);
×
1629
                                                                        
1630
                                                                        //reset the progress for the next task
1631
                                                                        setCompletedPercentage(0);
×
UNCOV
1632
                                                                        setMessage("Importing modules from remote server...");
×
UNCOV
1633
                                                                        setExecutingTask(WizardTask.ADD_MODULES);
×
1634
                                                                        
1635
                                                                        InputStream inModules = TestInstallUtil.getResourceInputStream(
×
UNCOV
1636
                                                                                wizardModel.remoteUrl + RELEASE_TESTING_MODULE_PATH + "getModules.htm",
×
1637
                                                                                wizardModel.remoteUsername, wizardModel.remotePassword);
×
1638
                                                                        
1639
                                                                        setCompletedPercentage(90);
×
UNCOV
1640
                                                                        setMessage("Adding imported modules...");
×
1641
                                                                        if (!TestInstallUtil.addZippedTestModules(inModules)) {
×
UNCOV
1642
                                                                                reportError(ErrorMessageConstants.ERROR_DB_UNABLE_TO_ADD_MODULES, DEFAULT_PAGE, "");
×
1643
                                                                                return;
×
1644
                                                                        } else {
1645
                                                                                wizardModel.workLog.add("Added Modules");
×
UNCOV
1646
                                                                                addExecutedTask(WizardTask.ADD_MODULES);
×
1647
                                                                        }
1648
                                                                }
1649
                                                                catch (APIAuthenticationException e) {
×
1650
                                                                        log.warn("Unable to authenticate as a User with the System Developer role");
×
1651
                                                                        reportError(ErrorMessageConstants.UPDATE_ERROR_UNABLE_AUTHENTICATE,
×
1652
                                                                                TESTING_REMOTE_DETAILS_SETUP, "");
1653
                                                                        return;
×
UNCOV
1654
                                                                }
×
1655
                                                        }
UNCOV
1656
                                                        catch (Exception e) {
×
UNCOV
1657
                                                                reportError(ErrorMessageConstants.ERROR_DB_IMPORT_TEST_DATA, DEFAULT_PAGE, e.getMessage());
×
1658
                                                                log.warn("Error while trying to import test data", e);
×
1659
                                                                return;
×
1660
                                                        }
×
1661
                                                }
1662
                                                
1663
                                                // add demo data only if creating tables fresh and user selected the option add demo data
1664
                                                if (wizardModel.createTables && wizardModel.addDemoData) {
×
1665
                                                        try {
UNCOV
1666
                                                                setMessage("Adding demo data");
×
1667
                                                                setCompletedPercentage(0);
×
UNCOV
1668
                                                                setExecutingTask(WizardTask.ADD_DEMO_DATA);
×
1669
                                                                
UNCOV
1670
                                                                log.debug("executing Liquibase file '{}' ", LIQUIBASE_DEMO_DATA);
×
1671
                                                                
UNCOV
1672
                                                                DatabaseUpdater.executeChangelog(LIQUIBASE_DEMO_DATA,
×
1673
                                                                        new PrintingChangeSetExecutorCallback("OpenMRS demo patients, users, and forms"));
UNCOV
1674
                                                                wizardModel.workLog.add("Added demo data");
×
1675
                                                                
1676
                                                                addExecutedTask(WizardTask.ADD_DEMO_DATA);
×
1677
                                                        }
1678
                                                        catch (Exception e) {
×
1679
                                                                reportError(ErrorMessageConstants.ERROR_DB_CREATE_TABLES_OR_ADD_DEMO_DATA, DEFAULT_PAGE,
×
UNCOV
1680
                                                                        e.getMessage());
×
1681
                                                                log.warn("Error while trying to add demo data", e);
×
UNCOV
1682
                                                        }
×
1683
                                                }
1684
                                                
1685
                                                // update the database to the latest version
1686
                                                try {
1687
                                                        setMessage("Updating the database to the latest version");
×
1688
                                                        setCompletedPercentage(0);
×
1689
                                                        setExecutingTask(WizardTask.UPDATE_TO_LATEST);
×
1690
                                                        
UNCOV
1691
                                                        String version = null;
×
1692
                                                        
1693
                                                        if (wizardModel.createTables) {
×
UNCOV
1694
                                                                version = changeLogVersionFinder.getLatestSnapshotVersion().get();
×
1695
                                                        } else {
UNCOV
1696
                                                                version = changeLogDetective.getInitialLiquibaseSnapshotVersion(DatabaseUpdater.CONTEXT,
×
1697
                                                                        new DatabaseUpdaterLiquibaseProvider());
1698
                                                        }
1699
                                                        
UNCOV
1700
                                                        log.debug(
×
1701
                                                                "updating the database with versions of liquibase-update-to-latest files greater than '{}'",
1702
                                                                version);
1703
                                                        
1704
                                                        List<String> changelogs = changeLogVersionFinder
×
1705
                                                                .getUpdateFileNames(changeLogVersionFinder.getUpdateVersionsGreaterThan(version));
×
1706
                                                        
1707
                                                        for (String changelog : changelogs) {
×
UNCOV
1708
                                                                log.debug("applying Liquibase changelog '{}'", changelog);
×
1709
                                                                
UNCOV
1710
                                                                DatabaseUpdater.executeChangelog(changelog,
×
1711
                                                                        new PrintingChangeSetExecutorCallback("executing Liquibase changelog " + changelog));
UNCOV
1712
                                                        }
×
UNCOV
1713
                                                        addExecutedTask(WizardTask.UPDATE_TO_LATEST);
×
1714
                                                }
1715
                                                catch (Exception e) {
×
1716
                                                        reportError(ErrorMessageConstants.ERROR_DB_UPDATE_TO_LATEST, DEFAULT_PAGE, e.getMessage());
×
1717
                                                        log.warn("Error while trying to update to the latest database version", e);
×
1718
                                                        return;
×
1719
                                                }
×
1720
                                                
1721
                                                setExecutingTask(null);
×
1722
                                                setMessage("Starting OpenMRS");
×
1723
                                                
1724
                                                // start spring
1725
                                                // after this point, all errors need to also call: contextLoader.closeWebApplicationContext(event.getServletContext())
1726
                                                // logic copied from org.springframework.web.context.ContextLoaderListener
1727
                                                ContextLoader contextLoader = new ContextLoader();
×
1728
                                                contextLoader.initWebApplicationContext(filterConfig.getServletContext());
×
1729
                                                
1730
                                                // output properties to the openmrs runtime properties file so that this wizard is not run again
UNCOV
1731
                                                FileOutputStream fos = null;
×
1732
                                                try {
UNCOV
1733
                                                        fos = new FileOutputStream(getRuntimePropertiesFile());
×
1734
                                                        OpenmrsUtil.storeProperties(runtimeProperties, fos,
×
1735
                                                                "Auto generated by OpenMRS initialization wizard");
1736
                                                        wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());
×
1737
                                                        
1738
                                                        /*
1739
                                                         * Fix file readability permissions:
1740
                                                         * first revoke read permission from everyone, then set read permissions for only the user
1741
                                                         * there is no function to set specific readability for only one user
1742
                                                         * and revoke everyone else's, therefore this is the only way to accomplish this.
1743
                                                         */
1744
                                                        wizardModel.workLog.add("Adjusting file posix properties to user readonly");
×
1745
                                                        if (getRuntimePropertiesFile().setReadable(false, false)
×
UNCOV
1746
                                                                && getRuntimePropertiesFile().setReadable(true)) {
×
1747
                                                                wizardModel.workLog
×
UNCOV
1748
                                                                        .add("Successfully adjusted RuntimePropertiesFile to disallow world to read it");
×
1749
                                                        } else {
1750
                                                                wizardModel.workLog
×
1751
                                                                        .add("Unable to adjust RuntimePropertiesFile to disallow world to read it");
×
1752
                                                        }
1753
                                                        // don't need to catch errors here because we tested it at the beginning of the wizard
1754
                                                }
1755
                                                finally {
UNCOV
1756
                                                        if (fos != null) {
×
UNCOV
1757
                                                                fos.close();
×
1758
                                                        }
1759
                                                }
1760
                                                
1761
                                                Context.openSession();
×
1762
                                                
UNCOV
1763
                                                if (!"".equals(wizardModel.implementationId)) {
×
1764
                                                        try {
UNCOV
1765
                                                                Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
UNCOV
1766
                                                                Context.addProxyPrivilege(PrivilegeConstants.MANAGE_CONCEPT_SOURCES);
×
1767
                                                                Context.addProxyPrivilege(PrivilegeConstants.GET_CONCEPT_SOURCES);
×
UNCOV
1768
                                                                Context.addProxyPrivilege(PrivilegeConstants.MANAGE_IMPLEMENTATION_ID);
×
1769
                                                                
UNCOV
1770
                                                                ImplementationId implId = new ImplementationId();
×
1771
                                                                implId.setName(wizardModel.implementationIdName);
×
1772
                                                                implId.setImplementationId(wizardModel.implementationId);
×
1773
                                                                implId.setPassphrase(wizardModel.implementationIdPassPhrase);
×
1774
                                                                implId.setDescription(wizardModel.implementationIdDescription);
×
1775
                                                                
1776
                                                                Context.getAdministrationService().setImplementationId(implId);
×
1777
                                                        }
1778
                                                        catch (Exception e) {
×
1779
                                                                reportError(ErrorMessageConstants.ERROR_SET_INPL_ID, DEFAULT_PAGE, e.getMessage());
×
UNCOV
1780
                                                                log.warn("Implementation ID could not be set.", e);
×
1781
                                                                Context.shutdown();
×
UNCOV
1782
                                                                WebModuleUtil.shutdownModules(filterConfig.getServletContext());
×
1783
                                                                contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
×
1784
                                                                return;
×
1785
                                                        }
1786
                                                        finally {
1787
                                                                Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
1788
                                                                Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_CONCEPT_SOURCES);
×
UNCOV
1789
                                                                Context.removeProxyPrivilege(PrivilegeConstants.GET_CONCEPT_SOURCES);
×
UNCOV
1790
                                                                Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_IMPLEMENTATION_ID);
×
1791
                                                        }
1792
                                                }
1793
                                                
1794
                                                try {
1795
                                                        // change the admin user password from "test" to what they input above
1796
                                                        if (wizardModel.createTables) {
×
1797
                                                                try {
1798
                                                                        Context.authenticate(new UsernamePasswordCredentials("admin", "test"));
×
1799
                                                                        
UNCOV
1800
                                                                        Properties props = Context.getRuntimeProperties();
×
UNCOV
1801
                                                                        String initValue = props.getProperty(UserService.ADMIN_PASSWORD_LOCKED_PROPERTY);
×
1802
                                                                        props.setProperty(UserService.ADMIN_PASSWORD_LOCKED_PROPERTY, "false");
×
UNCOV
1803
                                                                        Context.setRuntimeProperties(props);
×
1804
                                                                        
1805
                                                                        Context.getUserService().changePassword("test", wizardModel.adminUserPassword);
×
1806
                                                                        
1807
                                                                        if (initValue == null) {
×
1808
                                                                                props.remove(UserService.ADMIN_PASSWORD_LOCKED_PROPERTY);
×
1809
                                                                        } else {
UNCOV
1810
                                                                                props.setProperty(UserService.ADMIN_PASSWORD_LOCKED_PROPERTY, initValue);
×
1811
                                                                        }
UNCOV
1812
                                                                        Context.setRuntimeProperties(props);
×
UNCOV
1813
                                                                        Context.logout();
×
1814
                                                                }
1815
                                                                catch (ContextAuthenticationException ex) {
×
1816
                                                                        log.info("No need to change admin password.", ex);
×
UNCOV
1817
                                                                }
×
1818
                                                        }
1819
                                                }
1820
                                                catch (Exception e) {
×
1821
                                                        Context.shutdown();
×
UNCOV
1822
                                                        WebModuleUtil.shutdownModules(filterConfig.getServletContext());
×
1823
                                                        contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
×
UNCOV
1824
                                                        reportError(ErrorMessageConstants.ERROR_COMPLETE_STARTUP, DEFAULT_PAGE, e.getMessage());
×
UNCOV
1825
                                                        log.warn("Unable to complete the startup.", e);
×
UNCOV
1826
                                                        return;
×
1827
                                                }
×
1828
                                                
1829
                                                try {
1830
                                                        // Update PostgreSQL Sequences after insertion of core data
UNCOV
1831
                                                        Context.getAdministrationService().updatePostgresSequence();
×
1832
                                                }
1833
                                                catch (Exception e) {
×
UNCOV
1834
                                                        log.warn("Not able to update PostgreSQL sequence. Startup failed for PostgreSQL", e);
×
UNCOV
1835
                                                        reportError(ErrorMessageConstants.ERROR_COMPLETE_STARTUP, DEFAULT_PAGE, e.getMessage());
×
1836
                                                        return;
×
1837
                                                }
×
1838
                                                
1839
                                                // set this so that the wizard isn't run again on next page load
1840
                                                Context.closeSession();
×
1841
                                                
1842
                                                // start openmrs
1843
                                                try {
1844
                                                        UpdateFilter.setUpdatesRequired(false);
×
1845
                                                        WebDaemon.startOpenmrs(filterConfig.getServletContext());
×
1846
                                                }
UNCOV
1847
                                                catch (DatabaseUpdateException updateEx) {
×
UNCOV
1848
                                                        log.warn("Error while running the database update file", updateEx);
×
UNCOV
1849
                                                        reportError(ErrorMessageConstants.ERROR_DB_UPDATE, DEFAULT_PAGE, updateEx.getMessage());
×
UNCOV
1850
                                                        return;
×
1851
                                                }
1852
                                                catch (InputRequiredException inputRequiredEx) {
×
1853
                                                        // TODO display a page looping over the required input and ask the user for each.
1854
                                                        //                 When done and the user and put in their say, call DatabaseUpdater.update(Map);
1855
                                                        //                with the user's question/answer pairs
UNCOV
1856
                                                        log.warn(
×
1857
                                                                "Unable to continue because user input is required for the db updates and we cannot do anything about that right now");
UNCOV
1858
                                                        reportError(ErrorMessageConstants.ERROR_INPUT_REQ, DEFAULT_PAGE);
×
UNCOV
1859
                                                        return;
×
1860
                                                }
UNCOV
1861
                                                catch (MandatoryModuleException mandatoryModEx) {
×
1862
                                                        log.warn(
×
1863
                                                                "A mandatory module failed to start. Fix the error or unmark it as mandatory to continue.",
1864
                                                                mandatoryModEx);
UNCOV
1865
                                                        reportError(ErrorMessageConstants.ERROR_MANDATORY_MOD_REQ, DEFAULT_PAGE,
×
UNCOV
1866
                                                                mandatoryModEx.getMessage());
×
1867
                                                        return;
×
1868
                                                }
UNCOV
1869
                                                catch (OpenmrsCoreModuleException coreModEx) {
×
UNCOV
1870
                                                        log.warn(
×
1871
                                                                "A core module failed to start. Make sure that all core modules (with the required minimum versions) are installed and starting properly.",
1872
                                                                coreModEx);
UNCOV
1873
                                                        reportError(ErrorMessageConstants.ERROR_CORE_MOD_REQ, DEFAULT_PAGE, coreModEx.getMessage());
×
UNCOV
1874
                                                        return;
×
UNCOV
1875
                                                }
×
1876
                                                
1877
                                                // TODO catch openmrs errors here and drop the user back out to the setup screen
1878
                                                
1879
                                        }
UNCOV
1880
                                        catch (IOException e) {
×
1881
                                                reportError(ErrorMessageConstants.ERROR_COMPLETE_STARTUP, DEFAULT_PAGE, e.getMessage());
×
1882
                                        }
1883
                                        finally {
1884
                                                if (!hasErrors()) {
×
1885
                                                        // set this so that the wizard isn't run again on next page load
UNCOV
1886
                                                        setInitializationComplete(true);
×
1887
                                                        // we should also try to store selected by user language
1888
                                                        // if user wants to system will do it for him 
UNCOV
1889
                                                        FilterUtil.storeLocale(wizardModel.localeToSave);
×
1890
                                                }
UNCOV
1891
                                                setInstallationStarted(false);
×
1892
                                        }
UNCOV
1893
                                }
×
1894
                        };
1895
                        
UNCOV
1896
                        thread = new Thread(r);
×
UNCOV
1897
                }
×
1898
        }
1899
        
1900
        /**
1901
         * Convenience method that loads the database driver
1902
         *
1903
         * @param connection the database connection string
1904
         * @param databaseDriver the database driver class name to load
1905
         * @return the loaded driver string
1906
         */
1907
        public static String loadDriver(String connection, String databaseDriver) {
UNCOV
1908
                String loadedDriverString = null;
×
1909
                try {
UNCOV
1910
                        loadedDriverString = DatabaseUtil.loadDatabaseDriver(connection, databaseDriver);
×
UNCOV
1911
                        log.info("using database driver :" + loadedDriverString);
×
1912
                }
1913
                catch (ClassNotFoundException e) {
×
1914
                        log.error("The given database driver class was not found. "
×
1915
                                + "Please ensure that the database driver jar file is on the class path "
1916
                                + "(like in the webapp's lib folder)");
UNCOV
1917
                }
×
1918
                
UNCOV
1919
                return loadedDriverString;
×
1920
        }
1921
        
1922
        /**
1923
         * Utility method that checks if there is a runtime properties file containing database connection
1924
         * credentials
1925
         *
1926
         * @return
1927
         */
1928
        private static boolean skipDatabaseSetupPage() {
UNCOV
1929
                Properties props = OpenmrsUtil.getRuntimeProperties(WebConstants.WEBAPP_NAME);
×
1930
                return (props != null && StringUtils.hasText(props.getProperty("connection.url"))
×
1931
                        && StringUtils.hasText(props.getProperty("connection.username"))
×
1932
                        && StringUtils.hasText(props.getProperty("connection.password")));
×
1933
        }
1934
        
1935
        /**
1936
         * Utility methods that checks if the user clicked the back image
1937
         *
1938
         * @param httpRequest
1939
         * @return
1940
         */
1941
        private static boolean goBack(HttpServletRequest httpRequest) {
1942
                return "Back".equals(httpRequest.getParameter("back"))
×
UNCOV
1943
                        || (httpRequest.getParameter("back.x") != null && httpRequest.getParameter("back.y") != null);
×
1944
        }
1945
        
1946
        /**
1947
         * Convenience method to get custom installation script
1948
         *
1949
         * @return Properties from custom installation script or empty if none specified
1950
         * @throws RuntimeException if path to installation script is invalid
1951
         */
1952
        private Properties getInstallationScript() {
1953
                Properties prop = new Properties();
×
1954
                
UNCOV
1955
                String fileName = System.getProperty("OPENMRS_INSTALLATION_SCRIPT");
×
1956
                if (fileName == null) {
×
UNCOV
1957
                        return prop;
×
1958
                }
1959
                if (fileName.startsWith("classpath:")) {
×
1960
                        fileName = fileName.substring(10);
×
UNCOV
1961
                        InputStream input = null;
×
1962
                        try {
1963
                                input = getClass().getClassLoader().getResourceAsStream(fileName);
×
UNCOV
1964
                                prop.load(input);
×
UNCOV
1965
                                log.info("Using installation script from classpath: " + fileName);
×
1966
                                
1967
                                input.close();
×
1968
                        }
UNCOV
1969
                        catch (IOException ex) {
×
UNCOV
1970
                                log.error("Failed to load installation script from classpath: " + fileName, ex);
×
UNCOV
1971
                                throw new RuntimeException(ex);
×
1972
                        }
1973
                        finally {
UNCOV
1974
                                IOUtils.closeQuietly(input);
×
1975
                        }
UNCOV
1976
                } else {
×
UNCOV
1977
                        File file = new File(fileName);
×
UNCOV
1978
                        if (file.exists()) {
×
UNCOV
1979
                                InputStream input = null;
×
1980
                                try {
UNCOV
1981
                                        input = new FileInputStream(fileName);
×
UNCOV
1982
                                        prop.load(input);
×
UNCOV
1983
                                        log.info("Using installation script from absolute path: " + file.getAbsolutePath());
×
1984
                                        
UNCOV
1985
                                        input.close();
×
1986
                                }
UNCOV
1987
                                catch (IOException ex) {
×
UNCOV
1988
                                        log.error("Failed to load installation script from absolute path: " + file.getAbsolutePath(), ex);
×
UNCOV
1989
                                        throw new RuntimeException(ex);
×
1990
                                }
1991
                                finally {
UNCOV
1992
                                        IOUtils.closeQuietly(input);
×
1993
                                }
1994
                        }
1995
                }
UNCOV
1996
                return prop;
×
1997
        }
1998
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc