]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInput.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceInput.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.workbench;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Session;\r
16 import org.simantics.db.common.request.Queries;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 \r
19 \r
20 /**\r
21  * A wrapper class for parsing and generating the information given to Simantics\r
22  * views as a parameter through view secondary ID's.\r
23  * \r
24  * <p>\r
25  * To parse the input data (given in the secondary ID) just invoke:<br>\r
26  * <code>new ResourceInput(secondaryIdString)</code>\r
27  * </p>\r
28  * \r
29  * <p>\r
30  * To generate a secondary ID string based an input resource ID, invoke:<br>\r
31  * <code>new ResourceInput(inputResourceId).marshall()</code>\r
32  * </p>\r
33  * \r
34  * @author Tuukka Lehtonen\r
35  * \r
36  * @see ResourceViewPartUtils\r
37  */\r
38 public class ResourceInput {\r
39 \r
40     private static final String SUFFIX_SEPARATOR = "@";\r
41 \r
42     private static final String EMPTY_SUFFIX = "";\r
43 \r
44     private final String        inputResourceId;\r
45 \r
46     private final String        suffix;\r
47 \r
48     /**\r
49      * Construct a new input based on ID's.\r
50      * Use this to serialize ID data into an input string.\r
51      * \r
52      * @param randomAccessResourceId\r
53      */\r
54     public ResourceInput(String randomAccessResourceId) {\r
55         this.inputResourceId = randomAccessResourceId;\r
56         this.suffix = EMPTY_SUFFIX;\r
57     }\r
58 \r
59     /**\r
60      * Construct a new input based on ID's. Use this to serialize ID data into\r
61      * an input string.\r
62      * \r
63      * @param randomAccessResourceId\r
64      * @param uniqueSuffix a unique suffix to add to the secondary ID or\r
65      *        <code>null</code> to ignore the unique suffix\r
66      */\r
67     public ResourceInput(String randomAccessResourceId, String uniqueSuffix) {\r
68         this.inputResourceId = randomAccessResourceId;\r
69         this.suffix = (uniqueSuffix == null) ? EMPTY_SUFFIX : uniqueSuffix;\r
70     }\r
71 \r
72     /**\r
73      * Returns a new ResourceInput instance with the specified ID as\r
74      * input. This method exists in order to make sure that the client can get a\r
75      * clone of the original ResourceInput in every way except for the\r
76      * input ID in a unified fashion.\r
77      * \r
78      * @param newInputResourceId the new input resource ID\r
79      * @return new ResourceInput instance with the specified input resource ID\r
80      */\r
81     public ResourceInput changeInput(String newRandomAccessResourceId) {\r
82         if (suffix == EMPTY_SUFFIX)\r
83             return new ResourceInput(newRandomAccessResourceId);\r
84         return new ResourceInput(newRandomAccessResourceId, suffix);\r
85     }\r
86 \r
87     public String getInputResourceId() {\r
88         return inputResourceId;\r
89     }\r
90 \r
91     public String getSuffix() {\r
92         return suffix;\r
93     }\r
94 \r
95     @Override\r
96     public String toString() {\r
97         return marshall();\r
98     }\r
99 \r
100     /**\r
101      * @param session the Session to use for initiating the\r
102      *        transaction for retrieving the input Resource.\r
103      * @return <code>null</code> if the random access reference of this\r
104      *         ResourceInput is invalid.\r
105      */\r
106     public Resource toResource(Session session) throws DatabaseException {\r
107         return session.syncRequest(Queries.resource(inputResourceId));\r
108     }\r
109 \r
110     /**\r
111      * @param input Input format:\r
112      *        <code>client-id#input-resource-id[#unique-suffix]</code>\r
113      * @throws IllegalArgumentException if input is invalid\r
114      */\r
115     public String marshall() {\r
116         return String.format("%s@%s", inputResourceId, suffix);\r
117     }\r
118 \r
119     /**\r
120      * @param a previously marshalled ResourceInput using\r
121      *        {@link #marshall()}\r
122      * @throws IllegalArgumentException if input is invalid\r
123      */\r
124     public static ResourceInput unmarshall(String input) throws IllegalArgumentException {\r
125         if (input == null)\r
126             throw new IllegalArgumentException("null input");\r
127         String[] parts = input.split(SUFFIX_SEPARATOR);\r
128         if (parts.length < 1)\r
129             throw new IllegalArgumentException("invalid input: " + input);\r
130 \r
131         // Get input resource random access id\r
132         String id = parts[0];\r
133 \r
134         // Get arbitrary suffix\r
135         String suffix = EMPTY_SUFFIX;\r
136         if (parts.length > 1) {\r
137             suffix = input.substring(id.length() + 1);\r
138         }\r
139         return new ResourceInput(id, suffix);\r
140     }\r
141 \r
142     public static Resource unmarshallToResource(String input, Session session) throws DatabaseException {\r
143         ResourceInput in = unmarshall(input);\r
144         return in.toResource(session);\r
145     }\r
146 \r
147     @Override\r
148     public int hashCode() {\r
149         final int prime = 31;\r
150         int result = 1;\r
151         result = prime * result + ((inputResourceId == null) ? 0 : inputResourceId.hashCode());\r
152         result = prime * result + ((suffix == null) ? 0 : suffix.hashCode());\r
153         return result;\r
154     }\r
155 \r
156     @Override\r
157     public boolean equals(Object obj) {\r
158         if (this == obj)\r
159             return true;\r
160         if (obj == null)\r
161             return false;\r
162         if (getClass() != obj.getClass())\r
163             return false;\r
164         final ResourceInput other = (ResourceInput) obj;\r
165         if (inputResourceId == null) {\r
166             if (other.inputResourceId != null)\r
167                 return false;\r
168         } else if (!inputResourceId.equals(other.inputResourceId))\r
169             return false;\r
170         if (suffix == null) {\r
171             if (other.suffix != null)\r
172                 return false;\r
173         } else if (!suffix.equals(other.suffix))\r
174             return false;\r
175         return true;\r
176     }\r
177 \r
178 }\r