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