]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/IssueSourceUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / IssueSourceUtils.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.issues.common;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.ResourceSet;
24 import org.simantics.db.VirtualGraph;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
27 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
28 import org.simantics.db.common.request.BinaryRead;
29 import org.simantics.db.common.request.DependentInstances3;
30 import org.simantics.db.common.request.ResourceSetGraph;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.common.utils.NameUtils;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.changeset.MetadataUtils;
35 import org.simantics.db.layer0.genericrelation.DependencyChanges;
36 import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;
37 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
38 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
39 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;
40 import org.simantics.db.service.CollectionSupport;
41 import org.simantics.db.service.ManagementSupport;
42 import org.simantics.db.service.VirtualGraphSupport;
43 import org.simantics.issues.ontology.IssueResource;
44
45 public class IssueSourceUtils {
46         
47         public static final boolean DEBUG = false;
48         public static final boolean DEBUG_CHANGE_ENTRIES = false;
49         public static final boolean DEBUG_RESOURCES = false;
50
51         public static void update(WriteGraph graph, final Resource source) throws DatabaseException {
52
53                 IssueResource ISSUE = IssueResource.getInstance(graph);
54                 
55                 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
56                 if(revisionId == null) revisionId = 0L;
57                 
58                 ManagementSupport support = graph.getService(ManagementSupport.class);
59                 final long headRevision = support.getHeadRevisionId()+1;
60                 
61                 if(revisionId < headRevision) {
62                         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
63                 VirtualGraph vg = vgs.getWorkspacePersistent(IssueConstants.ISSUE_VG);
64                 graph.syncRequest(new WriteRequest(vg) {
65
66                                 @Override
67                                 public void perform(WriteGraph graph) throws DatabaseException {
68                                         IssueResource ISSUE = IssueResource.getInstance(graph);
69                                         graph.claimLiteral(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, headRevision, Bindings.LONG);
70                                         if(DEBUG)
71                                                 System.err.println("-update[" + NameUtils.getSafeName(graph, source) + "][" + headRevision + "]");
72                                 }
73
74                         });
75                 }
76                 
77         }
78         
79         public static boolean hasChanges(ReadGraph graph, DependencyChanges event, Resource root, Resource from) throws DatabaseException {
80                 Change[] changes = event.get(root);
81                 if(changes == null) return false;
82                 for(Change c : changes) {
83                         if(c instanceof ComponentAddition) {
84                                 return true;
85                         } else if(c instanceof ComponentRemoval) {
86                                 return true;
87                         } else if(c instanceof ComponentModification) {
88                                 return true;
89                         }
90                 }
91                 return false;
92         }
93
94     public static class Test3 extends BinaryRead<Resource, ResourceSet, Collection<Resource>> {
95
96         public Test3(Resource component, ResourceSet types) {
97             super(component, types);
98         }
99
100         @Override
101         public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
102             ResourceSetGraph rsg = graph.syncRequest(new DependentInstances3(parameter), TransientCacheListener.<ResourceSetGraph>instance());
103                 return rsg.resolve(graph, parameter2);
104         }
105
106     };
107
108         private static void test(ReadGraph graph, Resource component, ResourceSet types, Collection<Resource> result) throws DatabaseException {
109                 Collection<Resource> data = graph.syncRequest(new Test3(component, types), TransientCacheAsyncListener.<Collection<Resource>>instance());
110                 if(DEBUG_RESOURCES) {
111                         for(Resource r : data) {
112                                 System.err.println("IssueSourceUtils.test " + " " + NameUtils.getSafeName(graph, component) + " " + NameUtils.getSafeName(graph, types) + " " + NameUtils.getSafeName(graph, r));
113                         }
114                 }
115                 result.addAll(data);
116         }
117
118         public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource model, Resource from, Set<Resource> types, DependencyChanges event) throws DatabaseException {
119
120                 Change[] changes = event.get(model);
121                 if(changes == null) return Collections.emptySet();
122
123                 CollectionSupport cs = graph.getService(CollectionSupport.class);
124                 ResourceSet typeSet = cs.getResourceSet(graph, types);
125                 
126                 Set<Resource> result = new HashSet<Resource>();
127                 for(Change c : changes) {
128                         processChange(graph, c, typeSet, result);
129                 }
130
131                 return result;
132                 
133         }
134
135         public static void processChange(ReadGraph graph, Change c, ResourceSet types, Set<Resource> result) throws DatabaseException {
136                 if(DEBUG_CHANGE_ENTRIES)
137                         System.err.println("-processChange: " + c.toString(graph));
138                 if(c instanceof ComponentAddition) {
139                         ComponentAddition addition = (ComponentAddition)c;
140                         test(graph, addition.component, types, result);
141                 } else if(c instanceof ComponentRemoval) {
142                         ComponentRemoval removal = (ComponentRemoval)c;
143                         result.add(removal.component);
144                 } else if(c instanceof ComponentModification) {
145                         ComponentModification modification = (ComponentModification)c;
146                         test(graph, modification.component, types, result);
147                 }
148                 
149         }
150
151         public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource source, Resource model, Resource from, Set<Resource> types) throws DatabaseException {
152
153                 IssueResource ISSUE = IssueResource.getInstance(graph);
154                 
155                 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
156                 if(revisionId == null) revisionId = 0L;
157
158                 if(DEBUG)
159                         System.err.println("getChangedDependencies[" + NameUtils.getSafeName(graph, source) + "][" + revisionId + "]");
160                 
161                 Map<Resource, Collection<Change>> index = MetadataUtils.getDependencyChangesFrom(graph, revisionId+1);
162
163                 CollectionSupport cs = graph.getService(CollectionSupport.class);
164                 ResourceSet typeSet = cs.getResourceSet(graph, types);
165
166                 Collection<Change> modelChanges = index.get(model);
167                 if(modelChanges == null) return Collections.emptySet();
168                 
169                 Set<Resource> result = new HashSet<Resource>();
170                 for(Change c : modelChanges) {
171                         processChange(graph, c, typeSet, result);
172                 }
173
174                 return result;
175                 
176         }
177         
178 }