Commit 6ec6bcc5 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Added GroupStatusManager

parent 9f5fae50
......@@ -41,6 +41,11 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......
package it.inaf.ia2.gms.manager;
import it.inaf.ia2.gms.exception.UnauthorizedException;
import it.inaf.ia2.gms.model.Permission;
import it.inaf.ia2.gms.model.RapUser;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.MembershipsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.persistence.model.MembershipEntity;
import it.inaf.ia2.gms.rap.RapClient;
import it.inaf.ia2.gms.service.GroupNameService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GroupStatusManager extends UserAwareComponent {
private static final Logger LOG = LoggerFactory.getLogger(GroupStatusManager.class);
@Autowired
private PermissionsManager permissionsManager;
@Autowired
private GroupsDAO groupsDAO;
@Autowired
private MembershipsDAO membershipsDAO;
@Autowired
private GroupNameService groupNameService;
@Autowired
private RapClient rapClient;
public List<Object[]> generateStatus(GroupEntity parentGroup) {
Permission groupPermission = permissionsManager.getCurrentUserPermission(parentGroup);
if (groupPermission != Permission.ADMIN) {
throw new UnauthorizedException("ADMIN permission is needed for performing this action");
}
List<GroupEntity> groups = groupsDAO.getAllChildren(parentGroup.getPath());
List<String> names = groupNameService.getGroupsNames(groups);
List<MembershipEntity> memberships = membershipsDAO.findByGroups(groups.stream()
.map(g -> g.getId()).collect(Collectors.toList()));
Map<String, List<String>> membersMap = new HashMap<>();
for (MembershipEntity membership : memberships) {
List<String> users = membersMap.get(membership.getGroupId());
if (users == null) {
users = new ArrayList<>();
membersMap.put(membership.getGroupId(), users);
}
users.add(membership.getUserId());
}
Map<String, String> usersMap = new HashMap<>();
for (RapUser user : rapClient.getUsers(memberships.stream()
.map(u -> u.getUserId()).collect(Collectors.toSet()))) {
usersMap.put(user.getId(), user.getPrimaryEmail());
}
List<Object[]> rows = new ArrayList<>();
for (int i = 0; i < groups.size(); i++) {
GroupEntity group = groups.get(i);
String groupName = names.get(i);
List<String> users = membersMap.get(group.getId());
if (users != null) {
for (String userId : users) {
String email = usersMap.get(userId);
if (email == null) {
LOG.warn("Unable to retrieve information about user " + userId);
continue;
}
Object[] row = new Object[]{groupName, email};
rows.add(row);
}
}
}
return rows;
}
}
......@@ -5,6 +5,7 @@ import it.inaf.ia2.gms.persistence.model.MembershipEntity;
import java.sql.PreparedStatement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.sql.DataSource;
......@@ -42,6 +43,30 @@ public class MembershipsDAO {
});
}
public List<MembershipEntity> findByGroups(List<String> groupIds) {
String sql = "SELECT user_id, group_id FROM gms_membership WHERE group_id IN ("
+ String.join(",", Collections.nCopies(groupIds.size(), "?")) + ")";
return jdbcTemplate.query(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
int i = 0;
for (String groupId : groupIds) {
ps.setString(++i, groupId);
}
return ps;
}, resultSet -> {
List<MembershipEntity> members = new ArrayList<>();
while (resultSet.next()) {
MembershipEntity membership = new MembershipEntity();
membership.setGroupId(resultSet.getString("group_id"));
membership.setUserId(resultSet.getString("user_id"));
members.add(membership);
}
return members;
});
}
public List<GroupEntity> getUserMemberships(String userId) {
return getUserMemberships(userId, null);
}
......
Supports Markdown
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