]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/tester/CollectionResourcePropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / tester / CollectionResourcePropertyTester.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.tester;\r
13 \r
14 import java.util.Arrays;\r
15 \r
16 import org.eclipse.core.expressions.PropertyTester;\r
17 import org.simantics.DatabaseJob;\r
18 import org.simantics.Simantics;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.request.UniqueRead;\r
22 import org.simantics.db.common.utils.NameUtils;\r
23 import org.simantics.db.common.utils.RequestUtil;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.exception.ResourceNotFoundException;\r
26 import org.simantics.db.management.ISessionContext;\r
27 import org.simantics.scl.reflection.OntologyVersions;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
30 \r
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class CollectionResourcePropertyTester extends PropertyTester {\r
35 \r
36     private static final boolean DEBUG = false;\r
37 \r
38     /**\r
39      * Tests if the received resource is an instance of any of the URIs\r
40      * listed in the arguments.\r
41      */\r
42     protected static final String RESOURCE_TYPES = "resourceTypes";\r
43 \r
44     private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();\r
45     \r
46     @Override\r
47     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
48         if (DEBUG)\r
49             System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);\r
50 \r
51         final Resource[] resources = ResourceAdaptionUtils.toResources(receiver);\r
52         if (resources == null)\r
53             return false;\r
54 \r
55         ISessionContext ctx = Simantics.getSessionContext();\r
56         if (ctx == null)\r
57             return false;\r
58 \r
59         if (DatabaseJob.inProgress())\r
60             return false;\r
61 \r
62         try {\r
63             return RequestUtil.trySyncRequest(\r
64                     ctx.getSession(),\r
65                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
66                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
67                     false,\r
68                     new UniqueRead<Boolean>() {\r
69                 @Override\r
70                 public Boolean perform(ReadGraph g) throws DatabaseException {\r
71                     return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));\r
72                 }\r
73             });\r
74         } catch (DatabaseException | InterruptedException e) {\r
75             // Purposefully not logging these exceptions, there might be way too\r
76             // many even under normal circumstances.\r
77             // TODO: add debug tracing options controlling the printing of these exceptions\r
78             return false;\r
79         }\r
80     }\r
81 \r
82     protected boolean doTest(ReadGraph g, Resource[] resources, String property, Object[] args, Object expectedValue) throws DatabaseException {\r
83         if (RESOURCE_TYPES.equals(property)) {\r
84                 loop: for(Resource r : resources) {\r
85                         if (DEBUG)\r
86                                 System.out.println("** " + NameUtils.getSafeName(g, r));\r
87                         try {\r
88                                 for (int i = 0; i < args.length; i++) {\r
89                                         if (g.isInstanceOf(r, g.getResource(VERSIONS.currentVersion((String) args[i])))) {\r
90                                                 if (DEBUG)\r
91                                                         System.out.println("IS INSTANCE OF " + args[i]);\r
92                                                 continue loop;\r
93                                         }\r
94                                 }\r
95                         } catch (ResourceNotFoundException e) {\r
96                                 /* This is a natural situation (database does not contain all resources\r
97                    referred in plugins). No error reporting.\r
98                                  */\r
99                                 return false;\r
100                         }\r
101                         if (DEBUG)\r
102                                 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));\r
103                         return false;\r
104                 }\r
105             return true;\r
106         }\r
107         return false;\r
108     }\r
109 \r
110 }\r