1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.google.gwt.sample.dynatablerf.client;
17 
18 import com.google.gwt.event.shared.EventBus;
19 import com.google.gwt.event.shared.HandlerRegistration;
20 import com.google.gwt.event.shared.SimpleEventBus;
21 import com.google.gwt.sample.dynatablerf.client.events.MarkFavoriteEvent;
22 import com.google.gwt.sample.dynatablerf.shared.PersonProxy;
23 import com.google.gwt.user.client.Cookies;
24 import com.google.gwt.user.client.Window;
25 import com.google.gwt.user.client.Window.ClosingEvent;
26 import com.google.gwt.user.client.Window.ClosingHandler;
27 import com.google.web.bindery.requestfactory.shared.EntityProxyId;
28 import com.google.web.bindery.requestfactory.shared.RequestFactory;
29 
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Set;
33 
34 /**
35  * Manages client-side favorites.
36  */
37 public class FavoritesManager {
38   private static final String COOKIE_NAME = "Favorites";
39   private final EventBus eventBus = new SimpleEventBus();
40   private final Set<EntityProxyId<PersonProxy>> favoriteIds = new HashSet<EntityProxyId<PersonProxy>>();
41 
FavoritesManager(final RequestFactory requestFactory)42   public FavoritesManager(final RequestFactory requestFactory) {
43     String cookie = Cookies.getCookie(COOKIE_NAME);
44     if (cookie != null) {
45       try {
46         for (String fragment : cookie.split(",")) {
47           if (fragment.length() == 0) {
48             continue;
49           }
50           EntityProxyId<PersonProxy> id = requestFactory.getProxyId(fragment);
51           if (id != null) {
52             favoriteIds.add(id);
53           }
54         }
55       } catch (NumberFormatException e) {
56         // Not a big deal, start up without favorites
57         favoriteIds.clear();
58       }
59     }
60 
61     Window.addWindowClosingHandler(new ClosingHandler() {
62       public void onWindowClosing(ClosingEvent event) {
63         StringBuilder sb = new StringBuilder();
64         for (EntityProxyId<PersonProxy> id : favoriteIds) {
65           sb.append(requestFactory.getHistoryToken(id)).append(",");
66         }
67         Cookies.setCookie(COOKIE_NAME, sb.toString());
68       }
69     });
70   }
71 
addMarkFavoriteHandler( MarkFavoriteEvent.Handler handler)72   public HandlerRegistration addMarkFavoriteHandler(
73       MarkFavoriteEvent.Handler handler) {
74     return eventBus.addHandler(MarkFavoriteEvent.TYPE, handler);
75   }
76 
getFavoriteIds()77   public Set<EntityProxyId<PersonProxy>> getFavoriteIds() {
78     return Collections.unmodifiableSet(favoriteIds);
79   }
80 
isFavorite(PersonProxy person)81   public boolean isFavorite(PersonProxy person) {
82     return favoriteIds.contains(person.stableId());
83   }
84 
setFavorite(EntityProxyId<PersonProxy> id, boolean isFavorite)85   public void setFavorite(EntityProxyId<PersonProxy> id, boolean isFavorite) {
86     if (isFavorite) {
87       favoriteIds.add(id);
88     } else {
89       favoriteIds.remove(id);
90     }
91 
92     eventBus.fireEventFromSource(new MarkFavoriteEvent(id, isFavorite), this);
93   }
94 }
95