62 lines
2.5 KiB
Java
62 lines
2.5 KiB
Java
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.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;
|
|
|
|
@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<DescriptionTemplateType> get() {
|
|
List<DescriptionTemplateType> types = this.databaseRepository.getDescriptionTemplateTypeDao().asQueryable().toList();
|
|
DataTableData<DescriptionTemplateType> dataTableData = new DataTableData<>();
|
|
dataTableData.setData(types);
|
|
dataTableData.setTotalCount((long) types.size());
|
|
return dataTableData;
|
|
}
|
|
|
|
public DescriptionTemplateType create(String name) {
|
|
DescriptionTemplateType type = this.databaseRepository.getDescriptionTemplateTypeDao().findFromName(name);
|
|
if (type == null) {
|
|
type = new DescriptionTemplateType();
|
|
type.setId(UUID.randomUUID());
|
|
type.setName(name);
|
|
this.databaseRepository.getDescriptionTemplateTypeDao().createOrUpdate(type);
|
|
}
|
|
return type;
|
|
}
|
|
|
|
public void delete(UUID id) throws DescriptionTemplatesWithTypeException {
|
|
DescriptionTemplateType type = this.databaseRepository.getDescriptionTemplateTypeDao().find(id);
|
|
if (type != null) {
|
|
Long descriptionsWithType = this.databaseRepository.getDatasetProfileDao().countWithType(type.getId());
|
|
if(descriptionsWithType == 0) {
|
|
this.databaseRepository.getDescriptionTemplateTypeDao().delete(type);
|
|
}
|
|
else{
|
|
throw new DescriptionTemplatesWithTypeException("This type can not deleted, because Descriptions are associated with it");
|
|
}
|
|
}
|
|
}
|
|
}
|