001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.ibatis;
018    
019    import java.util.Iterator;
020    
021    import com.ibatis.sqlmap.client.SqlMapClient;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Message;
024    import org.apache.camel.impl.DefaultProducer;
025    import org.apache.camel.util.ExchangeHelper;
026    import org.apache.camel.util.ObjectHelper;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * @version $Revision: 12984 $
032     */
033    public class IBatisProducer extends DefaultProducer {
034        private static final Log LOG = LogFactory.getLog(IBatisProducer.class);
035        private String statement;
036        private IBatisEndpoint endpoint;
037    
038        public IBatisProducer(IBatisEndpoint endpoint) {
039            super(endpoint);
040            this.endpoint = endpoint;
041            this.statement = endpoint.getStatement();
042        }
043    
044        public void process(Exchange exchange) throws Exception {
045            switch (endpoint.getStatementType()) {
046            case QueryForObject:
047                doQueryForObject(exchange); break;
048            case QueryForList:
049                doQueryForList(exchange); break;
050            case Insert:
051                doInsert(exchange); break;
052            case Update:
053                doUpdate(exchange); break;
054            case Delete:
055                doDelete(exchange); break;
056            default:
057                throw new IllegalArgumentException("Unsupported statementType: " + endpoint.getStatementType());
058            }
059        }
060    
061        private void doQueryForObject(Exchange exchange) throws Exception {
062            SqlMapClient client = endpoint.getSqlMapClient();
063    
064            Object result;
065            Object in = exchange.getIn().getBody();
066            if (in != null) {
067                if (LOG.isTraceEnabled()) {
068                    LOG.trace("QueryForObject: " + in + "  using statement: " + statement);
069                }
070                result = client.queryForObject(statement, in);
071            } else {
072                if (LOG.isTraceEnabled()) {
073                    LOG.trace("QueryForObject using statement: " + statement);
074                }
075                result = client.queryForObject(statement);
076            }
077    
078            doProcessResult(exchange, result);
079        }
080    
081        private void doQueryForList(Exchange exchange) throws Exception {
082            SqlMapClient client = endpoint.getSqlMapClient();
083    
084            Object result;
085            Object in = exchange.getIn().getBody();
086            if (in != null) {
087                if (LOG.isTraceEnabled()) {
088                    LOG.trace("QueryForList: " + in + "  using statement: " + statement);
089                }
090                result = client.queryForList(statement, in);
091            } else {
092                if (LOG.isTraceEnabled()) {
093                    LOG.trace("QueryForList using statement: " + statement);
094                }
095                result = client.queryForList(statement);
096            }
097    
098            doProcessResult(exchange, result);
099        }
100    
101        private void doInsert(Exchange exchange) throws Exception {
102            SqlMapClient client = endpoint.getSqlMapClient();
103    
104            Object result;
105            Object in = exchange.getIn().getBody();
106            if (in != null) {
107                // lets handle arrays or collections of objects
108                Iterator iter = ObjectHelper.createIterator(in);
109                while (iter.hasNext()) {
110                    Object value = iter.next();
111                    if (LOG.isTraceEnabled()) {
112                        LOG.trace("Inserting: " + value + " using statement: " + statement);
113                    }
114                    result = client.insert(statement, value);
115                    doProcessResult(exchange, result);
116                }
117            } else {
118                if (LOG.isTraceEnabled()) {
119                    LOG.trace("Inserting using statement: " + statement);
120                }
121                result = client.insert(statement);
122                doProcessResult(exchange, result);
123            }
124        }
125    
126        private void doUpdate(Exchange exchange) throws Exception {
127            SqlMapClient client = endpoint.getSqlMapClient();
128    
129            Object result;
130            Object in = exchange.getIn().getBody();
131            if (in != null) {
132                // lets handle arrays or collections of objects
133                Iterator iter = ObjectHelper.createIterator(in);
134                while (iter.hasNext()) {
135                    Object value = iter.next();
136                    if (LOG.isTraceEnabled()) {
137                        LOG.trace("Updating: " + value + " using statement: " + statement);
138                    }
139                    result = client.update(statement, value);
140                    doProcessResult(exchange, result);
141                }
142            } else {
143                if (LOG.isTraceEnabled()) {
144                    LOG.trace("Updating using statement: " + statement);
145                }
146                result = client.update(statement);
147                doProcessResult(exchange, result);
148            }
149        }
150    
151        private void doDelete(Exchange exchange) throws Exception {
152            SqlMapClient client = endpoint.getSqlMapClient();
153    
154            Object result;
155            Object in = exchange.getIn().getBody();
156            if (in != null) {
157                // lets handle arrays or collections of objects
158                Iterator iter = ObjectHelper.createIterator(in);
159                while (iter.hasNext()) {
160                    Object value = iter.next();
161                    if (LOG.isTraceEnabled()) {
162                        LOG.trace("Deleting: " + value + " using statement: " + statement);
163                    }
164                    result = client.delete(statement, value);
165                    doProcessResult(exchange, result);
166                }
167            } else {
168                if (LOG.isTraceEnabled()) {
169                    LOG.trace("Deleting using statement: " + statement);
170                }
171                result = client.delete(statement);
172                doProcessResult(exchange, result);
173            }
174        }
175    
176        private void doProcessResult(Exchange exchange, Object result) {
177            if (endpoint.getStatementType() == StatementType.QueryForList || endpoint.getStatementType() == StatementType.QueryForObject) {
178                Message answer = exchange.getIn();
179                if (ExchangeHelper.isOutCapable(exchange)) {
180                    answer = exchange.getOut();
181                    // preserve headers
182                    answer.getHeaders().putAll(exchange.getIn().getHeaders());
183                }
184                // set the result as body for insert
185                answer.setBody(result);
186    
187                answer.setHeader(IBatisConstants.IBATIS_RESULT, result);
188                answer.setHeader(IBatisConstants.IBATIS_STATEMENT_NAME, statement);
189            } else {
190                Message msg = exchange.getIn();
191                msg.setHeader(IBatisConstants.IBATIS_RESULT, result);
192                msg.setHeader(IBatisConstants.IBATIS_STATEMENT_NAME, statement);
193            }
194        }
195    }