]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedObjectAccessor.java
Use trace level debug messages with ObjMap
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / rules / domain / RelatedObjectAccessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 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.objmap.structural.rules.domain;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.objmap.exceptions.MappingException;
21 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
22 import org.simantics.objmap.structural.StructuralResource;
23
24
25 public class RelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
26
27     static Logger LOGGER = LoggerFactory.getLogger(RelatedObjectAccessor.class);
28     
29         Resource relation;
30         boolean useTypeResource;
31         
32         private boolean preventStructuralChanges = true;
33         private boolean preventStructuralRootChanges = true;
34         
35         public RelatedObjectAccessor(Resource relation, boolean useTypeResource) {
36                 this.relation = relation;
37                 this.useTypeResource = useTypeResource;
38         }
39         
40         public RelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
41                 this.relation = relation;
42                 this.useTypeResource = useTypeResource;
43                 this.preventStructuralChanges = preventStructuralChanges;
44                 this.preventStructuralRootChanges = preventStructuralRootChanges;
45         }
46         
47  private boolean preventChange(StructuralResource element) {
48         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       
49     }
50
51         @Override
52         public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
53                 try {
54                     LOGGER.trace("        RelatedObjectAccessor.get");
55                     Resource res = getServiceResource(g, element);
56                     if (res == null)
57                         return null;
58                         Resource r =  g.getPossibleObject(res, relation);
59                         if (r == null)
60                                 return null;
61                         if (StructuralUtils.isStructuralInstance(g, r)) {
62                                 return new StructuralResource(g, r, element.getContext(),r);
63                         } else {
64                                 return new StructuralResource(g, r, element.getContext());
65                         }
66                 } catch (DatabaseException e) {
67                         throw new MappingException(e);
68                 }
69         }
70         
71         @Override
72         public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value)
73                         throws MappingException {
74                 try {
75                     LOGGER.trace("        RelatedObjectAccessor.set");
76                     Resource element = getServiceResource(g, selement);
77                     if (element == null)
78                         return false;
79                     Resource resource = g.getPossibleObject(element, relation);
80                         if(resource == null) {
81                             if(value == null)
82                                 return false;
83                             if (preventChange(selement))
84                                         return false;
85                             g.claim(element, relation, value.getResource());
86                             return true;
87                         }
88                         else if(resource.equals(value.getResource()))
89                             return false;
90                         else {
91                                 if (preventChange(selement))
92                                         return false;
93                             g.deny(element, relation);
94                             if(value != null)
95                                 g.claim(element, relation, value.getResource());
96                             return true;
97                         }
98                 } catch (DatabaseException e) {
99                         throw new MappingException(e);
100                 }
101                 
102         }
103         
104                 
105         
106         private Resource getServiceResource(ReadGraph g, StructuralResource element) {
107                 if(!useTypeResource)
108                         return element.getResource();
109                 return element.getTypeResource();
110         }
111
112 }