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