Removing old implementation of DescriptionTemplateType entity, fixing issues with entity scanning and field resolving on new implementation

This commit is contained in:
Thomas Georgios Giannos 2023-10-04 12:46:06 +03:00
parent 7c96078570
commit 074e374d9a
20 changed files with 66 additions and 450 deletions

View File

@ -1,31 +1,31 @@
package eu.eudat.data;
import jakarta.persistence.*;
import org.hibernate.annotations.GenericGenerator;
import jakarta.persistence.*;
import java.util.UUID;
@Entity
@Table(name = "DescriptionTemplateType")
@Table(name = "\"DescriptionTemplateType\"")
public class DescriptionTemplateTypeEntity implements BaseEntity {
@Id
@GeneratedValue
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "ID", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
@Column(name = "\"ID\"", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
private UUID id;
public static final String _id = "ID";
public static final String _id = "id";
@Column(name = "Name", nullable = false)
@Column(name = "\"Name\"", nullable = false)
private String name;
public static final String _name = "Name";
public static final String _name = "name";
@Column(name = "Status", nullable = false)
@Column(name = "\"Status\"", nullable = false)
private Short status;
public static final String _status = "Status";
public static final String _status = "status";
public UUID getId() {
return id;

View File

@ -1,14 +1,17 @@
package eu.eudat.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.UUID;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DescriptionTemplateType {
private UUID id;
private String name;
private short status;
private Short status;
public UUID getId() {
return id;
@ -26,11 +29,11 @@ public class DescriptionTemplateType {
this.name = name;
}
public short getStatus() {
public Short getStatus() {
return status;
}
public void setStatus(short status) {
public void setStatus(Short status) {
this.status = status;
}

View File

@ -3,7 +3,6 @@ package eu.eudat.model.builder;
import eu.eudat.convention.ConventionService;
import gr.cite.tools.data.builder.Builder;
import gr.cite.tools.data.query.QueryBase;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
@ -36,6 +35,11 @@ public abstract class BaseBuilder<M, D> implements Builder {
public abstract List<M> build(FieldSet directives, List<D> data);
/**
* Provides all the fields that can be projected on the entity listings associated with this builder
*
* @return The field set
*/
public abstract FieldSet getFullFieldSet();
public <K> Map<K, M> asForeignKey(QueryBase<D> query, FieldSet directives, Function<M, K> keySelector) {

View File

@ -1,6 +1,5 @@
package eu.eudat.query;
import eu.eudat.commons.enums.DescriptionTemplateTypeStatus;
import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.query.lookup.DescriptionTemplateTypeLookup;
import eu.eudat.query.lookup.LookupAware;
@ -73,9 +72,12 @@ public class DescriptionTemplateTypeQuery extends QueryBase<DescriptionTemplateT
@Override
public DescriptionTemplateTypeQuery fromLookup(DescriptionTemplateTypeLookup lookup) {
if (lookup == null) return this;
if (lookup.getName() != null) names(lookup.getName());
if (lookup.getStatus() != null) statuses(DescriptionTemplateTypeStatus.fromLabel(lookup.getStatus()).getValue());
if (lookup == null)
return this;
if (lookup.getName() != null)
names(lookup.getName());
if (lookup.getStatus() != null)
statuses(lookup.getStatus());
return this;
}
@ -95,19 +97,22 @@ public class DescriptionTemplateTypeQuery extends QueryBase<DescriptionTemplateT
if (this.ids != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionTemplateTypeEntity._id));
for (UUID item : this.ids) inClause.value(item);
for (UUID item : this.ids)
inClause.value(item);
predicates.add(inClause);
}
if (this.names != null) {
CriteriaBuilder.In<String> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionTemplateTypeEntity._name));
for (String item : this.names) inClause.value(item);
for (String item : this.names)
inClause.value(item);
predicates.add(inClause);
}
if (this.statuses != null) {
CriteriaBuilder.In<Short> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionTemplateTypeEntity._status));
for (Short item : this.statuses) inClause.value(item);
for (Short item : this.statuses)
inClause.value(item);
predicates.add(inClause);
}

View File

@ -4,7 +4,9 @@ import gr.cite.tools.data.query.Lookup;
public class DescriptionTemplateTypeLookup extends Lookup {
private String name, status;
private String name;
private Short status;
public String getName() {
return name;
@ -14,11 +16,11 @@ public class DescriptionTemplateTypeLookup extends Lookup {
this.name = name;
}
public String getStatus() {
public Short getStatus() {
return status;
}
public void setStatus(String status) {
public void setStatus(Short status) {
this.status = status;
}

View File

@ -7,6 +7,8 @@ import eu.eudat.model.builder.DescriptionTemplateTypeBuilder;
import eu.eudat.query.DescriptionTemplateTypeQuery;
import eu.eudat.query.lookup.DescriptionTemplateTypeLookup;
import gr.cite.tools.data.builder.BuilderFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
@ -14,8 +16,6 @@ import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.List;
import java.util.UUID;
@ -51,6 +51,11 @@ public class DescriptionTemplateTypeService {
.build(null, query.ids(id).first());
}
public DescriptionTemplateTypeEntity getEntityByName(String name) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
return query.names(name).first();
}
public DescriptionTemplateType create(DescriptionTemplateType payload) {
DescriptionTemplateTypeEntity created = new DescriptionTemplateTypeEntity();
created.setId(UUID.randomUUID());
@ -89,9 +94,10 @@ public class DescriptionTemplateTypeService {
return builderFactory.builder(DescriptionTemplateTypeBuilder.class).build(null, entity);
}
public boolean delete (UUID id) {
public boolean delete(UUID id) {
DescriptionTemplateTypeEntity entity = entityManager.find(DescriptionTemplateTypeEntity.class, id);
if (entity == null) return false;
if (entity == null)
return false;
entity.setStatus(DescriptionTemplateTypeStatus.DELETED.getValue());
entityManager.merge(entity);
entityManager.flush();

View File

@ -1,9 +1,9 @@
package eu.eudat.data.dao.entities;
import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.data.dao.DatabaseAccessLayer;
import eu.eudat.data.dao.criteria.DatasetProfileCriteria;
import eu.eudat.data.entities.DescriptionTemplate;
import eu.eudat.data.entities.DescriptionTemplateType;
import eu.eudat.queryable.QueryableList;
import java.util.List;
@ -19,6 +19,6 @@ public interface DatasetProfileDao extends DatabaseAccessLayer<DescriptionTempla
List<DescriptionTemplate> getAllIds();
Long countWithType(DescriptionTemplateType type);
Long countWithType(DescriptionTemplateTypeEntity type);
}

View File

@ -1,10 +1,10 @@
package eu.eudat.data.dao.entities;
import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.data.dao.DatabaseAccess;
import eu.eudat.data.dao.criteria.DatasetProfileCriteria;
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
import eu.eudat.data.entities.DescriptionTemplate;
import eu.eudat.data.entities.DescriptionTemplateType;
import eu.eudat.queryable.QueryableList;
import eu.eudat.queryable.types.FieldSelectionType;
import eu.eudat.queryable.types.SelectionField;
@ -128,7 +128,7 @@ public class DatasetProfileDaoImpl extends DatabaseAccess<DescriptionTemplate> i
}
@Override
public Long countWithType(DescriptionTemplateType type) {
public Long countWithType(DescriptionTemplateTypeEntity type) {
return this.getDatabaseService().getQueryable(DescriptionTemplate.class).where((builder, root) -> builder.equal(root.get("type"), type)).count();
}
}

View File

@ -1,10 +0,0 @@
package eu.eudat.data.dao.entities;
import eu.eudat.data.dao.DatabaseAccessLayer;
import eu.eudat.data.entities.DescriptionTemplateType;
import java.util.UUID;
public interface DescriptionTemplateTypeDao extends DatabaseAccessLayer<DescriptionTemplateType, UUID> {
DescriptionTemplateType findFromName(String name);
}

View File

@ -1,65 +0,0 @@
package eu.eudat.data.dao.entities;
import eu.eudat.data.dao.DatabaseAccess;
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
import eu.eudat.data.entities.DescriptionTemplateType;
import eu.eudat.queryable.QueryableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import jakarta.transaction.Transactional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@Component("descriptionTemplateTypeDao")
public class DescriptionTemplateTypeDaoImpl extends DatabaseAccess<DescriptionTemplateType> implements DescriptionTemplateTypeDao {
@Autowired
public DescriptionTemplateTypeDaoImpl(DatabaseService<DescriptionTemplateType> databaseService) {
super(databaseService);
}
@Override
public DescriptionTemplateType findFromName(String name){
try {
return this.getDatabaseService().getQueryable(DescriptionTemplateType.class).where((builder, root) -> builder.equal(root.get("name"), name)).getSingle();
}
catch(Exception e){
return null;
}
}
@Override
@Transactional
public DescriptionTemplateType createOrUpdate(DescriptionTemplateType item) {
return this.getDatabaseService().createOrUpdate(item, DescriptionTemplateType.class);
}
@Override
public DescriptionTemplateType find(UUID id) {
return getDatabaseService().getQueryable(DescriptionTemplateType.class).where((builder, root) -> builder.equal((root.get("id")), id)).getSingle();
}
@Override
public void delete(DescriptionTemplateType item) {
this.getDatabaseService().delete(item);
}
@Override
public QueryableList<DescriptionTemplateType> asQueryable() {
return this.getDatabaseService().getQueryable(DescriptionTemplateType.class).where((builder, root) -> builder.notEqual((root.get("status")), DescriptionTemplateType.Status.DELETED.getValue()));
}
@Async
@Override
public CompletableFuture<DescriptionTemplateType> createOrUpdateAsync(DescriptionTemplateType item) {
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
}
@Override
public DescriptionTemplateType find(UUID id, String hint) {
throw new UnsupportedOperationException();
}
}

View File

@ -1,6 +1,7 @@
package eu.eudat.data.entities;
import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.data.converters.DateToUTCConverter;
import eu.eudat.queryable.queryableentity.DataEntity;
import org.hibernate.annotations.GenericGenerator;
@ -87,7 +88,7 @@ public class DescriptionTemplate implements DataEntity<DescriptionTemplate,UUID>
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "\"Type\"", nullable = false)
private DescriptionTemplateType type;
private DescriptionTemplateTypeEntity type;
@OneToMany(mappedBy = "descriptionTemplate", fetch = FetchType.LAZY)
private Set<UserDatasetProfile> users;
@ -162,10 +163,10 @@ public class DescriptionTemplate implements DataEntity<DescriptionTemplate,UUID>
this.language = language;
}
public DescriptionTemplateType getType() {
public DescriptionTemplateTypeEntity getType() {
return type;
}
public void setType(DescriptionTemplateType type) {
public void setType(DescriptionTemplateTypeEntity type) {
this.type = type;
}

View File

@ -1,89 +0,0 @@
package eu.eudat.data.entities;
import eu.eudat.queryable.queryableentity.DataEntity;
import org.hibernate.annotations.GenericGenerator;
import jakarta.persistence.*;
import java.util.List;
import java.util.UUID;
@Entity
@Table(name = "\"DescriptionTemplateType\"")
public class DescriptionTemplateType implements DataEntity<DescriptionTemplateType, UUID> {
public enum Status {
SAVED((short) 0), FINALIZED((short) 1), DELETED((short) 99);
private short value;
private Status(short value) {
this.value = value;
}
public short getValue() {
return value;
}
public static Status fromInteger(int value) {
switch (value) {
case 0:
return SAVED;
case 1:
return FINALIZED;
case 99:
return DELETED;
default:
throw new RuntimeException("Unsupported Description Template Type Status");
}
}
}
@Id
@GeneratedValue
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "\"ID\"", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
private UUID id;
@Column(name = "\"Name\"", nullable = false)
private String name;
@Column(name = "\"Status\"", nullable = false)
private Short status;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getStatus() {
return status;
}
public void setStatus(Short status) {
this.status = status;
}
@Override
public void update(DescriptionTemplateType entity) {
this.name = entity.name;
this.status = entity.status;
}
@Override
public UUID getKeys() {
return this.id;
}
@Override
public DescriptionTemplateType buildFromTuple(List<Tuple> tuple, List<String> fields, String base) {
this.id = UUID.fromString((String) tuple.get(0).get(!base.isEmpty() ? base + "." + "id" : "id"));
return this;
}
}

View File

@ -1,7 +1,9 @@
package eu.eudat;
import eu.eudat.data.BaseEntity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;

View File

@ -1,13 +1,11 @@
package eu.eudat.configurations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -16,13 +14,14 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@Profile("devel")
@ComponentScan(basePackages = {"eu.eudat.data.entities"})
@ComponentScan(basePackages = {"eu.eudat.data"})
public class DevelDatabaseConfiguration {
@Autowired
@ -32,7 +31,7 @@ public class DevelDatabaseConfiguration {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"eu.eudat.data.entities"});
em.setPackagesToScan("eu.eudat.data");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());

View File

@ -19,6 +19,7 @@ import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.service.DescriptionTemplateTypeService;
import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.MetricNames;
import org.springframework.beans.factory.annotation.Autowired;
@ -46,13 +47,16 @@ public class Admin extends BaseController {
private ConfigLoader configLoader;
private final MetricsManager metricsManager;
private final DescriptionTemplateTypeService descriptionTemplateTypeService;
@Autowired
public Admin(ApiContext apiContext, DatasetProfileManager datasetProfileManager, UserManager userManager/*, Logger logger*/, ConfigLoader configLoader, MetricsManager metricsManager) {
public Admin(ApiContext apiContext, DatasetProfileManager datasetProfileManager, UserManager userManager/*, Logger logger*/, ConfigLoader configLoader, MetricsManager metricsManager, DescriptionTemplateTypeService descriptionTemplateTypeService) {
super(apiContext);
this.datasetProfileManager = datasetProfileManager;
this.userManager = userManager;
this.configLoader = configLoader;
this.metricsManager = metricsManager;
this.descriptionTemplateTypeService = descriptionTemplateTypeService;
}
@Transactional
@ -61,7 +65,8 @@ public class Admin extends BaseController {
//this.getLoggerService().info(principal, "Admin Added Dataset Profile");
DatasetProfile shortenProfile = profile.toShort();
DescriptionTemplate modelDefinition = AdminManager.generateViewStyleDefinition(shortenProfile, getApiContext());
modelDefinition.setType(getApiContext().getOperationsContext().getDatabaseRepository().getDescriptionTemplateTypeDao().findFromName(profile.getType()));
// modelDefinition.setType(getApiContext().getOperationsContext().getDatabaseRepository().getDescriptionTemplateTypeDao().findFromName(profile.getType()));
modelDefinition.setType(descriptionTemplateTypeService.getEntityByName(profile.getType()));
modelDefinition.setGroupId(UUID.randomUUID());
modelDefinition.setVersion((short) 0);

View File

@ -1,94 +0,0 @@
package eu.eudat.controllers;
import eu.eudat.exceptions.descriptiontemplate.DescriptionTemplatesWithTypeException;
import eu.eudat.logic.managers.DescriptionTemplateTypeManager;
import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.data.descriptiontemplatetype.DescriptionTemplateTypeModel;
import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.models.data.security.Principal;
import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.Authorities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.transaction.Transactional;
import java.util.UUID;
import static eu.eudat.types.Authorities.ADMIN;
@RestController
@CrossOrigin
@RequestMapping(value = {"/api/descriptionTemplateType/"})
public class DescriptionTemplateTypeController extends BaseController {
private DescriptionTemplateTypeManager descriptionTemplateTypeManager;
@Autowired
public DescriptionTemplateTypeController(ApiContext apiContext, DescriptionTemplateTypeManager descriptionTemplateTypeManager){
super(apiContext);
this.descriptionTemplateTypeManager = descriptionTemplateTypeManager;
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"create"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionTemplateTypeModel>> create(@RequestBody DescriptionTemplateTypeModel type, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
try {
this.descriptionTemplateTypeManager.create(type);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created"));
}
catch(DescriptionTemplatesWithTypeException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage()));
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"update"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionTemplateTypeModel>> update(@RequestBody DescriptionTemplateTypeModel type, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
try {
this.descriptionTemplateTypeManager.update(type);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Updated"));
}
catch(DescriptionTemplatesWithTypeException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage()));
}
}
@RequestMapping(method = RequestMethod.GET, value = {"get"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DataTableData<DescriptionTemplateTypeModel>>> get(@ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws Exception {
DataTableData<DescriptionTemplateTypeModel> dataTable = this.descriptionTemplateTypeManager.get();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<DescriptionTemplateTypeModel>>().status(ApiMessageCode.NO_MESSAGE).payload(dataTable));
}
@RequestMapping(method = RequestMethod.GET, value = {"get/{id}"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionTemplateTypeModel>> getSingle(@PathVariable(value = "id") UUID id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws Exception {
try {
DescriptionTemplateTypeModel typeModel = this.descriptionTemplateTypeManager.getSingle(id);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.NO_MESSAGE).payload(typeModel));
}
catch(DescriptionTemplatesWithTypeException e){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage()));
}
}
@Transactional
@RequestMapping(method = RequestMethod.DELETE, value = {"/delete/{id}"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DescriptionTemplateTypeModel>> delete(@PathVariable(value = "id") UUID id, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
try{
this.descriptionTemplateTypeManager.delete(id);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Deleted"));
}
catch(DescriptionTemplatesWithTypeException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DescriptionTemplateTypeModel>().status(ApiMessageCode.UNSUCCESS_DELETE).message(e.getMessage()));
}
}
}

View File

@ -1,84 +0,0 @@
package eu.eudat.logic.managers;
import eu.eudat.data.entities.DescriptionTemplateType;
import eu.eudat.exceptions.descriptiontemplate.DescriptionTemplatesWithTypeException;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.models.data.descriptiontemplatetype.DescriptionTemplateTypeModel;
import eu.eudat.models.data.helpers.common.DataTableData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Component
public class DescriptionTemplateTypeManager {
private static final Logger logger = LoggerFactory.getLogger(DescriptionTemplateTypeManager.class);
private ApiContext apiContext;
private DatabaseRepository databaseRepository;
@Autowired
public DescriptionTemplateTypeManager(ApiContext apiContext, DatabaseRepository databaseRepository) {
this.apiContext = apiContext;
this.databaseRepository = databaseRepository;
}
public DataTableData<DescriptionTemplateTypeModel> get() {
List<DescriptionTemplateType> types = this.databaseRepository.getDescriptionTemplateTypeDao().asQueryable().toList();
List<DescriptionTemplateTypeModel> typesModelList = types.stream().map(type -> new DescriptionTemplateTypeModel().fromDataModel(type)).collect(Collectors.toList());
DataTableData<DescriptionTemplateTypeModel> dataTableData = new DataTableData<>();
dataTableData.setData(typesModelList);
dataTableData.setTotalCount((long) typesModelList.size());
return dataTableData;
}
public DescriptionTemplateTypeModel getSingle(UUID id) throws Exception {
DescriptionTemplateType type = this.databaseRepository.getDescriptionTemplateTypeDao().find(id);
if (type != null) {
return new DescriptionTemplateTypeModel().fromDataModel(type);
}
else {
throw new DescriptionTemplatesWithTypeException("No description template type found with this id");
}
}
public void create(DescriptionTemplateTypeModel type) throws Exception {
DescriptionTemplateType existed = this.databaseRepository.getDescriptionTemplateTypeDao().findFromName(type.getName());
if (existed == null) {
this.databaseRepository.getDescriptionTemplateTypeDao().createOrUpdate(type.toDataModel());
}
else {
throw new DescriptionTemplatesWithTypeException("There is already a description template type with that name.");
}
}
public void update(DescriptionTemplateTypeModel type) throws Exception {
DescriptionTemplateType existed = this.databaseRepository.getDescriptionTemplateTypeDao().findFromName(type.getName());
if (existed != null) {
this.databaseRepository.getDescriptionTemplateTypeDao().createOrUpdate(type.toDataModel());
}
else {
throw new DescriptionTemplatesWithTypeException("No description template type found.");
}
}
public void delete(UUID id) throws DescriptionTemplatesWithTypeException {
DescriptionTemplateType type = this.databaseRepository.getDescriptionTemplateTypeDao().find(id);
if (type != null) {
Long descriptionsWithType = this.databaseRepository.getDatasetProfileDao().countWithType(type);
if(descriptionsWithType == 0) {
type.setStatus(DescriptionTemplateType.Status.DELETED.getValue());
this.databaseRepository.getDescriptionTemplateTypeDao().createOrUpdate(type);
}
else{
throw new DescriptionTemplatesWithTypeException("This type can not deleted, because Descriptions are associated with it");
}
}
}
}

View File

@ -64,7 +64,5 @@ public interface DatabaseRepository {
EntityDoiDao getEntityDoiDao();
DescriptionTemplateTypeDao getDescriptionTemplateTypeDao();
<T> void detachEntity(T entity);
}

View File

@ -41,7 +41,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository {
private NotificationDao notificationDao;
private FileUploadDao fileUploadDao;
private EntityDoiDao entityDoiDao;
private DescriptionTemplateTypeDao descriptionTemplateTypeDao;
private EntityManager entityManager;
@ -340,16 +339,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository {
this.entityDoiDao = entityDoiDao;
}
@Override
public DescriptionTemplateTypeDao getDescriptionTemplateTypeDao() {
return descriptionTemplateTypeDao;
}
@Autowired
public void setDescriptionTemplateTypeDao(DescriptionTemplateTypeDao descriptionTemplateTypeDao) {
this.descriptionTemplateTypeDao = descriptionTemplateTypeDao;
}
public <T> void detachEntity(T entity) {
this.entityManager.detach(entity);
}

View File

@ -1,56 +0,0 @@
package eu.eudat.models.data.descriptiontemplatetype;
import eu.eudat.data.entities.DescriptionTemplateType;
import eu.eudat.models.DataModel;
import java.util.UUID;
public class DescriptionTemplateTypeModel implements DataModel<DescriptionTemplateType, DescriptionTemplateTypeModel> {
private UUID id;
private String name;
private short status;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getStatus() {
return status;
}
public void setStatus(short status) {
this.status = status;
}
@Override
public DescriptionTemplateTypeModel fromDataModel(DescriptionTemplateType entity) {
this.id = entity.getId();
this.name = entity.getName();
this.status = entity.getStatus();
return this;
}
@Override
public DescriptionTemplateType toDataModel() throws Exception {
DescriptionTemplateType descriptionTemplateType = new DescriptionTemplateType();
descriptionTemplateType.setId(this.id);
descriptionTemplateType.setName(this.name);
descriptionTemplateType.setStatus(this.status);
return descriptionTemplateType;
}
@Override
public String getHint() {
return null;
}
}