@Override public List<ConfigGroup> getConfiguration(ConfigType type, final String namespace) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } if (!ConfigType.REPOSITORY.equals(type)) { return Collections.emptyList(); } return getConfiguration(type).stream() .filter(repoConfig -> namespace.equals(repoConfig.getConfigItemValue("space"))) .collect(Collectors.toList()); }
@Override public Map<String, List<ConfigGroup>> getConfigurationByNamespace(ConfigType type) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } if (!ConfigType.REPOSITORY.equals(type)) { return Collections.emptyMap(); } final Map<String, List<ConfigGroup>> repoConfigsBySpace = new HashMap<>(); final List<ConfigGroup> repoConfigs = getConfiguration(type); for (ConfigGroup repoConfig : repoConfigs) { final String space = repoConfig.getConfigItemValue("space"); if (space != null) { if (!repoConfigsBySpace.containsKey(space)) { repoConfigsBySpace.put(space, new ArrayList<>()); } repoConfigsBySpace.get(space).add(repoConfig); } } return repoConfigsBySpace; }
@Override public List<ConfigGroup> getConfiguration(ConfigType configType) { if (ConfigType.SPACE.equals(configType)) { configType = ConfigType.ORGANIZATIONAL_UNIT; } final ConfigType type = configType; final List<ConfigGroup> configGroups = new ArrayList<>(); final DirectoryStream<Path> foundConfigs = ioService.newDirectoryStream(ioService.get(systemRepository.getUri()), entry -> { if (!Files.isDirectory(entry) && !entry.getFileName().toString().startsWith(".") && entry.getFileName().toString().endsWith(type.getExt())) { return true; } return false; } ); //Only load and cache if a file was found! final Iterator<Path> it = foundConfigs.iterator(); if (it.hasNext()) { while (it.hasNext()) { final String content = ioService.readAllString(it.next()); final ConfigGroup configGroup = marshaller.unmarshall(content); configGroups.add(configGroup); } configGroupsByTypeWithoutNamespace.put(type, configGroups); } return configGroups; }
@Override public ConfigGroup newConfigGroup(ConfigType type, final String name, final String description) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } final ConfigGroup configGroup = new ConfigGroup(); configGroup.setDescription(description); configGroup.setName(name); configGroup.setType(type); configGroup.setEnabled(true); return configGroup; }
@Override public boolean updateConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Updated config " + filePath.getFileName()); saveConfiguration(configGroup, filePath, commentedOption); configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); return true; }
@Override public boolean removeConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); if (!ioService.exists(filePath)) { return true; } boolean result; try { ioService.startBatch(filePath.getFileSystem()); result = ioService.deleteIfExists(filePath); if (result) { updateLastModified(); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } return result; }
@Override public boolean addConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); // avoid duplicated writes to not cause cyclic cluster sync if (ioService.exists(filePath)) { return true; } final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Created config " + filePath.getFileName()); saveConfiguration(configGroup, filePath, commentedOption); configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); return true; }