View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    * Copyright (C) Eric Long <erilong@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 3 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, see
19   * <http://www.gnu.org/licenses/>.
20   */
21  
22  package org.jumpmind.symmetric.service.impl;
23  
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.util.Date;
27  import java.util.List;
28  
29  import org.jumpmind.symmetric.model.BatchInfo;
30  import org.jumpmind.symmetric.model.OutgoingBatchHistory;
31  import org.jumpmind.symmetric.model.OutgoingBatch.Status;
32  import org.jumpmind.symmetric.service.IAcknowledgeService;
33  import org.jumpmind.symmetric.service.IOutgoingBatchService;
34  import org.springframework.jdbc.core.RowCallbackHandler;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  public class AcknowledgeService extends AbstractService implements IAcknowledgeService {
38  
39      private IOutgoingBatchService outgoingBatchService;
40  
41      @Deprecated
42      public void ack(final List<BatchInfo> batches) {
43          for (BatchInfo batch : batches) {
44              ack(batch);
45          }
46      }
47  
48      @Transactional
49      public void ack(final BatchInfo batch) {
50          OutgoingBatchHistory history = new OutgoingBatchHistory(batch);
51          outgoingBatchService.setBatchStatus(batch.getBatchId(), batch.isOk() ? Status.OK : Status.ER);
52  
53          if (!batch.isOk() && batch.getErrorLine() != 0) {
54              CallBackHandler handler = new CallBackHandler(batch.getErrorLine());
55              jdbcTemplate.query(getSql("selectDataIdSql"), new Object[] { history.getBatchId() }, handler);
56              history.setFailedDataId(handler.getDataId());
57          }
58  
59          history.setEndTime(new Date());
60          outgoingBatchService.insertOutgoingBatchHistory(history);
61      }
62  
63      class CallBackHandler implements RowCallbackHandler {
64          int index = 0;
65  
66          long dataId = -1;
67  
68          long rowNumber;
69  
70          CallBackHandler(long rowNumber) {
71              this.rowNumber = rowNumber;
72          }
73  
74          public void processRow(ResultSet rs) throws SQLException {
75              if (++index == rowNumber) {
76                  dataId = rs.getLong(1);
77              }
78          }
79  
80          public long getDataId() {
81              return dataId;
82          }
83      }
84  
85      public void setOutgoingBatchService(IOutgoingBatchService outgoingBatchService) {
86          this.outgoingBatchService = outgoingBatchService;
87      }
88  
89  }