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