Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DST-15503 Fixes for user export changes #819

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build

on:
workflow_dispatch:
release:
types: [published, edited]
push:
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/uk/co/bconline/ndelius/model/ExportResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public final class ExportResult {
private String teams;
@CsvBindByName(column = "LAU")
@CsvBindByPosition(position = 14)
private String lau;
private String localAdminUnit;
@CsvBindByName(column = "PDU")
@CsvBindByPosition(position = 15)
private String pdu;
private String probationDeliveryUnit;
@CsvBindByPosition(position = 16)
private String provider;
@CsvBindByPosition(position = 17)
private String roleDescriptions;
private String roleNames;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import uk.co.bconline.ndelius.model.entity.converter.YNConverter;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;

@Getter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "BOROUGH")
public class BoroughExportEntity implements Serializable
{
public class BoroughExportEntity implements Serializable {
@Id
@Column(name = "BOROUGH_ID")
@GeneratedValue(generator = "BOROUGH_ID_SEQ")
Expand All @@ -35,11 +26,15 @@ public class BoroughExportEntity implements Serializable
@Column(name = "DESCRIPTION")
private String description;

@Column(name = "SELECTABLE")
@Convert(converter = YNConverter.class)
private boolean selectable;

@ManyToOne
@JoinColumn(name = "PROBATION_AREA_ID")
private ProbationAreaExportEntity probationArea;

@Column(name = "END_DATE")
@Type(type = "java.time.LocalDate")
private LocalDate endDate;
public String getExportDescription() {
return description + " (" + code + ") " + (isSelectable() ? " [Active]" : " [Inactive]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.co.bconline.ndelius.model.entity.export;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import uk.co.bconline.ndelius.model.entity.converter.YNConverter;

import javax.persistence.*;
import java.io.Serializable;

@Getter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "DISTRICT")
public class DistrictExportEntity implements Serializable
{
@Id
@Column(name = "DISTRICT_ID")
@GeneratedValue(generator = "DISTRICT_ID_SEQ")
@SequenceGenerator(name = "DISTRICT_ID_SEQ", sequenceName = "DISTRICT_ID_SEQ", allocationSize = 1)
private Long id;

@Column(name = "CODE")
private String code;

@Column(name = "DESCRIPTION")
private String description;

@Column(name = "SELECTABLE")
@Convert(converter = YNConverter.class)
private boolean selectable;

@ManyToOne
@JoinColumn(name = "BOROUGH_ID")
private BoroughExportEntity borough;

public String getExportDescription() {
return description + " (" + code + ") " + (isSelectable() ? " [Active]" : " [Inactive]");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ public class TeamExportEntity implements Serializable {
@Column(name = "DESCRIPTION")
private String description;

@ManyToOne
@JoinColumn(name = "LOCAL_DELIVERY_UNIT_ID")
private LDUExportEntity localDeliveryUnit;

@ManyToOne
@JoinColumn(name = "DISTRICT_ID")
private BoroughExportEntity borough;
private DistrictExportEntity district;

@Column(name = "END_DATE")
@Type(type = "java.time.LocalDate")
Expand All @@ -45,24 +41,4 @@ public String getExportDescription()
{
return description + " (" + code + ") " + ((getEndDate() != null && getEndDate().isBefore(now())) ? " [Inactive]" : " [Active]");
}

public String getLDUDescription()
{
if (localDeliveryUnit != null)
{
return localDeliveryUnit.getDescription() + " (" + localDeliveryUnit.getCode() + ") " + ((localDeliveryUnit.getEndDate() != null && localDeliveryUnit.getEndDate().isBefore(now())) ? " [Inactive]" : " [Active]");
} else {
return null;
}
}

public String getBoroughDescription()
{
if (borough != null)
{
return borough.getDescription() + " (" + borough.getCode() + ") " + ((borough.getEndDate() != null && borough.getEndDate().isBefore(now())) ? " [Inactive]" : " [Active]");
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.co.bconline.ndelius.service;

import uk.co.bconline.ndelius.model.entry.RoleAssociationEntry;
import uk.co.bconline.ndelius.model.entry.RoleEntry;

import java.util.List;
Expand All @@ -10,6 +9,8 @@ public interface UserRoleService
{
Set<RoleEntry> getRolesICanAssign();
Set<RoleEntry> getUserRoles(String username);

Set<String> getUserRoleNames(String username);
List<String> getAllUsersWithRole(String role);
Set<RoleEntry> getClientRoles(String clientId);
Set<String> getUserInteractions(String username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public Set<RoleEntry> getUserRoles(String username) {
return getAssignedRoles(username, usersBase);
}

@Override
public Set<String> getUserRoleNames(String username) {
return stream(getAssignedRoleAssociations(username, usersBase).spliterator(), false)
.map(RoleAssociationEntry::getName)
.collect(toSet());
}

@Override
public Set<RoleEntry> getClientRoles(String clientId) {
return getAssignedRoles(clientId, clientsBase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,31 @@
import uk.co.bconline.ndelius.model.ExportResult;
import uk.co.bconline.ndelius.model.ReferenceData;
import uk.co.bconline.ndelius.model.User;
import uk.co.bconline.ndelius.model.entity.ChangeNoteEntity;
import uk.co.bconline.ndelius.model.entity.OrganisationEntity;
import uk.co.bconline.ndelius.model.entity.ProbationAreaEntity;
import uk.co.bconline.ndelius.model.entity.ProbationAreaUserEntity;
import uk.co.bconline.ndelius.model.entity.ProbationAreaUserId;
import uk.co.bconline.ndelius.model.entity.ReferenceDataEntity;
import uk.co.bconline.ndelius.model.entity.StaffEntity;
import uk.co.bconline.ndelius.model.entity.StaffTeamEntity;
import uk.co.bconline.ndelius.model.entity.StaffTeamId;
import uk.co.bconline.ndelius.model.entity.SubContractedProviderEntity;
import uk.co.bconline.ndelius.model.entity.TeamEntity;
import uk.co.bconline.ndelius.model.entity.UserEntity;
import uk.co.bconline.ndelius.model.entity.export.BoroughExportEntity;
import uk.co.bconline.ndelius.model.entity.export.ProbationAreaExportEntity;
import uk.co.bconline.ndelius.model.entity.export.ReferenceDataExportEntity;
import uk.co.bconline.ndelius.model.entity.export.StaffExportEntity;
import uk.co.bconline.ndelius.model.entity.export.TeamExportEntity;
import uk.co.bconline.ndelius.model.entity.export.UserExportEntity;
import uk.co.bconline.ndelius.model.entity.*;
import uk.co.bconline.ndelius.model.entity.export.*;
import uk.co.bconline.ndelius.model.entry.ClientEntry;
import uk.co.bconline.ndelius.model.entry.RoleEntry;
import uk.co.bconline.ndelius.model.entry.UserEntry;
import uk.co.bconline.ndelius.service.DatasetService;
import uk.co.bconline.ndelius.service.ReferenceDataService;
import uk.co.bconline.ndelius.service.RoleService;
import uk.co.bconline.ndelius.service.TeamService;
import uk.co.bconline.ndelius.service.UserEntityService;
import uk.co.bconline.ndelius.service.UserHistoryService;
import uk.co.bconline.ndelius.service.UserRoleService;
import uk.co.bconline.ndelius.service.*;
import uk.co.bconline.ndelius.util.LdapUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.time.LocalDateTime.now;
import static java.time.temporal.ChronoUnit.MILLIS;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.*;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.*;
import static org.springframework.util.StringUtils.isEmpty;
import static uk.co.bconline.ndelius.util.LdapUtils.mapLdapStringToDate;
import static uk.co.bconline.ndelius.util.LdapUtils.mapToLdapString;
import static uk.co.bconline.ndelius.util.NameUtils.combineNames;
import static uk.co.bconline.ndelius.util.NameUtils.firstForename;
import static uk.co.bconline.ndelius.util.NameUtils.subsequentForenames;
import static uk.co.bconline.ndelius.util.NameUtils.*;

@Slf4j
@Component
Expand Down Expand Up @@ -182,7 +151,7 @@ public Optional<User> map(UserEntry user)
public ExportResult combine(UserExportEntity db, UserEntry ldap) {
if (ldap == null) return null;
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
val userRoles = ofNullable(ldap.getUsername()).map(userRoleService::getUserRoles).orElse(emptySet());
val userRoles = ofNullable(ldap.getUsername()).map(userRoleService::getUserRoleNames).orElse(emptySet());
return ExportResult.builder()
.username(ofNullable(ldap.getUsername()).orElseGet(db::getUsername))
.forenames(ofNullable(ldap.getForenames()).orElseGet(() -> combineNames(db.getForename(), db.getForename2())))
Expand All @@ -200,14 +169,23 @@ public ExportResult combine(UserExportEntity db, UserEntry ldap) {
.datasets(ofNullable(db.getDatasets()).map(l -> l.stream()
.map(ProbationAreaExportEntity::getCode).distinct().sorted().collect(joining(","))).orElse(null))
.lastAccessedDate(db.getLastAccessedDate() != null ? db.getLastAccessedDate().format(format) : null)
.lau(ofNullable(db.getStaff()).map(StaffExportEntity::getTeams).map(l -> l.stream()
.map(TeamExportEntity::getLDUDescription).filter(Objects::nonNull).distinct().sorted().collect(joining(","))).orElse(null))
.pdu(ofNullable(db.getStaff()).map(StaffExportEntity::getTeams).map(l -> l.stream()
.map(TeamExportEntity::getBoroughDescription).filter(Objects::nonNull).distinct().sorted().collect(joining(","))).orElse(null))
.localAdminUnit(ofNullable(db.getStaff()).map(StaffExportEntity::getTeams).map(l -> l.stream()
.map(TeamExportEntity::getDistrict)
.map(DistrictExportEntity::getExportDescription)
.filter(Objects::nonNull)
.distinct().sorted().collect(joining(","))).orElse(null))
.probationDeliveryUnit(ofNullable(db.getStaff()).map(StaffExportEntity::getTeams).map(l -> l.stream()
.map(TeamExportEntity::getDistrict)
.map(DistrictExportEntity::getBorough)
.map(BoroughExportEntity::getExportDescription)
.filter(Objects::nonNull).distinct().sorted().collect(joining(","))).orElse(null))
.provider(ofNullable(db.getStaff()).map(StaffExportEntity::getTeams).map(l -> l.stream()
.map(TeamExportEntity::getBorough).filter(Objects::nonNull).map(BoroughExportEntity::getProbationArea).map(ProbationAreaExportEntity::getExportDescription).distinct().sorted().collect(joining(","))).orElse(null))
.roleDescriptions(ofNullable(userRoles).map(l -> l.stream()
.map(RoleEntry::getName).distinct().sorted().collect(joining(","))).orElse(null))
.map(TeamExportEntity::getDistrict)
.map(DistrictExportEntity::getBorough)
.map(BoroughExportEntity::getProbationArea)
.map(ProbationAreaExportEntity::getExportDescription)
.filter(Objects::nonNull).distinct().sorted().collect(joining(","))).orElse(null))
.roleNames(userRoles.stream().sorted().collect(joining(",")))
.build();
}

Expand Down
Loading