Commit 601a2a36 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Enable @Validation in the service layer

parent c4557538
......@@ -619,11 +619,6 @@
<artifactId>jna</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
......
......@@ -16,6 +16,7 @@
package org.genesys.catalog.model.dataset;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;
......@@ -112,10 +113,12 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
private List<DatasetLocation> locations;
/** The min start date of all locations */
@Size(max = 8)
@Column(length = 8)
private String startDate;
/** The max end date of all locations */
@Size(max = 8)
@Column(length = 8)
private String endDate;
......@@ -176,6 +179,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
private String source;
/** Information about time of creation of the dataset. */
@Size(max = 200)
@Column(nullable = true, length = 200)
private String created;
......
......@@ -21,6 +21,8 @@ import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -57,6 +59,8 @@ public class DatasetCreator extends UuidModel implements PublishValidationInterf
/** The full name. */
@PublishValidation
@NotNull
@Size(max = 200)
@Column(length = 200, nullable = false)
private String fullName;
......
......@@ -22,6 +22,7 @@ import org.genesys.catalog.service.PublishValidationInterface;
import org.genesys2.util.MCPDUtil;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -68,14 +69,17 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
private Double decimalLongitude;
/** ISO3 country code of the location */
@Size(max = 3)
@Column(length = 3)
private String countryCode;
/** The start date */
@Size(max = 8)
@Column(length = 8)
private String startDate;
/** The end date */
@Size(max = 8)
@Column(length = 8)
private String endDate;
......
......@@ -40,6 +40,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
/**
* The Interface DatasetService.
*/
......@@ -54,7 +56,7 @@ public interface DatasetService {
* @param input new Dataset
* @return saved Dataset in db.
*/
Dataset createDataset(Dataset input);
Dataset createDataset(@Valid Dataset input);
/**
* Method updating Dataset.
......@@ -62,7 +64,7 @@ public interface DatasetService {
* @param input new Dataset
* @return updated Dataset in db.
*/
Dataset updateDataset(Dataset input);
Dataset updateDataset(@Valid Dataset input);
/**
* Method updating accessionRefs in Dataset.
......@@ -71,7 +73,7 @@ public interface DatasetService {
* @param accessionRefs new accessionRefs
* @return updated Dataset in db.
*/
Dataset updateAccessionRefs(Dataset dataset, Set<AccessionRef> accessionRefs);
Dataset updateAccessionRefs(Dataset dataset, @Valid Set<AccessionRef> accessionRefs);
/**
* Method adding new descriptor to Dataset.
......@@ -219,7 +221,7 @@ public interface DatasetService {
* @return updated Dataset
* @throws NotFoundElement the not found element
*/
Dataset upsertAccessions(Dataset dataset, Set<AccessionRef> accessionRefs) throws NotFoundElement;
Dataset upsertAccessions(Dataset dataset, @Valid Set<AccessionRef> accessionRefs) throws NotFoundElement;
/**
* Puts the dataset into the Review state to be reviewed by admin.
......@@ -293,7 +295,7 @@ public interface DatasetService {
* @return created DatasetCreator
* @throws NotFoundElement throws if don't match the version
*/
DatasetCreator createDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement;
DatasetCreator createDatasetCreator(Dataset dataset, @Valid DatasetCreator input) throws NotFoundElement;
/**
* Remove DatasetCreator of Dataset.
......@@ -349,7 +351,7 @@ public interface DatasetService {
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
DatasetCreator updateDatasetCreator(Dataset dataset, DatasetCreator datasetCreator) throws NotFoundElement;
DatasetCreator updateDatasetCreator(Dataset dataset, @Valid DatasetCreator datasetCreator) throws NotFoundElement;
/**
* Autocomplete creators.
......@@ -368,7 +370,7 @@ public interface DatasetService {
* @return added DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation createLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
DatasetLocation createLocation(Dataset dataset, @Valid DatasetLocation input) throws NotFoundElement;
/**
* Method for remove DatasetLocation of Dataset.
......@@ -424,7 +426,7 @@ public interface DatasetService {
* @return updated location
* @throws NotFoundElement the not found element
*/
DatasetLocation updateLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
DatasetLocation updateLocation(Dataset dataset, @Valid DatasetLocation input) throws NotFoundElement;
/**
* Clear accession references across all Datasets for specified accessions.
......
......@@ -85,6 +85,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Lists;
......@@ -96,6 +97,7 @@ import com.querydsl.core.types.Predicate;
*/
@Service("catalogDatasetService")
@Transactional(readOnly = true)
@Validated
public class DatasetServiceImpl implements DatasetService {
/** The dataset versions repository. */
......
......@@ -35,7 +35,6 @@ import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
......@@ -85,7 +84,6 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@Column(length = 100)
protected String dateCreated;
/** The wiews code. */
@NotNull
@Size(max = 8)
......@@ -93,7 +91,6 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
private String wiewsCode;
/** The institute. */
@NotNull
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "institute_id", updatable = false)
@JsonView({ JsonViews.Public.class })
......@@ -106,7 +103,6 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
indexes = { @Index(columnList = "subsetId, instCode, acceNumb"), @Index(columnList = "subsetId, genus") })
@Field(type = FieldType.Object)
@JsonIgnore
@Valid
private Set<AccessionRef> accessionRefs;
/** The accession count. */
......
......@@ -21,6 +21,8 @@ import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -57,6 +59,8 @@ public class SubsetCreator extends UuidModel implements PublishValidationInterfa
/** The full name. */
@PublishValidation
@NotNull
@Size(max = 200)
@Column(length = 200, nullable = false)
private String fullName;
......
......@@ -28,6 +28,8 @@ import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.validation.Valid;
public interface KPIService {
KPIParameter save(KPIParameter parameter);
......@@ -38,7 +40,7 @@ public interface KPIService {
KPIParameter delete(KPIParameter parameter);
<T extends Dimension<?>> T save(T dimension);
<T extends Dimension<?>> T save(@Valid T dimension);
Dimension<?> getDimension(long id);
......@@ -48,7 +50,7 @@ public interface KPIService {
<T> Set<T> getValues(Dimension<T> loadedJpa);
Execution save(Execution execution);
Execution save(@Valid Execution execution);
Execution getExecution(long id);
......
......@@ -31,6 +31,8 @@ import org.genesys2.server.service.filter.SubsetFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.validation.Valid;
/**
* @author Maxym Borodenko
*/
......@@ -78,7 +80,7 @@ public interface SubsetService {
* @param source the source
* @return saved Subset in db.
*/
Subset create(FaoInstitute institute, Subset source);
Subset create(FaoInstitute institute, @Valid Subset source);
/**
* Load subset based on identifiers and version provided in the input.
......@@ -111,7 +113,7 @@ public interface SubsetService {
* @param subset new Subset
* @return updated Subset in db.
*/
Subset update(Subset subset);
Subset update(@Valid Subset subset);
/**
* Remove subset.
......@@ -175,7 +177,7 @@ public interface SubsetService {
* @return created SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator createSubsetCreator(Subset Subset, SubsetCreator input) throws NotFoundElement;
SubsetCreator createSubsetCreator(Subset Subset, @Valid SubsetCreator input) throws NotFoundElement;
/**
* Remove SubsetCreator of Subset.
......@@ -241,7 +243,7 @@ public interface SubsetService {
* @return the Subset creator
* @throws NotFoundElement the not found element
*/
SubsetCreator updateSubsetCreator(Subset Subset, SubsetCreator SubsetCreator) throws NotFoundElement;
SubsetCreator updateSubsetCreator(Subset Subset, @Valid SubsetCreator SubsetCreator) throws NotFoundElement;
/**
* Autocomplete creators.
......
......@@ -52,6 +52,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
......@@ -83,6 +84,7 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Lazy
@Autowired
private DatasetService datasetService;
......
......@@ -60,6 +60,7 @@ import org.springframework.validation.annotation.Validated;
@Service
@Transactional(readOnly = true)
@Validated
public class KPIServiceImpl implements KPIService {
public static final Logger LOG = LoggerFactory.getLogger(KPIServiceImpl.class);
......@@ -118,8 +119,7 @@ public class KPIServiceImpl implements KPIService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dimension, 'ADMINISTRATION')")
@Override
@Transactional
@Validated
public <T extends Dimension<?>> T save(@Valid T dimension) {
public <T extends Dimension<?>> T save(T dimension) {
LOG.debug("Persising dimension {}", dimension);
return lazyLoad(dimensionRepository.save(dimension));
}
......@@ -174,8 +174,7 @@ public class KPIServiceImpl implements KPIService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
@Override
@Transactional
@Validated
public Execution save(@Valid Execution execution) {
public Execution save(Execution execution) {
Execution target = executionRepository.findByName(execution.getName());
if (target == null) {
target = execution;
......
......@@ -69,7 +69,6 @@ import org.springframework.validation.annotation.Validated;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import javax.validation.Valid;
/**
* The Class SubsetServiceImpl.
......@@ -78,6 +77,7 @@ import javax.validation.Valid;
*/
@Service
@Transactional(readOnly = true)
@Validated
public class SubsetServiceImpl implements SubsetService {
private static final Logger LOG = LoggerFactory.getLogger(SubsetServiceImpl.class);
......@@ -152,8 +152,7 @@ public class SubsetServiceImpl implements SubsetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE')")
@Validated
public Subset create(final FaoInstitute institute, @Valid final Subset source) {
public Subset create(final FaoInstitute institute, final Subset source) {
LOG.info("Create Subset.");
if (! StringUtils.equals(institute.getCode(), source.getWiewsCode())) {
throw new InvalidApiUsageException("Institute code of the subset does not match the code of Institute");
......@@ -254,8 +253,7 @@ public class SubsetServiceImpl implements SubsetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source, 'WRITE')")
@Validated
public Subset update(@Valid final Subset source) {
public Subset update(final Subset source) {
LOG.info("Update Subset.");
final Subset subset = loadSubset(source);
copyValues(subset, source);
......
......@@ -42,6 +42,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
@Configuration
@Import({ HazelcastConfig.class, CommonConfig.class, SchedulerConfig.class, DatabaseConfig.class, TemplatingConfig.class, MailConfig.class, OAuth2ServerConfig.class, SecurityConfig.class, CacheConfig.class,
......@@ -127,4 +128,9 @@ public class ApplicationConfig {
// This provides static access to current application context
return new CurrentApplicationContext();
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
......@@ -208,11 +208,11 @@ public class SubsetServiceTest extends CatalogServiceTest {
private Subset createSubset(UUID uuid) {
subset = new Subset();
subset.setTitle("Subset");
subset.setWiewsCode("TEST");
subset.setWiewsCode("0000");
subset.setInstitute(faoInstitute);
subset.setUuid(uuid);
return subsetRepository.save(subset);
return subsetService.create(faoInstitute, subset);
}
private FaoInstitute createInstitute() {
......
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