\r
import org.osgi.framework.InvalidSyntaxException;\r
import org.osgi.framework.ServiceReference;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.service.SerialisationSupport;\r
-import org.simantics.layer0.Layer0;\r
+import org.simantics.fileimport.dropins.FileImportDropins;\r
\r
+/**\r
+ * Utility class for Simantics File import functions\r
+ * \r
+ * @author Jani Simomaa\r
+ *\r
+ */\r
public class FileImportService {\r
\r
+ private FileImportService() {}\r
+ \r
public static final String DB_FILE = ".simanticsdb";\r
\r
- public static List<IGenericFileImport> getFileImportServices() {\r
+ private static List<IGenericFileImport> getFileImportServices() {\r
ServiceReference<?>[] serviceReferences = new ServiceReference<?>[0];\r
try {\r
serviceReferences = Activator.getContext().getAllServiceReferences(IGenericFileImport.class.getName(),\r
return services;\r
}\r
\r
+ /**\r
+ * Lists all supported file extensions which have a registered service for handling the import\r
+ * \r
+ * @return Map containing the extension and the description of the extension in that order\r
+ */\r
public static Map<String, String> supportedExtensionsWithFilters() {\r
List<IGenericFileImport> services = getFileImportServices();\r
Map<String, String> extensionsWithFilters = new HashMap<>();\r
return extensionsWithFilters;\r
}\r
\r
+ /**\r
+ * 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\r
+ * \r
+ * @param file Path file to be imported\r
+ * @param callback Optional callback which can be used to catch Throwables thrown in the import process\r
+ */\r
public static void performFileImport(Path file, Optional<Consumer<Throwable>> callback) {\r
if (file.getFileName().toString().equals(DB_FILE))\r
return;\r
});\r
}\r
\r
+ \r
+ /**\r
+ * 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\r
+ * \r
+ * @param file Path file that was deleted\r
+ * @param callback Optional callback to catch Throwables thrown during the deletion process\r
+ */\r
public static void removeResourceForFile(Path file, Optional<Consumer<Throwable>> callback) {\r
Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);\r
serviceOp.ifPresent(service -> {\r
return;\r
service.remove(resource.get());\r
removeResourceForPath(file);\r
+ try {\r
+ Files.delete(file);\r
+ } catch (IOException e) {\r
+ Files.delete(file);\r
+ }\r
} catch (Throwable t) {\r
if (callback.isPresent()) {\r
callback.get().accept(t);\r
\r
static final String FOLDER = "_folder_";\r
\r
+ /**\r
+ * Method for finding a File Import service for the given file based on the file extension\r
+ * \r
+ * @param file Path file for which the import service is looked for\r
+ * @return Optiona IGenerigFileImport service which is able to handle the import of this type of file\r
+ */\r
public static Optional<IGenericFileImport> findServiceForFileExtension(Path file) {\r
String extension = "";\r
\r
return Optional.empty();\r
}\r
\r
- public static Map<String, Long> getPathsAndResources() {\r
+ /**\r
+ * Method for listing all current paths and their corresponding identifiers in Simantics database\r
+ * \r
+ * @return Map containing \r
+ */\r
+ public static Map<String, String> getPathsAndResources() {\r
try {\r
Path db = Activator.getDropinsFolder().resolve(DB_FILE);\r
if (!Files.exists(db))\r
try (InputStream stream = Files.newInputStream(db)) {\r
props.load(stream);\r
}\r
- Map<String, Long> result = Simantics.getSession().syncRequest(new UniqueRead<Map<String, Long>>() {\r
-\r
- @Override\r
- public Map<String, Long> perform(ReadGraph graph) throws DatabaseException {\r
- Map<String, Long> map = new HashMap<>();\r
- for (Map.Entry<Object, Object> entry : props.entrySet()) {\r
- String value = (String) entry.getValue();\r
- Long id = Long.valueOf(value);\r
- SerialisationSupport ss = graph.getService(SerialisationSupport.class);\r
- try {\r
- Resource r = ss.getResource(id);\r
- String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);\r
- map.put(name, id);\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- return map;\r
- }\r
- });\r
-\r
- return result;\r
- } catch (IOException | DatabaseException e) {\r
+ Map<String, String> map = new HashMap<>();\r
+ for (Map.Entry<Object, Object> entry : props.entrySet()) {\r
+ String value = (String) entry.getValue();\r
+ String key = (String) entry.getKey();\r
+ map.put(key, value);\r
+ }\r
+ return map;\r
+ } catch (IOException e) {\r
e.printStackTrace();\r
return Collections.emptyMap();\r
}\r