Commit a31f8302 authored by Matija Obreza's avatar Matija Obreza

Merge branch '422-subsets-partner-as-owner' into 'master'

Subsets: Partner as owner

Closes #422

See merge request genesys-pgr/genesys-server!362
parents f6566b54 a5837401
......@@ -43,6 +43,7 @@ import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.catalog.model.Partner;
import org.genesys2.server.model.PublishState;
import org.genesys2.util.MCPDUtil;
import org.hibernate.annotations.Type;
......@@ -97,6 +98,12 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@JsonView({ JsonViews.Public.class })
private FaoInstitute institute;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "owner_id", updatable = false)
@JsonView({ JsonViews.Public.class })
@NotNull
private Partner owner;
/** The accessionRefs. */
@OneToMany(mappedBy="subset", orphanRemoval=true, fetch=FetchType.LAZY, cascade= { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE })
@Field(type = FieldType.Object)
......@@ -156,7 +163,7 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
*/
@Override
public AclAwareModel aclParentObject() {
return institute;
return this.owner;
}
/**
......@@ -403,4 +410,12 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
public void setSource(String source) {
this.source = source;
}
public Partner getOwner() {
return owner;
}
public void setOwner(Partner owner) {
this.owner = owner;
}
}
......@@ -22,6 +22,7 @@ import java.util.Set;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.impl.Subset;
......@@ -36,59 +37,64 @@ import com.querydsl.core.types.Predicate;
*/
public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> {
/** The title. */
public StringFilter title;
/** The description. */
public StringFilter description;
/** The publisher. */
public Set<String> publisher;
/** The date created. */
public StringFilter dateCreated;
/** Institutes. */
public Set<String> institutes;
/** The publish state. */
public Set<PublishState> state;
public Set<String> crop;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildPredicate() {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(subset, subset._super, and);
if ((institutes != null) && !institutes.isEmpty()) {
and.and(subset.institute.code.in(institutes));
}
if (title != null) {
and.and(title.buildQuery(subset.title));
}
if (description != null) {
and.and(description.buildQuery(subset.description));
}
if ((publisher != null) && !publisher.isEmpty()) {
and.and(subset.publisher.in(publisher));
}
if (dateCreated != null) {
and.and(dateCreated.buildQuery(subset.dateCreated));
}
if (state != null && !state.isEmpty()) {
and.and(subset.state.in(state));
}
if (crop != null && !crop.isEmpty()) {
and.and(subset.crops.any().in(crop));
}
return and;
}
/** The owner. */
public PartnerFilter owner;
/** The title. */
public StringFilter title;
/** The description. */
public StringFilter description;
/** The publisher. */
public Set<String> publisher;
/** The date created. */
public StringFilter dateCreated;
/** Institutes. */
public Set<String> institutes;
/** The publish state. */
public Set<PublishState> state;
public Set<String> crop;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildPredicate() {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(subset, subset._super, and);
if (owner != null) {
and.and(owner.buildQuery(subset.owner));
}
if ((institutes != null) && !institutes.isEmpty()) {
and.and(subset.institute.code.in(institutes));
}
if (title != null) {
and.and(title.buildQuery(subset.title));
}
if (description != null) {
and.and(description.buildQuery(subset.description));
}
if ((publisher != null) && !publisher.isEmpty()) {
and.and(subset.publisher.in(publisher));
}
if (dateCreated != null) {
and.and(dateCreated.buildQuery(subset.dateCreated));
}
if (state != null && !state.isEmpty()) {
and.and(subset.state.in(state));
}
if (crop != null && !crop.isEmpty()) {
and.and(subset.crops.any().in(crop));
}
return and;
}
public SubsetFilter state(PublishState ... state) {
if (this.state == null) {
......
......@@ -36,6 +36,7 @@ import com.querydsl.jpa.JPAExpressions;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.Partner;
import org.genesys2.server.component.security.SecurityUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
......@@ -165,8 +166,8 @@ public class SubsetServiceImpl implements SubsetService {
Page<Subset> res = subsetRepository.findAll(filter.buildPredicate(), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> instituteIds = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(FaoInstitute.class, BasePermission.WRITE));
Page<Subset> res = subsetRepository.findAll(QSubset.subset.institute.id.in(instituteIds).and(filter.buildPredicate()), markdownSortPageRequest);
final HashSet<Long> partnersIds = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
Page<Subset> res = subsetRepository.findAll(QSubset.subset.owner.id.in(partnersIds).and(filter.buildPredicate()), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -210,6 +211,7 @@ public class SubsetServiceImpl implements SubsetService {
target.setUuid(source.getUuid());
target.setDate(source.getDate());
target.setSource(source.getSource());
target.setOwner(source.getOwner());
if (source.getCrops() != null) {
if (target.getCrops() == null) {
......@@ -231,6 +233,9 @@ public class SubsetServiceImpl implements SubsetService {
throw new NotFoundElement("No such subset");
subset.getInstitute().getId();
if (subset.getOwner() != null) {
subset.getOwner().getId();
}
if (subset.getCrops() != null)
subset.getCrops().size();
if (subset.getCreators() != null)
......
......@@ -5578,3 +5578,52 @@ databaseChangeLog:
name: createdDate
indexName: IX_accession_createdDate
tableName: accession
- changeSet:
id: 1550773861456-1
author: vpavlov
comment: owner_id to subset
changes:
- addColumn:
columns:
- column:
name: owner_id
type: bigint(20)
tableName: subset
- changeSet:
id: 1550773861456-2
author: vpavlov
comment: added foreign key subset(owner_id)-partner(id)
changes:
- addForeignKeyConstraint:
baseColumnNames: owner_id
baseTableName: subset
constraintName: FK_952d6e2c297eae6a9ee7aa57b
deferrable: false
initiallyDeferred: false
onDelete: NO ACTION
onUpdate: NO ACTION
referencedColumnNames: id
referencedTableName: partner
- changeSet:
id: 1550773861456-3
author: vpavlov
changes:
- sql:
comment: Set subset.owner_id = subset.institute.partnerId
sql: update subset s
inner join faoinstitute f on f.id = s.institute_id
SET s.owner_id = f.partnerId
where f.partnerId is not null;
- changeSet:
id: 1550773861456-4
author: vpavlov
changes:
- addNotNullConstraint:
columnDataType: bigint(20)
columnName: owner_id
tableName: subset
......@@ -225,6 +225,7 @@ public class SubsetServiceTest extends CatalogServiceTest {
subset.setTitle("Subset");
subset.setWiewsCode("0000");
subset.setInstitute(faoInstitute);
subset.setOwner(partner);
subset.setUuid(uuid);
return subsetService.create(faoInstitute, subset);
......
......@@ -31,7 +31,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository;
import org.genesys.catalog.model.Partner;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.test.base.WithMockOAuth2Authentication;
import org.genesys2.server.api.v1.SubsetController;
......@@ -83,6 +83,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
private static final String DESCRIPTION_2 = "Subset test description 2";
private static final String PUBLISHER = "Test publisher";
private static final String DATE_CREATED = "2018-02-01";
private static Partner owner;
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");
......@@ -146,6 +147,8 @@ public class SubsetRestControllerTest extends AbstractApiTest {
institute.setFullName("An institute");
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute("INS001");
owner = setUpPartner("Subset test partner", true, "STP", UUID.randomUUID());
}
@Test
......@@ -164,6 +167,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(jsonPath("$.title", is(TITLE)))
.andExpect(jsonPath("$.description", is(DESCRIPTION)))
.andExpect(jsonPath("$.publisher", is(PUBLISHER)))
.andExpect(jsonPath("$.owner.name", is(owner.getName())))
.andExpect(jsonPath("$.dateCreated", is(DATE_CREATED)));
/*@formatter:on*/
}
......@@ -210,6 +214,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.title", is(TITLE_2)))
.andExpect(jsonPath("$.owner.name", is(owner.getName())))
.andExpect(jsonPath("$.description", is(DESCRIPTION_2)))
.andExpect(jsonPath("$.accessionCount", is(0)));
/*@formatter:on*/
......@@ -466,6 +471,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
subset.setState(PublishState.PUBLISHED);
subset.setDescription(DESCRIPTION);
subset.setPublisher(PUBLISHER);
subset.setOwner(owner);
subset.setDateCreated(DATE_CREATED);
return subset;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment