1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.remote.service;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.jboss.logging.Logger;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.util.UserHelper;
|
19
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
20
|
import eu.etaxonomy.cdm.model.name.Registration;
|
21
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
22
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
23
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
24
|
|
25
|
/**
|
26
|
* Provides filter functions for entities ({@link TaxonName}, {@link TypeDesignationBase})
|
27
|
* which can be subject to a {@link Registration}.
|
28
|
* Entities which are associated with a <code>Registration</code> must not be published as long
|
29
|
* as the registration status is not {@link RegistrationStatus#PUBLISHED PUBLISHED} and and the
|
30
|
* publication not released.
|
31
|
*
|
32
|
* @author a.kohlbecker
|
33
|
* @since Nov 17, 2020
|
34
|
*/
|
35
|
@Component
|
36
|
public class RegistrableEntityFilter {
|
37
|
|
38
|
private static Logger logger = Logger.getLogger(RegistrableEntityFilter.class);
|
39
|
|
40
|
protected UserHelper userHelper;
|
41
|
|
42
|
public static RegistrableEntityFilter newInstance(UserHelper userHelper) {
|
43
|
return new RegistrableEntityFilter(userHelper);
|
44
|
}
|
45
|
|
46
|
private RegistrableEntityFilter(UserHelper userHelper) {
|
47
|
this.userHelper = userHelper;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Filters a set of NameRelationship so that none of the relations contains a unpublished name.
|
52
|
*
|
53
|
* @param taxonName
|
54
|
* The name to which all the nameRelations belong to. It being is assumed that this
|
55
|
* name can be publicly visible.
|
56
|
*
|
57
|
* @param nameRelations
|
58
|
* The set of name relations to be filtered.
|
59
|
* @return
|
60
|
* A Set of name relations which can be publicly visible.
|
61
|
*/
|
62
|
public Set<NameRelationship> filterPublishedOnly(TaxonName taxonName, Set<NameRelationship> nameRelations) {
|
63
|
Set<NameRelationship> nameRelationsFiltered = new HashSet<>(nameRelations.size());
|
64
|
if(!currentUserMaySeeUnpublished()){
|
65
|
// need to filter out unpublished related names in this case
|
66
|
for(NameRelationship rel : nameRelations){
|
67
|
// check if the name has been published by any registration
|
68
|
Set<Registration> regsToCheck = new HashSet<>();
|
69
|
if(rel.getToName().equals(taxonName) && rel.getFromName().getRegistrations() != null) {
|
70
|
regsToCheck.addAll(rel.getFromName().getRegistrations());
|
71
|
}
|
72
|
if(rel.getFromName().equals(taxonName) && rel.getToName().getRegistrations() != null) {
|
73
|
regsToCheck.addAll(rel.getToName().getRegistrations());
|
74
|
}
|
75
|
// if there is no registration for this name we assume that it is published
|
76
|
boolean nameIsPublished = regsToCheck.size() == 0;
|
77
|
nameIsPublished |= regsToCheck.stream().anyMatch(reg -> reg.getStatus().equals(RegistrationStatus.PUBLISHED));
|
78
|
if(nameIsPublished){
|
79
|
nameRelationsFiltered.add(rel);
|
80
|
} else {
|
81
|
logger.debug("Hiding NameRelationship " + rel);
|
82
|
}
|
83
|
}
|
84
|
} else {
|
85
|
// no filtering needed
|
86
|
nameRelationsFiltered = nameRelations;
|
87
|
}
|
88
|
return nameRelationsFiltered;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Filters a set of TypeDesignationBase.
|
93
|
*
|
94
|
*
|
95
|
* @param typeDesignations
|
96
|
* The collection of TypeDesignationBase to be filtered.
|
97
|
* @return
|
98
|
* A Set of TypeDesignationBase which can be publicly visible.
|
99
|
*/
|
100
|
public Set<TypeDesignationBase> filterPublishedOnly(Collection<TypeDesignationBase> typeDesignations) {
|
101
|
Set<TypeDesignationBase> typeDesignationsFiltered = new HashSet<>(typeDesignations.size());
|
102
|
if(!currentUserMaySeeUnpublished()){
|
103
|
// need to filter out unpublished related names in this case
|
104
|
for(TypeDesignationBase<?> td : typeDesignations){
|
105
|
Set<Registration> regsToCheck = new HashSet<>();
|
106
|
if(td.getRegistrations() != null) {
|
107
|
regsToCheck.addAll(td.getRegistrations());
|
108
|
}
|
109
|
// if there is no registration for this type designation we assume that it is published
|
110
|
boolean isPublished = regsToCheck.size() == 0;
|
111
|
isPublished |= regsToCheck.stream().anyMatch(reg -> reg.getStatus().equals(RegistrationStatus.PUBLISHED));
|
112
|
if(isPublished){
|
113
|
typeDesignationsFiltered.add(td);
|
114
|
} else {
|
115
|
logger.debug("Hiding TypeDesignation " + td);
|
116
|
}
|
117
|
}
|
118
|
} else {
|
119
|
// no filtering needed
|
120
|
typeDesignationsFiltered.addAll(typeDesignations);
|
121
|
}
|
122
|
return typeDesignationsFiltered;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Check if the currently authenticated user is allowed to see unpublished entities wich are
|
127
|
* subject to a registration.
|
128
|
*
|
129
|
* @return
|
130
|
*/
|
131
|
protected boolean currentUserMaySeeUnpublished() {
|
132
|
return !(!userHelper.userIsAutheticated() || userHelper.userIsAnnonymous());
|
133
|
}
|
134
|
|
135
|
|
136
|
}
|