View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.jpa.transaction;
35  
36  import org.springframework.transaction.PlatformTransactionManager;
37  import org.springframework.transaction.TransactionException;
38  import org.springframework.transaction.TransactionStatus;
39  
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  
44  /**
45   * This transaction status wraps several {@link TransactionStatus}
46   */
47  public class MultiTransactionStatus implements TransactionStatus
48  {
49      private Map<PlatformTransactionManager, TransactionStatus> _transactionStatuses;
50      private PlatformTransactionManager _mainPTM;
51      private boolean _bNewSynchonization;
52  
53      /**
54       * Creates a TransactionStatus that handles several TransactionStatus
55       * @param mainPTM will be default {@link PlatformTransactionManager} for status informations (is*, has* methods)
56       */
57      public MultiTransactionStatus( PlatformTransactionManager mainPTM )
58      {
59          _mainPTM = mainPTM;
60          _transactionStatuses = new HashMap<PlatformTransactionManager, TransactionStatus>(  );
61      }
62  
63      /**
64       * Sets new synchronization to true
65       */
66      public void setNewSynchonization(  )
67      {
68          this._bNewSynchonization = true;
69      }
70  
71      /**
72       * true if new synchronization
73       * @return true if new synchronization
74       */
75      public boolean isNewSynchonization(  )
76      {
77          return this._bNewSynchonization;
78      }
79  
80      /**
81       * Gets the transaction status for the ptm
82       * @param ptm the {@link PlatformTransactionManager}
83       * @return transaction status found
84       */
85      public TransactionStatus getTransactionStatus( PlatformTransactionManager ptm )
86      {
87          return _transactionStatuses.get( ptm );
88      }
89  
90      /**
91       *
92       * {@inheritDoc}
93       */
94      public void flush(  )
95      {
96          for ( TransactionStatus ts : _transactionStatuses.values(  ) )
97          {
98              ts.flush(  );
99          }
100     }
101 
102     /**
103      * Gets the main transaction status
104      * @return the transaction status
105      */
106     private TransactionStatus getMainTransactionStatus(  )
107     {
108         return _transactionStatuses.get( _mainPTM );
109     }
110 
111     /**
112      *
113      * {@inheritDoc}
114      */
115     public boolean hasSavepoint(  )
116     {
117         return getMainTransactionStatus(  ).hasSavepoint(  );
118     }
119 
120     /**
121      *
122      * {@inheritDoc}
123      */
124     public boolean isCompleted(  )
125     {
126         return getMainTransactionStatus(  ).isCompleted(  );
127     }
128 
129     /**
130      *
131      * {@inheritDoc}
132      */
133     public boolean isNewTransaction(  )
134     {
135         return getMainTransactionStatus(  ).isNewTransaction(  );
136     }
137 
138     /**
139      *
140      * {@inheritDoc}
141      */
142     public boolean isRollbackOnly(  )
143     {
144         return getMainTransactionStatus(  ).isRollbackOnly(  );
145     }
146 
147     /**
148      *
149      * {@inheritDoc}
150      */
151     public void setRollbackOnly(  )
152     {
153         for ( TransactionStatus ts : _transactionStatuses.values(  ) )
154         {
155             ts.setRollbackOnly(  );
156         }
157     }
158 
159     /**
160      *
161      * {@inheritDoc}
162      */
163     public Object createSavepoint(  ) throws TransactionException
164     {
165         return null;
166     }
167 
168     /**
169      *
170      * {@inheritDoc}
171      */
172     public void releaseSavepoint( Object savepoint ) throws TransactionException
173     {
174         for ( TransactionStatus ts : _transactionStatuses.values(  ) )
175         {
176             ts.releaseSavepoint( savepoint );
177         }
178     }
179 
180     /**
181      *
182      * {@inheritDoc}
183      */
184     public void rollbackToSavepoint( Object savepoint )
185         throws TransactionException
186     {
187         for ( TransactionStatus ts : _transactionStatuses.values(  ) )
188         {
189             ts.rollbackToSavepoint( savepoint );
190         }
191     }
192 
193     /**
194      * "Getter method" for {@link #_transactionStatuses}
195      * @return value of {@link #_transactionStatuses}
196      */
197     public Map<PlatformTransactionManager, TransactionStatus> getTransactionStatuses(  )
198     {
199         return _transactionStatuses;
200     }
201 
202     /**
203      * "Setter method" for {@link #_transactionStatuses}
204      * @param statuses value of {@link #_transactionStatuses}
205      */
206     public void setTransactionStatuses( Map<PlatformTransactionManager, TransactionStatus> statuses )
207     {
208         _transactionStatuses = statuses;
209     }
210 }