001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.jdbc;
017    
018    import org.kuali.common.jdbc.context.JdbcContext;
019    import org.kuali.common.util.FormatUtils;
020    import org.kuali.common.util.execute.Executable;
021    import org.slf4j.Logger;
022    import org.slf4j.LoggerFactory;
023    import org.springframework.util.Assert;
024    
025    public class JdbcExecutable implements Executable {
026    
027            private static final Logger logger = LoggerFactory.getLogger(JdbcExecutable.class);
028            public static final JdbcService DEFAULT_JDBC_SERVICE = new DefaultJdbcService();
029    
030            JdbcService service = DEFAULT_JDBC_SERVICE;
031            JdbcContext context;
032            boolean skip;
033    
034            public JdbcExecutable() {
035                    this(DEFAULT_JDBC_SERVICE, null);
036            }
037    
038            public JdbcExecutable(JdbcContext context) {
039                    this(DEFAULT_JDBC_SERVICE, context);
040            }
041    
042            public JdbcExecutable(JdbcService service, JdbcContext context) {
043                    super();
044                    this.service = service;
045                    this.context = context;
046            }
047    
048            @Override
049            public void execute() {
050                    if (skip) {
051                            logger.info("Skipping execution");
052                            return;
053                    }
054    
055                    Assert.notNull(service, "service is null");
056                    Assert.notNull(context, "context is null");
057    
058                    ExecutionResult result = service.executeSql(context);
059                    if (result.getStatementsExecuted() > 0) {
060                            String updates = FormatUtils.getCount(result.getUpdateCount());
061                            String statements = FormatUtils.getCount(result.getStatementsExecuted());
062                            String elapsed = FormatUtils.getTime(result.getElapsed());
063                            Object[] args = { updates, statements, elapsed };
064                            logger.info("Rows updated: {}  SQL Statements: {}  Total time: {}", args);
065                    }
066            }
067    
068            public JdbcService getService() {
069                    return service;
070            }
071    
072            public void setService(JdbcService service) {
073                    this.service = service;
074            }
075    
076            public JdbcContext getContext() {
077                    return context;
078            }
079    
080            public void setContext(JdbcContext context) {
081                    this.context = context;
082            }
083    
084            public boolean isSkip() {
085                    return skip;
086            }
087    
088            public void setSkip(boolean skip) {
089                    this.skip = skip;
090            }
091    
092    }