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;
}
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));
}
}
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;
}
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;
}
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);
}
}
Aggregations