]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Some fileimport enhancements - Add support for Excel import 34/434/9
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 20 Apr 2017 07:40:09 +0000 (10:40 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Fri, 21 Apr 2017 11:28:41 +0000 (14:28 +0300)
Removed e.printStackTrace() method calls

refs #7154

Change-Id: Ic70c24330683b3477b928daf966fd44fa30ac363

bundles/org.simantics.fileimport/scl/Dropins/Core.scl
bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/IGenericFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java
bundles/org.simantics.spreadsheet.fileimport/META-INF/MANIFEST.MF
bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelImport.java

index 878326ba1e4ee6c7b42e99cc7f469f17071629f5..b795abbcc5cf6735ed1be906a3b50eab5f62a0bd 100644 (file)
@@ -1,3 +1,4 @@
+import "Simantics/DB"
 import "MMap" as MMap
 
 importJava "org.simantics.fileimport.scl.DropinsSCL" where
@@ -10,6 +11,13 @@ importJava "org.simantics.fileimport.scl.DropinsSCL" where
 
 importJava "org.simantics.fileimport.FileImportService" where
     performFileImport :: String -> String -> <Proc> String
+    importGenericFileWithExtension :: String -> String -> <Proc> String
+    importGenericFileWithExtensionAndParent :: Resource -> String -> String -> <Proc> Resource
+
+importGenericFileToResource :: String -> String -> <Proc> Resource
+importGenericFileToResource path extension = do
+    resourceId = importGenericFileWithExtension path extension
+    syncRead (\_ -> resourceFromId (read resourceId :: Long))
 
 getUploadedDropinFiles :: () -> <Proc> [Long]
 getUploadedDropinFiles dummy = do
index ddb9dabae71478a184ef9845af5debd7097327a8..15612d5fbd02905b8c2672d9621ad7d69dec719b 100644 (file)
@@ -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<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
-        if (serviceOp.isPresent()) {
-            IGenericFileImport service = serviceOp.get();
+        IGenericFileImport service = findServiceForFileExtension(file);
+        if (service != null) {
             try {
                 Optional<String> 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<Consumer<Throwable>> callback) {
-        Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
-        serviceOp.ifPresent(service -> {
-            try {
-                Optional<String> 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<String> 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<Consumer<Throwable>> 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<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
-        serviceOp.ifPresent(service -> {
+
+        try {
+            Optional<String> 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<String> 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<Path> 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<IGenericFileImport> 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<String> 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<IGenericFileImport> 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<IGenericFileImport> findServicesForExtension(String extension) {
+        List<IGenericFileImport> result = new ArrayList<>();
         List<IGenericFileImport> services = getFileImportServices();
         for (IGenericFileImport service : services) {
             for (Map.Entry<String, String> 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<String> 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<Resource> result = service.perform(parent, Paths.get(path));
+        return result.get();
+    }
+
+    private static boolean isPerfectMatch(Set<String> candidates, String extension) {
+        for (String ext : candidates) {
+            if (ext.startsWith("."))
+                ext = ext.substring(1);
+            if (ext.equals(extension))
+                return true;
+        }
+        return false;
+    }
 }
index aa367178125860447af3614a4a01086e3da77ea0..492eb3bc40b404c975944703464cd74a20c6214f 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
 
+import org.simantics.Simantics;
 import org.simantics.db.Resource;
 import org.simantics.graphfile.util.GraphFileUtil;
 
@@ -22,4 +23,8 @@ public class FileReferenceFileImport extends SimanticsResourceFileImport {
         return ALLOWED_EXTENSIONS;
     }
 
+    @Override
+    public Resource defaultParentResource() {
+        return Simantics.getProjectResource();
+    }
 }
index cba016f42a727fa1f0387952210ad7297fb7945a..a8bb132c98f8245013d250d5fd2f8c3f44e6e113 100644 (file)
@@ -4,6 +4,8 @@ import java.nio.file.Path;
 import java.util.Map;
 import java.util.Optional;
 
+import org.simantics.db.Resource;
+
 /**
  * Base interface for performing file imports. 
  * 
@@ -24,6 +26,8 @@ public interface IGenericFileImport {
      */
     Optional<String> perform(Path file) throws Exception;
 
+    Optional<Resource> perform(Resource parent, Path file) throws Exception;
+    
     /**
      * Remove the entity
      * 
index 012c757c214c4a2914703fa6c6341e0ed6d185c8..b453e07d627d42997c47f454fd15a56e6016c4c6 100644 (file)
@@ -32,4 +32,9 @@ public class LibraryFolderFileImport extends SimanticsResourceFileImport {
             }
         }));
     }
+    
+    @Override
+    public Resource defaultParentResource() {
+        return Simantics.getProjectResource();
+    }
 }
index fed7b292ff52c0561a7225d99e79e51ac7dd86d9..4d2bcbe69105ff9a402931446718ef5e70a4c7b8 100644 (file)
@@ -59,7 +59,6 @@ public abstract class SimanticsResourceFileImport implements IGenericFileImport
      * @return Optional Resource of the imported entity in Simantics database
      * @throws Exception
      */
-    public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;
     
     @Override
     public void remove(String resourceId) throws Exception {
@@ -129,5 +128,7 @@ public abstract class SimanticsResourceFileImport implements IGenericFileImport
             return null;
         }
     }
+    
+    public abstract Resource defaultParentResource();
 
 }
index 2c2e5c14479140644083a4bcf3d9cd5d91dda797..1e92738ee448b110fd4166191d08f72f7de2fc25 100644 (file)
@@ -6,7 +6,9 @@ Bundle-Version: 1.0.0.qualifier
 Bundle-Activator: org.simantics.spreadsheet.fileimport.Activator
 Require-Bundle: org.eclipse.core.runtime,
  org.simantics.fileimport,
- org.simantics.db
+ org.simantics.db,
+ org.simantics.spreadsheet.graph,
+ org.simantics
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
 Service-Component: OSGI-INF/component.xml
index 6719f5913fd6c288a741b6c0359d69d0eb47444b..c3a84f7a4ef6cf0709f17b0e494b376b4bbc05a4 100644 (file)
@@ -5,8 +5,10 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
 
+import org.simantics.Simantics;
 import org.simantics.db.Resource;
 import org.simantics.fileimport.SimanticsResourceFileImport;
+import org.simantics.spreadsheet.graph.ExcelImport;
 
 public class ExcelFileImport extends SimanticsResourceFileImport {
 
@@ -19,7 +21,7 @@ public class ExcelFileImport extends SimanticsResourceFileImport {
     
     @Override
     public Optional<Resource> perform(Resource parent, Path file) throws Exception {
-        throw new UnsupportedOperationException("Excel import is not yet supported");
+        return Optional.ofNullable(ExcelImport.importBookR(parent, file.toFile()));
     }
 
     @Override
@@ -27,4 +29,9 @@ public class ExcelFileImport extends SimanticsResourceFileImport {
         return ALLOWED_EXTENSIONS;
     }
 
+    @Override
+    public Resource defaultParentResource() {
+        return Simantics.getSession().getRootLibrary();
+    }
+
 }
index 246f38edf8654dc7145ffd63c33bbe56e4dd99b3..81fd2cfd4fb183277c7ea047819c6a66edd343a0 100644 (file)
@@ -28,7 +28,7 @@ import org.simantics.datatypes.utils.BTree;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.DelayedWriteRequest;
-import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.BindingException;
 import org.simantics.db.exception.DatabaseException;
@@ -46,12 +46,20 @@ import org.simantics.spreadsheet.graph.parser.ast.AstValue;
 import org.simantics.spreadsheet.resource.SpreadsheetResource;
 import org.simantics.spreadsheet.util.SpreadsheetUtils;
 import org.simantics.utils.DataContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ExcelImport {
-       
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelImport.class);
+    
        private static final double POINT_TO_PIXEL_RATIO = 1.33;
        
-    public static void importBook(Resource container, File file) {
+       public static void importBook(Resource container, File file) {
+           importBook(container, file);
+       }
+       
+    public static Resource importBookR(Resource container, File file) {
         
         try {
             
@@ -254,7 +262,7 @@ public class ExcelImport {
                         btreeContainer.set(result);
                         bookContainer.set(book);
                     } catch (Exception e) {
-                        e.printStackTrace();
+                        LOGGER.error("Could not import book " + file.getAbsolutePath(), e);
                         btreeContainer.add(Collections.emptyList());
                     }
                 }
@@ -269,20 +277,21 @@ public class ExcelImport {
                 }
             });
             
-            Simantics.getSession().sync(new WriteRequest() {
+            return Simantics.getSession().sync(new WriteResultRequest<Resource>() {
                 
                 @Override
-                public void perform(WriteGraph graph) throws DatabaseException {
+                public Resource perform(WriteGraph graph) throws DatabaseException {
                     Resource delayedBook = bookContainer.get();
                     XSupport support = graph.getService(XSupport.class);
                     Resource book = support.convertDelayedResourceToResource(delayedBook);
                     SpreadsheetGraphUtils.constructAndInitializeRunVariable(graph, book);
+                    return book;
                 }
             });
         } catch (Exception e) {
-            e.printStackTrace();
+            LOGGER.error("Could not import book " + file.getAbsolutePath(), e);
+            return null;
         }
-        
     }
     
     private static Resource assignStyles(WriteGraph graph, SpreadsheetResource SR, Cell cell, Resource book, Map<Integer, Resource> existingStyles, Map<Integer, SpreadsheetStyle> existingStyles2, String styleName) throws DatabaseException {