1
/*
2
 * Copyright (C) 2011 Openismus GmbH
3
 *
4
 * This file is part of GWT-Glom.
5
 *
6
 * GWT-Glom is free software: you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by the
8
 * Free Software Foundation, either version 3 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14
 * for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
package org.glom.web.client.place;
21
22
import java.util.HashMap;
23
import java.util.Iterator;
24
import java.util.Map;
25
import java.util.Map.Entry;
26
27
import org.glom.web.client.StringUtils;
28
29
import com.google.gwt.place.shared.Place;
30
31
/**
32
 * Super type for Place objects (bookmarkable URLs) that display the TableSelectionView.
33
 * 
34
 */
35
public abstract class HasSelectableTablePlace extends Place {
36
37
	private final String documentID;
38
	private final String tableName;
39
40
	public HasSelectableTablePlace(final String documentID, final String tableName) {
41
		this.documentID = documentID;
42
		this.tableName = tableName;
43
	}
44
45
	public String getDocumentID() {
46
		return documentID;
47
	}
48
49
	public String getTableName() {
50
		return tableName;
51
	}
52
53
	public static class Tokenizer {
54
		protected final String documentKey = "document";
55
		protected final String tableKey = "table";
56
		private final String separator = "&";
57
		private final String equals = "=";
58
59
		/**
60
		 * Get a HashMap of parameter names and values from the history token. This can parse tokens built by
61
		 * buildParamsToken().
62
		 * 
63
		 * @param historyToken
64
		 *            The historyToken provided to getPlace().
65
		 * @return A HasMap of names to values.
66
		 */
67
		protected HashMap<String, String> getTokenParams(final String historyToken) {
68
			final String[] arStr = historyToken.substring(0, historyToken.length()).split(separator);
69
			final HashMap<String, String> params = new HashMap<String, String>();
70
			for (int i = 0; i < arStr.length; i++) {
71
				final String[] substr = arStr[i].split(equals);
72
				if (substr.length != 2) {
73
					continue;
74
				}
75
76
				String key = "";
77
				String value = "";
78
				if (!StringUtils.isEmpty(substr[0])) {
79
					key = substr[0];
80
				}
81
82
				if (!StringUtils.isEmpty(substr[1])) {
83
					value = substr[1];
84
				}
85
86
				if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)) {
87
					params.put(key, value);
88
				}
89
			}
90
91
			return params;
92
		}
93
94
		/**
95
		 * Build a history token based on a HashMap of parameter names and values. This can later be parsed by
96
		 * getTokenParams().
97
		 * 
98
		 * @param params
99
		 *            A HashMap of names and values.
100
		 * @return A history string for use by getToken() implementation.
101
		 */
102
		protected String buildParamsToken(final HashMap<String, String> params) {
103
			String token = "";
104
			for (final Iterator<Entry<String, String>> it = params.entrySet().iterator(); it.hasNext();) {
105
				final Map.Entry<String, String> entry = it.next();
106
				final String key = entry.getKey();
107
				final String value = entry.getValue();
108
				if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value))
109
					continue;
110
111
				if (token != "") {
112
					token += separator;
113
				}
114
115
				token += key + equals + value;
116
			}
117
118
			return token;
119
		}
120
	}
121
122
}