Search in sources :

Example 11 with JDBCCallback

use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.

the class UserFactoryImpl method getUsers.

@SuppressWarnings("unchecked")
@Override
public List<User> getUsers() {
    return (List<User>) template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            List<User> result = new ArrayList<User>();
            Statement st = connection.createStatement();
            PreparedStatement groups = connection.prepareStatement("SELECT * FROM users_groups WHERE \"login\"=? ORDER BY group_name");
            ResultSet rs = st.executeQuery("SELECT * FROM users ORDER BY \"login\"");
            PreparedStatement groupQualifiers = template.getConnection().prepareStatement("SELECT qualifier_id FROM group_qualifier_access WHERE group_name=? ORDER BY qualifier_id");
            while (rs.next()) {
                User user = createUser(groups, rs, groupQualifiers);
                result.add(user);
            }
            groupQualifiers.close();
            groups.close();
            rs.close();
            st.close();
            return result;
        }
    });
}
Also used : User(com.ramussoft.net.common.User) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement)

Example 12 with JDBCCallback

use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.

the class IEngineImpl method deleteAttribute.

@Override
public void deleteAttribute(final long id) {
    throwExaptionIfNotCan(getAccessor().canDeleteAttribute(id), "Can not delete attribute.");
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            Transaction transaction = getAttributePropertyWhatWillBeDeleted(id);
            executeTransaction(transaction, connection);
            long branch = getActiveBranchId();
            PreparedStatement ps = connection.prepareStatement("UPDATE " + prefix + "attributes SET removed_branch_id=? WHERE ATTRIBUTE_ID=?");
            ps.setLong(1, branch);
            ps.setLong(2, id);
            ps.execute();
            ps.close();
            return null;
        }
    });
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) SQLException(java.sql.SQLException) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Example 13 with JDBCCallback

use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.

the class IEngineImpl method updateAttributes.

private void updateAttributes(List<Attribute> attributes, final boolean system, final long qualifierId, final long branch) {
    List<Attribute> olds = new ArrayList<Attribute>();
    loadAttributes(olds, system, qualifierId, branch);
    for (final Attribute old : olds) {
        if (attributes.indexOf(old) < 0) {
            template.execute(new JDBCCallback() {

                @Override
                public Object execute(Connection connection) throws SQLException {
                    Hashtable<Element, Transaction> hash = getAttributeWhatWillBeDeleted(qualifierId, old.getId());
                    for (Transaction transaction : hash.values()) {
                        executeTransaction(transaction, connection);
                    }
                    PreparedStatement ps;
                    ps = connection.prepareStatement("DELETE FROM " + prefix + "formula_dependences WHERE source_attribute_id=? AND source_element_id IN (SELECT element_id FROM " + prefix + "elements WHERE qualifier_id=?)");
                    ps.setLong(1, old.getId());
                    ps.setLong(2, qualifierId);
                    ps.execute();
                    ps.close();
                    ps = connection.prepareStatement("DELETE FROM " + prefix + "formulas WHERE attribute_id=? AND element_id IN (SELECT element_id FROM " + prefix + "elements WHERE qualifier_id=?)");
                    ps.setLong(1, old.getId());
                    ps.setLong(2, qualifierId);
                    ps.execute();
                    ps.close();
                    return null;
                }
            });
        }
    }
    long position = 0;
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement("DELETE FROM " + prefix + "qualifiers_attributes WHERE QUALIFIER_ID=? AND ATTRIBUTE_SYSTEM=? AND created_branch_id=?");
            ps.setLong(1, qualifierId);
            ps.setBoolean(2, system);
            ps.setLong(3, branch);
            ps.execute();
            ps.close();
            return null;
        }
    });
    for (Attribute a : attributes) {
        // if (olds.indexOf(a) < 0) {
        template.update("INSERT INTO " + prefix + "qualifiers_attributes(QUALIFIER_ID, ATTRIBUTE_ID, ATTRIBUTE_SYSTEM, ATTRIBUTE_POSITION, created_branch_id) " + "VALUES(?, ?, ?, ?, ?)", new Object[] { qualifierId, a.getId(), system, position, branch }, true);
        /*
             * } else { template.update( "UPDATE " + prefix +
			 * "qualifiers_attributes SET ATTRIBUTE_POSITION=? WHERE QUALIFIER_ID=? AND ATTRIBUTE_ID=? AND created_branch_id=?"
			 * , new Object[] { position, qualifierId, a.getId(), branch },
			 * true); }
			 */
        position++;
    }
}
Also used : Transaction(com.ramussoft.common.persistent.Transaction) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Example 14 with JDBCCallback

use of com.ramussoft.jdbc.JDBCCallback in project ramus by Vitaliy-Yakovchuk.

the class IEngineImpl method deleteQualifier.

@Override
public void deleteQualifier(final long id) {
    throwExaptionIfNotCan(getAccessor().canDeleteQualifier(id), "Can not delete qualifier.");
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            long branch = getActiveBranchId();
            PreparedStatement ps = connection.prepareStatement("UPDATE " + prefix + "qualifiers SET removed_branch_id=? WHERE QUALIFIER_ID=?");
            ps.setLong(1, branch);
            ps.setLong(2, id);
            ps.execute();
            ps.close();
            return null;
        }
    });
    if (cached)
        qualifeirsCache.remove(id);
}
Also used : SQLException(java.sql.SQLException) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement)

Aggregations

JDBCCallback (com.ramussoft.jdbc.JDBCCallback)14 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)12 SQLException (java.sql.SQLException)10 FindObject (com.ramussoft.common.attribute.FindObject)5 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 Transaction (com.ramussoft.common.persistent.Transaction)4 ArrayList (java.util.ArrayList)4 Attribute (com.ramussoft.common.Attribute)3 Qualifier (com.ramussoft.common.Qualifier)2 PersistentField (com.ramussoft.common.persistent.PersistentField)2 PersistentRow (com.ramussoft.common.persistent.PersistentRow)2 User (com.ramussoft.net.common.User)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Hashtable (java.util.Hashtable)2 Element (com.ramussoft.common.Element)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1