]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java
Fixed all line endings of the repository
[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     public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;
63     
64     @Override
65     public void remove(String resourceId) throws Exception {
66         Optional<Resource> resource = deserialize(resourceId);
67         resource.ifPresent(res -> {
68             try {
69                 Simantics.sync(new WriteRequest() {
70
71                     @Override
72                     public void perform(WriteGraph graph) throws DatabaseException {
73                         RemoverUtil.remove(graph, resource.get());
74                     }
75                 });
76             } catch (Exception e) {
77                 throw new RuntimeDatabaseException(e);
78             }
79         });
80     }
81
82     public String serialize(Resource resource) {
83         return Long.toString(resource.getResourceId());
84     }
85
86     public Optional<Resource> deserialize(String serialized) throws Exception {
87         long resourceId = Long.valueOf(serialized);
88
89         Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
90
91             @Override
92             public Resource perform(ReadGraph graph) throws DatabaseException {
93                 SerialisationSupport support = graph.getService(SerialisationSupport.class);
94                 Resource resource = support.getResource(resourceId);
95                 return resource;
96             }
97         });
98         return Optional.ofNullable(resource);
99     }
100     
101     private static Resource resolveParent(Resource parent, Path name) {
102         if (name.getParent() == null) {
103             return Simantics.getProjectResource();
104         } else {
105             name = name.getParent();
106             parent = resolveParent(parent, name);
107         }
108         final Resource newParent = parent;
109         final String folderName = name.getFileName().toString();
110
111         try {
112             return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
113
114                 @Override
115                 public Resource perform(ReadGraph graph) throws DatabaseException {
116                     Layer0 L0 = Layer0.getInstance(graph);
117                     Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));
118                     for (Resource library : libraries) {
119                         String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);
120                         if (libraryName.equals(folderName)) {
121                             return library;
122                         }
123                     }
124                     return null;
125                 }
126             });
127         } catch (DatabaseException e) {
128             e.printStackTrace();
129             return null;
130         }
131     }
132
133 }