1 package de.dlr.shepard.neo4Core.services; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import de.dlr.shepard.neo4Core.dao.PermissionsDAO; 7 import de.dlr.shepard.neo4Core.dao.UserDAO; 8 import de.dlr.shepard.neo4Core.dao.UserGroupDAO; 9 import de.dlr.shepard.neo4Core.entities.Permissions; 10 import de.dlr.shepard.neo4Core.entities.User; 11 import de.dlr.shepard.neo4Core.entities.UserGroup; 12 import de.dlr.shepard.neo4Core.io.UserGroupIO; 13 import de.dlr.shepard.util.DateHelper; 14 import de.dlr.shepard.util.PermissionType; 15 import de.dlr.shepard.util.QueryParamHelper; 16 17 public class UserGroupService { 18 19 private UserGroupDAO userGroupDAO = new UserGroupDAO(); 20 private UserDAO userDAO = new UserDAO(); 21 private PermissionsDAO permissionsDAO = new PermissionsDAO(); 22 private DateHelper dateHelper = new DateHelper(); 23 24 public List<UserGroup> getAllUserGroups(QueryParamHelper params, String username) { 25 return userGroupDAO.findAllUserGroups(params, username); 26 } 27 28 public UserGroup getUserGroup(Long userGroupId) { 29 return userGroupDAO.find(userGroupId); 30 } 31 32 public UserGroup createUserGroup(UserGroupIO userGroup, String username) { 33 var user = userDAO.find(username); 34 var toCreate = new UserGroup(); 35 toCreate.setName(userGroup.getName()); 36 toCreate.setCreatedBy(user); 37 toCreate.setCreatedAt(dateHelper.getDate()); 38 toCreate.setUsers(fetchUsers(userGroup.getUsernames())); 39 var created = userGroupDAO.createOrUpdate(toCreate); 40 permissionsDAO.createOrUpdate(new Permissions(created, user, PermissionType.Private)); 41 return created; 42 } 43 44 public UserGroup updateUserGroup(Long id, UserGroupIO userGroup, String username) { 45 var user = userDAO.find(username); 46 var old = userGroupDAO.find(id); 47 old.setUpdatedBy(user); 48 old.setUpdatedAt(dateHelper.getDate()); 49 old.setName(userGroup.getName()); 50 old.setUsers(fetchUsers(userGroup.getUsernames())); 51 var updated = userGroupDAO.createOrUpdate(old); 52 return updated; 53 } 54 55 public boolean deleteUserGroup(Long id) { 56 var old = userGroupDAO.find(id); 57 if (old == null) 58 return false; 59 60 var permissions = permissionsDAO.findByEntity(id); 61 var permissionsResult = permissions == null || permissionsDAO.delete(permissions.getId()); 62 if (!permissionsResult) 63 return false; 64 65 return userGroupDAO.delete(id); 66 } 67 68 private ArrayList<User> fetchUsers(String[] usernames) { 69 var result = new ArrayList<User>(usernames.length); 70 for (var username : usernames) { 71 if (username == null) { 72 continue; 73 } 74 var user = userDAO.find(username); 75 if (user != null) { 76 result.add(user); 77 } 78 } 79 return result; 80 } 81 82 }