Commit eb63f330 authored by Matija Obreza's avatar Matija Obreza
Browse files

Load country and WIEWS data if none are found at startup, create default

admin account when no users exist in the system
parent 56f2b4da
...@@ -19,6 +19,7 @@ package org.genesys2.server.listener.sample; ...@@ -19,6 +19,7 @@ package org.genesys2.server.listener.sample;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.listener.RunAsAdminListener; import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.UserRole; import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User; import org.genesys2.server.model.impl.User;
...@@ -34,8 +35,13 @@ public class CreateAdminListener extends RunAsAdminListener { ...@@ -34,8 +35,13 @@ public class CreateAdminListener extends RunAsAdminListener {
@Override @Override
public void init() throws Exception { public void init() throws Exception {
_logger.info("Checking for admin account"); createDefaultAdminUser();
if (!userService.exists("admin@example.com")) { // createSampleUserAccount();
}
private void createDefaultAdminUser() throws UserException {
_logger.info("Checking for at least one account");
if (userService.getCurrentPage(0, 1).getTotalElements() == 0) {
User user = new User(); User user = new User();
user.setEmail("admin@example.com"); user.setEmail("admin@example.com");
user.setPassword("admin"); user.setPassword("admin");
...@@ -46,7 +52,24 @@ public class CreateAdminListener extends RunAsAdminListener { ...@@ -46,7 +52,24 @@ public class CreateAdminListener extends RunAsAdminListener {
// user.setUserGroups(userGroupService.getUserGroupList()); // user.setUserGroups(userGroupService.getUserGroupList());
userService.addUser(user); userService.addUser(user);
_logger.info("Sample admin has been added successfully"); _logger.info("Sample admin 'admin@example.com' has been added successfully");
} }
} }
// private void createSampleUserAccount() throws UserException {
// _logger.info("Checking for user account");
// if (!userService.exists("user@example.com")) {
// User user = new User();
// user.setEmail("user@example.com");
// user.setPassword("user");
// user.setName("Sample User");
// Set<UserRole> userRoles = new HashSet<UserRole>();
// userRoles.add(UserRole.USER);
// user.setRoles(userRoles);
// // user.setUserGroups(userGroupService.getUserGroupList());
//
// userService.addUser(user);
// _logger.info("Sample user has been added successfully");
// }
// }
} }
...@@ -16,37 +16,43 @@ ...@@ -16,37 +16,43 @@
package org.genesys2.server.listener.sample; package org.genesys2.server.listener.sample;
import java.util.HashSet;
import java.util.Set;
import org.genesys2.server.listener.RunAsAdminListener; import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.UserRole; import org.genesys2.server.service.GeoService;
import org.genesys2.server.model.impl.User; import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.UserService; import org.genesys2.server.service.impl.InstituteUpdater;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/**
* Import country data and WIEWS institutes if none exist at startup
*
* @author mobreza
*/
@Component @Component
public class CreateUserListener extends RunAsAdminListener { public class FirstRunListener extends RunAsAdminListener {
@Autowired @Autowired
private UserService userService; GeoService geoService;
@Autowired
InstituteService instituteService;
@Autowired
InstituteUpdater instituteUpdater;
@Override @Override
public void init() throws Exception { protected void init() throws Exception {
_logger.info("Checking for user account");
if (!userService.exists("user@example.com")) { if (geoService.listAll().size() == 0) {
User user = new User(); _logger.info("No country data found. Loading");
user.setEmail("user@example.com"); geoService.updateCountryData();
user.setPassword("user"); }
user.setName("Sample User");
Set<UserRole> userRoles = new HashSet<UserRole>(); if (instituteService.list(new PageRequest(0, 1)).getTotalElements() == 0) {
userRoles.add(UserRole.USER); _logger.info("No WIEWS institutes found. Loading.");
user.setRoles(userRoles); instituteUpdater.updateFaoInstitutes();
// user.setUserGroups(userGroupService.getUserGroupList());
userService.addUser(user);
_logger.info("Sample user has been added successfully");
} }
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment