1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.validation;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collections;
\r
16 import java.util.List;
\r
18 import org.simantics.databoard.Bindings;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.issue.StandardIssue;
\r
23 import org.simantics.db.common.utils.ListUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.variable.Variable;
\r
26 import org.simantics.layer0.Layer0;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
30 * Issue that can have string contexts.
\r
32 * String contexts are written to a graph like resource contexts and can be accessed
\r
33 * in issue description functions. This eliminates the need for making complex calculations
\r
34 * for obtaining values that have already been calculated in the issue source function.
\r
36 * @author Teemu Lempinen
\r
39 public class IssueWithStringContext extends StandardIssue {
\r
41 String[] stringContexts;
\r
44 * Creates an issue with one resource context and an arbitrary number of string contexts
\r
47 * @param stringContexts
\r
49 public IssueWithStringContext(Resource type, Resource context, String... stringContexts) {
\r
50 super(type, context);
\r
51 this.stringContexts = stringContexts;
\r
55 * Get the string contexts of this issue
\r
56 * @return string contexts
\r
58 public String[] getStringContexts() {
\r
59 return stringContexts;
\r
63 * Overridden function from StandardResource. This writes a list of string contexts to the database.
\r
65 public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException {
\r
66 super.writeAdditionalContext(graph, issue);
\r
68 // Add possible string contexts to the contexts array
\r
69 if(stringContexts != null && stringContexts.length > 0) {
\r
70 SysdynResource SR = SysdynResource.getInstance(graph);
\r
71 Layer0 L0 = Layer0.getInstance(graph);
\r
72 List<Resource> contexts = new ArrayList<Resource>();
\r
73 for(String s : stringContexts) {
\r
74 Resource r = graph.newResource();
\r
75 graph.claim(r, L0.InstanceOf, L0.String);
\r
76 graph.claimValue(r, s, Bindings.STRING);
\r
79 graph.claim(issue, SR.Validations_Issue_stringContexts, ListUtils.create(graph, L0.List, contexts));
\r
85 * Gets string contexts from IssueWithStringContexts
\r
86 * @param graph ReadGraph
\r
87 * @param property issue property
\r
88 * @return String contexts of issue or empty list if none is found
\r
89 * @throws DatabaseException
\r
91 public static List<String> getStringContexts(ReadGraph graph, Variable property) throws DatabaseException {
\r
92 SysdynResource SR = SysdynResource.getInstance(graph);
\r
93 Variable issueVariable = property.getParent(graph);
\r
94 Resource issueResource = issueVariable.getRepresents(graph);
\r
95 Resource list = graph.getPossibleObject(issueResource, SR.Validations_Issue_stringContexts);
\r
97 return Collections.emptyList();
\r
99 List<Resource> stringResources = ListUtils.toList(graph, list);
\r
100 ArrayList<String> result = new ArrayList<String>(stringResources.size());
\r
101 for(Resource r : stringResources) {
\r
102 result.add((String)graph.getValue(r, Bindings.STRING));
\r