View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.application.base.place;
5   
6   import java.util.logging.Logger;
7   
8   import org.appfuse.webapp.client.application.ApplicationProxyFactory;
9   
10  import com.google.gwt.place.shared.Place;
11  import com.google.gwt.place.shared.PlaceTokenizer;
12  import com.google.gwt.place.shared.Prefix;
13  import com.google.web.bindery.requestfactory.shared.EntityProxy;
14  import com.google.web.bindery.requestfactory.shared.RequestFactory;
15  
16  /**
17   * @author ivangsa
18   *
19   */
20  public class EntitySearchPlace extends Place {
21      private static final String PREFIX = "l";
22  
23      private final Class<? extends EntityProxy> proxyClass;
24      private Object searchCriteria;
25      private int firstResult = 0;
26      private int maxResults = 25;
27  
28      /**
29       * @param proxyType
30       */
31      public EntitySearchPlace(Class<? extends EntityProxy> proxyClass) {
32          this.proxyClass = proxyClass;
33          this.searchCriteria = null;
34      }
35  
36      /**
37       * @param proxyType
38       * @param searchCriteria
39       */
40      public EntitySearchPlace(Class<? extends EntityProxy> proxyClass, Object searchCriteria) {
41          super();
42          this.proxyClass = proxyClass;
43          this.searchCriteria = searchCriteria;
44      }
45  
46      /**
47       * @param proxyClass
48       * @param searchCriteria
49       * @param firstResult
50       * @param maxResults
51       */
52      public EntitySearchPlace(Class<? extends EntityProxy> proxyClass, int firstResult, int maxResults, Object searchCriteria) {
53          super();
54          this.proxyClass = proxyClass;
55          this.firstResult = firstResult;
56          this.maxResults = maxResults;
57          this.searchCriteria = searchCriteria;
58      }
59  
60      public Class<? extends EntityProxy> getProxyClass() {
61          return proxyClass;
62      }
63  
64      public Object getSearchCriteria() {
65          return searchCriteria;
66      }
67  
68      public int getFirstResult() {
69          return firstResult;
70      }
71  
72      public void setSearchCriteria(Object searchCriteria) {
73          this.searchCriteria = searchCriteria;
74      }
75  
76      public void setFirstResult(int firstResult) {
77          this.firstResult = firstResult;
78      }
79  
80      public int getMaxResults() {
81          return maxResults;
82      }
83  
84      public void setMaxResults(int maxResults) {
85          this.maxResults = maxResults;
86      }
87  
88      /**
89       * Tokenizer.
90       */
91      @Prefix(PREFIX)
92      public static class Tokenizer implements PlaceTokenizer<EntitySearchPlace> {
93          protected final Logger logger = Logger.getLogger(getClass().getName());
94  
95          private static final String SEPARATOR = "!";
96          private final ApplicationProxyFactory proxyFactory;
97          private final RequestFactory requests;
98  
99          /**
100          * @param proxyFactory
101          */
102         public Tokenizer(ApplicationProxyFactory proxyFactory, RequestFactory requests) {
103             super();
104             this.proxyFactory = proxyFactory;
105             this.requests = requests;
106         }
107 
108         public EntitySearchPlace getPlace(String token) {
109             logger.fine("Slicing token: " + token);
110             String tokens[] = token.split(SEPARATOR);
111             Class<? extends EntityProxy> proxyType = requests.getProxyClass(tokens[0]);
112             Object searchCriteria = null;
113             int firstResult = 0;
114             int maxResults = 0;
115             if (tokens.length > 2) {
116                 firstResult = parseInt(tokens[1]);
117                 maxResults = parseInt(tokens[2]);
118             }
119             if (tokens.length > 3) {
120                 Class<? extends Object> searchCriteriaClass = proxyFactory.getSearchCriteriaTypeForProxy(proxyType);
121                 searchCriteria = proxyFactory.deserialize(searchCriteriaClass, tokens[3]);
122             }
123             return new EntitySearchPlace(proxyType, firstResult, maxResults, searchCriteria);
124         }
125 
126         public String getToken(EntitySearchPlace place) {
127             StringBuilder sb = new StringBuilder();
128             sb.append(requests.getHistoryToken(place.getProxyClass()));
129             sb.append(SEPARATOR);
130             sb.append(place.getFirstResult());
131             sb.append(SEPARATOR);
132             sb.append(place.getMaxResults());
133             if (place.getSearchCriteria() != null) {
134                 sb.append(SEPARATOR);
135                 sb.append(proxyFactory.serialize(place.getSearchCriteria()));
136             }
137             return sb.toString();
138         }
139 
140         public String getFullHistoryToken(EntitySearchPlace place) {
141             return PREFIX + ":" + getToken(place);
142         }
143 
144         private int parseInt(String text) {
145             try {
146                 return Integer.parseInt(text);
147             } catch (NumberFormatException e) {
148                 return 0;
149             }
150         }
151     }
152 
153 }