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