All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.swm.gwt.storage.BigLocalStorage Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
package de.swm.gwt.storage;

import com.google.code.gwt.database.client.GenericRow;
import com.google.code.gwt.database.client.service.DataServiceException;
import com.google.code.gwt.database.client.service.ListCallback;
import com.google.code.gwt.database.client.service.VoidCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.storage.client.StorageEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import de.swm.gwt.client.mobile.keystore.IStorageOperationCompleted;
import de.swm.gwt.client.mobile.keystore.IStorage;
import de.swm.gwt.client.mobile.keystore.ITransaction;
import de.swm.gwt.client.utils.Utils;

import java.util.*;

/**
 * This class implements all operation specified in http://www.w3.org/TR/webstorage/.  Under the hood
 * webdatabase is used which allows store bigger amounts of data.
 * In IOs 6 applications are allowed to use up to 70 Mb limit. Because of the asynchronous nature of
 * http://www.w3.org/TR/webdatabase/ all date are loaded into memory and changes are persisted
 * asynchronously.
 *
 * @author wiese.daniel
 *         
copyright (C) 2012, SWM Services GmbH * @see http://www.w3.org/TR/webstorage/ * but avoiding the 5 mb limit. */ public class BigLocalStorage implements IStorage { public static final String NOT_SUPPORTED = "Not supported"; private final SQLStorageLayer sqlLayer = GWT.create(SQLStorageLayer.class); private Map inMemoryStorage = new HashMap(); private boolean isSupported = false; private GWT.UncaughtExceptionHandler uncaughtExceptionHandler; public BigLocalStorage() { } @Override public ITransaction beginTransaction() { throw new IllegalArgumentException("Usupported Operation Use: TransactionStorage"); } /** * Hanlder for uncought exceptions. * * @param handler */ @Override public void addUncaughtExceptionHanlder(GWT.UncaughtExceptionHandler handler) { this.uncaughtExceptionHandler = handler; } /** * Will initialize the local storage implementation. * * @param afterInitHanlder */ @Override public void initialize(final AsyncCallback afterInitHanlder) { Utils.console("initializing SQL-Lite-Datatbase ..."); sqlLayer.initTable(new VoidCallback() { @Override public void onSuccess() { sqlLayer.getAllValues(new ListCallback() { @Override public void onSuccess(List result) { for (GenericRow genericRow : result) { inMemoryStorage.put(genericRow.getString("id"), genericRow.getString("value")); } Utils.console("SQL-Lite-Datatbase initialized"); afterInitHanlder.onSuccess(null); } @Override public void onFailure(DataServiceException error) { Utils.console("Can't read storage"); afterInitHanlder.onFailure(error); } }); isSupported = true; } @Override public void onFailure(DataServiceException e) { Utils.console("SQL Lite datatbase not supported"); afterInitHanlder.onFailure(e); } }); } /** * Registriert einen Handler für die StorageEvents. * * @param handler . * @return . */ @Override public HandlerRegistration addStorageEventHandler(StorageEvent.Handler handler) { throw new IllegalArgumentException("Not supported"); } /** * Liefert den local Storage zurück. * * @return . */ @Override public IStorage getLocalStorageIfSupported() { return this; } /** * Prüft ob local storage vorhanden ist. * * @return true - wenn ja,false - wenn nein */ @Override public boolean isLocalStorageSupported() { return isSupported; } /** * Löscht einen bestimmten Handler, der auf StorageEvents horcht. * * @param handler . */ @Override public void removeStorageEventHandler(StorageEvent.Handler handler) { throw new IllegalArgumentException(NOT_SUPPORTED); } /** * Löscht den Inhalt der Storage. */ @Override public void clear() { inMemoryStorage.clear(); this.sqlLayer.deleteAll(new VoidCallback() { @Override public void onSuccess() { Utils.console("All rows deleted"); } @Override public void onFailure(DataServiceException e) { Utils.console("Can't delete rowns"); if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); } } }); } /** * Holt die Daten für einen bestimmten Schlüssel. * * @param key . * @return . */ @Override public String getItem(String key) { //will be always returnd from memory return inMemoryStorage.get(key); } /** * Gibt die Anzahl der gespeicherten Elemente zurück. * * @return . */ @Override public int getLength() { //will be always returnd from memory return inMemoryStorage.size(); } /** * Liefert den Schlüssel an einer bestimmten Position zurück. * * @param index index position * @return . */ @Override public String key(int index) { throw new IllegalArgumentException(NOT_SUPPORTED); } /** * Löscht den Wert für einen bestimmten Schlüssel. * * @param key . */ @Override public void removeItem(String key) { final VoidCallback callback = new VoidCallback() { @Override public void onSuccess() { //nothing to do } @Override public void onFailure(DataServiceException e) { if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); } } }; inMemoryStorage.remove(key); this.sqlLayer.deleteKeyAndValue(key, callback); } /** * Löscht den Wert für einen bestimmten Schlüssel. * * @param key . */ private void removeItemInternal(String key, VoidCallback callback) { inMemoryStorage.remove(key); this.sqlLayer.deleteKeyAndValue(key, callback); } /** * Speichert einen String für einen bestimmten Schlüssel (überschreibt falls vorhanden). * * @param key the key * @param data the value */ @Override public void setItem(final String key, final String data) { final VoidCallback callback = new VoidCallback() { @Override public void onSuccess() { //nothing to do } @Override public void onFailure(DataServiceException e) { if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); } } }; setItemInternal(key, data, callback); } public void setItemInternal(final String key, final String data, final VoidCallback callback) { if (inMemoryStorage.containsKey(key)) { inMemoryStorage.put(key, data); //update value sqlLayer.updateValue(key, data, callback); } else { inMemoryStorage.put(key, data); //insert value sqlLayer.insertValue(key, data, callback); } } /** * Will delete the underlying storage asynchronously. * * @param callback callback when completed */ @Override public void clearAsync(final IStorageOperationCompleted callback) { inMemoryStorage.clear(); this.sqlLayer.deleteAll(new VoidCallback() { @Override public void onSuccess() { Utils.console("All rows deleted"); callback.isCompleted(); } @Override public void onFailure(DataServiceException e) { Utils.console("Can't delete rows"); if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); } else { callback.isCompleted(); } } }); } /** * @param keys the keys to delete. * @param callback callback when completed */ @Override public void removeItemsAsync(Set keys, final IStorageOperationCompleted callback) { final Queue keysToInsert = new LinkedList(keys); final VoidCallback afterOneRowIsDeletedCallback = new VoidCallback() { @Override public void onSuccess() { final String nextKey = keysToInsert.poll(); if (nextKey != null) { removeItemInternal(nextKey, this); } else { callback.isCompleted(); } } @Override public void onFailure(DataServiceException e) { if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); }else{ final String nextKey = keysToInsert.poll(); if (nextKey != null) { removeItemInternal(nextKey, this); } else { callback.isCompleted(); } } } }; //Chain starten final String nextKey = keysToInsert.poll(); if (nextKey != null) { removeItemInternal(nextKey, afterOneRowIsDeletedCallback); } else { callback.isCompleted(); } } /** * @param values the values to store. * @param callback callback when completed */ @Override public void setItemsAsync(final Map values, final IStorageOperationCompleted callback) { final Queue keysToInsert = new LinkedList(values.keySet()); final VoidCallback afterOneRowIsInsertedCallback = new VoidCallback() { @Override public void onSuccess() { final String nextKey = keysToInsert.poll(); if (nextKey != null) { setItemInternal(nextKey, values.get(nextKey), this); } else { callback.isCompleted(); } } @Override public void onFailure(DataServiceException e) { if (uncaughtExceptionHandler != null) { uncaughtExceptionHandler.onUncaughtException(e); }else{ final String nextKey = keysToInsert.poll(); if (nextKey != null) { setItemInternal(nextKey, values.get(nextKey), this); } else { callback.isCompleted(); } } } }; //Chain starten final String nextKey = keysToInsert.poll(); if (nextKey != null) { setItemInternal(nextKey, values.get(nextKey), afterOneRowIsInsertedCallback); } else { callback.isCompleted(); } } @Override public Set getKeys() { return this.inMemoryStorage.keySet(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy