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=a3a6a8c41efe415f37b6c5e102d376e7bbd311d3;hp=e07dfaadd5d5999af4b6ddbfe2256bb623ee062a;hb=b809a171b6dfb81ed9ef9e84870dcbcbc5912f0e;hpb=96bb7ef9cbe42d82eb58306d8f9b62392cc29ba8 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 e07dfaadd..a3a6a8c41 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java @@ -17,19 +17,19 @@ import java.util.function.Consumer; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; -import org.simantics.Simantics; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.UniqueRead; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.service.SerialisationSupport; -import org.simantics.layer0.Layer0; +import org.simantics.fileimport.dropins.FileImportDropins; +/** + * Utility class for Simantics File import functions + * + * @author Jani Simomaa + * + */ public class FileImportService { public static final String DB_FILE = ".simanticsdb"; - public static List getFileImportServices() { + private static List getFileImportServices() { ServiceReference[] serviceReferences = new ServiceReference[0]; try { serviceReferences = Activator.getContext().getAllServiceReferences(IGenericFileImport.class.getName(), @@ -48,6 +48,11 @@ public class FileImportService { return services; } + /** + * Lists all supported file extensions which have a registered service for handling the import + * + * @return Map containing the extension and the description of the extension in that order + */ public static Map supportedExtensionsWithFilters() { List services = getFileImportServices(); Map extensionsWithFilters = new HashMap<>(); @@ -57,6 +62,12 @@ public class FileImportService { return extensionsWithFilters; } + /** + * 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 callback Optional callback which can be used to catch Throwables thrown in the import process + */ public static void performFileImport(Path file, Optional> callback) { if (file.getFileName().toString().equals(DB_FILE)) return; @@ -75,6 +86,13 @@ public class FileImportService { }); } + + /** + * 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 + * + * @param file Path file that was deleted + * @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 -> { @@ -143,6 +161,12 @@ public class FileImportService { static final String FOLDER = "_folder_"; + /** + * 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 + */ public static Optional findServiceForFileExtension(Path file) { String extension = ""; @@ -174,7 +198,12 @@ public class FileImportService { return Optional.empty(); } - public static Map getPathsAndResources() { + /** + * Method for listing all current paths and their corresponding identifiers in Simantics database + * + * @return Map containing + */ + public static Map getPathsAndResources() { try { Path db = Activator.getDropinsFolder().resolve(DB_FILE); if (!Files.exists(db)) @@ -183,29 +212,14 @@ public class FileImportService { try (InputStream stream = Files.newInputStream(db)) { props.load(stream); } - Map result = Simantics.getSession().syncRequest(new UniqueRead>() { - - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - Map map = new HashMap<>(); - for (Map.Entry entry : props.entrySet()) { - String value = (String) entry.getValue(); - Long id = Long.valueOf(value); - SerialisationSupport ss = graph.getService(SerialisationSupport.class); - try { - Resource r = ss.getResource(id); - String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName); - map.put(name, id); - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - return map; - } - }); - - return result; - } catch (IOException | DatabaseException e) { + Map map = new HashMap<>(); + for (Map.Entry entry : props.entrySet()) { + String value = (String) entry.getValue(); + String key = (String) entry.getKey(); + map.put(key, value); + } + return map; + } catch (IOException e) { e.printStackTrace(); return Collections.emptyMap(); }