]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java
Updated file importer interface and fixed Spreadsheet import
[simantics/platform.git] / bundles / org.simantics.fileimport / src / org / simantics / fileimport / FileImportService.java
index 96276154237e11b31209dc1707c207edd68d8c71..5f9c7257b7da977057d5ac6ef3951aff787724bb 100644 (file)
@@ -93,27 +93,35 @@ public class FileImportService {
         Files.write(file, bytes);
         
         ConsumerHolder holder = new ConsumerHolder();
-        String result = performFileImport(file, Optional.of(holder));
+        String result = performFileImport(file, Optional.empty(), Optional.of(holder));
         if (holder.getThrowable() != null)
             throw holder.getThrowable();
         return result;
     }
-
+    
     /**
      * Method that performs the import of the given file. This method is called when e.g. {@link FileImportDropins} watcher detects {@link java.nio.file.StandardWatchEventKinds.ENTRY_CREATE} operation
      * 
      * @param file Path file to be imported
+     * @param possibleSelection - the selected resource (if exists)
      * @param callback Optional callback which can be used to catch Throwables thrown in the import process
      */
-    public static String performFileImport(Path file, Optional<Consumer<Throwable>> callback) {
+    public static String performFileImport(Path file, Optional<Resource> possibleSelection, Optional<Consumer<Throwable>> callback) {
         if (file.getFileName().toString().equals(DB_FILE)) {
             return null;
         }
         String result = "Import failed";
         IGenericFileImport service = findServiceForFileExtension(file);
+        
         if (service != null) {
             try {
-                Optional<String> resource = service.perform(file);
+               Optional<String> resource;
+               if (possibleSelection.isPresent() && service.defaultParentResource() == null) {
+                       resource = Optional.of(Long.toString(service.perform(possibleSelection.get(), file).get().getResourceId()));
+               }
+               else {
+                       resource = service.performWithDefaultParent(file);
+               }
                 saveResourceForPath(file, resource);
                 result = resource.get();
             } catch (Throwable t) {
@@ -130,7 +138,6 @@ public class FileImportService {
         }
         return result;
     }
-
     
     /**
      * Remove the entity that matches the file. This method is called when e.g. the {@link FileImportDropins} watcher detects {@link java.nio.file.StandardWatchEventKinds.ENTRY_DELETE} operation
@@ -353,12 +360,27 @@ public class FileImportService {
         return Optional.of(value);
     }
     
+    /**
+     * Calls the proper imported without a selection (null possibleSelection)
+     * @param path
+     * @param extension
+     * @return
+     * @throws Exception
+     */
     public static String importGenericFileWithExtension(String path, String extension) throws Exception {
         IGenericFileImport service = findServiceForExtension(extension);
-        Optional<String> result = service.perform(Paths.get(path));
+        Optional<String> result = service.performWithDefaultParent(Paths.get(path));
         return result.get();
     }
     
+    /**
+     * Calls the proper imported without a selection (null possibleSelection)
+     * @param parent
+     * @param path
+     * @param extension
+     * @return
+     * @throws Exception
+     */
     public static Resource importGenericFileWithExtensionAndParent(Resource parent, String path, String extension) throws Exception {
         IGenericFileImport service = findServiceForExtension(extension);
         Optional<Resource> result = service.perform(parent, Paths.get(path));