diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenCoordinate.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenCoordinate.java index 96f51f2c4..6d19bf331 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenCoordinate.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenCoordinate.java @@ -1,11 +1,12 @@ package com.devonfw.cobigen.api.util; +import java.util.Arrays; import java.util.Objects; /** * This MavenCoordinate class is just a dataholder with maven coordinates. */ -public class MavenCoordinate { +public class MavenCoordinate implements Comparable { /** * the groupId of the maven artifact @@ -86,4 +87,22 @@ public boolean equals(Object obj) { && Objects.equals(this.version, other.version); } + @Override + public int compareTo(MavenCoordinate other) { + + if (!this.artifactId.equals(other.artifactId)) { + throw new ClassCastException("The artifactID of the comprarable should be the same"); + // TODO + } + int[] versionNumbersCurrent = Arrays.stream(this.version.split("\\.")).mapToInt(Integer::parseInt).toArray(); + int[] versionNumbersOther = Arrays.stream(other.getVersion().split("\\.")).mapToInt(Integer::parseInt).toArray(); + for (int i = 0; i < versionNumbersCurrent.length; i++) { + if (versionNumbersCurrent[i] > versionNumbersOther[i]) { + return 1; + } else if (versionNumbersCurrent[i] < versionNumbersOther[i]) + return -1; + } + return 0; + } + } diff --git a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java index 6f122cb4f..434656395 100644 --- a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java +++ b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java @@ -1,15 +1,19 @@ package com.devonfw.cobigen.gui.controllers; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.ListIterator; import java.util.ResourceBundle; import com.devonfw.cobigen.api.constants.ConfigurationConstants; import com.devonfw.cobigen.api.util.CobiGenPaths; +import com.devonfw.cobigen.api.util.MavenCoordinate; import com.devonfw.cobigen.api.util.MavenUtil; import com.devonfw.cobigen.gui.Controller; import com.devonfw.cobigen.gui.model.TemplateSetModel; @@ -87,7 +91,7 @@ public void initialize(URL location, ResourceBundle resources) { Path artifactCachePath = CobiGenPaths.getTemplateSetsFolderPath() .resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER); - + List cachedArtifacts = new ArrayList<>(); if (!Files.exists(artifactCachePath)) { try { Files.createDirectory(artifactCachePath); @@ -95,25 +99,15 @@ public void initialize(URL location, ResourceBundle resources) { // TODO Auto-generated catch block e.printStackTrace(); } + } else { + + for (File artifact : Arrays.asList(artifactCachePath.toFile().listFiles())) { + cachedArtifacts.add(artifact.toPath()); + } } - // Initialize template set artifacts // TODO: read CobiGen properties - List groupIds = Arrays.asList(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID); - List urlList = ArtifactRetriever.retrieveTemplateSetXmlDownloadLinks(groupIds, - MavenUtil.determineMavenSettings()); - - List downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList); - - List templateSetConfigurations = ArtifactRetriever - .retrieveArtifactsFromCache(downloadedArtifacts); - - ObservableList observableList = FXCollections.observableArrayList(); - - observableList.addAll(templateSetConfigurations); - - // binds the List with model - this.searchResultsView.setItems(observableList); + refresh(); // Load increments of selected template set // call back functions @@ -164,6 +158,68 @@ public void initialize(URL location, ResourceBundle resources) { */ @FXML public void refresh() { + // check if clear the list is needed + + Path artifactCachePath = CobiGenPaths.getTemplateSetsFolderPath() + .resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER); + List cachedArtifacts = new ArrayList<>(); + if (!Files.exists(artifactCachePath)) { + try { + Files.createDirectory(artifactCachePath); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else { + + for (File artifact : Arrays.asList(artifactCachePath.toFile().listFiles())) { + cachedArtifacts.add(artifact.toPath()); + } + } + + // TODO: read CobiGen properties + List groupIds = Arrays.asList(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID); + List urlList = ArtifactRetriever.retrieveTemplateSetXmlDownloadLinks(groupIds, + MavenUtil.determineMavenSettings()); + + // check for Update + // TODO error handling with replace all and maybe jar is corrupted just replace it + // also try to export this to other functions + List cachedMavenCoordinates = new ArrayList<>(); + for (Path p : cachedArtifacts) { + String version = p.getFileName().toString().replaceAll("-template-set.xml", "") + .replaceAll("^(([a-zA-z]+[\\w]-)+)", ""); + String artifactID = p.getFileName().toString().replaceAll("-template-set.xml", "").replaceAll("-" + version, ""); + cachedMavenCoordinates.add(new MavenCoordinate(null, artifactID, version)); + } + ListIterator iterator = urlList.listIterator(); + while (iterator.hasNext()) { + String maven = iterator.next().getFile().replaceAll("-template-set.xml", ""); + String version = maven.replaceAll("^(([a-zA-z]+[\\w]-)+)", ""); + String artifactID = maven.replaceAll("-" + version, ""); + MavenCoordinate repo = new MavenCoordinate(null, artifactID, version); + for (MavenCoordinate cached : cachedMavenCoordinates) { + if (cached.getArtifactId().equals(repo.getArtifactId())) { + int result = cached.compareTo(repo); + if (result <= 0) { + // no download needed + iterator.remove(); + } else { + // download needed ccan be removed, leave it for debugging pruposes + } + } + } + } + + List downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList); + + List templateSetConfigurations = ArtifactRetriever + .retrieveArtifactsFromCache(downloadedArtifacts); + + ObservableList observableList = FXCollections.observableArrayList(); + + observableList.addAll(templateSetConfigurations); + this.searchResultsView.setItems(observableList); }