Search in sources :

Example 1 with INosqlDb

use of org.hortonmachine.dbs.nosql.INosqlDb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getDeleteCollectionDocumentByIdAction.

public Action getDeleteCollectionDocumentByIdAction(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer) {
    if (isNosql) {
        return new AbstractAction("Delete document by OID") {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (databaseViewer.currentSelectedTable != null) {
                        String oid = GuiUtilities.showInputDialog(databaseViewer, "Insert the document OID", "");
                        if (oid != null && oid.trim().length() > 0) {
                            oid = oid.trim();
                            INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                            INosqlCollection collection = db.getCollection(databaseViewer.currentSelectedTable.tableName);
                            collection.deleteByOid(oid);
                        }
                    } else {
                        GuiUtilities.showWarningMessage(databaseViewer, "Select a collection to delete the document from.");
                    }
                } catch (Exception e1) {
                    GuiUtilities.handleError(databaseViewer, e1);
                    Logger.INSTANCE.e("Error", e1);
                }
            }
        };
    }
    return null;
}
Also used : ActionEvent(java.awt.event.ActionEvent) INosqlCollection(org.hortonmachine.dbs.nosql.INosqlCollection) AbstractAction(javax.swing.AbstractAction) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) IOException(java.io.IOException)

Example 2 with INosqlDb

use of org.hortonmachine.dbs.nosql.INosqlDb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getSwitchDatabaseAction.

public Action getSwitchDatabaseAction(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer) {
    if (isNosql) {
        return new AbstractAction("Switch database") {

            @Override
            public void actionPerformed(ActionEvent e) {
                INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                List<String> databasesNames = db.getDatabasesNames();
                String selectedName = GuiUtilities.showComboDialog(databaseViewer, "Select database", "Select the database to switch to", databasesNames.toArray(new String[0]), db.getDbName());
                if (selectedName != null && !selectedName.equals(db.getDbName())) {
                    ConnectionData connectionData = db.getConnectionData();
                    connectionData.connectionLabel = selectedName;
                    connectionData.connectionUrl = db.getDbEngineUrl() + "/" + selectedName;
                    databaseViewer.openDatabase(connectionData, false);
                }
            }
        };
    } else if (databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGIS || databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGRES) {
        return new AbstractAction("Switch database") {

            @Override
            public void actionPerformed(ActionEvent e) {
                ADb db = databaseViewer.currentConnectedSqlDatabase;
                try {
                    List<String> databasesNames = PGDb.getDatabases(db);
                    ConnectionData connectionData = db.getConnectionData();
                    int lastSlash = connectionData.connectionUrl.lastIndexOf('/');
                    String engineUrl = connectionData.connectionUrl.substring(0, lastSlash);
                    String dbName = connectionData.connectionUrl.substring(lastSlash + 1);
                    String selectedName = GuiUtilities.showComboDialog(databaseViewer, "Select database", "Select the database to switch to", databasesNames.toArray(new String[0]), dbName);
                    if (selectedName != null && !selectedName.equals(dbName)) {
                        connectionData.connectionUrl = engineUrl + "/" + selectedName;
                        connectionData.connectionLabel = selectedName;
                        databaseViewer.openDatabase(connectionData, false);
                    }
                } catch (Exception ex) {
                    GuiUtilities.handleError(databaseViewer, ex);
                    Logger.INSTANCE.e("Error", ex);
                }
            }
        };
    }
    return null;
}
Also used : ActionEvent(java.awt.event.ActionEvent) List(java.util.List) ArrayList(java.util.ArrayList) AbstractAction(javax.swing.AbstractAction) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) ConnectionData(org.hortonmachine.dbs.compat.ConnectionData) IOException(java.io.IOException) ADb(org.hortonmachine.dbs.compat.ADb)

Example 3 with INosqlDb

use of org.hortonmachine.dbs.nosql.INosqlDb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getDropDatabaseAction.

public Action getDropDatabaseAction(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer) {
    if (isNosql) {
        return new AbstractAction("Drop current database") {

            @Override
            public void actionPerformed(ActionEvent e) {
                INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                try {
                    boolean doDrop = GuiUtilities.showYesNoDialog(databaseViewer, "Are you sure you want to drop the current database '" + db.getDbName() + "'?\nThis can't be undone!");
                    if (doDrop) {
                        db.drop();
                        databaseViewer.closeCurrentDb(true);
                    }
                } catch (Exception e1) {
                    GuiUtilities.handleError(databaseViewer, e1);
                    Logger.INSTANCE.e("Error", e1);
                }
            }
        };
    // } else if (databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGIS
    // || databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGRES) {
    // return new AbstractAction("Switch database"){
    // @Override
    // public void actionPerformed( ActionEvent e ) {
    // // TODO?
    // }
    // };
    }
    return null;
}
Also used : ActionEvent(java.awt.event.ActionEvent) AbstractAction(javax.swing.AbstractAction) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) IOException(java.io.IOException)

Example 4 with INosqlDb

use of org.hortonmachine.dbs.nosql.INosqlDb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getSaveConnectionAction.

public Action getSaveConnectionAction(DatabaseViewer databaseViewer) {
    return new AbstractAction("Save Connection") {

        @SuppressWarnings("unchecked")
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                ConnectionData connectionData = null;
                if (databaseViewer.currentConnectedSqlDatabase != null) {
                    ADb db = databaseViewer.currentConnectedSqlDatabase;
                    connectionData = db.getConnectionData();
                } else if (databaseViewer.currentConnectedNosqlDatabase != null) {
                    INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                    connectionData = db.getConnectionData();
                }
                String newName = GuiUtilities.showInputDialog(databaseViewer, "Enter a name for the saved connection", "db connection " + new DateTime().toString(HMConstants.dateTimeFormatterYYYYMMDDHHMMSS));
                connectionData.connectionLabel = newName;
                byte[] savedDbs = PreferencesHandler.getPreference(HM_SAVED_DATABASES, new byte[0]);
                List<ConnectionData> connectionDataList = new ArrayList<>();
                try {
                    connectionDataList = (List<ConnectionData>) convertFromBytes(savedDbs);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                connectionDataList.add(connectionData);
                byte[] inBytes = convertObjectToBytes(connectionDataList);
                PreferencesHandler.setPreference(HM_SAVED_DATABASES, inBytes);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) AbstractAction(javax.swing.AbstractAction) ConnectionData(org.hortonmachine.dbs.compat.ConnectionData) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) ADb(org.hortonmachine.dbs.compat.ADb)

Example 5 with INosqlDb

use of org.hortonmachine.dbs.nosql.INosqlDb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getUpdateCollectionDocumentAction.

public Action getUpdateCollectionDocumentAction(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer) {
    if (isNosql) {
        return new AbstractAction("Update document by OID") {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (databaseViewer.currentSelectedTable != null) {
                        INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                        String oid = GuiUtilities.showInputDialog(databaseViewer, "Insert the document OID", "");
                        if (oid != null && oid.trim().length() > 0) {
                            oid = oid.trim();
                            String json = GuiUtilities.showInputAreaDialog(databaseViewer, "Add json to convert to document here.", "");
                            if (json != null) {
                                INosqlCollection collection = db.getCollection(databaseViewer.currentSelectedTable.tableName);
                                collection.updateByOid(oid, json);
                            }
                        }
                    } else {
                        GuiUtilities.showWarningMessage(databaseViewer, "Select a collection to insert the document to.");
                    }
                } catch (Exception e1) {
                    GuiUtilities.handleError(databaseViewer, e1);
                    Logger.INSTANCE.e("Error", e1);
                }
            }
        };
    }
    return null;
}
Also used : ActionEvent(java.awt.event.ActionEvent) INosqlCollection(org.hortonmachine.dbs.nosql.INosqlCollection) AbstractAction(javax.swing.AbstractAction) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) IOException(java.io.IOException)

Aggregations

ActionEvent (java.awt.event.ActionEvent)6 IOException (java.io.IOException)6 AbstractAction (javax.swing.AbstractAction)6 INosqlDb (org.hortonmachine.dbs.nosql.INosqlDb)6 INosqlCollection (org.hortonmachine.dbs.nosql.INosqlCollection)3 ArrayList (java.util.ArrayList)2 ADb (org.hortonmachine.dbs.compat.ADb)2 ConnectionData (org.hortonmachine.dbs.compat.ConnectionData)2 List (java.util.List)1 DateTime (org.joda.time.DateTime)1