Commit 96d88513 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '137-descriptor-references' into 'master'

Resolve "Descriptor references"

Closes #137

See merge request !129
parents e38793dc 8c0c286e
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -42,6 +42,7 @@ import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.Publishable;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.vocab.ControlledVocabulary;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.common.model.Partner;
......@@ -203,6 +204,13 @@ public class Descriptor extends UuidModel implements SelfCleaning, Publishable,
@JsonIgnore
private List<DescriptorList> descriptorLists;
/** The datasets. */
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "datasetDescriptor", joinColumns = @JoinColumn(name = "descriptorId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"))
// @OrderColumn(name = "position")
@JsonIgnore
private List<Dataset> datasets;
/** The crop. */
@Column(name = "crop", length = 100, nullable = true)
private String crop;
......@@ -593,6 +601,24 @@ public class Descriptor extends UuidModel implements SelfCleaning, Publishable,
public void setDescriptorLists(final List<DescriptorList> descriptorLists) {
this.descriptorLists = descriptorLists;
}
/**
* Gets the datasets.
*
* @return the datasets
*/
public List<Dataset> getDatasets() {
return datasets;
}
/**
* Sets the datasets.
*
* @param datasets the new datasets
*/
public void setDatasets(List<Dataset> datasets) {
this.datasets = datasets;
}
/**
* Gets the crop.
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -18,7 +18,9 @@ package org.genesys.catalog.persistence.traits;
import java.util.List;
import java.util.UUID;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
......@@ -49,11 +51,20 @@ public interface DescriptorRepository extends JpaRepository<Descriptor, Long>, Q
Descriptor findByUuidAndVersion(UUID uuid, int version);
/**
* Load list by ids.
* List descriptor lists.
*
* @param ids the ids
* @param descriptor the descriptor
* @return the list
*/
@Query("select distinct d from Dataset d where d.id in :ids")
List<Descriptor> loadListByIds(@Param("ids") List<Long> ids);
@Query("select d.descriptorLists from Descriptor d where d = :descriptor")
List<DescriptorList> listDescriptorLists(@Param("descriptor") Descriptor descriptor);
/**
* List datasets.
*
* @param descriptor the descriptor
* @return the list
*/
@Query("select d.datasets from Descriptor d where d = :descriptor")
List<Dataset> listDatasets(@Param("descriptor") Descriptor descriptor);
}
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -18,11 +18,15 @@ package org.genesys.catalog.service;
import java.util.List;
import java.util.UUID;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.service.filters.DescriptorFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
/**
......@@ -62,9 +66,10 @@ public interface DescriptorService {
*
* @param uuid uuid of descriptor
* @return loaded descriptor
* @throws NotFoundElement if descriptor is not found
*/
@PostAuthorize("returnObject==null || returnObject.published || hasPermission(returnObject, 'read')")
Descriptor getDescriptor(UUID uuid);
Descriptor getDescriptor(UUID uuid) throws NotFoundElement;
/**
* Gets the descriptor by uuid and version.
......@@ -72,9 +77,10 @@ public interface DescriptorService {
* @param uuid the uuid
* @param version the version
* @return the descriptor
* @throws NotFoundElement if descriptor is not found
*/
@PostAuthorize("returnObject==null || returnObject.published || hasPermission(returnObject, 'read')")
Descriptor getDescriptor(UUID uuid, int version);
@PostAuthorize("returnObject==null || returnObject.published || hasPermission(returnObject, 'read')")
Descriptor getDescriptor(UUID uuid, int version) throws NotFoundElement;
/**
* Page with descriptor list by user.
......@@ -131,4 +137,24 @@ public interface DescriptorService {
@PreAuthorize("hasRole('ADMINISTRATOR')")
Descriptor unpublishDescriptor(Descriptor descriptor);
/**
* Gets the descriptor lists using specified descriptor
*
* @param descriptor the descriptor
* @return the descriptor lists
*/
@PreAuthorize("#descriptor.published || hasPermission(#descriptor, 'read')")
@PostFilter("filterObject==null || filterObject.published || hasPermission(filterObject, 'write')")
List<DescriptorList> getDescriptorLists(Descriptor descriptor);
/**
* Gets the datasets where descriptor is used
*
* @param descriptor the descriptor
* @return the datasets
*/
@PreAuthorize("#descriptor.published || hasPermission(#descriptor, 'read')")
@PostFilter("filterObject==null || filterObject.published || hasPermission(filterObject, 'write')")
List<Dataset> getDatasets(Descriptor descriptor);
}
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -22,10 +22,14 @@ import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.DataType;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.user.UserRole;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.persistence.traits.DescriptorRepository;
......@@ -69,6 +73,9 @@ public class DescriptorServiceImpl implements DescriptorService {
@Autowired
private Utils utils;
@Autowired
private EntityManager entityManager;
/**
* {@inheritDoc}
*/
......@@ -317,4 +324,30 @@ public class DescriptorServiceImpl implements DescriptorService {
loaded.setPublished(false);
return lazyLoad(descriptorRepository.save(loaded));
}
/*
* (non-Javadoc)
* @see
* org.genesys.catalog.service.DescriptorService#getDescriptorLists(org.genesys.
* catalog.model.traits.Descriptor)
*/
@Override
public List<DescriptorList> getDescriptorLists(Descriptor descriptor) {
List<DescriptorList> list = descriptorRepository.listDescriptorLists(descriptor);
list.forEach(d -> entityManager.detach(d));
return list;
}
/*
* (non-Javadoc)
* @see
* org.genesys.catalog.service.DescriptorService#getDescriptorLists(org.genesys.
* catalog.model.traits.Descriptor)
*/
@Override
public List<Dataset> getDatasets(Descriptor descriptor) {
List<Dataset> list = descriptorRepository.listDatasets(descriptor);
list.forEach(d -> entityManager.detach(d));
return list;
}
}
......@@ -18,8 +18,11 @@ package org.genesys.catalog.server.controller.api.v0;
import java.util.List;
import java.util.UUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.Category;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.filters.DescriptorFilter;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -37,6 +40,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
/**
* @author Maxym Borodenko
*/
......@@ -68,6 +73,25 @@ public class DescriptorController {
return descriptorService.getDescriptor(uuid);
}
/**
* Get additional information about descriptor that is not provided in the
* {@link #getDescriptor(UUID)} call
*
* @param uuid descriptor UUID
* @return
*/
@RequestMapping(value = "/extra/{UUID}", method = RequestMethod.GET)
@SuppressWarnings("unused")
@JsonView(JsonViews.Minimal.class)
public Object getOtherDescriptorInfo(@PathVariable("UUID") final UUID uuid) {
final Descriptor descriptor = descriptorService.getDescriptor(uuid);
return new Object() {
public List<DescriptorList> descriptorLists = descriptorService.getDescriptorLists(descriptor);
public List<Dataset> datasets = descriptorService.getDatasets(descriptor);
};
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated()")
public Descriptor createDescriptor(@RequestBody final Descriptor source) {
......
Supports Markdown
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