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