View Javadoc

1   package org.appfuse.webapp.client.application.base.place;
2   
3   import com.google.gwt.place.shared.Place;
4   import com.google.gwt.place.shared.PlaceTokenizer;
5   import com.google.gwt.place.shared.Prefix;
6   import com.google.web.bindery.requestfactory.shared.EntityProxy;
7   import com.google.web.bindery.requestfactory.shared.EntityProxyId;
8   import com.google.web.bindery.requestfactory.shared.RequestFactory;
9   
10  /**
11   * A place in the app that deals with a specific {@link EntityProxy}
12   */
13  public class EntityProxyPlace extends Place {
14      /**
15       * The things you do with a record, each of which is a different
16       * bookmarkable location in the application.
17       */
18      public enum Operation {
19          CREATE,
20          EDIT,
21          DETAILS
22      }
23  
24      protected String entityId;
25      protected Class<? extends EntityProxy> proxyClass;
26      protected Operation operation;
27  
28      /**
29       * @param proxyClass
30       */
31      public EntityProxyPlace(Class<? extends EntityProxy> proxyClass) {
32          super();
33          this.operation = Operation.CREATE;
34          this.proxyClass = proxyClass;
35      }
36  
37      /**
38       * @param proxyClass
39       * @param entityId
40       * @param operation
41       */
42      public EntityProxyPlace(Class<? extends EntityProxy> proxyClass, String entityId, Operation operation) {
43          super();
44          this.proxyClass = proxyClass;
45          this.entityId = entityId;
46          this.operation = operation;
47      }
48  
49      public Operation getOperation() {
50          return operation;
51      }
52  
53      public String getEntityId() {
54          return entityId;
55      }
56  
57      public Class<? extends EntityProxy> getProxyClass() {
58          return proxyClass;
59      }
60  
61      /**
62       * 
63       * Tokenizer
64       *
65       */
66      @Prefix("r")
67      public static class Tokenizer implements PlaceTokenizer<EntityProxyPlace> {
68  
69          private static final String SEPARATOR = "!";
70          private final RequestFactory requests;
71  
72          /**
73           * @param proxyFactory
74           */
75          public Tokenizer(RequestFactory requests) {
76              super();
77              this.requests = requests;
78          }
79  
80          public EntityProxyPlace getPlace(String token) {
81              String bits[] = token.split(SEPARATOR);
82              Class<? extends EntityProxy> proxyClass = requests.getProxyClass(bits[0]);
83              String entityId = bits[1];
84              Operation operation = Operation.valueOf(bits[2]);
85              return new EntityProxyPlace(proxyClass, entityId, operation);
86          }
87  
88          public String getToken(EntityProxyPlace place) {
89              return requests.getHistoryToken(
90                      place.getProxyClass()) +
91                      SEPARATOR + place.getEntityId() +
92                      SEPARATOR + place.getOperation();
93          }
94      }
95  
96      @Override
97      public boolean equals(Object obj) {
98          if (!(obj instanceof EntityProxyPlace)) {
99              return false;
100         }
101         if (this == obj) {
102             return true;
103         }
104 
105         EntityProxyPlace other = (EntityProxyPlace) obj;
106         if (operation != other.operation) {
107             return false;
108         }
109         if (proxyClass == null) {
110             if (other.proxyClass != null) {
111                 return false;
112             }
113         } else if (!proxyClass.equals(other.proxyClass)) {
114             return false;
115         }
116         if (entityId == null) {
117             if (other.entityId != null) {
118                 return false;
119             }
120         } else if (!entityId.equals(other.entityId)) {
121             return false;
122         }
123         return true;
124     }
125 
126     @Override
127     public int hashCode() {
128         final int prime = 31;
129         int result = 1;
130         result = prime * result + ((operation == null) ? 0 : operation.hashCode());
131         result = prime * result + ((proxyClass == null) ? 0 : proxyClass.hashCode());
132         result = prime * result + ((entityId == null) ? 0 : entityId.hashCode());
133         return result;
134     }
135 
136     @Override
137     public String toString() {
138         return "ProxyPlace [operation=" + operation + ", proxy=" + entityId + ", proxyClass=" + proxyClass + "]";
139     }
140 }