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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench;
\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
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
25 * To parse the input data (given in the secondary ID) just invoke:<br>
\r
26 * <code>new ResourceInput(secondaryIdString)</code>
\r
30 * To generate a secondary ID string based an input resource ID, invoke:<br>
\r
31 * <code>new ResourceInput(inputResourceId).marshall()</code>
\r
34 * @author Tuukka Lehtonen
\r
36 * @see ResourceViewPartUtils
\r
38 public class ResourceInput {
\r
40 private static final String SUFFIX_SEPARATOR = "@";
\r
42 private static final String EMPTY_SUFFIX = "";
\r
44 private final String inputResourceId;
\r
46 private final String suffix;
\r
49 * Construct a new input based on ID's.
\r
50 * Use this to serialize ID data into an input string.
\r
52 * @param randomAccessResourceId
\r
54 public ResourceInput(String randomAccessResourceId) {
\r
55 this.inputResourceId = randomAccessResourceId;
\r
56 this.suffix = EMPTY_SUFFIX;
\r
60 * Construct a new input based on ID's. Use this to serialize ID data into
\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
67 public ResourceInput(String randomAccessResourceId, String uniqueSuffix) {
\r
68 this.inputResourceId = randomAccessResourceId;
\r
69 this.suffix = (uniqueSuffix == null) ? EMPTY_SUFFIX : uniqueSuffix;
\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
78 * @param newInputResourceId the new input resource ID
\r
79 * @return new ResourceInput instance with the specified input resource ID
\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
87 public String getInputResourceId() {
\r
88 return inputResourceId;
\r
91 public String getSuffix() {
\r
96 public String toString() {
\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
106 public Resource toResource(Session session) throws DatabaseException {
\r
107 return session.syncRequest(Queries.resource(inputResourceId));
\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
115 public String marshall() {
\r
116 return String.format("%s@%s", inputResourceId, suffix);
\r
120 * @param a previously marshalled ResourceInput using
\r
121 * {@link #marshall()}
\r
122 * @throws IllegalArgumentException if input is invalid
\r
124 public static ResourceInput unmarshall(String input) throws IllegalArgumentException {
\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
131 // Get input resource random access id
\r
132 String id = parts[0];
\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
139 return new ResourceInput(id, suffix);
\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
148 public int hashCode() {
\r
149 final int prime = 31;
\r
151 result = prime * result + ((inputResourceId == null) ? 0 : inputResourceId.hashCode());
\r
152 result = prime * result + ((suffix == null) ? 0 : suffix.hashCode());
\r
157 public boolean equals(Object obj) {
\r
162 if (getClass() != obj.getClass())
\r
164 final ResourceInput other = (ResourceInput) obj;
\r
165 if (inputResourceId == null) {
\r
166 if (other.inputResourceId != null)
\r
168 } else if (!inputResourceId.equals(other.inputResourceId))
\r
170 if (suffix == null) {
\r
171 if (other.suffix != null)
\r
173 } else if (!suffix.equals(other.suffix))
\r