Commit d1099c48 authored by Matija Obreza's avatar Matija Obreza Committed by Matija Obreza
Browse files

AccessionList custom repository

parent 43c94803
......@@ -17,6 +17,7 @@ import org.genesys2.server.model.genesys.AccessionExchange;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.util.MCPDUtil;
......@@ -92,6 +93,9 @@ public class AccessionDetails {
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
private String acceUrl;
@Field(type = FieldType.Nested)
private Set<AccessionList> lists;
public static AccessionDetails from(AccessionData accession) {
AccessionDetails ad = new AccessionDetails();
ad.version = accession.getVersion();
......@@ -112,6 +116,7 @@ public class AccessionDetails {
ad.art15 = accession.getInTrust();
ad.sampStat = accession.getSampleStatus();
ad.storage = new ArrayList<Integer>(accession.getStoRage());
ad.lists = accession.getAccessionId().getLists();
ad.taxonomy = Taxonomy.from(accession.getTaxonomy());
ad.acceUrl = accession.getAcceUrl();
......
......@@ -17,6 +17,7 @@
package org.genesys2.server.model.genesys;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Column;
......@@ -24,6 +25,9 @@ import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
......@@ -31,6 +35,7 @@ import javax.persistence.Table;
import org.genesys2.server.model.IdUUID;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.impl.AccessionList;
/**
* Entity holds the assigned accession identifiers regardless of active or
......@@ -55,6 +60,10 @@ public class AccessionId extends VersionedAuditedModel implements IdUUID {
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "accession", orphanRemoval = false)
private List<AccessionTrait> traits;
@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "accelistitems", joinColumns = @JoinColumn(name = "acceid") , inverseJoinColumns = @JoinColumn(name = "listid") )
private Set<AccessionList> lists;
@PrePersist
private void prepersist() {
if (uuid == null) {
......@@ -78,6 +87,14 @@ public class AccessionId extends VersionedAuditedModel implements IdUUID {
public void setPdci(PDCI pdci) {
this.pdci = pdci;
}
public Set<AccessionList> getLists() {
return lists;
}
public void setLists(Set<AccessionList> lists) {
this.lists = lists;
}
//
// public List<AccessionTrait> getTraits() {
// return traits;
......
......@@ -24,26 +24,29 @@ import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Entity
@Table(name = "accelist")
public class AccessionList extends VersionedAuditedModel implements AclAwareModel {
private static final long serialVersionUID = 991886970995006680L;
@Column(name = "uuid", unique = true, nullable = false, updatable = false)
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
@Column(name = "uuid", unique = true, nullable = false, updatable = false, columnDefinition = "binary(16)")
protected UUID uuid;
@OneToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "accelistitems")
@JoinColumn(name = "listId")
private Set<Accession> accessions;
@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "accelistitems", joinColumns = @JoinColumn(name = "listid"), inverseJoinColumns = @JoinColumn(name="acceid"))
private Set<AccessionId> accessionIds;
@Column(name = "title", nullable = false)
private String title;
......@@ -69,12 +72,12 @@ public class AccessionList extends VersionedAuditedModel implements AclAwareMode
super();
}
public Set<Accession> getAccessions() {
return accessions;
protected Set<AccessionId> getAccessionIds() {
return accessionIds;
}
public void setAccessions(Set<Accession> accessions) {
this.accessions = accessions;
protected void setAccessionIds(Set<AccessionId> accessionIds) {
this.accessionIds = accessionIds;
}
public String getDescription() {
......
/**
* Copyright 2015 Global Crop Diversity Trust
*
* 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 org.genesys2.server.persistence.domain;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
public interface AccessionListCustomRepository {
int addAll(AccessionList loaded, AppliedFilters filters);
int removeAll(AccessionList accessionList);
}
......@@ -18,17 +18,33 @@ package org.genesys2.server.persistence.domain;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.AccessionList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionListRepository extends JpaRepository<AccessionList, Long> {
public interface AccessionListRepository extends JpaRepository<AccessionList, Long>, AccessionListCustomRepository {
@Query("from AccessionList al where al.createdBy=?1")
List<AccessionList> listByOwner(Long userId);
@Query("select a.id from AccessionList al join al.accessions a where al.id=?1")
Set<Long> getAccessionIds(long listId);
@Query("select a.id from AccessionList al join al.accessionIds a where al = ?1")
Set<Long> getAccessionIds(AccessionList list);
AccessionList findByUuid(UUID uuid);
@Query(nativeQuery = true, value = "insert into accelistitems (listid, acceid) values (?1, ?2)")
@Modifying
void addOne(AccessionList list, AccessionId accession);
@Query(nativeQuery = true, value = "insert into accelistitems (listid, acceid) values (?1, ?2)")
@Modifying
void addOne(AccessionList acceList, long acceId);
@Query("select count(*) from AccessionList al where al = ?1")
int sizeOf(AccessionList list);
}
/**
* Copyright 2015 Global Crop Diversity Trust
*
* 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 org.genesys2.server.persistence.domain;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.service.impl.DirectMysqlQuery;
import org.genesys2.server.service.impl.DirectMysqlQuery.MethodResolver;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class AccessionListRepositoryCustomImpl implements AccessionListCustomRepository {
public static final Log LOG = LogFactory.getLog(AccessionListRepositoryCustomImpl.class);
@PersistenceContext
private EntityManager entityManager;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private MethodRepository methodRepository;
@Override
public int addAll(AccessionList list, AppliedFilters filter) {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accession", "a");
directQuery.jsonFilter(filter, new MethodResolver() {
@Override
public Method getMethod(final long methodId) {
return AccessionListRepositoryCustomImpl.this.methodRepository.findOne(methodId);
}
});
directQuery.sort(new Sort("acceNumb"));
Query q = entityManager.createNativeQuery("insert ignore into accelistitems (listid, acceid) (" + directQuery.getQuery("?, a.id") + ")");
q.setParameter(1, list.getId());
if (LOG.isDebugEnabled())
LOG.debug("Updating list: " + list.getUuid() + " id=" + list.getId());
int param = 2;
for (Object pv : directQuery.getParameters()) {
q.setParameter(param++, pv);
}
return q.executeUpdate();
}
@Override
public int removeAll(AccessionList accessionList) {
Query q = entityManager.createNativeQuery("delete from accelistitems ali where ali.listid = ?1");
q.setParameter(1, accessionList);
return q.executeUpdate();
}
}
......@@ -16,10 +16,14 @@
package org.genesys2.server.service;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
public interface AccessionListService {
......@@ -33,5 +37,17 @@ public interface AccessionListService {
List<AccessionList> getMyLists();
Set<Long> getAccessionIds(long listId);
Set<Long> getAccessionIds(AccessionList loaded);
AccessionList getList(UUID uuid);
void removeAll(AccessionList accessionList);
void addToList(AccessionList list, AccessionData accession);
void addToList(AccessionList acceList, Collection<Long> accessionIds);
void addToList(AccessionList loaded, AppliedFilters filters);
int sizeOf(AccessionList loaded);
}
\ No newline at end of file
......@@ -77,4 +77,6 @@ public interface FilterConstants {
public static final String SEQUENTIAL_NUMBER = "seqNo";
public static final String LIST = "list";
}
/**
* Copyright 2015 Global Crop Diversity Trust
*
* 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 org.genesys2.server.service.impl;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.persistence.domain.AccessionListRepository;
import org.genesys2.server.service.AccessionListService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.impl.FilterHandler.LiteralValueFilter;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
......@@ -17,6 +41,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class AccessionListServiceImpl implements AccessionListService {
private static final Logger LOG = Logger.getLogger(AccessionListServiceImpl.class);
@Autowired
AccessionListRepository accessionListRepository;
......@@ -28,7 +53,13 @@ public class AccessionListServiceImpl implements AccessionListService {
}
@Override
@PreAuthorize("isAuthenticated() and (#userAccessionList.id==null or hasRole('ADMINISTRATOR') or hasPermission(#userAccessionList, 'WRITE'))")
@PostAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'READ')")
public AccessionList getList(UUID uuid) {
return accessionListRepository.findByUuid(uuid);
}
@Override
@PreAuthorize("isAuthenticated() and (#accessionList.id==null or hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'WRITE'))")
@Transactional
public void save(AccessionList accessionList) {
accessionListRepository.save(accessionList);
......@@ -48,15 +79,57 @@ public class AccessionListServiceImpl implements AccessionListService {
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'DELETE')")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'WRITE')")
@Transactional
public void removeAll(AccessionList accessionList) {
accessionListRepository.removeAll(accessionList);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'WRITE')")
@Transactional
public void addToList(AccessionList list, AccessionData accession) {
accessionListRepository.addOne(list, accession.getAccessionId());
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'WRITE')")
@Transactional
public void addToList(AccessionList acceList, Collection<Long> accessionIds) {
if (LOG.isDebugEnabled())
LOG.debug("Adding a bunch " + accessionIds.size());
AppliedFilters filters = new AppliedFilters();
filters.add(new AppliedFilter().setFilterName(FilterConstants.ID).addFilterValues(LiteralValueFilter.class, accessionIds));
if (LOG.isDebugEnabled())
LOG.debug("Ready with query :-)");
accessionListRepository.addAll(acceList, filters);
if (LOG.isDebugEnabled())
LOG.debug("Bhwaha");
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'WRITE')")
@Transactional
public void addToList(AccessionList loaded, AppliedFilters filters) {
accessionListRepository.addAll(loaded, filters);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'DELETE')")
@Transactional
public void delete(AccessionList accessionList) {
accessionListRepository.delete(accessionList);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'DELETE')")
public Set<Long> getAccessionIds(long listId) {
return accessionListRepository.getAccessionIds(listId);
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#accessionList, 'DELETE')")
public Set<Long> getAccessionIds(AccessionList accessionList) {
return accessionListRepository.getAccessionIds(accessionList);
}
@Override
public int sizeOf(AccessionList list) {
return accessionListRepository.sizeOf(list);
}
}
......@@ -101,6 +101,11 @@ public class DirectMysqlQuery {
}
protected DirectMysqlQuery join(AppliedFilters filters) {
if (hasFilter(filters, FilterConstants.LIST)) {
innerJoin("accelistitem", "ali", "ali.acceid=a.id");
innerJoin("accelist", "al", "al.id=ali.listid");
}
if (filters.hasFilter(FilterConstants.CROPS) || filters.hasFilter(FilterConstants.TAXONOMY_GENUS)
|| filters.hasFilter(FilterConstants.TAXONOMY_SPECIES) || filters.hasFilter(FilterConstants.TAXONOMY_SUBTAXA)
|| filters.hasFilter(FilterConstants.TAXONOMY_SCINAME)) {
......@@ -159,6 +164,7 @@ public class DirectMysqlQuery {
createQuery(whereBuffer, "t.taxGenus", filters.get("genusId"), params);
createQuery(whereBuffer, "t.taxSpecies", filters.get("speciesId"), params);
}
createQuery(whereBuffer, "al.uuid", filters.get(FilterConstants.LIST), params);
createQuery(whereBuffer, "a.acceNumb", filters.get(FilterConstants.ACCENUMB), params);
createQuery(whereBuffer, "a.seqNo", filters.get(FilterConstants.SEQUENTIAL_NUMBER), params);
createQuery(whereBuffer, "a.orgCty", filters.get(FilterConstants.ORGCTY_ISO3), params);
......@@ -299,12 +305,12 @@ public class DirectMysqlQuery {
}
private int handleOperations(StringBuffer sb, String dbName, Set<FilterValue> set, List<Object> params) {
if (LOG.isDebugEnabled()) {
LOG.debug("Inspecting " + dbName + " ... " + set);
}
int counter = 0;
for (FilterValue filterValue : set) {
if (LOG.isDebugEnabled()) {
LOG.debug("Inspecting " + dbName + " ... " + filterValue);
}
if (filterValue instanceof ValueRangeFilter) {
ValueRangeFilter vrf = (ValueRangeFilter) filterValue;
......@@ -383,12 +389,12 @@ public class DirectMysqlQuery {
}
private int handleExplicitValues(StringBuffer sb, String dbName, Set<FilterValue> set, List<Object> params) {
if (LOG.isDebugEnabled()) {
LOG.debug("Addinfg " + dbName + " ... " + set.size());
}
int counter = 0;
for (FilterValue filterValue : set) {
if (LOG.isDebugEnabled()) {
LOG.debug("Inspecting " + dbName + " ... " + filterValue);
}
if (filterValue instanceof LiteralValueFilter) {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding " + filterValue);
......@@ -397,6 +403,7 @@ public class DirectMysqlQuery {
addParam(params, ((LiteralValueFilter) filterValue).getValue());
}
}
if (counter == 0) {
// Nothing..
} else if (counter == 1) {
......
......@@ -17,6 +17,7 @@
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
......@@ -86,6 +87,8 @@ public class FilterHandler {
this.availableFilters = new ArrayList<GenesysFilter>();
this.availableFilters.add(new BasicFilter(FilterConstants.CROPS, DataType.STRING));
this.availableFilters.add(new BasicFilter(FilterConstants.LIST, DataType.STRING));
this.availableFilters.add(new I18nListFilter<Integer>(FilterConstants.SAMPSTAT, DataType.NUMERIC).build("accession.sampleStatus", new Integer[] { 100,
110, 120, 130, 200, 300, 400, 410, 411, 412, 413, 414, 415, 416, 420, 421, 422, 423, 500, 600, 999 }));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_GENUS, "/explore/ac/" + FilterConstants.TAXONOMY_GENUS));
......@@ -539,6 +542,27 @@ public class FilterHandler {
}
return this.filterName;
}
public AppliedFilter addFilterValues(Class<LiteralValueFilter> clazz, Collection<Long> accessionIds) {
try {
for (long acceId : accessionIds) {
values.add(clazz.getDeclaredConstructor(Object.class).newInstance(acceId));
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return this;
}
}
@JsonSerialize(using = LiteralValueFilter.Serializer.class)
......@@ -561,6 +585,11 @@ public class FilterHandler {
return value;
}
@Override
public String toString() {
return getClass().getSimpleName() + "="+ getValue();
}
@Override
public int hashCode() {
final int prime = 31;
......@@ -641,6 +670,12 @@ public class FilterHandler {
return to;
}
@Override
public String toString() {
return getClass().getSimpleName() + " " + getFrom() + "<="+ getType() + "<=" + getTo();
}
@Override
public int hashCode() {
final int prime = 31;
......@@ -704,6 +739,11 @@ public class FilterHandler {
return to;
}
@Override
public String toString() {
return getClass().getSimpleName() + " " + getType() + ": " + getTo();
}
@Override
public int hashCode() {
final int prime = 31;
......@@ -761,6 +801,13 @@ public class FilterHandler {
return from;
}
@Override