Search in sources :

Example 1 with IdmScriptType

use of eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType in project CzechIdMng by bcvsolutions.

the class DefaultIdmScriptService method findScripts.

/**
 * Return list of {@link IdmScriptType} from resources.
 * {@link IdmScriptType} 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, IdmScriptType> findScripts() {
    // last script with the same is used
    // => last location has the highest priority
    Map<String, IdmScriptType> scripts = new HashMap<>();
    // 
    for (String location : configurationService.getValues(SCRIPT_FOLDER)) {
        location = location + configurationService.getValue(SCRIPT_FILE_SUFIX, DEFAULT_SCRIPT_FILE_SUFIX);
        Map<String, IdmScriptType> locationScripts = new HashMap<>();
        try {
            Resource[] resources = applicationContext.getResources(location);
            LOG.debug("Found [{}] resources on location [{}]", resources == null ? 0 : resources.length, location);
            // 
            if (ArrayUtils.isEmpty(resources)) {
                continue;
            }
            // 
            for (Resource resource : resources) {
                try {
                    IdmScriptType scriptType = readType(location, resource.getInputStream());
                    // log error, if script with the same code was found twice in one resource
                    if (locationScripts.containsKey(scriptType.getCode())) {
                        LOG.error("More scripts with code [{}], category [{}] found on the same location [{}].", scriptType.getCode(), scriptType.getCategory(), location);
                    }
                    // last one wins
                    locationScripts.put(scriptType.getCode(), scriptType);
                } catch (IOException ex) {
                    LOG.error("Failed get input stream from, file name [{}].", resource.getFilename(), ex);
                }
            }
            scripts.putAll(locationScripts);
        } catch (IOException ex) {
            throw new ResultCodeException(CoreResultCode.DEPLOY_ERROR, ImmutableMap.of("path", location), ex);
        }
    }
    return scripts;
}
Also used : IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) HashMap(java.util.HashMap) Resource(org.springframework.core.io.Resource) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException)

Example 2 with IdmScriptType

use of eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType in project CzechIdMng by bcvsolutions.

the class DefaultIdmScriptService method init.

@Override
@Transactional
public void init() {
    for (IdmScriptType scriptType : findScripts().values()) {
        IdmScriptDto script = this.getByCode(scriptType.getCode());
        // if script exist don't save it again => init only
        if (script != null) {
            LOG.info("Load script with code [{}], script is already initialized, skipping.", scriptType.getCode());
            continue;
        }
        // 
        LOG.info("Load script with code [{}], script will be initialized.", scriptType.getCode());
        // save script
        script = this.save(toDto(scriptType, null));
        // save authorities
        this.scriptAuthorityService.saveAll(authorityTypeToDto(scriptType, script));
    }
}
Also used : IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) IdmScriptDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with IdmScriptType

use of eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType in project CzechIdMng by bcvsolutions.

the class DefaultIdmScriptService method toType.

/**
 * Transform dto to type.
 *
 * @param dto
 * @return
 */
@Override
protected IdmScriptType toType(IdmScriptDto dto) {
    IdmScriptType type = new IdmScriptType();
    if (dto == null) {
        return type;
    }
    // transform DTO to type
    type.setCode(dto.getCode());
    type.setName(dto.getName());
    // parameter isn't implemented yet
    // type.setParameters(dto.getParameter());
    type.setBody(dto.getScript());
    type.setCategory(dto.getCategory());
    type.setDescription(dto.getDescription());
    type.setType(SCRIPT_DEFAULT_TYPE);
    // 
    if (dto.getId() == null) {
        return type;
    }
    IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
    filter.setScriptId(dto.getId());
    List<IdmScriptAuthorityDto> authorities = scriptAuthorityService.find(filter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmScriptAuthority_.type.getName(), IdmScriptAuthority_.service.getName(), IdmScriptAuthority_.className.getName()))).getContent();
    if (authorities.isEmpty()) {
        return type;
    }
    // 
    List<IdmScriptAllowClassType> classes = new ArrayList<>();
    List<IdmScriptServiceType> services = new ArrayList<>();
    for (IdmScriptAuthorityDto auth : authorities) {
        if (auth.getType() == ScriptAuthorityType.CLASS_NAME) {
            IdmScriptAllowClassType classType = new IdmScriptAllowClassType();
            classType.setClassName(auth.getClassName());
            classes.add(classType);
        } else {
            IdmScriptServiceType service = new IdmScriptServiceType();
            service.setClassName(auth.getClassName());
            service.setName(auth.getService());
            services.add(service);
        }
    }
    if (!classes.isEmpty()) {
        type.setAllowClasses(new IdmScriptAllowClassesType());
        type.getAllowClasses().setAllowClasses(classes);
    }
    if (!services.isEmpty()) {
        type.setServices(new IdmScriptServicesType());
        type.getServices().setServices(services);
    }
    // 
    return type;
}
Also used : IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) IdmScriptServicesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServicesType) IdmScriptAllowClassType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) IdmScriptServiceType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType) ArrayList(java.util.ArrayList) IdmScriptAllowClassesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType) IdmScriptAuthorityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)

Example 4 with IdmScriptType

use of eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType in project CzechIdMng by bcvsolutions.

the class DefaultIdmScriptService method dtoToType.

/**
 * Transform dto to type.
 *
 * @param dto
 * @return
 */
private IdmScriptType dtoToType(IdmScriptDto dto, List<IdmScriptAuthorityDto> authorities) {
    IdmScriptType type = new IdmScriptType();
    if (dto == null) {
        return type;
    }
    // transform DTO to type
    type.setCode(dto.getCode());
    type.setName(dto.getName());
    // parameter isn't implemented yet
    // type.setParameters(dto.getParameter());
    type.setBody(dto.getScript());
    type.setCategory(dto.getCategory());
    type.setDescription(dto.getDescription());
    type.setType(SCRIPT_DEFAULT_TYPE);
    // 
    if (authorities != null && !authorities.isEmpty()) {
        List<IdmScriptAllowClassType> classes = new ArrayList<>();
        List<IdmScriptServiceType> services = new ArrayList<>();
        for (IdmScriptAuthorityDto auth : authorities) {
            if (auth.getType() == ScriptAuthorityType.CLASS_NAME) {
                IdmScriptAllowClassType classType = new IdmScriptAllowClassType();
                classType.setClassName(auth.getClassName());
                classes.add(classType);
            } else {
                IdmScriptServiceType service = new IdmScriptServiceType();
                service.setClassName(auth.getClassName());
                service.setName(auth.getService());
                services.add(service);
            }
        }
        if (!classes.isEmpty()) {
            type.setAllowClasses(new IdmScriptAllowClassesType());
            type.getAllowClasses().setAllowClasses(classes);
        }
        if (!services.isEmpty()) {
            type.setServices(new IdmScriptServicesType());
            type.getServices().setServices(services);
        }
    }
    return type;
}
Also used : IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) IdmScriptServicesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServicesType) IdmScriptAllowClassType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType) IdmScriptServiceType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) ArrayList(java.util.ArrayList) IdmScriptAllowClassesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType)

Example 5 with IdmScriptType

use of eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType in project CzechIdMng by bcvsolutions.

the class DefaultIdmScriptService method backup.

@Override
public void backup(IdmScriptDto 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 script: {} failed, backup folder path: [{}] can't be created.", dto.getCode(), backupFolder.getAbsolutePath());
            throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()));
        }
    }
    // 
    IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
    filter.setScriptId(dto.getId());
    IdmScriptType type = dtoToType(dto, this.scriptAuthorityService.find(filter, null).getContent());
    // 
    File file = new File(getBackupFileName(directory, dto));
    LOG.info("Backup for script code: [{}] to file: [{}]", dto.getCode(), file.getAbsolutePath());
    try {
        jaxbMarshaller.marshal(type, file);
    } catch (JAXBException e) {
        LOG.error("Backup for script: {} failed, error message: {}", dto.getCode(), e.getLocalizedMessage(), e);
        throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()), e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) JAXBException(javax.xml.bind.JAXBException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) File(java.io.File) IdmScriptAuthorityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)

Aggregations

IdmScriptType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType)5 IdmScriptAuthorityDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto)2 IdmScriptAuthorityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 IdmScriptAllowClassType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType)2 IdmScriptAllowClassesType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType)2 IdmScriptServiceType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType)2 IdmScriptServicesType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServicesType)2 ArrayList (java.util.ArrayList)2 IdmScriptDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptDto)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 Resource (org.springframework.core.io.Resource)1 Transactional (org.springframework.transaction.annotation.Transactional)1