]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.common/src/org/simantics/issues/common/AllVisibleIssues.java
Revert "Support enumerated property types in UC interface"
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / AllVisibleIssues.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 gnu.trove.map.hash.TObjectByteHashMap;
15 import gnu.trove.set.hash.THashSet;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Set;
20
21 import org.simantics.Simantics;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.BinaryRead;
25 import org.simantics.db.common.request.ObjectsWithType;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.Instances;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.layer0.variable.Variables;
30 import org.simantics.issues.common.preferences.IssuePrefs;
31 import org.simantics.issues.ontology.IssueResource;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.operation.Layer0X;
34 import org.simantics.simulation.ontology.SimulationResource;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class AllVisibleIssues extends BinaryRead<Resource, Boolean, Set<Variable>> {
40
41     public AllVisibleIssues(Resource project) {
42         super(project, Boolean.FALSE);
43     }
44
45     public AllVisibleIssues(Resource project, boolean onlyUnresolved) {
46         super(project, onlyUnresolved);
47     }
48
49     @Override
50     public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
51         Layer0 L0 = Layer0.getInstance(graph);
52         Layer0X L0X = Layer0X.getInstance(graph);
53         IssueResource ISSUE = IssueResource.getInstance(graph);
54         SimulationResource SIMU = SimulationResource.getInstance(graph);
55
56         Resource project = Simantics.getProjectResource();
57         boolean showHidden = false;
58         boolean showNormal = true;
59         boolean showUser = true;
60         if (project != null) {
61             showHidden = IssuePrefs.showHiddenIssues(graph, project);
62             showNormal = IssuePrefs.showNormalIssues(graph, project);
63             showUser = IssuePrefs.showUserIssues(graph, project);
64         }
65
66         Instances issueIndex = graph.getPossibleAdapter(ISSUE.Issue, Instances.class);
67
68         // Cache for source activeness.
69         // 0 == not in cache, 1 == false, 2 == true
70         TObjectByteHashMap<Resource> sourceActivenessCache = new TObjectByteHashMap<Resource>();
71
72         Set<Variable> result = new THashSet<Variable>(1013);
73
74         for (Resource model : graph.syncRequest(new ObjectsWithType(parameter, L0X.Activates, SIMU.Model))) {
75             Collection<Resource> modelIssues = graph.syncRequest(new ObjectsWithType(model, L0.ConsistsOf, ISSUE.Issue));
76             Collection<Resource> indexedIssues = issueIndex != null ? issueIndex.find(graph, model) : Collections.<Resource>emptyList();
77             Collection<Resource> issues = !indexedIssues.isEmpty() ? new THashSet<Resource>(modelIssues.size() + indexedIssues.size()) : modelIssues;
78             if (!indexedIssues.isEmpty()) {
79                 issues.addAll(modelIssues);
80                 issues.addAll(indexedIssues);
81             }
82
83             for (Resource issue : issues) {
84                 // Filter out unwanted material
85                 boolean resolved = graph.hasStatement(issue, ISSUE.Resolved);
86                 if (parameter2 && resolved)
87                     continue;
88                 boolean hidden = graph.hasStatement(issue, ISSUE.Hidden);
89                 boolean user = graph.hasStatement(issue, ISSUE.UserIssue);
90                 boolean normal = !hidden && !user;
91                 if (!showHidden && hidden)
92                     continue;
93                 if (!showUser && user)
94                     continue;
95                 if (!showNormal && normal)
96                     continue;
97
98                 Resource source = graph.getPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse);
99                 if (source != null) {
100                     byte cache = sourceActivenessCache.get(source);
101                     boolean active = cache == 2 ? true : false;
102                     if (cache == 0) {
103                         active = Boolean.TRUE.equals(graph.getPossibleRelatedValue(source, ISSUE.IssueSource_active));
104                         sourceActivenessCache.put(source, active ? (byte) 2 : (byte) 1);
105                     }
106                     if (!active)
107                         continue;
108                 }
109
110                 Variable var = Variables.getPossibleVariable(graph, issue);
111                 if (var != null)
112                     result.add(var);
113             }
114         }
115
116         // System.out.println("AllActiveIssues returned " + result.size());
117         return result;
118     }
119
120 }