Commit 8252d12f authored by Matija Obreza's avatar Matija Obreza

Added missing controller from the Catalog

- fixed NPE in Dataset#prepersist
parent 8990167d
......@@ -16,6 +16,7 @@
package org.genesys.catalog.api.v0;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
......@@ -244,6 +245,13 @@ public class DatasetController {
return datasetService.addDescriptors(dataset, descriptors.toArray(new Descriptor[] {}));
}
@PostMapping(value = "/set-descriptors/{UUID},{version}")
public Dataset updateDescriptors(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final List<UUID> descriptorUuids) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
final List<Descriptor> descriptors = descriptorUuids.stream().map(descriptorUuid -> descriptorService.getDescriptor(descriptorUuid)).collect(Collectors.toList());
return datasetService.updateDescriptors(dataset, descriptors);
}
/**
* Removes the descriptors.
*
......
/*
* 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.
* 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.genesys.catalog.api.v0;
import java.util.UUID;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* The Class LocationController.
*
* @author Andrey Lugovskoy.
*/
@RestController
@RequestMapping(LocationController.CONTROLLER_URL)
public class LocationController {
/** The Constant API_BASE. */
protected static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/location";
/** The dataset service. */
@Autowired
protected DatasetService datasetService;
@Autowired
private LocationService locationService;
/**
* List location.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param uuid the uuid
* @return the page
*/
@GetMapping(value = "/list")
public Page<DatasetLocation> listLocation(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @PathVariable("UUID") final UUID uuid) {
return locationService.listLocation(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param locationUuid the location uuid
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/{locationUuid}")
public DatasetLocation loadByUuid(@PathVariable("locationUuid") final UUID locationUuid) throws NotFoundElement {
return locationService.loadLocation(locationUuid);
}
/**
* Creates the location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/create")
public DatasetLocation createLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.createLocation(dataset, datasetLocation);
}
/**
* Delete location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public DatasetLocation deleteLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.removeLocation(dataset, datasetLocation);
}
/**
* Update location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/update")
public DatasetLocation updateLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.updateLocation(dataset, datasetLocation);
}
}
......@@ -46,7 +46,7 @@ import java.util.Set;
* @author Maxim Borodenko
*/
@Entity
@Table(name="dataset")
@Table(name = "dataset")
@Cacheable
@Audited
@Document(indexName = "catalog")
......@@ -74,7 +74,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@CollectionTable(name = "dataset_accessions", joinColumns = @JoinColumn(name = "datasetId"),
// index
indexes = { @Index(columnList = "datasetId, instCode, acceNumb"), @Index(columnList = "datasetId, genus") })
@Field(type=FieldType.Object)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private Set<AccessionIdentifier> accessionIdentifiers;
......@@ -90,21 +90,21 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@OneToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "dataset_repositoryfile", joinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "repositoryfileId", referencedColumnName = "id"))
@OrderColumn(name = "position")
@Field(type=FieldType.Object)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<RepositoryFile> repositoryFiles;
/** The creators. */
@PublishValidation(innerCheck = true)
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "dataset", orphanRemoval = true, fetch = FetchType.LAZY)
@Field(type=FieldType.Object)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<DatasetCreator> creators;
/** The locations. */
@PublishValidation(innerCheck = true)
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "dataset", orphanRemoval = true, fetch = FetchType.LAZY)
@Field(type=FieldType.Object)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<DatasetLocation> locations;
......@@ -192,14 +192,13 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
this.descriptorCount = descriptors.size();
}
if (locations != null) {
this.startDate = locations.stream().map(DatasetLocation::getStartDate).min(String::compareTo).orElse(null);
this.endDate = locations.stream().map(DatasetLocation::getEndDate).max(String::compareTo).orElse(null);
this.startDate = locations.stream().map(DatasetLocation::getStartDate).filter(d -> d != null).min(String::compareTo).orElse(null);
this.endDate = locations.stream().map(DatasetLocation::getEndDate).filter(d -> d != null).max(String::compareTo).orElse(null);
}
trimStringsToNull();
}
/**
* Owner is the ACL parent object for the dataset
*/
......@@ -313,7 +312,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
* @param startDate the new start date
*/
public void setStartDate(String startDate) {
if(MCPDUtil.isMcpdDate(startDate)) {
if (MCPDUtil.isMcpdDate(startDate)) {
this.startDate = startDate;
}
}
......@@ -333,7 +332,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
* @param endDate the new start date
*/
public void setEndDate(String endDate) {
if(MCPDUtil.isMcpdDate(endDate)) {
if (MCPDUtil.isMcpdDate(endDate)) {
this.endDate = endDate;
}
}
......
......@@ -661,6 +661,16 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(loadedDataset.getLocations(), hasSize(1));
assertEquals(savedLocation.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation.getEndDate(), loadedDataset.getEndDate());
savedLocation.setEndDate(null);
savedLocation.setStartDate(null);
savedLocation = locationService.updateLocation(loadedDataset, savedLocation);
loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(1));
assertEquals(savedLocation.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation.getEndDate(), loadedDataset.getEndDate());
}
......
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