]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/StructuralRelatedObjectAccessor.java
Switch from org.apache.log4j to org.slf4j.
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / rules / domain / StructuralRelatedObjectAccessor.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 java.util.ArrayList;
15 import java.util.List;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.objmap.exceptions.MappingException;
24 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
25 import org.simantics.objmap.structural.StructuralResource;
26
27
28 public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
29
30     static Logger LOGGER = LoggerFactory.getLogger(StructuralRelatedObjectAccessor.class);
31     
32         Resource relation;
33         boolean useTypeResource;
34         
35         private boolean preventStructuralChanges = true;
36         private boolean preventStructuralRootChanges = true;
37         
38         public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {
39                 this.relation = relation;
40                 this.useTypeResource = useTypeResource;
41         }
42         
43         public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
44                 this.relation = relation;
45                 this.useTypeResource = useTypeResource;
46                 this.preventStructuralChanges = preventStructuralChanges;
47                 this.preventStructuralRootChanges = preventStructuralRootChanges;
48         }
49         
50  private boolean preventChange(StructuralResource element) {
51         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       
52     }
53
54         @Override
55         public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
56                 try {
57                         
58                         LOGGER.info("        RelatedObjectAccessor.get");
59                         
60                         if (!element.isStructural())
61                                 return null;
62                         Resource instance = StructuralUtils.getContainingInstance(element);
63                             
64                         Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
65
66                      if (publicRelation == null)
67                         return null;
68                         Resource r =  g.getPossibleObject(instance, publicRelation);
69                         if (r == null)
70                                 return null;
71                         List<Resource> context = new ArrayList<Resource>();
72                         for (int i = 0; i < element.getContext().size()-1; i++)
73                                 context.add(element.getContext().get(i));
74                         if (StructuralUtils.isStructuralInstance(g, r)) {
75                                 return new StructuralResource(g, r, context,r);
76                         } else {
77                                 return new StructuralResource(g, r, context);
78                         }
79                 } catch (DatabaseException e) {
80                         throw new MappingException(e);
81                 }
82         }
83         
84         @Override
85         public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)
86                         throws MappingException {
87                 try {
88                     LOGGER.info("        RelatedObjectAccessor.set");
89                     Resource instance = StructuralUtils.getContainingInstance(element);
90                     Resource publicRelation = null;
91                     if (instance == null)
92                         return false;
93                     publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
94                     if (value == null) {
95                         if (publicRelation == null)
96                                 return false;
97                         if (preventChange(element))
98                                 return false;
99                         g.deny(instance, publicRelation);
100                         return true;
101                     } else {
102                         if (publicRelation == null) {
103                                 if (preventChange(element))
104                                         return false;
105                                 publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
106                                 g.claim(instance, publicRelation, value.getResource());
107                                 return true;
108                         } else {
109                                 Resource r = g.getPossibleObject(instance, publicRelation);
110                                 if (r == null) {
111                                         if (preventChange(element))
112                                                 return false;
113                                         g.claim(instance, publicRelation, value.getResource());
114                                         return true;
115                                 } else {
116                                         if (r.equals(value.getResource()))
117                                                 return false;
118                                         if (preventChange(element))
119                                                 return false;
120                                         g.deny(instance, publicRelation);
121                                         g.claim(instance, publicRelation, value.getResource());
122                                         return true;
123                                 }
124                         }
125                     }
126                 } catch (DatabaseException e) {
127                         throw new MappingException(e);
128                 }
129                 
130         }
131
132 }