]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/validation/ValidationUtils.java
Added resourceId and GUID to diagram DnD content
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / validation / ValidationUtils.java
1 package org.simantics.db.layer0.validation;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.simantics.db.Issue;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.utils.Functions;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
17 import org.simantics.db.layer0.util.DomainProcessor3;
18 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
19 import org.simantics.layer0.Layer0;
20
21 public class ValidationUtils {
22
23     /**
24      * Performs L0.Constraint validations for the specified resource and puts
25      * the resulting issues in the specified {@link Issue} set. If the specified
26      * issue set is <code>null</code>, a new set will be created and returned.
27      * Otherwise the method function will return the same set as its return
28      * value.
29      * 
30      * @param graph
31      *            database read access handle
32      * @param r
33      *            resource to validate against ontology-defined constraints
34      * @param result
35      *            a set for the resulting issues or <code>null</code> to
36      *            allocate a new set if necessary
37      * @return the discovered set of issues in <code>r</code>
38      * @throws DatabaseException
39      */
40     public static Set<Issue> validateConstraints(ReadGraph graph, Resource r, Set<Issue> result) throws DatabaseException {
41         Layer0 L0 = Layer0.getInstance(graph);
42
43         for (Resource constraint : graph.syncRequest(new GetConstraints(r))) {
44             try {
45                 Resource function = graph.getSingleObject(constraint, L0.Constraint_Validator);
46                 @SuppressWarnings("unchecked")
47                 List<Issue> issues = (List<Issue>)Functions.exec(graph, function, graph, r);
48                 if (issues != null && !issues.isEmpty()) {
49                     if (result == null)
50                         result = new THashSet<Issue>();
51                     result.addAll(issues);
52                 }
53             } catch (Throwable t) {
54                 Logger.defaultLogError(t);
55             }
56         }
57
58         return result != null ? result : Collections.<Issue>emptySet();
59     }
60
61     public static Set<Issue> validateConstraints(ReadGraph graph, Resource r) throws DatabaseException {
62         return validateConstraints(graph, r, null);
63     }
64
65     public static Set<Issue> validateConstraintsForDomain(ReadGraph graph, Resource r) throws DatabaseException {
66         Set<Issue> result = null;
67
68         DomainProcessor3 dp = ModelTransferableGraphSourceRequest.getDomainOnly(graph, null, r);
69         for(Map.Entry<Resource,ExtentStatus> status : dp.getStatus().entrySet()) {
70             if(ExtentStatus.INTERNAL.equals(status.getValue())) {
71                 Resource rr = status.getKey();
72                 Set<Issue> issues = validateConstraints(graph, rr, result);
73                 if (!issues.isEmpty())
74                     result = issues;
75             }
76         }
77
78         return result != null ? result : Collections.<Issue>emptySet();
79     }
80
81 }