argos/dmp-backend/core/src/main/java/eu/eudat/service/supportivematerial/SupportiveMaterialServiceIm...

131 lines
6.3 KiB
Java

package eu.eudat.service.supportivematerial;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.authorization.Permission;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.enums.SupportiveMaterialFieldType;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.SupportiveMaterialEntity;
import eu.eudat.data.TenantEntityManager;
import eu.eudat.model.SupportiveMaterial;
import eu.eudat.model.builder.SupportiveMaterialBuilder;
import eu.eudat.model.deleter.SupportiveMaterialDeleter;
import eu.eudat.model.persist.SupportiveMaterialPersist;
import eu.eudat.query.SupportiveMaterialQuery;
import eu.eudat.service.dmpblueprint.DmpBlueprintServiceImpl;
import eu.eudat.service.storage.StorageFileService;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
@Service
public class SupportiveMaterialServiceImpl implements SupportiveMaterialService{
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintServiceImpl.class));
private final TenantEntityManager entityManager;
private final AuthorizationService authorizationService;
private final DeleterFactory deleterFactory;
private final BuilderFactory builderFactory;
private final ConventionService conventionService;
private final MessageSource messageSource;
private final QueryFactory queryFactory;
private final SupportiveMaterialCacheService supportiveMaterialCacheService;
private final StorageFileService storageFileService;
public SupportiveMaterialServiceImpl(
TenantEntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory,
ConventionService conventionService, MessageSource messageSource, QueryFactory queryFactory,
SupportiveMaterialCacheService supportiveMaterialCacheService, StorageFileService storageFileService
) {
this.entityManager = entityManager;
this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory;
this.builderFactory = builderFactory;
this.conventionService = conventionService;
this.messageSource = messageSource;
this.queryFactory = queryFactory;
this.supportiveMaterialCacheService = supportiveMaterialCacheService;
this.storageFileService = storageFileService;
}
public byte[] loadFromFile(String language, SupportiveMaterialFieldType type) {
SupportiveMaterialCacheService.SupportiveMaterialCacheValue supportiveMaterialCacheItem = this.supportiveMaterialCacheService.lookup(this.supportiveMaterialCacheService.buildKey(language, type));
if(supportiveMaterialCacheItem == null){
byte[] content = this.storageFileService.getSupportiveMaterial(type, language);
if (content == null) throw new MyNotFoundException("Material not found");
supportiveMaterialCacheItem = new SupportiveMaterialCacheService.SupportiveMaterialCacheValue(language, type, content);
this.supportiveMaterialCacheService.put(supportiveMaterialCacheItem);
}
return supportiveMaterialCacheItem.getContent();
}
public SupportiveMaterial persist(SupportiveMaterialPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException{
logger.debug(new MapLogEntry("persisting data").And("model", model).And("fields", fields));
this.authorizationService.authorizeForce(Permission.EditSupportiveMaterial);
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
SupportiveMaterialEntity d;
if (isUpdate) {
d = this.entityManager.find(SupportiveMaterialEntity.class, model.getId());
if (d == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), SupportiveMaterial.class.getSimpleName()}, LocaleContextHolder.getLocale()));
} else {
List<SupportiveMaterialEntity> data = this.queryFactory.query(SupportiveMaterialQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).languageCodes(model.getLanguageCode()).types(model.getType()).collect();
if(data != null && !data.isEmpty()){
throw new MyApplicationException("Could not create a new Data with same type and lang code !");
}
d = new SupportiveMaterialEntity();
d.setId(UUID.randomUUID());
d.setIsActive(IsActive.Active);
d.setCreatedAt(Instant.now());
}
d.setType(model.getType());
d.setLanguageCode(model.getLanguageCode());
d.setPayload(model.getPayload());
d.setUpdatedAt(Instant.now());
if (isUpdate) this.entityManager.merge(d);
else this.entityManager.persist(d);
this.entityManager.flush();
return this.builderFactory.builder(SupportiveMaterialBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, SupportiveMaterial._id), d);
}
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
logger.debug("deleting : {}", id);
this.authorizationService.authorizeForce(Permission.DeleteSupportiveMaterial);
this.deleterFactory.deleter(SupportiveMaterialDeleter.class).deleteAndSaveByIds(List.of(id));
}
}