Skip to content

Commit db87b45

Browse files
committed
Merge pull request #1939 from stavamichal/getFacilityAttributesByListOfAttributes
New method getAttributes for facility by list of attr names
1 parent 07bd6e8 commit db87b45

File tree

8 files changed

+127
-3
lines changed

8 files changed

+127
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ public interface AttributesManager {
123123
*/
124124
List<Attribute> getAttributes(PerunSession sess, Facility facility) throws PrivilegeException, FacilityNotExistsException, InternalErrorException;
125125

126+
/**
127+
* Get all attributes associated with the facility which have name in list attrNames (empty too).
128+
* Virtual attribute too.
129+
*
130+
* PRIVILEGE: Get only those attributes the principal has access to.
131+
*
132+
* @param sess perun session
133+
* @param facility to get the attributes from
134+
* @param attrNames list of attributes' names
135+
* @return list of attributes
136+
* @throws PrivilegeException if privileges are not given
137+
* @throws InternalErrorException if an exception raise in concrete implementation, the exception is wrapped in InternalErrorException
138+
* @throws FacilityNotExistsException if the facility not exists in Perun
139+
*/
140+
List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws PrivilegeException, InternalErrorException, FacilityNotExistsException;
141+
126142
/**
127143
* Get all <b>non-empty</b> attributes associated with the vo.
128144
* PRIVILEGE: Get only those attributes the principal has access to.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public interface AttributesManagerBl {
5858
*/
5959
List<Attribute> getAttributes(PerunSession sess, Facility facility) throws InternalErrorException;
6060

61+
/**
62+
* Get all attributes associated with the facility which have name in list attrNames (empty too).
63+
* Virtual attribute too.
64+
*
65+
* @param sess perun session
66+
* @param facility to get the attributes from
67+
* @param attrNames list of attributes' names
68+
* @return list of attributes
69+
* @throws InternalErrorException if an exception raise in concrete implementation, the exception is wrapped in InternalErrorException
70+
*/
71+
List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws InternalErrorException;
72+
6173
/**
6274
* Get all <b>non-empty</b> attributes associated with the vo.
6375
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public List<Attribute> getAttributes(PerunSession sess, Facility facility) throw
122122
return attributes;
123123
}
124124

125+
public List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws InternalErrorException {
126+
if (attrNames.isEmpty()) return new ArrayList<>();
127+
128+
return getAttributesManagerImpl().getAttributes(sess, facility, attrNames);
129+
}
130+
125131
public List<Attribute> getAttributes(PerunSession sess, Vo vo) throws InternalErrorException {
126132
//get virtual attributes
127133
List<Attribute> attributes = getAttributesManagerImpl().getVirtualAttributes(sess, vo);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ public List<Attribute> getAttributes(PerunSession sess, Facility facility) throw
6464
return attributes;
6565
}
6666

67+
public List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws PrivilegeException, InternalErrorException, FacilityNotExistsException {
68+
Utils.checkPerunSession(sess);
69+
getPerunBl().getFacilitiesManagerBl().checkFacilityExists(sess, facility);
70+
List<Attribute> attributes = getAttributesManagerBl().getAttributes(sess, facility, attrNames);
71+
Iterator<Attribute> attrIter = attributes.iterator();
72+
//Choose to which attributes has the principal access
73+
while(attrIter.hasNext()) {
74+
Attribute attrNext = attrIter.next();
75+
if(!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, new AttributeDefinition(attrNext), facility, null)) attrIter.remove();
76+
else attrNext.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrNext, facility, null));
77+
}
78+
return attributes;
79+
}
80+
6781
public List<Attribute> getAttributes(PerunSession sess, Vo vo) throws PrivilegeException, VoNotExistsException, InternalErrorException {
6882
Utils.checkPerunSession(sess);
6983
getPerunBl().getVosManagerBl().checkVoExists(sess, vo);

perun-core/src/main/java/cz/metacentrum/perun/core/impl/AttributesManagerImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,27 @@ public List<Attribute> getAttributes(PerunSession sess, Facility facility) throw
687687
}
688688
}
689689

690+
public List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws InternalErrorException {
691+
MapSqlParameterSource parameters = new MapSqlParameterSource();
692+
parameters.addValue("fId", facility.getId());
693+
parameters.addValue("nSC", AttributesManager.NS_FACILITY_ATTR_CORE);
694+
parameters.addValue("nSO", AttributesManager.NS_FACILITY_ATTR_OPT);
695+
parameters.addValue("nSD", AttributesManager.NS_FACILITY_ATTR_DEF);
696+
parameters.addValue("nSV", AttributesManager.NS_FACILITY_ATTR_VIRT);
697+
parameters.addValue("attrNames", attrNames);
698+
699+
try {
700+
return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("fav") + " from attr_names " +
701+
"left join facility_attr_values fav on id=fav.attr_id and facility_id=:fId " +
702+
"where namespace in ( :nSC,:nSO,:nSD,:nSV ) and attr_names.attr_name in ( :attrNames )",
703+
parameters, new SingleBeanAttributeRowMapper<>(sess, this, facility));
704+
} catch (EmptyResultDataAccessException ex) {
705+
return new ArrayList<>();
706+
} catch (RuntimeException ex) {
707+
throw new InternalErrorException(ex);
708+
}
709+
}
710+
690711
private List<Attribute> getVirtualAttributes(RowMapper<Attribute> rowMapper, String namespace) throws InternalErrorException {
691712
try {
692713
return jdbc.query("SELECT " + attributeDefinitionMappingSelectQuery + ", NULL AS attr_value FROM attr_names WHERE namespace=?", rowMapper, namespace);

perun-core/src/main/java/cz/metacentrum/perun/core/implApi/AttributesManagerImplApi.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ public interface AttributesManagerImplApi {
5757
*/
5858
List<Attribute> getAttributes(PerunSession sess, Facility facility) throws InternalErrorException;
5959

60+
/**
61+
* Get all attributes associated with the facility which have name in list attrNames (empty too).
62+
* Virtual attribute too.
63+
*
64+
* @param sess perun session
65+
* @param facility to get the attributes from
66+
* @param attrNames list of attributes' names
67+
* @return list of attributes
68+
* @throws InternalErrorException if an exception raise in concrete implementation, the exception is wrapped in InternalErrorException
69+
*/
70+
List<Attribute> getAttributes(PerunSession sess, Facility facility, List<String> attrNames) throws InternalErrorException;
71+
6072
/**
6173
* Get all virtual attributes associated with the facility.
6274
*

perun-core/src/test/java/cz/metacentrum/perun/core/entry/AttributesManagerEntryIntegrationTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.lang.reflect.Method;
5858
import java.util.ArrayList;
5959
import java.util.Arrays;
60+
import java.util.Collections;
6061
import java.util.HashMap;
6162
import java.util.HashSet;
6263
import java.util.LinkedHashMap;
@@ -515,7 +516,7 @@ public void setGroupNameWillProduceSettingMoreThanOneGIDAtOnce() throws Exceptio
515516
perun.getAttributesManagerBl().setAttribute(sess, f1, GIDNamespaceForFacilities);
516517
GIDNamespaceForFacilities.setValue(namespaceBBB);
517518
perun.getAttributesManagerBl().setAttribute(sess, f2, GIDNamespaceForFacilities);
518-
519+
519520
//create new service and assigne it to resources
520521
Service s1 = new Service(0, "testService01", null);
521522
s1 = perun.getServicesManagerBl().createService(sess, s1);
@@ -931,6 +932,33 @@ public void getFacilityAttributes() throws Exception {
931932

932933
}
933934

935+
@Test
936+
public void getFacilityAttributesByListOfAttrNames() throws Exception {
937+
System.out.println(CLASS_NAME + "getFacilityAttributesByListOfAttrNames");
938+
939+
facility = setUpFacility();
940+
attributes = setUpFacilityAttribute();
941+
attributesManager.setAttribute(sess, facility, attributes.get(0));
942+
943+
List<Attribute> retAttr = attributesManager.getAttributes(sess, facility, Collections.singletonList(attributes.get(0).getName()));
944+
945+
assertTrue("our atttribute not returned",retAttr.contains(attributes.get(0)));
946+
assertTrue("returned more than 1 attribute",retAttr.size() == 1);
947+
}
948+
949+
@Test
950+
public void getFacilityAttributesByEmptyListOfAttrNames() throws Exception {
951+
System.out.println(CLASS_NAME + "getFacilityAttributesByEmptyListOfAttrNames");
952+
953+
facility = setUpFacility();
954+
attributes = setUpFacilityAttribute();
955+
attributesManager.setAttribute(sess, facility, attributes.get(0));
956+
957+
List<Attribute> retAttr = attributesManager.getAttributes(sess, facility, Collections.EMPTY_LIST);
958+
959+
assertTrue("our list of attributes is not empty",retAttr.isEmpty());
960+
}
961+
934962
@Test (expected=FacilityNotExistsException.class)
935963
public void getFacilityAttributesWhenFacilityNotExists() throws Exception {
936964
System.out.println(CLASS_NAME + "getFacilityAttributesWhenFacilityNotExists");

perun-rpc/src/main/java/cz/metacentrum/perun/rpc/methods/AttributesManagerMethod.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public enum AttributesManagerMethod implements ManagerMethod {
3232
* @return List<Attribute> All non-empty Facility attributes
3333
* @throw FacilityNotExistsException When Facility with <code>id</code> doesn't exist.
3434
*/
35+
/*#
36+
* Returns all specified Facility attributes for selected Facility
37+
* If <code>attrNames</code> is empty, it returns empty list of attributes
38+
*
39+
* @param facility int Facility <code>id</code>
40+
* @param attrNames List<String> Attribute names
41+
* @return List<Attribute> Specified Facility attributes
42+
* @throw FacilityNotExistsException When Facility with <code>id</code> doesn't exist.
43+
*/
3544
/*#
3645
* Returns all non-empty Vo attributes for selected Vo.
3746
*
@@ -254,8 +263,14 @@ public List<Attribute> call(ApiCaller ac, Deserializer parms) throws PerunExcept
254263
ac.getUserById(parms.readInt("user")));
255264
}
256265
} else {
257-
return ac.getAttributesManager().getAttributes(ac.getSession(),
258-
ac.getFacilityById(parms.readInt("facility")));
266+
if (parms.contains("attrNames")) {
267+
return ac.getAttributesManager().getAttributes(ac.getSession(),
268+
ac.getFacilityById(parms.readInt("facility")),
269+
parms.readList("attrNames", String.class));
270+
} else {
271+
return ac.getAttributesManager().getAttributes(ac.getSession(),
272+
ac.getFacilityById(parms.readInt("facility")));
273+
}
259274
}
260275
} else if (parms.contains("vo")) {
261276
if (parms.contains("attrNames")) {

0 commit comments

Comments
 (0)