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;
import java.util.HashSet;
import java.util.Set;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
......@@ -34,8 +35,13 @@ public class CreateAdminListener extends RunAsAdminListener {
@Override
public void init() throws Exception {
_logger.info("Checking for admin account");
if (!userService.exists("admin@example.com")) {
createDefaultAdminUser();
// 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.setEmail("admin@example.com");
user.setPassword("admin");
......@@ -46,7 +52,24 @@ public class CreateAdminListener extends RunAsAdminListener {
// user.setUserGroups(userGroupService.getUserGroupList());
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 @@
package org.genesys2.server.listener.sample;
import java.util.HashSet;
import java.util.Set;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.UserService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.impl.InstituteUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
/**
* Import country data and WIEWS institutes if none exist at startup
*
* @author mobreza
*/
@Component
public class CreateUserListener extends RunAsAdminListener {
public class FirstRunListener extends RunAsAdminListener {
@Autowired
private UserService userService;
GeoService geoService;
@Autowired
InstituteService instituteService;
@Autowired
InstituteUpdater instituteUpdater;
@Override
public void init() throws Exception {
_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");
protected void init() throws Exception {
if (geoService.listAll().size() == 0) {
_logger.info("No country data found. Loading");
geoService.updateCountryData();
}
if (instituteService.list(new PageRequest(0, 1)).getTotalElements() == 0) {
_logger.info("No WIEWS institutes found. Loading.");
instituteUpdater.updateFaoInstitutes();
}
}
}
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