Commit 3b3ca49a authored by Matija Obreza's avatar Matija Obreza
Browse files

Updated Subsets API and UI

parent 7dc9d393
/**
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -12,11 +12,11 @@
* 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 org.genesys2.server.model.impl;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.security.model.AclAwareModel;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
......@@ -25,74 +25,89 @@ import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import java.util.List;
import java.util.UUID;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys2.server.model.genesys.AccessionId;
import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
// TODO: Auto-generated Javadoc
/**
* The Class Subset.
*/
@Entity
@Table
public class Subset extends AuditedVersionedModel implements AclAwareModel {
@Table(name = "subset")
public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 7021405309572916429L;
@Column(unique = true, updatable = false, nullable = false, columnDefinition = "binary(16)")
protected UUID uuid;
@Column
/** The published. */
@Column()
protected boolean published;
@Column
/** The title. */
@Column(length = 250, nullable = false)
protected String title;
/** The description. */
@Column
@Lob
@Type(type = "org.hibernate.type.TextType")
protected String description;
@Column
/** The publisher. */
@Column(length = 250)
protected String publisher;
@Column
/** The date created. */
@Column(length = 100)
protected String dateCreated;
@Column
/** The rights. */
@Column(length = 100)
protected String rights;
/** The wiews code. */
@Column(name = "wiews_code", length = 8, nullable = false)
private String wiewsCode;
/** The institute. */
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "institute_id", updatable = false)
// @JsonView({ JsonViews.Public.class })
@JsonIgnore
private FaoInstitute institute;
/** The accessions. */
@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "SubsetAccessions", joinColumns = @JoinColumn(name = "subsetId"), inverseJoinColumns = @JoinColumn(name = "acceId"))
private List<AccessionId> accessionIds;
@JoinTable(name = "subset_accession", joinColumns = @JoinColumn(name = "subset_id"), inverseJoinColumns = @JoinColumn(name = "acce_id"))
@JsonIdentityReference(alwaysAsId = true)
private List<AccessionId> accessions;
/** The accession count. */
@Column(name = "accession_count", nullable = false)
private int accessionCount = 0;
/**
* Generate UUID if missing
* Generate UUID if missing.
*/
@PrePersist
protected void prepersist() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
}
trimStringsToNull();
/**
* Gets the uuid.
*
* @return the uuid
*/
public UUID getUuid() {
return uuid;
}
this.wiewsCode = this.institute.getCode();
/**
* Sets the uuid.
*
* @param uuid the new uuid
*/
public void setUuid(final UUID uuid) {
this.uuid = uuid;
if (this.getAccessionIds() != null) {
this.accessionCount=this.getAccessionIds().size();
}
}
/**
......@@ -204,20 +219,65 @@ public class Subset extends AuditedVersionedModel implements AclAwareModel {
}
/**
* Gets the accessionIds list.
* Gets the accessions.
*
* @return the accessionIds list
* @return the accessions
*/
public List<AccessionId> getAccessionIds() {
return accessionIds;
return accessions;
}
/**
* Sets the accessions.
*
* @param accessions the new accessions
*/
public void setAccessionIds(List<AccessionId> accessions) {
this.accessions = accessions;
}
/**
* Gets the wiews code.
*
* @return the wiews code
*/
public String getWiewsCode() {
return wiewsCode;
}
/**
* Sets the wiews code.
*
* @param wiewsCode the new wiews code
*/
public void setWiewsCode(String wiewsCode) {
this.wiewsCode = wiewsCode;
}
/**
* Gets the institute.
*
* @return the institute
*/
public FaoInstitute getInstitute() {
return institute;
}
/**
* Sets the institute.
*
* @param institute the new institute
*/
public void setInstitute(FaoInstitute institute) {
this.institute = institute;
}
/**
* Sets the accessionIds list.
* Get accession count.
*
* @param accessionIds the new accessionIds list
* @return the accession count
*/
public void setAccessionIds(final List<AccessionId> accessionIds) {
this.accessionIds = accessionIds;
public int getAccessionCount() {
return this.accessionCount;
}
}
......@@ -18,7 +18,6 @@ package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.genesys2.server.model.genesys.AccessionId;
import org.springframework.data.domain.Pageable;
......@@ -36,6 +35,4 @@ public interface AccessionIdRepository extends JpaRepository<AccessionId, Long>
@Query("select id from AccessionId")
Collection<Long> findAllIds();
AccessionId findByUuid(UUID uuid);
}
......@@ -16,16 +16,21 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
public interface AccessionRepositoryCustom {
List<AccessionData> findActiveAndHistoric(Collection<UUID> accessionUuids);
List<Accession> find(List<Accession> accessions);
List<Accession> findById(List<? extends AccessionIdentifier3> identifiers);
......
......@@ -17,10 +17,12 @@
package org.genesys2.server.persistence.domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
......@@ -33,6 +35,8 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -215,4 +219,31 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
return em.createQuery(cq).getSingleResult();
}
@Override
public List<AccessionData> findActiveAndHistoric(Collection<UUID> accessionUuids) {
if (accessionUuids.isEmpty()) {
return Collections.emptyList();
}
List<AccessionData> activeAndHistoric=new ArrayList<>(accessionUuids.size());
CriteriaQuery<Accession> cq = criteriaBuilder.createQuery(Accession.class);
Root<Accession> active = cq.from(Accession.class);
cq.distinct(true);
cq.select(active);
cq.where(active.get("accessionId").get("uuid").in(accessionUuids));
activeAndHistoric.addAll(em.createQuery(cq).getResultList());
CriteriaQuery<AccessionHistoric> cqhist = criteriaBuilder.createQuery(AccessionHistoric.class);
Root<AccessionHistoric> historic = cqhist.from(AccessionHistoric.class);
cqhist.distinct(true);
cqhist.select(historic);
cqhist.where(historic.get("accessionId").get("uuid").in(accessionUuids));
activeAndHistoric.addAll(em.createQuery(cqhist).getResultList());
return activeAndHistoric;
}
}
/**
/*
* Copyright 2018 Global Crop Diversity Trust
* <p/>
*
* 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
* <p/>
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
*
* 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 org.genesys2.server.persistence.domain;
import java.util.UUID;
......
......@@ -249,4 +249,6 @@ public interface GenesysService {
Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable page);
List<AccessionData> listActiveAndHistoric(List<UUID> accessionUuids);
}
/**
/*
* Copyright 2018 Global Crop Diversity Trust
* <p/>
*
* 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
* <p/>
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
*
* 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 org.genesys2.server.service;
import java.util.Set;
......@@ -20,7 +21,6 @@ import java.util.UUID;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -91,18 +91,18 @@ public interface SubsetService {
* Method removing preserved accessionIds from Subset.
*
* @param input current Subset
* @param accesionsUuid the accessions UUIDs
* @param accesionsUuids the accessions UUIDs
* @return updated Subset in db.
*/
Subset removeAccessions(Subset input, Set<UUID> accesionsUuid);
Subset removeAccessions(Subset input, Set<UUID> accesionsUuids);
/**
* Method adding new accessionId to Subset.
*
* @param input current Subset
* @param accessionIds the accessions IDs
* @param accessionUuids the accessions IDs
* @return updated Subset in db.
*/
Subset addAccessions(Subset input, Set<AccessionHeaderJson> accessionIds);
Subset addAccessions(Subset input, Set<UUID> accessionUuids);
}
/**
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -12,24 +12,25 @@
* 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 org.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QSubset.subset;
import java.util.Set;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
/**
* The Class SubsetFilter.
*
* @author Maxym Borodenko
*/
public class SubsetFilter extends AuditedVersionedModelFilter {
public class SubsetFilter extends UuidModelFilter {
/** The published. */
public Boolean published;
......@@ -49,6 +50,9 @@ public class SubsetFilter extends AuditedVersionedModelFilter {
/** The rights. */
public Set<String> rights;
/** Institutes */
public Set<String> institutes;
/**
* Builds the query.
*
......@@ -58,6 +62,9 @@ public class SubsetFilter extends AuditedVersionedModelFilter {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(subset._super, and);
if ((institutes != null) && !institutes.isEmpty()) {
and.and(subset.institute.code.in(institutes));
}
if (published != null) {
and.and(subset.published.eq(published));
}
......
......@@ -1578,4 +1578,9 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return accessionRepository.lastUpdatedStatistics(faoInstitute, historic);
}
@Override
public List<AccessionData> listActiveAndHistoric(List<UUID> accessionUuids) {
return accessionRepository.findActiveAndHistoric(accessionUuids);
}
}
/**
/*
* Copyright 2018 Global Crop Diversity Trust
* <p/>
*
* 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
* <p/>
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
*
* 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 org.genesys2.server.service.impl;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.domain.AccessionIdRepository;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.FaoInstituteRepository;
import org.genesys2.server.persistence.domain.SubsetRepository;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -55,9 +53,6 @@ public class SubsetServiceImpl implements SubsetService {
@Autowired
private SubsetRepository subsetRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
@Autowired
private AccessionRepository accessionRepository;
......@@ -75,7 +70,7 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Create Subset.");
final Subset subset = new Subset();
copyValues(subset, source);
copyAccessionIds(subset, source.getAccessionIds());
copyAccessions(subset, source.getAccessionIds());
return deepLoad(subsetRepository.save(subset));
}
......@@ -83,21 +78,20 @@ public class SubsetServiceImpl implements SubsetService {
* Copy subset accessions.
*
* @param target the target
* @param accessionIds the subset accessions
* @param list the subset accessions
*/
private void copyAccessionIds(final Subset target, final List<AccessionId> accessionIds) {
if ((accessionIds == null) || (accessionIds.size() == 0)) {
private void copyAccessions(final Subset target, final List<AccessionId> list) {
if ((list == null) || (list.size() == 0)) {
if (target.getAccessionIds() != null)
target.getAccessionIds().clear();
return;
}
final List<Long> ids = accessionIds.stream().map(BasicModel::getId).collect(Collectors.toList());
final List<AccessionId> accessionIdList = accessionIdRepository.findAll(ids);
if (target.getAccessionIds() != null && !target.getAccessionIds().isEmpty()) {
target.getAccessionIds().clear();
target.getAccessionIds().addAll(accessionIdList.stream().distinct().collect(Collectors.toList()));
target.getAccessionIds().addAll(list.stream().distinct().collect(Collectors.toList()));
} else {
target.setAccessionIds(accessionIdList.stream().distinct().collect(Collectors.toList()));
target.setAccessionIds(list.stream().distinct().collect(Collectors.toList()));
}
}
......@@ -109,6 +103,7 @@ public class SubsetServiceImpl implements SubsetService {
* @param source the source
*/
private void copyValues(final Subset target, final Subset source) {
target.setInstitute(instituteRepository.findByCode(source.getWiewsCode()));
target.setTitle(source.getTitle());
target.setPublished(source.isPublished());
target.setDescription(source.getDescription());
......@@ -128,6 +123,7 @@ public class SubsetServiceImpl implements SubsetService {
if (subset == null)
throw new NotFoundElement("No such subset");
subset.getInstitute().getId();
if (subset.getAccessionIds() != null)
subset.getAccessionIds().size();
return subset;
......@@ -169,7 +165,7 @@ public class SubsetServiceImpl implements SubsetService {
final Subset subset = loadSubset(source);
subset.getAccessionIds().clear();
copyAccessionIds(subset, source.getAccessionIds());
copyAccessions(subset, source.getAccessionIds());
copyValues(subset, source);
return deepLoad(subsetRepository.save(subset));
......@@ -189,11 +185,10 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Remove accessions of subset {}.", input);
final Subset subset = loadSubset(input);
final Set<AccessionId> accessions = accessionsUuids.stream().map(accessionUuid -> accessionIdRepository.findByUuid(accessionUuid)).filter(Objects::nonNull).collect(
Collectors.toSet());
final List<AccessionData> accessions = accessionRepository.findActiveAndHistoric(accessionsUuids);
// Which UUIDs to remove?
final Set<UUID> uuidsToRemove = accessions.stream().map(AccessionId::getUuid).collect(Collectors.toSet());
final Set<UUID> uuidsToRemove = accessions.stream().map(AccessionData::getUuid).collect(Collectors.toSet());
// Keep accessions that are not in the list
subset.setAccessionIds(subset.getAccessionIds().stream().filter(accessionId -> !uuidsToRemove.contains(accessionId.getUuid())).collect(Collectors.toList()));
......@@ -203,26 +198,20 @@ public class SubsetServiceImpl implements SubsetService {
@Override