gwt-client-storage


Save and retrieve data on each "HTML5 Web Storage" supporting browser

This project aims to provide an interface to save data in the web browser. Therefore you need to do four simple steps.

1. Get the module

Checkout this project and build the module by executing ant in the root of the source or download the gwt-client-store.jar(see download section). Make sure the module is available on the classpath.

2. Setup

Create a local storage backend to store the data in the web browser. final Storage localStorage = Storage.getLocalStorage(); final StoreBackend backend = new LocalStorageBackend(localStorage);

Or, use an in memory backend for testing your app. (All data will be lost after the application is closed) final StoreBackend backend = new MemoryBackend();

Create the store service that provides you access to the stores. final StoreService<JsStorable> storeService = new JsStoreService(backend);

3. Define your entities

Your entities have to derive from the JsStorable class. ``` public class JsAirport extends JsStorable { public static final String NAMESPACE = "org.krypsis.Airport";

protected JsAirport() {}

/** * @return The country name of this airport / public final native String getCountry() /-{ return this.country; }-*/;

/** * Sets the country name of this airport * * @param country The country name / public final native void setCountry(String country) /-{ this.country = country; }-*/;

/** * @return Builds and returns a new instance of an Airport. */ public static JsAirport build() { return build("{}"); }

public static native JsAirport build(String json) /-{ return eval('(' + json + ')'); }-/; } ```

4. Get the stores for your entities

The store service manages stores. The stores will be created if necessary and will be cached for the runtime of your application. The service creates for each namespace only one instance.

// The entity namespace is any string of your choice. For example: "org.krypsis.Airline" Store<JsAirline> airlineStore = storeService.getStore(JsAirline.class, JsAirline.NAMESPACE); Store<JsAirplane> airplaneStore = storeService.getStore(JsAirplane.class, JsAirplane.NAMESPACE);

Work with your entities and stores

If you need more information about stores see the org.krypsis.gwt.store.client.Store<E> interface. Now a view examples of how to use a store: ``` /** * Create a new airplane and save it */ final JsAirplane airplane = JsAirplane.build(); airplane.setFlightNumber("LH-123"); final String id = store.save(airplane);

/** * Find the airplane to ensure it was saved */ final JsAirplane first = store.findFirst(new Matcher() { public boolean match(JsAirplane instance) { return instance.getFlightNumber().equals("LH-123"); } }); assert first.getId().equals(id);

/** * Get an airplane by unique id */ final JsAirplane savedAirplane = store.get("200");

/** * Get all airplanes */ final List airplanes1 = store.all();

/** * Load airplanes with the given ids */ final List airplanes2 = store.all(Arrays.asList("1", "2", "3"));

/** * Delete a particular airplane */ store.delete(savedAirplane);

/** * Deletes the airplanes with the id 1 and 2 */ store.deleteAll(Arrays.asList("1", "2"));

/** * Remove all airplanes */ store.deleteAll();

/** * Find all airplanes with the destination airport id of 100 */ final List airplanes3 = store.findAll(new Matcher() { public boolean match(JsAirplane instance) { return instance.getDestinationAirportId().equals("100"); } });

/** * Find all airlines where the name starts with a "P" * and collect the ids. */ final List airlineIds = airlineStore.findAll(new PropertyMatcher() { public boolean match(JsAirline instance) { return instance.getName().startsWith("P"); }

public String getValueOf(JsAirline instance) { return instance.getId(); } });

/** * Get all airplanes of those airlines. */ final List airplanes4 = store.findAll(new Matcher() { public boolean match(JsAirplane instance) { return airlineIds.contains(instance.getAirlineId()); } }); ```

For a complete example just checkout the sources and have a look inside the "example" dir.

Browser support

Should work with each browser that implements the W3C Webstorage specification.

Dependencies

This project depends on the Storage API of the GWT mobile webkit project. The needed libraries are included, so you don't need to download any dependencies. Just make sure the gwt-mobile-webkit-storage.jar is located on the classpath.

More info

For a more detailed explanation see my blog entry Clientseitige Persistenz. (German)

Project Information

Labels:
HTML5 keyvaluedatabase webstorage webkit iPhone store clientsidestorage persistence android