1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench;
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;
21 * A wrapper class for parsing and generating the information given to Simantics
22 * views as a parameter through view secondary ID's.
25 * To parse the input data (given in the secondary ID) just invoke:<br>
26 * <code>new ResourceInput(secondaryIdString)</code>
30 * To generate a secondary ID string based an input resource ID, invoke:<br>
31 * <code>new ResourceInput(inputResourceId).marshall()</code>
34 * @author Tuukka Lehtonen
36 * @see ResourceViewPartUtils
38 public class ResourceInput {
40 private static final String SUFFIX_SEPARATOR = "@";
42 private static final String EMPTY_SUFFIX = "";
44 private final String inputResourceId;
46 private final String suffix;
49 * Construct a new input based on ID's.
50 * Use this to serialize ID data into an input string.
52 * @param randomAccessResourceId
54 public ResourceInput(String randomAccessResourceId) {
55 this.inputResourceId = randomAccessResourceId;
56 this.suffix = EMPTY_SUFFIX;
60 * Construct a new input based on ID's. Use this to serialize ID data into
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
67 public ResourceInput(String randomAccessResourceId, String uniqueSuffix) {
68 this.inputResourceId = randomAccessResourceId;
69 this.suffix = (uniqueSuffix == null) ? EMPTY_SUFFIX : uniqueSuffix;
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.
78 * @param newInputResourceId the new input resource ID
79 * @return new ResourceInput instance with the specified input resource ID
81 public ResourceInput changeInput(String newRandomAccessResourceId) {
82 if (suffix == EMPTY_SUFFIX)
83 return new ResourceInput(newRandomAccessResourceId);
84 return new ResourceInput(newRandomAccessResourceId, suffix);
87 public String getInputResourceId() {
88 return inputResourceId;
91 public String getSuffix() {
96 public String toString() {
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.
106 public Resource toResource(Session session) throws DatabaseException {
107 return session.syncRequest(Queries.resource(inputResourceId));
111 * @param input Input format:
112 * <code>client-id#input-resource-id[#unique-suffix]</code>
113 * @throws IllegalArgumentException if input is invalid
115 public String marshall() {
116 return String.format("%s@%s", inputResourceId, suffix);
120 * @param a previously marshalled ResourceInput using
121 * {@link #marshall()}
122 * @throws IllegalArgumentException if input is invalid
124 public static ResourceInput unmarshall(String input) throws IllegalArgumentException {
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);
131 // Get input resource random access id
132 String id = parts[0];
134 // Get arbitrary suffix
135 String suffix = EMPTY_SUFFIX;
136 if (parts.length > 1) {
137 suffix = input.substring(id.length() + 1);
139 return new ResourceInput(id, suffix);
142 public static Resource unmarshallToResource(String input, Session session) throws DatabaseException {
143 ResourceInput in = unmarshall(input);
144 return in.toResource(session);
148 public int hashCode() {
149 final int prime = 31;
151 result = prime * result + ((inputResourceId == null) ? 0 : inputResourceId.hashCode());
152 result = prime * result + ((suffix == null) ? 0 : suffix.hashCode());
157 public boolean equals(Object obj) {
162 if (getClass() != obj.getClass())
164 final ResourceInput other = (ResourceInput) obj;
165 if (inputResourceId == null) {
166 if (other.inputResourceId != null)
168 } else if (!inputResourceId.equals(other.inputResourceId))
170 if (suffix == null) {
171 if (other.suffix != null)
173 } else if (!suffix.equals(other.suffix))