Skip to content

Commit

Permalink
Merge pull request #26 from maeisabelle/Quick-Start-UIChanges
Browse files Browse the repository at this point in the history
Quick Start UI and configuration changes
  • Loading branch information
paxtonhare committed Feb 7, 2016
2 parents 20764c3 + 61c2c54 commit 38237a8
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 58 deletions.
11 changes: 11 additions & 0 deletions quick-start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ Quick Start is a single-page web application that is deployed in an embedded Tom
- run ```gradle build```
- run ```java -jar build/libs/quick-start-0.1.0.war```


## To pass properties to the app, either do the following (in order of priority):
- Add environment.properties in the quick-start directory with the following properties:
mlRestPort=<host>
mlHost=<port>
mlUsername=<username>
mlPassword=<password>
OR
- Add the command line properties --mlHost=<host> --mlRestPort=<port> --mlUsername=<username> --mlPassword=<password>


## Build and deploy the web app via Eclipse
- Select Application.java then Run as Java Application

Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,162 @@
package com.marklogic.hub.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/***
*
* @author mturiana
* This class is used to get the value of the keys in application.properties
* @author mturiana This class is used to get the value of the keys in
* application.properties
*/
@Component
public class EnvironmentConfiguration {

private static final Logger LOGGER = LoggerFactory
.getLogger(EnvironmentConfiguration.class);

private static final String PROPERTIES_FILENAME = "environment.properties";

@Autowired
private Environment environment;

private Properties properties = new Properties();

public String getServerPort() {
return this.environment.getProperty("server.port");
}



public String getMLHost() {
String value = this.properties.getProperty("mlHost");
if(value == null) {
value = this.environment.getProperty("mlHost.default");
if (value != null) {
return value;
}
return value;
value = this.environment.getProperty("mlHost");
if (value != null) {
this.properties.setProperty("mlHost", value);
return value;
}
return this.environment.getProperty("mlHost.default");
}

public String getMLUsername() {
String value = this.properties.getProperty("mlUsername");
if(value == null) {
value = this.environment.getProperty("mlUsername.default");
if (value != null) {
return value;
}
return value;
value = this.environment.getProperty("mlUsername");
if (value != null) {
this.properties.setProperty("mlUsername", value);
return value;
}
return this.environment.getProperty("mlUsername.default");
}

public String getMLPassword() {
String value = this.properties.getProperty("mlPassword");
if(value == null) {
value = this.environment.getProperty("mlPassword.default");
if (value != null) {
return value;
}
value = this.environment.getProperty("mlPassword");
if (value != null) {
this.properties.setProperty("mlPassword", value);
return value;
}
return value;
return this.environment.getProperty("mlPassword.default");
}

public String getMLRestPort() {
String value = this.properties.getProperty("mlRestPort");
if(value == null) {
value = this.environment.getProperty("mlRestPort.default");
if (value != null) {
return value;
}
return value;
value = this.environment.getProperty("mlRestPort");
if (value != null) {
this.properties.setProperty("mlRestPort", value);
return value;
}
return this.environment.getProperty("mlRestPort.default");
}

public String getMLAuth() {
String value = this.properties.getProperty("mlAuth");
if(value == null) {
value = this.environment.getProperty("mlAuth.default");
if (value != null) {
return value;
}
return value;
value = this.environment.getProperty("mlAuth");
if (value != null) {
this.properties.setProperty("mlAuth", value);
return value;
}
return this.environment.getProperty("mlAuth.default");
}

public void setMLHost(String mlHost) {
this.properties.setProperty("mlHost", mlHost);
}

public void setMLRestPort(String mlRestPort) {
this.properties.setProperty("mlRestPort", mlRestPort);
this.properties.setProperty("mlRestPort", mlRestPort);
}

public void setMLUsername(String mlUsername) {
this.properties.setProperty("mlUsername", mlUsername);
}

public void setMLPassword(String mlPassword) {
this.properties.setProperty("mlPassword", mlPassword);
}

public void loadConfigurationFromFile() {
InputStream is = null;
try {
File file = new File(PROPERTIES_FILENAME);
if(file.exists()) {
is = new FileInputStream( file );
properties.load( is );
}
} catch ( Exception e ) {
is = null;
}
}

public void saveConfigurationToFile() {
OutputStream out = null;
try {
out = new FileOutputStream(new File(PROPERTIES_FILENAME));
this.properties.store(out, null);
} catch (FileNotFoundException e) {
LOGGER.error("environment.properties is not found", e.getMessage());
} catch (IOException e) {
LOGGER.error("Error saving configuration.", e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.error("Error closing output stream.", e.getMessage());
}
}
}
}

public void removeSavedConfiguration() {
this.properties = new Properties();
File file = new File(PROPERTIES_FILENAME);
if(file.exists()) {
file.delete();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import com.marklogic.hub.web.form.BaseForm;

@Controller
public class BaseController {

Expand Down Expand Up @@ -42,4 +44,19 @@ public void displayErrors(Model model, List<String> errors) {
model.addAttribute("errors", errors);
model.addAttribute("hasErrors", true);
}

public void displayError(BaseForm form, String errorKey, Object[] messageParams, String defaultError) {
List<String> errors = new ArrayList<String>();
String error = defaultError;
if(errorKey != null) {
error = this.convertMessage(errorKey, messageParams, defaultError);
}
errors.add(error);
displayErrors(form, errors);
}

public void displayErrors(BaseForm form, List<String> errors) {
form.setErrors(errors);
form.setHasErrors(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import com.marklogic.hub.config.EnvironmentConfiguration;
import com.marklogic.hub.exception.DataHubException;
import com.marklogic.hub.service.DataHubService;
import com.marklogic.hub.web.controller.BaseController;
import com.marklogic.hub.web.form.LoginForm;

@RestController
@RequestMapping("/api/data-hub")
public class DataHubServerApiController {
public class DataHubServerApiController extends BaseController {
private static final Logger LOGGER = LoggerFactory.getLogger(DataHubServerApiController.class);

@Autowired
Expand All @@ -36,22 +37,41 @@ public LoginForm postLogin(@RequestBody LoginForm loginForm, BindingResult bindi

loginForm.setInstalled(dataHubService.isInstalled());
loginForm.setServerVersionAccepted(dataHubService.isServerAcceptable());
loginForm.setLoginAccepted(true);
loginForm.setHasErrors(false);
loginForm.setLoggedIn(true);

environmentConfiguration.saveConfigurationToFile();

session.setAttribute("loginForm", loginForm);
}
catch (DataHubException e) {
LOGGER.error("Login failed", e);

loginForm.setLoginAccepted(false);
loginForm.setLoggedIn(false);
displayError(loginForm, null, null, e.getMessage());
}

return loginForm;
}

@RequestMapping(value="login", method = RequestMethod.GET)
public LoginForm getLoginStatus(HttpSession session) {
return (LoginForm) session.getAttribute("loginForm");
LoginForm loginForm = (LoginForm) session.getAttribute("loginForm");
if(loginForm == null) {
loginForm = new LoginForm();
this.environmentConfiguration.loadConfigurationFromFile();
this.retrieveEnvironmentConfiguration(loginForm);
session.setAttribute("loginForm", loginForm);
}
return loginForm;
}

@RequestMapping(value="logout", method = RequestMethod.POST)
public LoginForm postLogout(HttpSession session) {
LoginForm loginForm = (LoginForm) session.getAttribute("loginForm");
loginForm.setLoggedIn(false);
this.environmentConfiguration.removeSavedConfiguration();
this.retrieveEnvironmentConfiguration(loginForm);
return loginForm;
}

@RequestMapping(value="install", method = RequestMethod.POST)
Expand All @@ -75,4 +95,11 @@ private void updateEnvironmentConfiguration(LoginForm loginForm) {
environmentConfiguration.setMLUsername(loginForm.getMlUsername());
environmentConfiguration.setMLPassword(loginForm.getMlPassword());
}

private void retrieveEnvironmentConfiguration(LoginForm loginForm) {
loginForm.setMlHost(environmentConfiguration.getMLHost());
loginForm.setMlRestPort(environmentConfiguration.getMLRestPort());
loginForm.setMlUsername(environmentConfiguration.getMLUsername());
loginForm.setMlPassword(environmentConfiguration.getMLPassword());
}
}
27 changes: 27 additions & 0 deletions quick-start/src/main/java/com/marklogic/hub/web/form/BaseForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.marklogic.hub.web.form;

import java.util.ArrayList;
import java.util.List;

public class BaseForm {

private boolean hasErrors;
private List<String> errors = new ArrayList<String>();

public boolean isHasErrors() {
return hasErrors;
}
public void setHasErrors(boolean hasErrors) {
this.hasErrors = hasErrors;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(List<String> errors) {
this.errors = errors;
}




}
27 changes: 16 additions & 11 deletions quick-start/src/main/java/com/marklogic/hub/web/form/LoginForm.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.marklogic.hub.web.form;

public class LoginForm {

public class LoginForm extends BaseForm {

private String mlHost;
private String mlRestPort;
private String mlUsername;
private String mlPassword;
private boolean loginAccepted;
private boolean serverVersionAccepted;
private boolean installed;

private boolean loggedIn;

public String getMlHost() {
return mlHost;
}
Expand Down Expand Up @@ -40,14 +42,6 @@ public String getMlPassword() {
public void setMlPassword(String mlPassword) {
this.mlPassword = mlPassword;
}

public boolean isLoginAccepted() {
return loginAccepted;
}

public void setLoginAccepted(boolean loginAccepted) {
this.loginAccepted = loginAccepted;
}

public boolean isServerVersionAccepted() {
return serverVersionAccepted;
Expand All @@ -64,4 +58,15 @@ public boolean isInstalled() {
public void setInstalled(boolean installed) {
this.installed = installed;
}

public boolean isLoggedIn() {
return loggedIn;
}

public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}



}
Loading

0 comments on commit 38237a8

Please sign in to comment.