Search in sources :

Example 1 with IdmNotificationTemplateType

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));
    }
}
Also used : IdmNotificationTemplateType(eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with IdmNotificationTemplateType

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) IdmNotificationTemplateType(eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType) JAXBException(javax.xml.bind.JAXBException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) File(java.io.File)

Example 3 with IdmNotificationTemplateType

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;
}
Also used : IdmNotificationTemplateType(eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType)

Example 4 with IdmNotificationTemplateType

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;
}
Also used : HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) IdmNotificationTemplateType(eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Resource(org.springframework.core.io.Resource) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

IdmNotificationTemplateType (eu.bcvsolutions.idm.core.notification.jaxb.IdmNotificationTemplateType)4 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)2 JAXBException (javax.xml.bind.JAXBException)2 IdmNotificationTemplateDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 Resource (org.springframework.core.io.Resource)1 Transactional (org.springframework.transaction.annotation.Transactional)1