243 lines
13 KiB
Java
243 lines
13 KiB
Java
package gr.cite.notification.service.notificationtemplate;
|
|
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
|
import gr.cite.notification.authorization.AuthorizationFlags;
|
|
import gr.cite.notification.authorization.Permission;
|
|
import gr.cite.notification.common.JsonHandlingService;
|
|
import gr.cite.notification.common.enums.IsActive;
|
|
import gr.cite.notification.common.enums.NotificationTemplateChannel;
|
|
import gr.cite.notification.common.enums.NotificationTemplateKind;
|
|
import gr.cite.notification.common.types.notification.FieldInfo;
|
|
import gr.cite.notification.common.types.notificationtemplate.FieldInfoEntity;
|
|
import gr.cite.notification.common.types.notificationtemplate.FieldOptionsEntity;
|
|
import gr.cite.notification.common.types.notificationtemplate.NotificationTemplateValueEntity;
|
|
import gr.cite.notification.convention.ConventionService;
|
|
import gr.cite.notification.data.NotificationTemplateEntity;
|
|
import gr.cite.notification.data.TenantScopedEntityManager;
|
|
import gr.cite.notification.errorcode.ErrorThesaurusProperties;
|
|
import gr.cite.notification.model.Language;
|
|
import gr.cite.notification.model.NotificationTemplate;
|
|
import gr.cite.notification.model.builder.NotificationTemplateBuilder;
|
|
import gr.cite.notification.model.deleter.NotificationTemplateDeleter;
|
|
import gr.cite.notification.model.notificationtemplate.FieldOptions;
|
|
import gr.cite.notification.model.notificationtemplate.NotificationTemplateValue;
|
|
import gr.cite.notification.model.persist.NotificationTemplatePersist;
|
|
import gr.cite.notification.model.persist.notificationtemplate.FieldInfoPersist;
|
|
import gr.cite.notification.model.persist.notificationtemplate.FieldOptionsPersist;
|
|
import gr.cite.notification.model.persist.notificationtemplate.NotificationTemplateValuePersist;
|
|
import gr.cite.notification.query.NotificationTemplateQuery;
|
|
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 org.jetbrains.annotations.NotNull;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.context.annotation.RequestScope;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.time.Instant;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
@RequestScope
|
|
public class NotificationServiceTemplateImpl implements NotificationTemplateService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(NotificationServiceTemplateImpl.class));
|
|
private final TenantScopedEntityManager entityManager;
|
|
private final AuthorizationService authService;
|
|
private final AuthorizationService authorizationService;
|
|
private final DeleterFactory deleterFactory;
|
|
private final BuilderFactory builderFactory;
|
|
private final QueryFactory queryFactory;
|
|
private final ConventionService conventionService;
|
|
private final ErrorThesaurusProperties errors;
|
|
private final MessageSource messageSource;
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
@Autowired
|
|
public NotificationServiceTemplateImpl(
|
|
TenantScopedEntityManager entityManager,
|
|
AuthorizationService authService, AuthorizationService authorizationService,
|
|
DeleterFactory deleterFactory,
|
|
BuilderFactory builderFactory,
|
|
QueryFactory queryFactory, ConventionService conventionService,
|
|
ErrorThesaurusProperties errors,
|
|
MessageSource messageSource,
|
|
JsonHandlingService jsonHandlingService) {
|
|
this.entityManager = entityManager;
|
|
this.authService = authService;
|
|
this.authorizationService = authorizationService;
|
|
this.deleterFactory = deleterFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.queryFactory = queryFactory;
|
|
this.conventionService = conventionService;
|
|
this.errors = errors;
|
|
this.messageSource = messageSource;
|
|
this.jsonHandlingService = jsonHandlingService;
|
|
}
|
|
|
|
@Override
|
|
public NotificationTemplate persist(NotificationTemplatePersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
|
logger.debug(new MapLogEntry("persisting notification template").And("model", model).And("fields", fields));
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditNotificationTemplate);
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
NotificationTemplateEntity data = null;
|
|
if (isUpdate) {
|
|
data = this.entityManager.find(NotificationTemplateEntity.class, model.getId());
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), NotificationTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
if (model.getNotificationType() != null){
|
|
data.setNotificationType(model.getNotificationType());
|
|
}
|
|
} else {
|
|
data = new NotificationTemplateEntity();
|
|
data.setId(UUID.randomUUID());
|
|
data.setIsActive(IsActive.Active);
|
|
data.setCreatedAt(Instant.now());
|
|
data.setNotificationType(UUID.randomUUID());
|
|
}
|
|
|
|
data.setChannel(model.getChannel());
|
|
data.setKind(model.getKind());
|
|
data.setLanguageId(model.getLanguageId());
|
|
data.setValue(this.jsonHandlingService.toJsonSafe(this.buildNotificationTemplateValueEntity(model.getValue())));
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
else this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
|
|
|
return this.builderFactory.builder(NotificationTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, NotificationTemplate._id, NotificationTemplate._hash), data);
|
|
}
|
|
|
|
private @NotNull NotificationTemplateValueEntity buildNotificationTemplateValueEntity(NotificationTemplateValuePersist persist) {
|
|
NotificationTemplateValueEntity data = new NotificationTemplateValueEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setSubjectText(persist.getSubjectText());
|
|
data.setSubjectKey(persist.getSubjectKey());
|
|
if (!this.conventionService.isListNullOrEmpty(List.of(persist.getSubjectFieldOptions()))) {
|
|
data.setSubjectFieldOptions(this.buildFieldOptionsEntity(persist.getSubjectFieldOptions()));
|
|
}
|
|
data.setBodyText(persist.getBodyText());
|
|
data.setBodyKey(persist.getBodyKey());
|
|
data.setPriorityKey(persist.getPriorityKey());
|
|
data.setAllowAttachments(persist.getAllowAttachments());
|
|
data.setCc(persist.getCc());
|
|
data.setCcMode(persist.getCcMode());
|
|
data.setBcc(persist.getBcc());
|
|
data.setBccMode(persist.getBccMode());
|
|
data.setExtraDataKeys(persist.getExtraDataKeys());
|
|
if (!this.conventionService.isListNullOrEmpty(List.of(persist.getBodyFieldOptions()))) {
|
|
data.setBodyFieldOptions(this.buildFieldOptionsEntity(persist.getBodyFieldOptions()));
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull FieldOptionsEntity buildFieldOptionsEntity(FieldOptionsPersist persist) {
|
|
FieldOptionsEntity data = new FieldOptionsEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setMandatory(persist.getMandatory());
|
|
if (!this.conventionService.isListNullOrEmpty(persist.getOptional())) {
|
|
data.setOptional(new ArrayList<>());
|
|
for (FieldInfoPersist optionalPersist : persist.getOptional()) {
|
|
data.getOptional().add(this.buildFieldInfoEntity(optionalPersist));
|
|
}
|
|
}
|
|
data.setFormatting(persist.getFormatting());
|
|
|
|
return data;
|
|
}
|
|
|
|
private @NotNull FieldInfoEntity buildFieldInfoEntity(FieldInfoPersist persist) {
|
|
FieldInfoEntity data = new FieldInfoEntity();
|
|
if (persist == null) return data;
|
|
|
|
data.setKey(persist.getKey());
|
|
data.setType(persist.getType());
|
|
data.setValue(persist.getValue());
|
|
|
|
return data;
|
|
}
|
|
|
|
@Override
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
logger.debug("deleting notification template: {}", id);
|
|
this.authService.authorizeForce(Permission.DeleteNotificationTemplate);
|
|
this.deleterFactory.deleter(NotificationTemplateDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
}
|
|
|
|
@Override
|
|
public NotificationTemplate lookupOverriddenTemplates(UUID notificationType, NotificationTemplateChannel channel, UUID language) {
|
|
NotificationTemplateQuery query = this.queryFactory.query(NotificationTemplateQuery.class)
|
|
.notificationTypes(notificationType)
|
|
.channels(channel)
|
|
.languages(language)
|
|
.kinds(NotificationTemplateKind.Primary)
|
|
.isActive(IsActive.Active);
|
|
|
|
if (query.first() == null){
|
|
return null;
|
|
}
|
|
FieldSet fieldSet = new BaseFieldSet(
|
|
NotificationTemplate._id,
|
|
NotificationTemplate._channel,
|
|
NotificationTemplate._notificationType,
|
|
NotificationTemplate._kind,
|
|
|
|
NotificationTemplate._language + "." + Language._id,
|
|
NotificationTemplate._language + "." + Language._code,
|
|
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectKey,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectText,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions + "." + FieldOptions._mandatory,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions + "." + FieldOptions._optional + "." + FieldInfo._key,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions + "." + FieldOptions._optional + "." +FieldInfo._type,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions + "." + FieldOptions._optional + "." + FieldInfo._value,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._subjectFieldOptions + "." + FieldOptions._formatting,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyKey,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyText,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions + "." + FieldOptions._mandatory,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions + "." + FieldOptions._optional + "." + FieldInfo._key,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions + "." + FieldOptions._optional + "." + FieldInfo._type,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions + "." + FieldOptions._optional + "." + FieldInfo._value,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bodyFieldOptions + "." + FieldOptions._formatting,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._priorityKey,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._allowAttachments,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._cc,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._ccMode,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bcc,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._bccMode,
|
|
NotificationTemplate._value + "." + NotificationTemplateValue._extraDataKeys,
|
|
|
|
NotificationTemplate._createdAt,
|
|
NotificationTemplate._updatedAt,
|
|
NotificationTemplate._hash,
|
|
NotificationTemplate._isActive
|
|
);
|
|
return this.builderFactory.builder(NotificationTemplateBuilder.class).build(fieldSet, query.first());
|
|
}
|
|
|
|
}
|