use of eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType in project CzechIdMng by bcvsolutions.
the class DefaultIdmNotificationTemplateService method init.
@Override
@Transactional
public void init() {
for (IdmNotificationTemplateType templateType : findTemplates().values()) {
IdmNotificationTemplateDto template = this.getByCode(templateType.getCode());
// if template exist don't save it again => init only
if (template != null) {
LOG.info("Load template with code [{}], template is already initialized, skipping.", templateType.getCode());
continue;
}
//
LOG.info("Load template with code [{}], template will be initialized.", templateType.getCode());
// save
this.save(typeToDto(templateType, null));
}
}
use of eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType in project CzechIdMng by bcvsolutions.
the class DefaultIdmNotificationTemplateService method backup.
@Override
public void backup(IdmNotificationTemplateDto dto) {
String directory = getDirectoryForBackup();
//
Marshaller jaxbMarshaller = initJaxbMarshaller();
//
File backupFolder = new File(directory);
if (!backupFolder.exists()) {
boolean success = backupFolder.mkdirs();
// if make dir after check if exist, throw error.
if (!success) {
LOG.error("Backup for template: {} failed, backup folder path: [{}] can't be created.", dto.getCode(), backupFolder.getAbsolutePath());
throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()));
}
}
//
IdmNotificationTemplateType type = dtoToType(dto);
//
File file = new File(getBackupFileName(directory, dto));
try {
jaxbMarshaller.marshal(type, file);
} catch (JAXBException e) {
LOG.error("Backup for template: {} failed", dto.getCode());
throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()), e);
}
}
use of eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType in project CzechIdMng by bcvsolutions.
the class DefaultIdmNotificationTemplateService method dtoToType.
/**
* Transform dto to type.
*
* @param dto
* @return
*/
private IdmNotificationTemplateType dtoToType(IdmNotificationTemplateDto dto) {
IdmNotificationTemplateType type = new IdmNotificationTemplateType();
if (dto == null) {
return type;
}
// transform DTO to type
type.setCode(dto.getCode());
type.setName(dto.getName());
type.setBodyHtml(dto.getBodyHtml());
type.setBodyText(dto.getBodyText());
type.setModuleId(dto.getModule());
type.setSubject(dto.getSubject());
type.setSystemTemplate(dto.isUnmodifiable());
type.setParameter(dto.getParameter());
return type;
}
use of eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType in project CzechIdMng by bcvsolutions.
the class DefaultIdmNotificationTemplateService method findTemplates.
/**
* Return list of {@link IdmNotificationTemplateType} from resources.
* {@link IdmNotificationTemplateType} are found by configured locations and by priority - last one wins.
* So default location should be configured first, then external, etc.
*
* @return <code, script>
*/
private Map<String, IdmNotificationTemplateType> findTemplates() {
Unmarshaller jaxbUnmarshaller = null;
//
try {
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new ResultCodeException(CoreResultCode.XML_JAXB_INIT_ERROR, e);
}
// last script with the same is used
// => last location has the highest priority
Map<String, IdmNotificationTemplateType> templates = new HashMap<>();
//
for (String location : configurationService.getValues(TEMPLATE_FOLDER)) {
location = location + configurationService.getValue(TEMPLATE_FILE_SUFIX, DEFAULT_TEMPLATE_FILE_SUFIX);
Map<String, IdmNotificationTemplateType> locationTemplates = new HashMap<>();
try {
Resource[] resources = applicationContext.getResources(location);
LOG.debug("Found [{}] resources on location [{}]", resources == null ? 0 : resources.length, location);
//
if (ArrayUtils.isNotEmpty(resources)) {
for (Resource resource : resources) {
try {
IdmNotificationTemplateType templateType = (IdmNotificationTemplateType) jaxbUnmarshaller.unmarshal(resource.getInputStream());
// log error, if script with the same code was found twice in one resource
if (locationTemplates.containsKey(templateType.getCode())) {
LOG.error("More templates with code [{}] found on the same location [{}].", templateType.getCode(), location);
}
// last one wins
locationTemplates.put(templateType.getCode(), templateType);
} catch (JAXBException ex) {
LOG.error("Template validation failed, file name [{}].", resource.getFilename(), ex);
} catch (IOException ex) {
LOG.error("Failed get input stream from, file name [{}].", resource.getFilename(), ex);
}
}
templates.putAll(locationTemplates);
}
} catch (IOException ex) {
throw new ResultCodeException(CoreResultCode.DEPLOY_ERROR, ImmutableMap.of("path", location), ex);
}
}
return templates;
}
Aggregations