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