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.service;
017    
018    import org.kuali.common.jdbc.model.ExecutionResult;
019    import org.kuali.common.jdbc.model.context.JdbcContext;
020    import org.kuali.common.util.Assert;
021    import org.kuali.common.util.FormatUtils;
022    import org.kuali.common.util.execute.Executable;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    public final class JdbcExecutable implements Executable {
027    
028            private static final Logger logger = LoggerFactory.getLogger(JdbcExecutable.class);
029    
030            public static final boolean DEFAULT_SKIP = false;
031    
032            private final JdbcService service;
033            private final JdbcContext context;
034            private final boolean skip;
035    
036            public JdbcExecutable(JdbcService service, JdbcContext context) {
037                    this(service, context, DEFAULT_SKIP);
038            }
039    
040            public JdbcExecutable(JdbcService service, JdbcContext context, boolean skip) {
041                    Assert.noNulls(service, context);
042                    this.service = service;
043                    this.context = context;
044                    this.skip = skip;
045            }
046    
047            @Override
048            public void execute() {
049    
050                    if (skip) {
051                            return;
052                    }
053    
054                    ExecutionResult result = service.executeSql(context);
055                    if (result.getStatementsExecuted() > 0) {
056                            showResult(result);
057                    }
058            }
059    
060            protected void showResult(ExecutionResult result) {
061                    String updates = FormatUtils.getCount(result.getUpdateCount());
062                    String statements = FormatUtils.getCount(result.getStatementsExecuted());
063                    String elapsed = FormatUtils.getTime(result.getElapsed());
064                    Object[] args = { updates, statements, elapsed };
065                    logger.info("Rows updated: {}  SQL Statements: {}  Total time: {}", args);
066            }
067    
068            public JdbcService getService() {
069                    return service;
070            }
071    
072            public JdbcContext getContext() {
073                    return context;
074            }
075    
076            public boolean isSkip() {
077                    return skip;
078            }
079    
080    }