]> gerrit.simantics Code Review - simantics/platform.git/blob - GraphJavaFile.java
5dd6799de603812fea73541ee94ac0298d692b0a
[simantics/platform.git] / GraphJavaFile.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphfile.hack;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileFilter;\r
16 import java.io.FilenameFilter;\r
17 import java.io.IOException;\r
18 import java.net.MalformedURLException;\r
19 import java.net.URI;\r
20 import java.net.URL;\r
21 \r
22 import org.eclipse.core.runtime.IStatus;\r
23 import org.eclipse.core.runtime.Status;\r
24 import org.simantics.Simantics;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.Session;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.common.request.WriteRequest;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.request.Read;\r
32 import org.simantics.graphfile.Activator;\r
33 import org.simantics.graphfile.ontology.GraphFileResource;\r
34 import org.simantics.layer0.Layer0;\r
35 \r
36 /**\r
37  * This is a wrapper for Graph based files. \r
38  * \r
39  * DO NOT USE this with File(Input/Output)Stream. \r
40  * \r
41  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
42  *\r
43  */\r
44 public class GraphJavaFile extends File {\r
45         \r
46         private static final long serialVersionUID = 8213749101292672725L;\r
47         private Session session;\r
48         private Resource fileResource;\r
49         \r
50         public GraphJavaFile(Resource fileResource) {\r
51                 super("");\r
52                 this.fileResource = fileResource;\r
53                 this.session = Simantics.getSession();\r
54         }\r
55         \r
56         private byte[] getResourceData(ReadGraph graph) throws DatabaseException {\r
57                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
58                 return graph.getRelatedValue(fileResource, gf.HasFiledata);\r
59         }\r
60         \r
61         private byte[] getResourceData() throws DatabaseException {\r
62                 return Simantics.getSession().syncRequest(new Read<byte[]>() {\r
63                         @Override\r
64                         public byte[] perform(ReadGraph graph) throws DatabaseException {\r
65                                 return getResourceData(graph);\r
66                         }\r
67                 });\r
68         }\r
69         \r
70         @Override\r
71         public boolean canExecute() {\r
72                 return false;\r
73         }\r
74         \r
75         @Override\r
76         public boolean canRead() {\r
77                 return false;\r
78         }\r
79         \r
80         @Override\r
81         public boolean canWrite() {\r
82                 return false;\r
83         }\r
84         \r
85         @Override\r
86         public boolean createNewFile() throws IOException {\r
87                 return false;\r
88         }\r
89         \r
90         @Override\r
91         public boolean delete() {\r
92                 try {\r
93                         session.syncRequest(new WriteRequest() {\r
94                                 \r
95                                 @Override\r
96                                 public void perform(WriteGraph graph) throws DatabaseException {\r
97                                         graph.deny(fileResource);\r
98                                 }\r
99                         });\r
100                         return true;\r
101                 } catch (DatabaseException e) {\r
102                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to delete file resource " + fileResource, e));\r
103                         return false;\r
104                 }\r
105         }\r
106         \r
107         @Override\r
108         public void deleteOnExit() {\r
109                 \r
110         }\r
111         \r
112         @Override\r
113         public boolean equals(Object obj) {\r
114                 if (obj == null)\r
115                         return false;\r
116                 if (!obj.getClass().equals(getClass()))\r
117                         return false;\r
118                 GraphJavaFile other = (GraphJavaFile)obj;\r
119                 return fileResource.equals(other.fileResource);\r
120         }\r
121         \r
122         @Override\r
123         public File getAbsoluteFile() {\r
124                 return this;\r
125         }\r
126         \r
127         @Override\r
128         public boolean exists() {\r
129                 try {\r
130                         return session.syncRequest(new Read<Boolean>() {\r
131                                 @Override\r
132                                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
133                                         GraphFileResource gf = GraphFileResource.getInstance(graph);\r
134                                         return graph.getPossibleRelatedValue(fileResource, gf.HasFiledata) != null;\r
135                                 }\r
136                         });\r
137                 } catch (DatabaseException e) {\r
138                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "exists check failed for file resource " + fileResource, e));\r
139                         return false;\r
140                 }\r
141         }\r
142         \r
143         @Override\r
144         public String getAbsolutePath() {\r
145                 return null;\r
146         }\r
147         \r
148         @Override\r
149         public File getCanonicalFile() throws IOException {\r
150                 return this;\r
151         }\r
152         \r
153         @Override\r
154         public String getCanonicalPath() throws IOException {\r
155                 return null;\r
156         }\r
157         \r
158         @Override\r
159         public long getFreeSpace() {\r
160                 return 0;\r
161         }\r
162         \r
163         @Override\r
164         public String getName() {\r
165                 try {\r
166                         return Simantics.getSession().syncRequest(new Read<String>() {\r
167                                 @Override\r
168                                 public String perform(ReadGraph graph) throws DatabaseException {\r
169                                         Layer0 l0 = Layer0.getInstance(graph);\r
170                                         return graph.getRelatedValue(fileResource, l0.HasName);\r
171                                 }\r
172                         });\r
173                 } catch (DatabaseException e) {\r
174                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "getName failed for file resource " + fileResource, e));\r
175                         return e.getMessage();\r
176                 }\r
177         }\r
178         \r
179         @Override\r
180         public String getParent() {\r
181                 return null;\r
182         }\r
183         \r
184         @Override\r
185         public File getParentFile() {\r
186                 return null;\r
187         }\r
188         \r
189         @Override\r
190         public String getPath() {\r
191                 return null;\r
192         }\r
193         \r
194         @Override\r
195         public long getTotalSpace() {\r
196                 return 0;\r
197         }\r
198         \r
199         @Override\r
200         public long getUsableSpace() {\r
201                 return 0;\r
202         }\r
203         \r
204         @Override\r
205         public boolean isAbsolute() {\r
206                 return false;\r
207         }\r
208         \r
209         @Override\r
210         public boolean isDirectory() {\r
211                 return false;\r
212         }\r
213         \r
214         @Override\r
215         public boolean isFile() {\r
216                 return true;\r
217         }\r
218         \r
219         @Override\r
220         public int hashCode() {\r
221                 return fileResource.hashCode();\r
222         }\r
223         \r
224         @Override\r
225         public boolean isHidden() {\r
226                 return false;\r
227         }\r
228         \r
229         @Override\r
230         public long lastModified() {\r
231                 try {\r
232                         return Simantics.getSession().syncRequest(new Read<Long>() {\r
233                                 @Override\r
234                                 public Long perform(ReadGraph graph) throws DatabaseException {\r
235                                         GraphFileResource gf = GraphFileResource.getInstance(graph);\r
236                                         return graph.getRelatedValue(fileResource, gf.LastModified);\r
237                                 }\r
238                         });\r
239                 } catch (DatabaseException e) {\r
240                         return 0;\r
241                 }\r
242         }\r
243         \r
244         @Override\r
245         public long length() {\r
246                 try {\r
247                         return getResourceData().length;\r
248                 } catch (DatabaseException e) {\r
249                         return 0;\r
250                 }\r
251         }\r
252         \r
253         @Override\r
254         public boolean mkdir() {\r
255                 return false;\r
256         }\r
257         \r
258         @Override\r
259         public boolean mkdirs() {\r
260                 return false;\r
261         }\r
262         \r
263         @Override\r
264         public boolean renameTo(File dest) {\r
265                 return false;\r
266         }\r
267         \r
268         @Override\r
269         public boolean setReadable(boolean readable) {\r
270                 return false;\r
271         }\r
272         \r
273         @Override\r
274         public boolean setReadable(boolean readable, boolean ownerOnly) {\r
275                 return false;\r
276         }\r
277         \r
278         @Override\r
279         public boolean setWritable(boolean writable) {\r
280                 return false;\r
281         }\r
282         \r
283         @Override\r
284         public boolean setWritable(boolean writable, boolean ownerOnly) {\r
285                 return false;\r
286         }\r
287         \r
288         @Override\r
289         public boolean setExecutable(boolean executable) {\r
290                 return false;\r
291         }\r
292         \r
293         @Override\r
294         public boolean setExecutable(boolean executable, boolean ownerOnly) {\r
295                 return false;\r
296         }\r
297         \r
298         @Override\r
299         public boolean setLastModified(long time) {\r
300                 return false;\r
301         }\r
302         \r
303         @Override\r
304         public String[] list() {\r
305                 return null;\r
306         }\r
307         \r
308         @Override\r
309         public String[] list(FilenameFilter filter) {\r
310                 return null;\r
311         }\r
312         \r
313         @Override\r
314         public File[] listFiles() {\r
315                 return null;\r
316         }\r
317         \r
318         @Override\r
319         public File[] listFiles(FileFilter filter) {\r
320                 return null;\r
321         }\r
322         \r
323         @Override\r
324         public File[] listFiles(FilenameFilter filter) {\r
325                 return null;\r
326         }\r
327         \r
328         @Override\r
329         public boolean setReadOnly() {\r
330                 return false;\r
331         }\r
332         \r
333         @Override\r
334         public String toString() {\r
335                 return getName();\r
336         }\r
337         \r
338         @Override\r
339         public URL toURL() throws MalformedURLException {\r
340                 return null;\r
341         }\r
342         \r
343         @Override\r
344         public URI toURI() {\r
345                 return null;\r
346         }\r
347         \r
348         \r
349 \r
350 }\r