Skip to content

Commit d488fed

Browse files
committed
Merge pull request #1940 from Vojtech-Sassmann/dev4267
GetFacilities searcher
1 parent db87b45 commit d488fed

File tree

8 files changed

+523
-86
lines changed

8 files changed

+523
-86
lines changed

perun-core/src/main/java/cz/metacentrum/perun/core/api/Searcher.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,24 @@ public interface Searcher {
113113
*/
114114
List<Member> getMembersByExpiration(PerunSession sess, String operator, Calendar date) throws PrivilegeException, InternalErrorException;
115115

116+
/**
117+
* This method get Map of Attributes with searching values and try to find all facilities, which have specific attributes in format.
118+
* Better information about format below. When there are more than 1 attribute in Map, it means all must be true "looking for all of them" (AND)
119+
*
120+
* @param sess perun session
121+
* @param attributesWithSearchingValues map of attributes names
122+
* when attribute is type String, so value is string and we are looking for total match (Partial is not supported now, will be supported later by symbol *)
123+
* when attribute is type Integer, so value is integer in String and we are looking for total match
124+
* when attribute is type List<String>, so value is String and we are looking for at least one total or partial matching element
125+
* when attribute is type Map<String> so value is String in format "key=value" and we are looking total match of both or if is it "key" so we are looking for total match of key
126+
* IMPORTANT: In map there is not allowed char '=' in key. First char '=' is delimiter in MAP item key=value!!!
127+
* @return list of facilities that have attributes with specific values (behaviour above)
128+
* if no such facility exists, returns empty list
129+
*
130+
* @throws PrivilegeException insufficient permission
131+
* @throws InternalErrorException internal error
132+
* @throws AttributeNotExistsException when specified attribute does not exist
133+
* @throws WrongAttributeAssignmentException wrong attribute assignment
134+
*/
135+
List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws PrivilegeException, InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException;
116136
}

perun-core/src/main/java/cz/metacentrum/perun/core/bl/SearcherBl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cz.metacentrum.perun.core.bl;
22

33
import cz.metacentrum.perun.core.api.Attribute;
4+
import cz.metacentrum.perun.core.api.Facility;
45
import cz.metacentrum.perun.core.api.Group;
56
import cz.metacentrum.perun.core.api.Member;
67
import cz.metacentrum.perun.core.api.PerunSession;
@@ -99,4 +100,24 @@ public interface SearcherBl {
99100
* @throws InternalErrorException if any of attributes is null or has empty value, if any of attributes is not in expected namespace
100101
*/
101102
List<Group> getGroupsByGroupResourceSetting(PerunSession sess, Attribute groupResourceAttribute, Attribute resourceAttribute) throws InternalErrorException;
103+
104+
/**
105+
* This method get Map of Attributes with searching values and try to find all facilities, which have specific attributes in format.
106+
* Better information about format below. When there are more than 1 attribute in Map, it means all must be true "looking for all of them" (AND)
107+
*
108+
* @param sess perun session
109+
* @param attributesWithSearchingValues map of attributes names
110+
* when attribute is type String, so value is string and we are looking for total match (Partial is not supported now, will be supported later by symbol *)
111+
* when attribute is type Integer, so value is integer in String and we are looking for total match
112+
* when attribute is type List<String>, so value is String and we are looking for at least one total or partial matching element
113+
* when attribute is type Map<String> so value is String in format "key=value" and we are looking total match of both or if is it "key" so we are looking for total match of key
114+
* IMPORTANT: In map there is not allowed char '=' in key. First char '=' is delimiter in MAP item key=value!!!
115+
* @return list of facilities that have attributes with specific values (behaviour above)
116+
* if no such facility exists, returns empty list
117+
*
118+
* @throws InternalErrorException internal error
119+
* @throws AttributeNotExistsException when specified attribute does not exist
120+
* @throws WrongAttributeAssignmentException wrong attribute assignment
121+
*/
122+
List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException;
102123
}

perun-core/src/main/java/cz/metacentrum/perun/core/blImpl/SearcherBlImpl.java

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cz.metacentrum.perun.core.api.Attribute;
44
import cz.metacentrum.perun.core.api.AttributeDefinition;
55
import cz.metacentrum.perun.core.api.AttributesManager;
6+
import cz.metacentrum.perun.core.api.Facility;
67
import cz.metacentrum.perun.core.api.Group;
78
import cz.metacentrum.perun.core.api.Member;
89
import cz.metacentrum.perun.core.api.PerunSession;
@@ -107,6 +108,101 @@ public List<Group> getGroupsByGroupResourceSetting(PerunSession sess, Attribute
107108
return getSearcherImpl().getGroupsByGroupResourceSetting(sess, groupResourceAttribute, resourceAttribute);
108109
}
109110

111+
@Override
112+
public List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException {
113+
if (attributesWithSearchingValues == null || attributesWithSearchingValues.isEmpty()) {
114+
return perunBl.getFacilitiesManagerBl().getFacilities(sess);
115+
}
116+
117+
Map<Attribute, String> mapOfAttrsWithValues = new HashMap<>();
118+
Map<AttributeDefinition, String> mapOfCoreAttributesWithValues = new HashMap<>();
119+
120+
for(String name: attributesWithSearchingValues.keySet()) {
121+
if(name == null || name.equals("")) {
122+
throw new AttributeNotExistsException("There is no attribute with specified name!");
123+
}
124+
125+
AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, name);
126+
127+
if(getPerunBl().getAttributesManagerBl().isCoreAttribute(sess, attrDef)) {
128+
mapOfCoreAttributesWithValues.put(attrDef, attributesWithSearchingValues.get(name));
129+
} else {
130+
mapOfAttrsWithValues.put(new Attribute(attrDef), attributesWithSearchingValues.get(name));
131+
}
132+
}
133+
134+
List<Facility> facilitiesFromCoreAttributes = getFacilitiesForCoreAttributesByMapOfAttributes(sess, mapOfCoreAttributesWithValues);
135+
List<Facility> facilitiesFromAttributes = getSearcherImpl().getFacilities(sess, mapOfAttrsWithValues);
136+
facilitiesFromCoreAttributes.retainAll(facilitiesFromAttributes);
137+
return facilitiesFromCoreAttributes;
138+
}
139+
140+
private List<Facility> getFacilitiesForCoreAttributesByMapOfAttributes(PerunSession sess, Map<AttributeDefinition, String> coreAttributesWithSearchingValues) throws InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException {
141+
List<Facility> facilities = getPerunBl().getFacilitiesManagerBl().getFacilities(sess);
142+
if (coreAttributesWithSearchingValues == null || coreAttributesWithSearchingValues.isEmpty()) {
143+
return facilities;
144+
}
145+
146+
Set<AttributeDefinition> keys = coreAttributesWithSearchingValues.keySet();
147+
for(Iterator<Facility> facilityIter = facilities.iterator(); facilityIter.hasNext();) {
148+
Facility facilityFromIterator = facilityIter.next();
149+
150+
//Compare all needed attributes and their value to the attributes of every facility. If he does not fit, remove it from the array of returned facilities.
151+
for(AttributeDefinition attrDef: keys) {
152+
153+
String value = coreAttributesWithSearchingValues.get(attrDef);
154+
Attribute attrForFacility = getPerunBl().getAttributesManagerBl().getAttribute(sess, facilityFromIterator, attrDef.getName());
155+
156+
//One of attributes is not equal so remove him and continue with next facility
157+
if (!isAttributeValueMatching(attrForFacility, value)) {
158+
facilityIter.remove();
159+
break;
160+
}
161+
}
162+
}
163+
return facilities;
164+
}
165+
166+
/**
167+
* Returns true if the given value corresponds with value of given attribute.
168+
*
169+
* Accepted types of values are Integer and String. If given attribute has any other
170+
* value type, exception is risen.
171+
*
172+
* @param entityAttribute attribute
173+
* @param value value
174+
* @return true, if the given value corresponds with value of given attribute
175+
* @throws InternalErrorException internal error
176+
*/
177+
private boolean isAttributeValueMatching(Attribute entityAttribute, String value) throws InternalErrorException {
178+
boolean shouldBeAccepted = true;
179+
180+
if(entityAttribute.getValue() == null) {
181+
//We are looking for entities with null value in this core attribute
182+
if(value!=null && !value.isEmpty()) {
183+
shouldBeAccepted = false;
184+
}
185+
} else {
186+
//We need to compare those values, if they are equals,
187+
if (entityAttribute.getValue() instanceof String) {
188+
String attrValue = entityAttribute.valueAsString();
189+
if (!attrValue.equals(value)) {
190+
shouldBeAccepted = false;
191+
}
192+
} else if (entityAttribute.getValue() instanceof Integer) {
193+
Integer attrValue = entityAttribute.valueAsInteger();
194+
Integer valueInInteger = Integer.valueOf(value);
195+
if (attrValue.intValue() != valueInInteger.intValue()) {
196+
shouldBeAccepted = false;
197+
}
198+
} else {
199+
throw new InternalErrorException("Core attribute: " + entityAttribute + " is not type of String or Integer!");
200+
}
201+
}
202+
203+
return shouldBeAccepted;
204+
}
205+
110206
/**
111207
* This method take map of coreAttributes with search values and return all
112208
* users who have the specific match for all of these core attributes.
@@ -128,29 +224,11 @@ private List<User> getUsersForCoreAttributesByMapOfAttributes(PerunSession sess,
128224

129225
//Compare all needed attributes and their value to the attributes of every user. If he does not fit, remove him from the array of returned users.
130226
for(AttributeDefinition attrDef: keys) {
131-
boolean userIsAccepted = true;
132227
String value = coreAttributesWithSearchingValues.get(attrDef);
133228
Attribute attrForUser = getPerunBl().getAttributesManagerBl().getAttribute(sess, userFromIterator, attrDef.getName());
134229

135-
if(attrForUser.getValue() == null) {
136-
//We are looking for users with null value in this core attribute
137-
if(value!=null && !value.isEmpty()) userIsAccepted = false;
138-
} else {
139-
//We need to compare those values, if they are equals,
140-
if (attrForUser.getValue() instanceof String) {
141-
String attrValue = (String) attrForUser.getValue();
142-
if (!attrValue.equals(value)) userIsAccepted = false;
143-
} else if (attrForUser.getValue() instanceof Integer) {
144-
Integer attrValue = (Integer) attrForUser.getValue();
145-
Integer valueInInteger = Integer.valueOf(value);
146-
if (attrValue.intValue() != valueInInteger.intValue()) userIsAccepted = false;
147-
} else {
148-
throw new InternalErrorException("Core attribute: " + attrForUser + " is not type of String or Integer!");
149-
}
150-
}
151-
152230
//One of attributes is not equal so remove him and continue with next user
153-
if(!userIsAccepted) {
231+
if(!isAttributeValueMatching(attrForUser, value)) {
154232
userIter.remove();
155233
break;
156234
}

perun-core/src/main/java/cz/metacentrum/perun/core/entry/SearcherEntry.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cz.metacentrum.perun.core.api.AttributeDefinition;
66
import cz.metacentrum.perun.core.api.AttributesManager;
77
import cz.metacentrum.perun.core.api.AuthzResolver;
8+
import cz.metacentrum.perun.core.api.Facility;
89
import cz.metacentrum.perun.core.api.Member;
910
import cz.metacentrum.perun.core.api.PerunSession;
1011
import cz.metacentrum.perun.core.api.Role;
@@ -144,6 +145,17 @@ public List<Member> getMembersByExpiration(PerunSession sess, String operator, C
144145

145146
}
146147

148+
@Override
149+
public List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws PrivilegeException, InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException {
150+
151+
// Authorization
152+
if (!AuthzResolver.isAuthorized(sess, Role.PERUNADMIN)) {
153+
throw new PrivilegeException(sess, "getFacilities");
154+
}
155+
156+
return searcherBl.getFacilities(sess, attributesWithSearchingValues);
157+
}
158+
147159
public SearcherBl getSearcherBl() {
148160
return this.searcherBl;
149161
}

0 commit comments

Comments
 (0)