]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultValuations.java
Prevent paste to resources that are `L0.Entity_published`
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultValuations.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.db.layer0.adapter.impl;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.uri.ResourceToURI;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.Valuations;
19 import org.simantics.simulation.ontology.SimulationResource;
20 import org.simantics.utils.datastructures.Pair;
21
22 public class DefaultValuations implements Valuations {
23
24         final private Resource resource;
25         
26         public DefaultValuations(Resource module) {
27                 this.resource = module;
28         }
29
30         public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {
31                 int position = path.indexOf("/", startPosition);
32                 String prefix = path.substring(0, position);
33 //              System.out.println("prefix=" + prefix);
34                 Resource r = graph.getResource(prefix);
35                 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {
36                         return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length())); 
37                 } else {
38                         return splitModelAndStructure(graph, path, position + 1);
39                 }
40         }
41
42         public static Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {
43                 Resource r = graph.getResource(path);
44                 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {
45                         return new Pair<String, String>(path, "");
46                 } else {
47                         return splitModelAndStructure(graph, path, 8);
48                 }
49         }
50         
51         @Override
52         public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment) throws DatabaseException {
53 //              System.out.println("getValue " + module);
54                 String path = graph.syncRequest(new ResourceToURI(resource));
55 //              System.out.println("path=" + path);
56                 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);
57 //              System.out.println("mas=" + modelAndStructure);
58                 // TODO: fixme please
59                 try {
60                         return graph.getResource(modelAndStructure.first + "/" + experiment + "/" + modelAndStructure.second);
61                 } catch (DatabaseException e) {
62                         try {
63                                 return graph.getResource(modelAndStructure.first + "/BaseRealization/" + modelAndStructure.second);
64                         } catch (DatabaseException e2) {
65                                 return null;
66                         }
67                 }
68         }
69
70         @Override
71         public Resource getValue(ReadGraph graph, String variableIdentityPrefix, String experiment, String suffix) throws DatabaseException {
72 //              System.out.println("getValue " + resource);
73                 String path = graph.syncRequest(new ResourceToURI(resource));
74 //              System.out.println("path=" + path);
75                 Pair<String, String> modelAndStructure = splitModelAndStructure(graph, path);
76 //              System.out.println("mas=" + modelAndStructure);
77                 String prefix = modelAndStructure.second.isEmpty() ? "" : "/" + modelAndStructure.second;
78                 String uri = modelAndStructure.first + "/" + experiment + prefix + (suffix.isEmpty() ? "" : "/" + suffix);
79 //              System.out.println("defaultValuations uri=" + uri);
80                 return graph.getResource(uri);
81         }
82
83         
84 }