Skip to content
Snippets Groups Projects
Commit ce0f33be authored by felmer's avatar felmer
Browse files

SSDM-5721: Person with role assignments implemented in Java and JS with system test.

SVN: 38961
parent 1fd203f4
No related branches found
No related tags found
No related merge requests found
Showing
with 222 additions and 7 deletions
/*
* Copyright 2017 ETH Zuerich, SIS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.ethz.sis.openbis.generic.server.asapi.v3.translator.person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.fetchoptions.RoleAssignmentFetchOptions;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.common.IObjectToManyRelationTranslator;
/**
*
*
* @author Franz-Josef Elmer
*/
public interface IPersonRoleAssignmentTranslator extends IObjectToManyRelationTranslator<RoleAssignment, RoleAssignmentFetchOptions>
{
}
...@@ -42,4 +42,8 @@ public interface PersonQuery extends ObjectQuery ...@@ -42,4 +42,8 @@ public interface PersonQuery extends ObjectQuery
@Select(sql = "select id as objectId, pers_id_registerer as relatedId from persons where id = any(?{1})", parameterBindings = { LongSetMapper.class }, fetchSize = FETCH_SIZE) @Select(sql = "select id as objectId, pers_id_registerer as relatedId from persons where id = any(?{1})", parameterBindings = { LongSetMapper.class }, fetchSize = FETCH_SIZE)
public List<ObjectRelationRecord> getRegistratorIds(LongSet personIds); public List<ObjectRelationRecord> getRegistratorIds(LongSet personIds);
@Select(sql = "select pers_id_grantee as objectId, id as relatedId from role_assignments where pers_id_grantee = any(?{1})",
parameterBindings = { LongSetMapper.class }, fetchSize = FETCH_SIZE)
public List<ObjectRelationRecord> getRoleAssignmentIds(LongSet personIds);
} }
/*
* Copyright 2017 ETH Zuerich, SIS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.ethz.sis.openbis.generic.server.asapi.v3.translator.person;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.fetchoptions.RoleAssignmentFetchOptions;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.TranslationContext;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.common.ObjectRelationRecord;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.common.ObjectToManyRelationTranslator;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.roleassignment.IRoleAssignmentTranslator;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import net.lemnik.eodsql.QueryTool;
/**
*
*
* @author Franz-Josef Elmer
*/
@Component
public class PersonRoleAssignmentTranslator
extends ObjectToManyRelationTranslator<RoleAssignment, RoleAssignmentFetchOptions>
implements IPersonRoleAssignmentTranslator
{
@Autowired
private IRoleAssignmentTranslator roleAssignmentTranslator;
@Override
protected List<ObjectRelationRecord> loadRecords(LongOpenHashSet objectIds)
{
PersonQuery query = QueryTool.getManagedQuery(PersonQuery.class);
return query.getRoleAssignmentIds(objectIds);
}
@Override
protected Map<Long, RoleAssignment> translateRelated(TranslationContext context, Collection<Long> relatedIds,
RoleAssignmentFetchOptions relatedFetchOptions)
{
return roleAssignmentTranslator.translate(context, relatedIds, relatedFetchOptions);
}
@Override
protected Collection<RoleAssignment> createCollection()
{
return new ArrayList<>();
}
}
\ No newline at end of file
...@@ -17,12 +17,14 @@ ...@@ -17,12 +17,14 @@
package ch.ethz.sis.openbis.generic.server.asapi.v3.translator.person; package ch.ethz.sis.openbis.generic.server.asapi.v3.translator.person;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.AbstractCachingTranslator; import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.AbstractCachingTranslator;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.TranslationContext; import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.TranslationContext;
import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.TranslationResults; import ch.ethz.sis.openbis.generic.server.asapi.v3.translator.TranslationResults;
...@@ -43,6 +45,9 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe ...@@ -43,6 +45,9 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe
@Autowired @Autowired
private IPersonRegistratorTranslator registratorTranslator; private IPersonRegistratorTranslator registratorTranslator;
@Autowired
private IPersonRoleAssignmentTranslator roleAssignmentTranslator;
@Override @Override
protected Person createObject(TranslationContext context, Long personId, PersonFetchOptions fetchOptions) protected Person createObject(TranslationContext context, Long personId, PersonFetchOptions fetchOptions)
{ {
...@@ -68,6 +73,11 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe ...@@ -68,6 +73,11 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe
relations.put(IPersonRegistratorTranslator.class, registratorTranslator.translate(context, personIds, fetchOptions.withRegistrator())); relations.put(IPersonRegistratorTranslator.class, registratorTranslator.translate(context, personIds, fetchOptions.withRegistrator()));
} }
if (fetchOptions.hasRoleAssignments())
{
relations.put(IPersonRoleAssignmentTranslator.class, roleAssignmentTranslator.translate(context, personIds, fetchOptions.withRoleAssignments()));
}
return relations; return relations;
} }
...@@ -96,5 +106,11 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe ...@@ -96,5 +106,11 @@ public class PersonTranslator extends AbstractCachingTranslator<Long, Person, Pe
result.setRegistrator(relations.get(IPersonRegistratorTranslator.class, personId)); result.setRegistrator(relations.get(IPersonRegistratorTranslator.class, personId));
result.getFetchOptions().withRegistratorUsing(fetchOptions.withRegistrator()); result.getFetchOptions().withRegistratorUsing(fetchOptions.withRegistrator());
} }
}
if (fetchOptions.hasRoleAssignments())
{
result.setRoleAssignments((List<RoleAssignment>) relations.get(IPersonRoleAssignmentTranslator.class, personId));
result.getFetchOptions().withRoleAssignmentsUsing(fetchOptions.withRoleAssignments());
}
}
} }
...@@ -18,6 +18,7 @@ define([ "stjs", "util/Exceptions" ], function(stjs, exceptions) { ...@@ -18,6 +18,7 @@ define([ "stjs", "util/Exceptions" ], function(stjs, exceptions) {
prototype.active = null; prototype.active = null;
prototype.space = null; prototype.space = null;
prototype.registrator = null; prototype.registrator = null;
prototype.roleAssignments = null;
prototype.getFetchOptions = function() { prototype.getFetchOptions = function() {
return this.fetchOptions; return this.fetchOptions;
}; };
...@@ -86,12 +87,26 @@ define([ "stjs", "util/Exceptions" ], function(stjs, exceptions) { ...@@ -86,12 +87,26 @@ define([ "stjs", "util/Exceptions" ], function(stjs, exceptions) {
prototype.setRegistrator = function(registrator) { prototype.setRegistrator = function(registrator) {
this.registrator = registrator; this.registrator = registrator;
}; };
prototype.getRoleAssignments = function() {
if (this.getFetchOptions() && this.getFetchOptions().hasRoleAssignments()) {
return this.roleAssignments;
} else {
throw new exceptions.NotFetchedException("RoleAssignments have not been fetched.");
}
};
prototype.setRoleAssignments = function(roleAssignments) {
this.roleAssignments = roleAssignments;
};
}, { }, {
fetchOptions : "PersonFetchOptions", fetchOptions : "PersonFetchOptions",
permId : "PersonPermId", permId : "PersonPermId",
registrationDate : "Date", registrationDate : "Date",
space : "Space", space : "Space",
registrator : "Person" registrator : "Person",
roleAssignments : {
name : "List",
arguments : [ "RoleAssignment" ]
}
}); });
return Person; return Person;
}) })
\ No newline at end of file
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* Class automatically generated with * Class automatically generated with
* {@link ch.ethz.sis.openbis.generic.shared.api.v3.dto.generators.DtoGenerator} * {@link ch.ethz.sis.openbis.generic.shared.api.v3.dto.generators.DtoGenerator}
*/ */
define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/space/fetchoptions/SpaceFetchOptions", "as/dto/person/fetchoptions/PersonSortOptions" ], function(require, stjs, FetchOptions) { define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/space/fetchoptions/SpaceFetchOptions",
"as/dto/person/fetchoptions/PersonSortOptions", "as/dto/roleassignment/fetchoptions/RoleAssignmentFetchOptions" ], function(require, stjs, FetchOptions) {
var PersonFetchOptions = function() { var PersonFetchOptions = function() {
}; };
stjs.extend(PersonFetchOptions, FetchOptions, [ FetchOptions ], function(constructor, prototype) { stjs.extend(PersonFetchOptions, FetchOptions, [ FetchOptions ], function(constructor, prototype) {
...@@ -10,6 +11,7 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s ...@@ -10,6 +11,7 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s
constructor.serialVersionUID = 1; constructor.serialVersionUID = 1;
prototype.space = null; prototype.space = null;
prototype.registrator = null; prototype.registrator = null;
prototype.roleAssignments = null;
prototype.sort = null; prototype.sort = null;
prototype.withSpace = function() { prototype.withSpace = function() {
if (this.space == null) { if (this.space == null) {
...@@ -36,6 +38,19 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s ...@@ -36,6 +38,19 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s
prototype.hasRegistrator = function() { prototype.hasRegistrator = function() {
return this.registrator != null; return this.registrator != null;
}; };
prototype.withRoleAssignments = function() {
if (this.roleAssignments == null) {
var RoleAssignmentsFetchOptions = require("as/dto/roleassignment/fetchoptions/RoleAssignmentFetchOptions");
this.roleAssignments = new RoleAssignmentsFetchOptions();
}
return this.roleAssignments;
};
prototype.withRoleAssignmentsUsing = function(fetchOptions) {
return this.roleAssignments = fetchOptions;
};
prototype.hasRoleAssignments = function() {
return this.roleAssignments != null;
};
prototype.sortBy = function() { prototype.sortBy = function() {
if (this.sort == null) { if (this.sort == null) {
var PersonSortOptions = require("as/dto/person/fetchoptions/PersonSortOptions"); var PersonSortOptions = require("as/dto/person/fetchoptions/PersonSortOptions");
...@@ -49,6 +64,7 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s ...@@ -49,6 +64,7 @@ define([ "require", "stjs", "as/dto/common/fetchoptions/FetchOptions", "as/dto/s
}, { }, {
space : "SpaceFetchOptions", space : "SpaceFetchOptions",
registrator : "PersonFetchOptions", registrator : "PersonFetchOptions",
roleAssignments : "RoleAssignmentFetchOptions",
sort : "PersonSortOptions" sort : "PersonSortOptions"
}); });
return PersonFetchOptions; return PersonFetchOptions;
......
...@@ -27,6 +27,7 @@ import org.testng.annotations.Test; ...@@ -27,6 +27,7 @@ import org.testng.annotations.Test;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.search.PersonSearchCriteria; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.search.PersonSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space; import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
/** /**
...@@ -40,18 +41,21 @@ public class SearchPersonTest extends AbstractTest ...@@ -40,18 +41,21 @@ public class SearchPersonTest extends AbstractTest
public void testSearchPersonByUserId() public void testSearchPersonByUserId()
{ {
// Given // Given
String sessionToken = v3api.login(TEST_SPACE_USER, PASSWORD); String sessionToken = v3api.login(TEST_USER, PASSWORD);
PersonSearchCriteria searchCriteria = new PersonSearchCriteria(); PersonSearchCriteria searchCriteria = new PersonSearchCriteria();
searchCriteria.withUserId().thatStartsWith("observer"); searchCriteria.withUserId().thatStartsWith("observer");
PersonFetchOptions fetchOptions = new PersonFetchOptions(); PersonFetchOptions fetchOptions = new PersonFetchOptions();
fetchOptions.withSpace(); fetchOptions.withSpace();
fetchOptions.withRoleAssignments().withSpace();
// Then // Then
List<Person> persons = v3api.searchPersons(sessionToken, searchCriteria, fetchOptions).getObjects(); List<Person> persons = v3api.searchPersons(sessionToken, searchCriteria, fetchOptions).getObjects();
// When // When
assertEquals(render(persons), "observer: John Observer observer@o.o\n" assertEquals(render(persons), "observer: John Observer observer@o.o, home space:CISD, "
+ "observer_cisd: John ObserverCISD observer_cisd@o.o\n"); + "[SPACE_OBSERVER Space TESTGROUP]\n"
+ "observer_cisd: John ObserverCISD observer_cisd@o.o, home space:CISD, "
+ "[SPACE_ADMIN Space TESTGROUP, SPACE_OBSERVER Space CISD]\n");
} }
private String render(List<Person> persons) private String render(List<Person> persons)
...@@ -80,10 +84,24 @@ public class SearchPersonTest extends AbstractTest ...@@ -80,10 +84,24 @@ public class SearchPersonTest extends AbstractTest
Space space = person.getSpace(); Space space = person.getSpace();
if (space != null) if (space != null)
{ {
builder.append(" home space:").append(space.getCode()); builder.append(", home space:").append(space.getCode());
} }
List<RoleAssignment> roleAssignments = person.getRoleAssignments();
String string = renderAssignments(roleAssignments);
builder.append(", ").append(string);
return builder.toString(); return builder.toString();
} }
private String renderAssignments(List<RoleAssignment> roleAssignments)
{
List<String> renderedAssignments = new ArrayList<>();
for (RoleAssignment roleAssignment : roleAssignments)
{
renderedAssignments.add(roleAssignment.getRoleLevel() + "_" + roleAssignment.getRole() + " " + roleAssignment.getSpace());
}
Collections.sort(renderedAssignments);
return renderedAssignments.toString();
}
private void appendTo(StringBuilder builder, String stringOrNull) private void appendTo(StringBuilder builder, String stringOrNull)
{ {
......
...@@ -22,6 +22,7 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.interfaces.ISpaceHolder; ...@@ -22,6 +22,7 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.interfaces.ISpaceHolder;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.id.PersonPermId; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.id.PersonPermId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space; import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
import ch.ethz.sis.openbis.generic.asapi.v3.exceptions.NotFetchedException; import ch.ethz.sis.openbis.generic.asapi.v3.exceptions.NotFetchedException;
import ch.systemsx.cisd.base.annotation.JsonObject; import ch.systemsx.cisd.base.annotation.JsonObject;
...@@ -29,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; ...@@ -29,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List;
/* /*
* Class automatically generated with DtoGenerator * Class automatically generated with DtoGenerator
...@@ -68,6 +70,9 @@ public class Person implements Serializable, IPermIdHolder, IRegistrationDateHol ...@@ -68,6 +70,9 @@ public class Person implements Serializable, IPermIdHolder, IRegistrationDateHol
@JsonProperty @JsonProperty
private Person registrator; private Person registrator;
@JsonProperty
private List<RoleAssignment> roleAssignments;
// Method automatically generated with DtoGenerator // Method automatically generated with DtoGenerator
@JsonIgnore @JsonIgnore
public PersonFetchOptions getFetchOptions() public PersonFetchOptions getFetchOptions()
...@@ -216,6 +221,24 @@ public class Person implements Serializable, IPermIdHolder, IRegistrationDateHol ...@@ -216,6 +221,24 @@ public class Person implements Serializable, IPermIdHolder, IRegistrationDateHol
this.registrator = registrator; this.registrator = registrator;
} }
@JsonIgnore
public List<RoleAssignment> getRoleAssignments()
{
if (getFetchOptions() != null && getFetchOptions().hasRoleAssignments())
{
return roleAssignments;
}
else
{
throw new NotFetchedException("Role assignments have not been fetched.");
}
}
public void setRoleAssignments(List<RoleAssignment> roleAssignments)
{
this.roleAssignments = roleAssignments;
}
// Method automatically generated with DtoGenerator // Method automatically generated with DtoGenerator
@Override @Override
public String toString() public String toString()
......
...@@ -19,6 +19,7 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.fetchoptions.FetchOptions ...@@ -19,6 +19,7 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.fetchoptions.FetchOptions
import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.fetchoptions.FetchOptionsToStringBuilder; import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.fetchoptions.FetchOptionsToStringBuilder;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions; import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.fetchoptions.RoleAssignmentFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions; import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions;
import ch.systemsx.cisd.base.annotation.JsonObject; import ch.systemsx.cisd.base.annotation.JsonObject;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
...@@ -38,6 +39,9 @@ public class PersonFetchOptions extends FetchOptions<Person> implements Serializ ...@@ -38,6 +39,9 @@ public class PersonFetchOptions extends FetchOptions<Person> implements Serializ
@JsonProperty @JsonProperty
private PersonFetchOptions registrator; private PersonFetchOptions registrator;
@JsonProperty
private RoleAssignmentFetchOptions roleAssignments;
@JsonProperty @JsonProperty
private PersonSortOptions sort; private PersonSortOptions sort;
...@@ -85,6 +89,25 @@ public class PersonFetchOptions extends FetchOptions<Person> implements Serializ ...@@ -85,6 +89,25 @@ public class PersonFetchOptions extends FetchOptions<Person> implements Serializ
return registrator != null; return registrator != null;
} }
public RoleAssignmentFetchOptions withRoleAssignments()
{
if (roleAssignments == null)
{
roleAssignments = new RoleAssignmentFetchOptions();
}
return roleAssignments;
}
public RoleAssignmentFetchOptions withRoleAssignmentsUsing(RoleAssignmentFetchOptions fetchOptions)
{
return roleAssignments = fetchOptions;
}
public boolean hasRoleAssignments()
{
return roleAssignments != null;
}
// Method automatically generated with DtoGenerator // Method automatically generated with DtoGenerator
@Override @Override
public PersonSortOptions sortBy() public PersonSortOptions sortBy()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment