private List<TypeDTO> fetchTypes(File[] typeDirectories) { final List<TypeDTO> results = new ArrayList<>(); for (File typeDirectory : typeDirectories) { if (typeDirectory.isDirectory() && !typeDirectory.getName().startsWith(".")) { final File typeJson = new File(typeDirectory, "type.json"); if (typeJson.exists()) { final TypeDTO jsonTypeDTO = unSerializeType(typeJson); final TypeDTO.Builder typeDTOBuilder = new TypeDTO.Builder(jsonTypeDTO); if (StringUtils.isBlank(jsonTypeDTO.getId())) { if (!StringUtils.isBlank(jsonTypeDTO.getName())) { typeDTOBuilder.withId(jsonTypeDTO.getName().replaceAll(ID_REGEX, "")); } else { typeDTOBuilder.withId(typeDirectory.getName().replaceAll(ID_REGEX, "")); } } final File typeIconFile = new File(typeDirectory, ICON_NAME); if (typeIconFile.exists()) { typeDTOBuilder.withIcon(typeIconFile.toURI()); } typeDTOBuilder.withCategories(fetchCategories(typeDTOBuilder.getId(), typeDirectory)); final TypeDTO type = typeDTOBuilder.build(); results.add(type); } } } results.sort(Comparator.comparing(TypeDTO::getName)); return results; }
protected TypeDTO mergeTypes(TypeDTO leftCategory, TypeDTO rightCategory) { final Map<String, CategoryDTO> leftCategories = createSortedMap(leftCategory.getCategories(), CategoryDTO::getId); final Map<String, CategoryDTO> rightCategories = createSortedMap(rightCategory.getCategories(), CategoryDTO::getId); final SortedMap<String, CategoryDTO> mergedCategories = new TreeMap<>(rightCategories); for (Map.Entry<String, CategoryDTO> entry : leftCategories.entrySet()) { final CategoryDTO category = entry.getValue(); if (mergedCategories.containsKey(entry.getKey())) { mergedCategories.put(entry.getKey(), mergeCategories(mergedCategories.get(entry.getKey()), category)); } else { mergedCategories.put(entry.getKey(), category); } } final List<CategoryDTO> categories = new ArrayList<>(mergedCategories.values()); categories.sort(CategoryDTO.nameComparator()); return new TypeDTO.Builder() .withId(leftCategory.getId()) .withName(leftCategory.getName()) .withCategories(categories) .withIcon(leftCategory.getIcon()) .build(); }
private TypeDTO buildType(String typeFileName) throws RepositoryException { try { final String jsonTypePath = packagePath + "/" + typeFileName + "/type.json"; final URL jsonTypeFile = getClass().getResource(jsonTypePath); if (jsonTypeFile != null) { final TypeDTO typeDTO = objectMapper.readValue(jsonTypeFile, TypeDTO.class); TypeDTO.Builder typeDTOBuilder = new TypeDTO.Builder(typeDTO); if (StringUtils.isBlank(typeDTO.getId())) { if (!StringUtils.isBlank(typeDTO.getName())) { typeDTOBuilder.withId(typeDTO.getName().replaceAll(ID_REGEX, "")); } else { typeDTOBuilder.withId(typeFileName.replaceAll(ID_REGEX, "")); } } typeDTOBuilder.withCategories(buildCategories(typeDTOBuilder.getId(), typeFileName)).build(); try { typeDTOBuilder.withIcon(new URI(packagePath + "/" + typeFileName + "/icon.png")); } catch (URISyntaxException e) { LOGGER.warn("Invalid icon path", e); } return typeDTOBuilder.build(); } else { LOGGER.debug(String.format("type.json %s for classpath repository does not exist", jsonTypePath)); return new TypeDTO.Builder().build(); } } catch (IOException e) { throw new RepositoryException("Could not build type", e); } }
@Override public RepositoryDTO fetchInstallableApplications() { return new RepositoryDTO.Builder() .withTypes(Collections.singletonList( new TypeDTO.Builder() .withId("type_1") .withCategories(Collections.singletonList( new CategoryDTO.Builder() .withApplications(Arrays.asList( new ApplicationDTO.Builder() .withId("engines") .withScripts(Arrays.asList( new ScriptDTO.Builder() .withScriptName("Wine") .withCompatibleOperatingSystems( Arrays.asList( OperatingSystem.LINUX, OperatingSystem.MACOSX)) .withScript(wineScript()) .build())) .build())) .withName("Functions") .build())) .build())) .build(); }
public Builder(TypeDTO categoryDTO) { this.withId(categoryDTO.getId()).withName(categoryDTO.getName()) .withCategories(categoryDTO.getCategories()).withIcon(categoryDTO.getIcon()); }