Search in sources :

Example 1 with RowMapper

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

the class EngineConnection method init.

public void init(String[] args) throws Exception {
    try {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        java.sql.Connection jdbcConnection = null;
        Properties ps = EngineFactory.getPropeties();
        if (ps != null) {
            try {
                Class.forName(ps.getProperty("driver"));
                jdbcConnection = DriverManager.getConnection(ps.getProperty("url"), ps.getProperty("user"), ps.getProperty("password"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (jdbcConnection == null)
            jdbcConnection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/ramus_public", "postgres", "postgres");
        template = new JDBCTemplate(jdbcConnection);
        userFactory = new UserFactoryImpl(template);
        JDBCTemplate template = new JDBCTemplate(jdbcConnection);
        String password = template.queryForObjects("SELECT \"password\" FROM users WHERE \"login\"=?", new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int index) throws SQLException {
                return rs.getString(1);
            }
        }, new Object[] { "admin" }, true).toString().trim();
        connection = new TcpClientConnection("127.0.0.1", Metadata.TCP_PORT) {

            @Override
            protected void objectReaded(Object object) {
                if (tcpClientEngine != null)
                    tcpClientEngine.call((EvenstHolder) object);
            }
        };
        connection.start();
        connection.invoke("login", new Object[] { "admin", password });
        tcpClientEngine = new TcpClientEngine((EngineInvocker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { EngineInvocker.class }, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return connection.invoke(method.getName(), args);
            }
        }), connection);
        List<Class> interfaces = new ArrayList<Class>();
        interfaces.add(Engine.class);
        interfaces.add(Journaled.class);
        List<PluginProvider> suits = new ArrayList<PluginProvider>();
        suits.add(new SimpleAttributePluginSuit());
        initAdditionalPluginSuits(suits);
        PluginFactory factory = createPluginFactory(suits);
        for (Plugin plugin : factory.getPlugins()) if (plugin.getFunctionalInterface() != null)
            interfaces.add(plugin.getFunctionalInterface());
        final Engine engine1 = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), tcpClientEngine);
        // new CachedEngine(engine1);
        final Engine cachedEngine = engine1;
        final Hashtable<Method, Object> hashtable = new Hashtable<Method, Object>();
        for (Method m : Engine.class.getMethods()) {
            hashtable.put(m, cachedEngine);
        }
        for (Method m : Cached.class.getMethods()) {
            hashtable.put(m, cachedEngine);
        }
        interfaces.add(Cached.class);
        Engine engine = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object o = hashtable.get(method);
                if (o == null)
                    return tcpClientEngine.invoke(proxy, method, args);
                return method.invoke(o, args);
            }
        });
        tcpClientEngine.setEngine(engine);
        for (Plugin plugin : factory.getPlugins()) plugin.init(engine, rules);
        rules = (AccessRules) createDeligate(AccessRules.class);
        this.engine = engine;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SQLException(java.sql.SQLException) EngineInvocker(com.ramussoft.net.common.tcp.EngineInvocker) ArrayList(java.util.ArrayList) Properties(java.util.Properties) TcpClientEngine(com.ramussoft.client.TcpClientEngine) UserFactoryImpl(com.ramussoft.server.UserFactoryImpl) JDBCTemplate(com.ramussoft.jdbc.JDBCTemplate) ResultSet(java.sql.ResultSet) PluginFactory(com.ramussoft.common.PluginFactory) TcpClientEngine(com.ramussoft.client.TcpClientEngine) Engine(com.ramussoft.common.Engine) RowMapper(com.ramussoft.jdbc.RowMapper) Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) PluginProvider(com.ramussoft.common.PluginProvider) InvocationHandler(java.lang.reflect.InvocationHandler) SQLException(java.sql.SQLException) SimpleAttributePluginSuit(com.ramussoft.core.attribute.simple.SimpleAttributePluginSuit) TcpClientConnection(com.ramussoft.client.TcpClientConnection) StandardAttributesPlugin(com.ramussoft.core.attribute.standard.StandardAttributesPlugin) Plugin(com.ramussoft.common.Plugin)

Example 2 with RowMapper

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

the class IEngineImpl method getAttributeWhatWillBeDeleted.

private Transaction getAttributeWhatWillBeDeleted(long elementId, Attribute attribute, boolean attributeDeteting) {
    final Transaction transaction = new Transaction();
    AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
    final Class<? extends Persistent>[] classes;
    if (elementId >= 0)
        classes = plugin.getAttributePersistents();
    else
        classes = plugin.getAttributePropertyPersistents();
    for (int i = 0; i < classes.length; i++) {
        final Class<? extends Persistent> clazz = classes[i];
        final PersistentRow row = metadata.get(clazz);
        final PersistentWrapper wrapper = wrappers.get(clazz);
        ArrayList<Object> params = new ArrayList<Object>(2);
        ArrayList<String> paramFields = new ArrayList<String>(2);
        boolean delete = false;
        for (PersistentField field : row.getFields()) {
            if (field.isAutoset()) {
                if (field.getType() == PersistentField.ELEMENT) {
                    params.add(elementId);
                    paramFields.add(field.getDatabaseName());
                    if (!attributeDeteting)
                        delete = true;
                } else if (field.getType() == PersistentField.ATTRIBUTE) {
                    params.add(attribute.getId());
                    paramFields.add(field.getDatabaseName());
                    if (attributeDeteting)
                        delete = true;
                } else if (field.getType() == PersistentField.VALUE_BRANCH_ID) {
                    params.add(getActiveBranchId());
                    paramFields.add(field.getDatabaseName());
                    if (attributeDeteting)
                        delete = true;
                }
            }
        }
        if (delete) {
            StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName() + " WHERE ");
            boolean first = true;
            for (int x = 0; x < params.size(); x++) {
                if (first) {
                    first = false;
                } else {
                    sb.append(" AND ");
                }
                sb.append(paramFields.get(x));
                sb.append("=?");
            }
            template.query(sb.toString(), new RowMapper() {

                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    try {
                        Persistent persistent = clazz.newInstance();
                        for (PersistentField field : row.getFields()) {
                            wrapper.setDatabaseField(persistent, field, rs);
                        }
                        transaction.getDelete().add(persistent);
                        return null;
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    throw new RuntimeException();
                }
            }, params.toArray(new Object[params.size()]), true);
        }
    }
    transaction.setRemoveBranchInfo(true);
    return transaction;
}
Also used : AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) Transaction(com.ramussoft.common.persistent.Transaction) ResultSet(java.sql.ResultSet) FindObject(com.ramussoft.common.attribute.FindObject) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 3 with RowMapper

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

the class IEngineImpl method setStream.

@SuppressWarnings("unchecked")
@Override
public void setStream(String path, byte[] bytes) {
    if (!path.startsWith("/"))
        throw new RuntimeException("Path should start with / symbol.");
    if (path.startsWith("/elements")) {
        if (!canUpdateElement(path)) {
            throw new RuntimeException("You can not update path " + path);
        }
    }
    throwExaptionIfNotCan(getAccessor().canUpdateStream(path), "Can not update stream.");
    writeStream(path, bytes);
    List<String> list = template.query("SELECT * FROM " + prefix + "streams WHERE STREAM_ID=?", new RowMapper() {

        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("STREAM_ID");
        }
    }, new Object[] { path }, true);
    if (list.size() == 0) {
        template.update("INSERT INTO " + prefix + "streams (STREAM_ID) VALUES (?)", new Object[] { path }, true);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) FindObject(com.ramussoft.common.attribute.FindObject) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 4 with RowMapper

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

the class IEngineImpl method getBinaryBranchAttribute.

@Override
public List<Persistent>[] getBinaryBranchAttribute(long elementId, long attributeId, long branchId) {
    throwExaptionIfNotCan(getAccessor().canReadElement(elementId, attributeId), "Can not get attribute.");
    Attribute attribute = getAttribute(attributeId);
    AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
    final Class<? extends Persistent>[] classes;
    if (elementId >= 0)
        classes = plugin.getAttributePersistents();
    else
        classes = plugin.getAttributePropertyPersistents();
    List<Persistent>[] lists = new List[classes.length];
    for (int i = 0; i < lists.length; i++) {
        final Class<? extends Persistent> clazz = classes[i];
        final PersistentRow row = metadata.get(clazz);
        final PersistentWrapper wrapper = wrappers.get(clazz);
        ArrayList<Object> params = new ArrayList<Object>(3);
        ArrayList<String> paramFields = new ArrayList<String>(3);
        plugin.fillAttributeQuery(row, attributeId, elementId, params, paramFields, this);
        if (elementId >= 0l && attribute.getAttributeType().getTypeName().equals("ElementList") && attribute.getAttributeType().getPluginName().equals("Core")) {
            return getEListFixed(row, clazz, wrapper, paramFields, params, getActiveBranchId());
        }
        params.add(branchId);
        paramFields.add("value_branch_id");
        StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName());
        if (params.size() > 0) {
            sb.append(" WHERE ");
        }
        boolean first = true;
        for (int j = 0; j < params.size(); j++) {
            if (first) {
                first = false;
            } else {
                sb.append(" AND ");
            }
            sb.append(paramFields.get(j));
            sb.append("=?");
        }
        List<Persistent> list = template.query(sb.toString(), new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    Persistent persistent = clazz.newInstance();
                    for (PersistentField field : row.getFields()) {
                        wrapper.setDatabaseField(persistent, field, rs);
                    }
                    return persistent;
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException();
            }
        }, params.toArray(new Object[params.size()]), true);
        lists[i] = list;
    }
    return lists;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) RowMapper(com.ramussoft.jdbc.RowMapper) AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 5 with RowMapper

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

the class IEngineImpl method createAttribute.

@Override
public Attribute createAttribute(long attributeId, AttributeType attributeType, boolean system) {
    throwExaptionIfNotCan(getAccessor().canCreateAttribute(), "Can not create attribute.");
    if (attributeId == -1) {
        attributeId = nextValue("attributes_sequence");
        long id = template.queryForLong("SELECT MAX(ATTRIBUTE_ID) FROM " + prefix + "attributes;");
        while (id >= attributeId) {
            attributeId = template.nextVal(prefix + "attributes_sequence");
        }
    }
    Attribute attribute = new Attribute();
    attribute.setId(attributeId);
    attribute.setAttributeType(attributeType);
    attribute.setSystem(system);
    long branchId = getActiveBranchId();
    Long l = (Long) template.queryForObjects("SELECT COUNT(*) FROM " + prefix + "attributes WHERE ATTRIBUTE_ID=? AND removed_branch_id=?", new RowMapper() {

        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getLong(1);
        }
    }, new Object[] { attributeId, branchId }, false);
    if (l.longValue() > 0)
        template.update("UPDATE " + prefix + "attributes SET ATTRIBUTE_NAME=?, ATTRIBUTE_SYSTEM=?, ATTRIBUTE_TYPE_PLUGIN_NAME=?, ATTRIBUTE_TYPE_NAME=?, ATTRIBUTE_TYPE_COMPARABLE=?, " + "removed_branch_id=? WHERE ATTRIBUTE_ID = ? AND removed_branch_id=?", new Object[] { attribute.getName(), system, attributeType.getPluginName(), attributeType.getTypeName(), attributeType.isComparable(), Integer.MAX_VALUE, attribute.getId(), branchId }, true);
    else
        template.update("INSERT INTO " + prefix + "attributes(ATTRIBUTE_ID, ATTRIBUTE_NAME, ATTRIBUTE_SYSTEM, ATTRIBUTE_TYPE_PLUGIN_NAME, ATTRIBUTE_TYPE_NAME, ATTRIBUTE_TYPE_COMPARABLE, created_branch_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)", new Object[] { attribute.getId(), attribute.getName(), system, attributeType.getPluginName(), attributeType.getTypeName(), attributeType.isComparable(), branchId }, true);
    return attribute;
}
Also used : Attribute(com.ramussoft.common.Attribute) ResultSet(java.sql.ResultSet) FindObject(com.ramussoft.common.attribute.FindObject) RowMapper(com.ramussoft.jdbc.RowMapper)

Aggregations

RowMapper (com.ramussoft.jdbc.RowMapper)30 ResultSet (java.sql.ResultSet)30 SQLException (java.sql.SQLException)22 FindObject (com.ramussoft.common.attribute.FindObject)14 ArrayList (java.util.ArrayList)14 Attribute (com.ramussoft.common.Attribute)11 List (java.util.List)11 Qualifier (com.ramussoft.common.Qualifier)7 JDBCTemplate (com.ramussoft.jdbc.JDBCTemplate)7 Element (com.ramussoft.common.Element)6 PersistentRow (com.ramussoft.common.persistent.PersistentRow)6 IEngineImpl (com.ramussoft.core.impl.IEngineImpl)6 Hashtable (java.util.Hashtable)6 Engine (com.ramussoft.common.Engine)5 AttributePlugin (com.ramussoft.common.attribute.AttributePlugin)5 Persistent (com.ramussoft.common.persistent.Persistent)5 PersistentField (com.ramussoft.common.persistent.PersistentField)5 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)4 DeleteStatus (com.ramussoft.common.DeleteStatus)3 IEngine (com.ramussoft.common.IEngine)3