Example usage for com.google.gwt.gears.client Factory createDatabase

List of usage examples for com.google.gwt.gears.client Factory createDatabase

Introduction

In this page you can find the example usage for com.google.gwt.gears.client Factory createDatabase.

Prototype

public Database createDatabase() 

Source Link

Document

Creates a new Database instance.

Usage

From source file:de.lilawelt.zmachine.client.storage.GearsAdapterReal.java

License:Open Source License

public boolean Initialize() throws StorageException {
    if (initialized)
        return true;

    Factory f = Factory.getInstance();
    if (f == null)
        throw new StorageException("Gears could not be initialized");

    if ((!f.hasPermission()) && (!f.getPermission()))
        throw new StorageException("Sorry, this site may not access your gears installation");

    Log.debug("Trying gears init...");

    // Create the database if it doesn't exist.
    try {/*from   www .j  ava 2  s.  c  om*/
        db = f.createDatabase();
        db.open("textadventure-saves");
        // The 'int' type will store up to 8 byte ints depending on the magnitude of the 
        // value added.
        db.execute(
                "create table if not exists saves (savename text, gameuid text, savedate date, savedata blob)");
    } catch (GearsException e) {
        throw new StorageException(
                "Sorry, an error was encountered while initializing gears: " + e.getMessage());
    }

    Log.debug("Dine with gears init");

    initialized = true;
    return true;
}

From source file:org.sigmah.client.offline.sigmah.handler.GetOrganizationHandler.java

License:Open Source License

@Override
public CommandResult execute(GetOrganization cmd, User user) throws CommandException {
    OrganizationDTO organizationDTO = null;

    final Factory factory = Factory.getInstance();
    if (factory != null) {

        final Database database = factory.createDatabase();
        database.open(OnlineMode.LOCAL_DATABASE_NAME);
        try {/*from   ww  w  .  j a v a2 s  . c  o  m*/
            organizationDTO = OrganizationDAO.selectOrganization(cmd.getId(), database);

        } catch (DatabaseException ex) {
            Log.debug("Error while reading the organization dto " + cmd.getId() + " from the local database.",
                    ex);

        } finally {
            try {
                database.close();
            } catch (DatabaseException ex) {
                Log.debug("Database closing error.", ex);
            }
        }
    }

    return organizationDTO;
}

From source file:org.sigmah.client.offline.sigmah.OnlineMode.java

License:Open Source License

/**
 * Verify if the usage of Google Gears has been approved by the user.<br>
 * If the permission hasn't been granted yet, this method will ask for
 * permission if <code>askForPermission</code> is true.<br>
 * <br>/*w w w . j av  a  2  s.c  o m*/
 * Also, this method creates the database
 * @param askForPermission <code>true</code> to ask for permission to use
 * Google Gears (only if needed), <code>false</code> otherwise.
 */
private void checkPermission(boolean askForPermission) {
    final Factory factory = Factory.getInstance();

    if (factory != null && !databaseCheckIsDone) {

        if (!factory.hasPermission() && askForPermission)
            factory.getPermission("Sigmah", "desktopicons/48x48.png",
                    I18N.CONSTANTS.sigmahOfflineDescription());

        if (factory.hasPermission()) {
            localDatabase = factory.createDatabase();

            // Verifying the existence of the "status" table
            localDatabase.open(LOCAL_DATABASE_NAME);
            try {
                localDatabase.execute("SELECT online FROM status WHERE id = 1");

            } catch (DatabaseException ex) {
                // Most likely, the database doesn't exists.

                // Creating the database
                createStatusTable();

            } finally {
                try {
                    localDatabase.close();
                } catch (DatabaseException ex) {
                    Log.debug("Database closing error.", ex);
                }
            }

            databaseCheckIsDone = true;
        }
    }
}

From source file:org.sigmah.client.offline.sigmah.Query.java

License:Open Source License

public ResultSet execute() throws DatabaseException {
    final Factory factory = Factory.getInstance();
    final Database database = factory.createDatabase();

    database.open(OnlineMode.LOCAL_DATABASE_NAME);

    final ResultSet resultSet = database.execute(statement, arguments);

    database.close();// w  ww.jav a2  s  . c  o m

    return resultSet;
}

From source file:org.sigmah.client.offline.sigmah.sync.OrganizationSynchronizer.java

License:Open Source License

@Override
public void synchronizeLocalDatabase() {
    fireOnStart();/*www .  j  av  a2s  .co m*/
    fireOnTaskChange(I18N.CONSTANTS.synchronizerOrganizationDownload_0());

    final Factory factory = Factory.getInstance();

    if (factory != null) {

        dispatcher.execute(new GetOrganization(authentication.getOrganizationId()), null,
                new AsyncCallback<OrganizationDTO>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        fireOnFailure(false, I18N.CONSTANTS.synchronizerOrganizationDownload_0_failed()
                                + caught.getMessage());
                    }

                    @Override
                    public void onSuccess(OrganizationDTO result) {
                        final Database database = factory.createDatabase();
                        database.open(OnlineMode.LOCAL_DATABASE_NAME);

                        try {
                            fireOnUpdate(0.1);
                            fireOnTaskChange(I18N.CONSTANTS.synchronizerOrganizationDownload_1());
                            OrganizationDAO.createTablesIfNotExists(database);

                            fireOnUpdate(0.2);
                            fireOnTaskChange(I18N.CONSTANTS.synchronizerOrganizationDownload_2());
                            OrganizationDAO.insertOrReplaceOrganization(result, database);

                            final LocalServer localServer = factory.createLocalServer();
                            final ResourceStore store = localServer.createStore(OnlineMode.LOCAL_DATABASE_NAME);

                            fireOnUpdate(0.8);
                            fireOnTaskChange(I18N.CONSTANTS.synchronizerOrganizationDownload_3());
                            store.capture(new ResourceStoreUrlCaptureHandler() {

                                @Override
                                public void onCapture(ResourceStoreUrlCaptureEvent event) {
                                    fireOnComplete();
                                }
                            }, GWT.getModuleBaseURL() + "image-provider?" + FileUploadUtils.IMAGE_URL + "="
                                    + result.getLogo());

                        } catch (DatabaseException ex) {
                            Log.debug("Error while writing the organization dto to the local database.", ex);
                            fireOnFailure(false, I18N.CONSTANTS.synchronizerOrganizationDownload_0_failed()
                                    + ex.getMessage());

                        } finally {
                            try {
                                database.close();
                            } catch (DatabaseException ex) {
                                Log.debug("Database closing error.", ex);
                                fireOnFailure(false, I18N.CONSTANTS.synchronizerOrganizationDownload_0_failed()
                                        + ex.getMessage());
                            }
                        }
                    }
                });

    } else
        fireOnFailure(false,
                I18N.CONSTANTS.synchronizerOrganizationDownload_0_failed() + "Google Gears isn't available.");
}

From source file:org.sigmah.client.offline.sigmah.sync.OrganizationSynchronizer.java

License:Open Source License

@Override
public void updateDistantDatabase() {
    fireOnStart();//from ww  w .  java  2  s .c o  m
    fireOnTaskChange(I18N.CONSTANTS.synchronizerOrganizationUpload_0());

    final Factory factory = Factory.getInstance();

    if (factory != null) {
        final Database database = factory.createDatabase();
        database.open(OnlineMode.LOCAL_DATABASE_NAME);

        try {
            OrganizationDAO.truncateTables(database);

            fireOnComplete();

        } catch (DatabaseException ex) {
            Log.debug("Error while removing the organization dto from the local database.", ex);
            fireOnFailure(false, I18N.CONSTANTS.synchronizerOrganizationUpload_0_failed() + ex.getMessage());

        } finally {
            try {
                database.close();
            } catch (DatabaseException ex) {
                Log.debug("Database closing error.", ex);
                fireOnFailure(false,
                        I18N.CONSTANTS.synchronizerOrganizationUpload_0_failed() + ex.getMessage());
            }
        }

    } else
        fireOnFailure(false,
                I18N.CONSTANTS.synchronizerOrganizationUpload_0_failed() + "Google Gears isn't available.");
}