Don't use Strings to hold primary key values.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / place / DetailsPlace.java
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 org.glom.web.shared.PrimaryKeyItem;
23 import org.glom.web.shared.layout.LayoutItemField.GlomFieldType;
24
25 import com.google.gwt.place.shared.PlaceTokenizer;
26 import com.google.gwt.place.shared.Prefix;
27
28 public class DetailsPlace extends HasSelectableTablePlace {
29         private PrimaryKeyItem primaryKeyValue;
30
31         public DetailsPlace(String documentID, String tableName, PrimaryKeyItem primarykeyValue) {
32                 super(documentID, tableName);
33                 this.primaryKeyValue = primarykeyValue;
34         }
35
36         public PrimaryKeyItem getPrimaryKeyValue() {
37                 return primaryKeyValue;
38         }
39
40         @Prefix("details")
41         public static class Tokenizer extends HasSelectableTablePlace.Tokenizer implements PlaceTokenizer<DetailsPlace> {
42
43                 private final String primaryKeyValueKey = "value=";
44
45                 /**
46                  * Creates the URL string that is shown in the browser. This is the bookmarked URL.
47                  * 
48                  * @see com.google.gwt.place.shared.PlaceTokenizer#getToken(com.google.gwt.place.shared.Place)
49                  */
50                 @Override
51                 public String getToken(DetailsPlace place) {
52                         PrimaryKeyItem primaryKeyValue = place.getPrimaryKeyValue();
53                         GlomFieldType glomFieldType = primaryKeyValue.getGlomFieldType();
54
55                         // create the URL string based on the
56                         String primaryKeyValueString = "";
57                         switch (glomFieldType) {
58                         case TYPE_NUMERIC:
59                                 primaryKeyValueString = new Double(primaryKeyValue.getNumber()).toString();
60                                 // Remove the trailing point and zero on integers.
61                                 if (primaryKeyValueString.endsWith(".0"))
62                                         primaryKeyValueString = primaryKeyValueString.substring(0, primaryKeyValueString.length() - 2);
63                                 break;
64                         case TYPE_TEXT:
65                                 primaryKeyValueString = primaryKeyValue.getText();
66                                 break;
67                         default:
68                                 // Unknown types are represented in the URL by an empty string. This means the details view with these
69                                 // types will run the query with an empty Value item based on the Glom type from the Glom document. The
70                                 // fir result will be shown.
71                                 // TODO update the URL location string and the PrimaryKeyItem when this happens
72                                 break;
73                         }
74
75                         return documentKey + place.getDocumentID() + seperator + tableKey + place.getTableName() + seperator
76                                         + primaryKeyValueKey + primaryKeyValueString;
77                 }
78
79                 /**
80                  * Create a DetailPlace that should be loaded from a URL string. This is called when users load the details view
81                  * directly with the URL string (a bookmark or link).
82                  * 
83                  * @see com.google.gwt.place.shared.PlaceTokenizer#getPlace(java.lang.String)
84                  */
85                 @Override
86                 public DetailsPlace getPlace(String token) {
87                         String[] tokenArray = token.split(seperator);
88
89                         // default empty values
90                         String documentID = "", tableName = "";
91                         PrimaryKeyItem primaryKeyValue = new PrimaryKeyItem();
92
93                         if (tokenArray.length != 3) {
94                                 // The URL string doesn't match what we're expecting. Just use the initial values for the details place.
95                                 // TODO Shouldn't this just go to the document selection place?
96                                 return new DetailsPlace(documentID, tableName, primaryKeyValue);
97                         }
98
99                         // Get the document ID string.
100                         // TODO else go to the document selection place?
101                         if (documentKey.equals(tokenArray[0].substring(0, documentKey.length()))) {
102                                 documentID = tokenArray[0].substring(documentKey.length());
103                         }
104
105                         // Get the table name string.
106                         // An empty table name will load the default table so it's ok if this key isn't found
107                         if (tableKey.equals(tokenArray[1].substring(0, tableKey.length()))) {
108                                 tableName = tokenArray[1].substring(tableKey.length());
109                         }
110
111                         // Get the primary key value.
112                         if (primaryKeyValueKey.equals(tokenArray[2].substring(0, primaryKeyValueKey.length()))) {
113                                 // the text after the 'value='
114                                 String primaryKeyValueString = tokenArray[2].substring(primaryKeyValueKey.length());
115
116                                 // Try to create a PrimaryKeyItem from the text. Check if it's a number first.
117                                 try {
118                                         primaryKeyValue.setNumber(new Double(primaryKeyValueString));
119                                 } catch (NumberFormatException e) {
120                                         // It's not a number, use the string.
121                                         primaryKeyValue.setText(primaryKeyValueString);
122                                 }
123                         }
124
125                         return new DetailsPlace(documentID, tableName, primaryKeyValue);
126                 }
127         }
128
129 }