]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java
Some fileimport enhancements - Add support for Excel import
[simantics/platform.git] / bundles / org.simantics.fileimport / src / org / simantics / fileimport / SimanticsResourceFileImport.java
1 package org.simantics.fileimport;
2
3 import java.nio.file.Path;
4 import java.util.Collection;
5 import java.util.Optional;
6
7 import org.simantics.Simantics;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.request.ObjectsWithType;
13 import org.simantics.db.common.request.UniqueRead;
14 import org.simantics.db.common.request.WriteRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.exception.RuntimeDatabaseException;
17 import org.simantics.db.layer0.util.RemoverUtil;
18 import org.simantics.db.request.Read;
19 import org.simantics.db.service.SerialisationSupport;
20 import org.simantics.layer0.Layer0;
21
22 /**
23  * Most of the implementations should extend this class which handles the storing of
24  * the identifier of the imported entity and the removing of the entity
25  * 
26  * @author Jani Simomaa
27  *
28  */
29 public abstract class SimanticsResourceFileImport implements IGenericFileImport {
30
31     @Override
32     final public Optional<String> perform(Path file) throws Exception {
33         
34         Path dropins = Activator.getDropinsFolder();
35         
36         Path parts;
37         if (file.toAbsolutePath().toString().startsWith(dropins.toAbsolutePath().toString())) {
38                 parts = dropins.relativize(file);
39         } else {
40                 parts = file.getFileName();
41         }
42         
43         Resource parent = resolveParent(null, parts);
44         if (parent == null)
45             return Optional.empty();
46         Optional<Resource> imported = perform(parent, file); 
47         if (imported.isPresent()) {
48             return Optional.of(serialize(imported.get()));
49         } else {
50             return Optional.empty();
51         }
52     }
53     
54     /**
55      * Performs the import for the given file
56      * 
57      * @param parent Resource parent of the imported entity in Simantics database
58      * @param file Path file location of file
59      * @return Optional Resource of the imported entity in Simantics database
60      * @throws Exception
61      */
62     
63     @Override
64     public void remove(String resourceId) throws Exception {
65         Optional<Resource> resource = deserialize(resourceId);
66         resource.ifPresent(res -> {
67             try {
68                 Simantics.sync(new WriteRequest() {
69
70                     @Override
71                     public void perform(WriteGraph graph) throws DatabaseException {
72                         RemoverUtil.remove(graph, resource.get());
73                     }
74                 });
75             } catch (Exception e) {
76                 throw new RuntimeDatabaseException(e);
77             }
78         });
79     }
80
81     public String serialize(Resource resource) {
82         return Long.toString(resource.getResourceId());
83     }
84
85     public Optional<Resource> deserialize(String serialized) throws Exception {
86         long resourceId = Long.valueOf(serialized);
87
88         Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
89
90             @Override
91             public Resource perform(ReadGraph graph) throws DatabaseException {
92                 SerialisationSupport support = graph.getService(SerialisationSupport.class);
93                 Resource resource = support.getResource(resourceId);
94                 return resource;
95             }
96         });
97         return Optional.ofNullable(resource);
98     }
99     
100     private static Resource resolveParent(Resource parent, Path name) {
101         if (name.getParent() == null) {
102             return Simantics.getProjectResource();
103         } else {
104             name = name.getParent();
105             parent = resolveParent(parent, name);
106         }
107         final Resource newParent = parent;
108         final String folderName = name.getFileName().toString();
109
110         try {
111             return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
112
113                 @Override
114                 public Resource perform(ReadGraph graph) throws DatabaseException {
115                     Layer0 L0 = Layer0.getInstance(graph);
116                     Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));
117                     for (Resource library : libraries) {
118                         String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);
119                         if (libraryName.equals(folderName)) {
120                             return library;
121                         }
122                     }
123                     return null;
124                 }
125             });
126         } catch (DatabaseException e) {
127             e.printStackTrace();
128             return null;
129         }
130     }
131     
132     public abstract Resource defaultParentResource();
133
134 }