X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.fileimport%2Fsrc%2Forg%2Fsimantics%2Ffileimport%2FFileImportService.java;h=15612d5fbd02905b8c2672d9621ad7d69dec719b;hp=ddb9dabae71478a184ef9845af5debd7097327a8;hb=0d82e7808541ed486f6027a28038d44d2d93711a;hpb=99f23679ed1b7c4a47cd841fa75be142225eb7aa diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java index ddb9dabae..15612d5fb 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java @@ -13,13 +13,15 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Properties; +import java.util.Set; import java.util.function.Consumer; +import java.util.stream.Collectors; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.simantics.databoard.util.Base64; +import org.simantics.db.Resource; import org.simantics.fileimport.dropins.FileImportDropins; -import org.simantics.utils.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -108,9 +110,8 @@ public class FileImportService { return null; } String result = "Import failed"; - Optional serviceOp = findServiceForFileExtension(file); - if (serviceOp.isPresent()) { - IGenericFileImport service = serviceOp.get(); + IGenericFileImport service = findServiceForFileExtension(file); + if (service != null) { try { Optional resource = service.perform(file); saveResourceForPath(file, resource); @@ -138,22 +139,25 @@ public class FileImportService { * @param callback Optional callback to catch Throwables thrown during the deletion process */ public static void removeResourceForFile(Path file, Optional> callback) { - Optional serviceOp = findServiceForFileExtension(file); - serviceOp.ifPresent(service -> { - try { - Optional resource = getResourceForPath(file); - if (!resource.isPresent()) - return; - service.remove(resource.get()); - removeResourceForPath(file); - } catch (Throwable t) { - if (callback.isPresent()) { - callback.get().accept(t); - } else { - t.printStackTrace(); - } + try { + Optional resource = getResourceForPath(file); + if (!resource.isPresent()) + return; + IGenericFileImport service = findServiceForFileExtension(file); + if (service == null) { + LOGGER.warn("Could not find service for importing file " + file); + if (callback.isPresent()) + callback.get().accept(new Exception("Could not find IGenericFileImport service for file " + file)); } - }); + service.remove(resource.get()); + removeResourceForPath(file); + } catch (Throwable t) { + if (callback.isPresent()) { + callback.get().accept(t); + } else { + LOGGER.error("Could not remove resource for file " + file.toAbsolutePath(), t); + } + } } public static void removeFileForResource(long id, Optional> callback) { @@ -161,33 +165,37 @@ public class FileImportService { try { fileOp = findPathForId(id); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Could not remove file for resource id " + id, e); return; } if (!fileOp.isPresent()) return; Path file = fileOp.get(); - Optional serviceOp = findServiceForFileExtension(file); - serviceOp.ifPresent(service -> { + + try { + Optional resource = getResourceForPath(file); + if (!resource.isPresent()) + return; + IGenericFileImport service = findServiceForFileExtension(file); + if (service == null) { + LOGGER.warn("Could not find service for importing file " + file); + if (callback.isPresent()) + callback.get().accept(new Exception("Could not find IGenericFileImport service for file " + file)); + } + service.remove(resource.get()); + removeResourceForPath(file); try { - Optional resource = getResourceForPath(file); - if (!resource.isPresent()) - return; - service.remove(resource.get()); - removeResourceForPath(file); - try { - Files.delete(file); - } catch (IOException e) { - Files.delete(file); - } - } catch (Throwable t) { - if (callback.isPresent()) { - callback.get().accept(t); - } else { - t.printStackTrace(); - } + Files.delete(file); + } catch (IOException e) { + Files.delete(file); } - }); + } catch (Throwable t) { + if (callback.isPresent()) { + callback.get().accept(t); + } else { + LOGGER.error("Could not remove file for resource " + id, t); + } + } } private static Optional findPathForId(long id) throws IOException { @@ -214,9 +222,9 @@ public class FileImportService { * Method for finding a File Import service for the given file based on the file extension * * @param file Path file for which the import service is looked for - * @return Optiona IGenerigFileImport service which is able to handle the import of this type of file + * @return Optional IGenerigFileImport service which is able to handle the import of this type of file */ - public static Optional findServiceForFileExtension(Path file) { + public static IGenericFileImport findServiceForFileExtension(Path file) { String extension = ""; int i = file.getFileName().toString().lastIndexOf('.'); @@ -228,7 +236,30 @@ public class FileImportService { extension = FOLDER; } } + return findServiceForExtension(extension); + } + + public static List filterSupportedExtensions(String filter) { + return getFileImportServices().stream().filter(s -> s.allowedExtensionsWithFilters().keySet().contains(filter)).map(s -> s.allowedExtensionsWithFilters().keySet()).flatMap(Set::stream).collect(Collectors.toList()); + } + public static IGenericFileImport findServiceForExtension(String extension) { + List services = findServicesForExtension(extension); + IGenericFileImport service = null; + if (services.size() == 1) { + service = services.get(0); + } else { + for (IGenericFileImport servicee : services) { + service = servicee; + if (isPerfectMatch(servicee.allowedExtensionsWithFilters().keySet(), extension)) + break; + } + } + return service; + } + + public static List findServicesForExtension(String extension) { + List result = new ArrayList<>(); List services = getFileImportServices(); for (IGenericFileImport service : services) { for (Map.Entry entry : service.allowedExtensionsWithFilters().entrySet()) { @@ -237,14 +268,14 @@ public class FileImportService { possibleExtensions = possibleExtensions.substring(1); if (possibleExtensions.equals(extension) || possibleExtensions.isEmpty()) { if (extension.equals(FOLDER) && possibleExtensions.equals(FOLDER)) { - return Optional.of(service); + result.add(service); } else if (!extension.isEmpty() && !extension.equals(FOLDER)){ - return Optional.of(service); + result.add(service); } } } } - return Optional.empty(); + return result; } /** @@ -269,7 +300,7 @@ public class FileImportService { } return map; } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Could not get current paths and resources!", e); return Collections.emptyMap(); } } @@ -289,7 +320,7 @@ public class FileImportService { props.store(stream, null); } } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Could not save resource for path " + file.toAbsolutePath() + " and resource " + resource.get(), e); } }); } @@ -321,4 +352,26 @@ public class FileImportService { return Optional.empty(); return Optional.of(value); } + + public static String importGenericFileWithExtension(String path, String extension) throws Exception { + IGenericFileImport service = findServiceForExtension(extension); + Optional result = service.perform(Paths.get(path)); + return result.get(); + } + + public static Resource importGenericFileWithExtensionAndParent(Resource parent, String path, String extension) throws Exception { + IGenericFileImport service = findServiceForExtension(extension); + Optional result = service.perform(parent, Paths.get(path)); + return result.get(); + } + + private static boolean isPerfectMatch(Set candidates, String extension) { + for (String ext : candidates) { + if (ext.startsWith(".")) + ext = ext.substring(1); + if (ext.equals(extension)) + return true; + } + return false; + } }