118 lines
5.3 KiB
Java
118 lines
5.3 KiB
Java
package eu.eudat.service.language;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.authorization.Permission;
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.convention.ConventionService;
|
|
import eu.eudat.data.LanguageEntity;
|
|
import eu.eudat.data.TenantEntityManager;
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
import eu.eudat.model.Language;
|
|
import eu.eudat.model.builder.LanguageBuilder;
|
|
import eu.eudat.model.deleter.LanguageDeleter;
|
|
import eu.eudat.model.persist.LanguagePersist;
|
|
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.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 org.hibernate.FlushMode;
|
|
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.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.Instant;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
public class LanguageServiceImpl implements LanguageService {
|
|
|
|
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 ErrorThesaurusProperties errors;
|
|
private final StorageFileService storageFileService;
|
|
|
|
|
|
public LanguageServiceImpl(
|
|
TenantEntityManager entityManager, AuthorizationService authorizationService, DeleterFactory deleterFactory, BuilderFactory builderFactory,
|
|
ConventionService conventionService, MessageSource messageSource, ErrorThesaurusProperties errors, StorageFileService storageFileService){
|
|
this.entityManager = entityManager;
|
|
this.authorizationService = authorizationService;
|
|
this.deleterFactory = deleterFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.conventionService = conventionService;
|
|
this.messageSource = messageSource;
|
|
this.errors = errors;
|
|
this.storageFileService = storageFileService;
|
|
}
|
|
|
|
|
|
public Language persist(LanguagePersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException{
|
|
logger.debug(new MapLogEntry("persisting data").And("model", model).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditLanguage);
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
LanguageEntity data;
|
|
if (isUpdate) {
|
|
((org.hibernate.Session) entityManager).setHibernateFlushMode(FlushMode.MANUAL);
|
|
data = this.entityManager.find(LanguageEntity.class, model.getId());
|
|
if (data == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Language.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
} else {
|
|
|
|
data = new LanguageEntity();
|
|
data.setId(UUID.randomUUID());
|
|
data.setIsActive(IsActive.Active);
|
|
data.setCreatedAt(Instant.now());
|
|
}
|
|
data.setCode(model.getCode());
|
|
data.setPayload(model.getPayload() != null && !model.getPayload().isEmpty() ? model.getPayload() : null);
|
|
data.setOrdinal(model.getOrdinal());
|
|
data.setUpdatedAt(Instant.now());
|
|
data.setIsActive(IsActive.Inactive);
|
|
// if (isUpdate) this.entityManager.merge(data);
|
|
// else this.entityManager.persist(data);
|
|
//
|
|
// this.entityManager.flush();
|
|
|
|
return this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, Language._id), data);
|
|
}
|
|
|
|
public String getPayload(String code) throws IOException {
|
|
this.authorizationService.authorizeForce(Permission.BrowseLanguage);
|
|
|
|
byte[] content = this.storageFileService.getLanguage(code);
|
|
|
|
return new String(content, StandardCharsets.UTF_8);
|
|
}
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
logger.debug("deleting : {}", id);
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteLanguage);
|
|
|
|
this.deleterFactory.deleter(LanguageDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
}
|
|
}
|