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

org.jumpmind.symmetric.service.impl.AbstractService Maven / Gradle / Ivy

Go to download

SymmetricDS is an open source database synchronization solution. It is platform-independent, web-enabled, and database-agnostic. SymmetricDS was first built to replicate changes between 'retail store' databases and ad centralized 'corporate' database.

The newest version!
/*
 * Licensed to JumpMind Inc under one or more contributor 
 * license agreements.  See the NOTICE file distributed
 * with this work for additional information regarding 
 * copyright ownership.  JumpMind Inc licenses this file
 * to you under the GNU Lesser General Public License (the
 * "License"); you may not use this file except in compliance
 * with the License. 
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see           
 * .
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License. 
 */


package org.jumpmind.symmetric.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.service.IService;
import org.jumpmind.symmetric.service.ISqlProvider;
import org.jumpmind.symmetric.util.AppUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

abstract public class AbstractService implements IService, ISqlProvider {

    protected ILog log = LogFactory.getLog(getClass());

    protected IParameterService parameterService;

    protected JdbcTemplate jdbcTemplate;

    protected TransactionTemplate newTransactionTemplate;

    protected DataSource dataSource;

    protected IDbDialect dbDialect;

    protected String tablePrefix;

    private Map sql;

    public void setJdbcTemplate(JdbcTemplate jdbc) {
        this.jdbcTemplate = jdbc;
    }

    protected SimpleJdbcTemplate getSimpleTemplate() {
        return new SimpleJdbcTemplate(jdbcTemplate);
    }

    synchronized public void synchronize(Runnable runnable) {
        runnable.run();
    }
    
    protected boolean isSet(Object value) {
        if (value != null && value.toString().equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    protected SQLException unwrapSqlException(Throwable e) {
        List exs = ExceptionUtils.getThrowableList(e);
        for (Throwable throwable : exs) {
            if (throwable instanceof SQLException) {
                return (SQLException) throwable;
            }
        }
        return null;
    }

    public void setSql(Map sql) {
        this.sql = sql;
    }

    public String getSql(String... keys) {
        StringBuilder sqlBuffer = new StringBuilder();
        if (keys != null) {
            for (String key : keys) {
                sqlBuffer.append(sql.get(key));
            }

            if (sql != null) {
                if (dbDialect != null) {
                    Map replacementTokens = dbDialect
                            .getSqlScriptReplacementTokens();
                    if (replacementTokens != null) {
                        sqlBuffer = new StringBuilder(AppUtils.replaceTokens(sqlBuffer.toString(),
                                replacementTokens).trim());
                    }                    
                }
            }
        }
        return sqlBuffer.toString();
    }

    public void setTablePrefix(String tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

    public void setParameterService(IParameterService parameterService) {
        this.parameterService = parameterService;
    }

    public void setNewTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.newTransactionTemplate = transactionTemplate;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setDbDialect(IDbDialect dbDialect) {
        this.dbDialect = dbDialect;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy