1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.annotation;
24  
25  import com.liferay.portal.kernel.annotation.TransactionDefinition;
26  import com.liferay.portal.kernel.annotation.Transactional;
27  import com.liferay.portal.util.PropsValues;
28  
29  import java.io.Serializable;
30  
31  import java.lang.reflect.AnnotatedElement;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.springframework.transaction.annotation.TransactionAnnotationParser;
37  import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
38  import org.springframework.transaction.interceptor.RollbackRuleAttribute;
39  import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
40  import org.springframework.transaction.interceptor.TransactionAttribute;
41  
42  /**
43   * <a href="PortalTransactionAnnotationParser.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Michael Young
47   */
48  public class PortalTransactionAnnotationParser
49      implements TransactionAnnotationParser, Serializable {
50  
51      public TransactionAttribute parseTransactionAnnotation(
52          AnnotatedElement annotatedElement) {
53  
54          Transactional annotation = annotatedElement.getAnnotation(
55              Transactional.class);
56  
57          if (annotation == null) {
58              return null;
59          }
60  
61          RuleBasedTransactionAttribute ruleBasedTransactionAttribute =
62              new RuleBasedTransactionAttribute();
63  
64          int isolationLevel = annotation.isolation().value();
65  
66          if (isolationLevel == TransactionDefinition.ISOLATION_PORTAL) {
67              ruleBasedTransactionAttribute.setIsolationLevel(
68                  PropsValues.TRANSACTION_ISOLATION_PORTAL);
69          }
70          else {
71              ruleBasedTransactionAttribute.setIsolationLevel(isolationLevel);
72          }
73  
74          ruleBasedTransactionAttribute.setPropagationBehavior(
75              annotation.propagation().value());
76          ruleBasedTransactionAttribute.setReadOnly(annotation.readOnly());
77          ruleBasedTransactionAttribute.setTimeout(annotation.timeout());
78  
79          List<RollbackRuleAttribute> rollBackAttributes =
80              new ArrayList<RollbackRuleAttribute>();
81  
82          Class<?>[] rollbackFor = annotation.rollbackFor();
83  
84          for (int i = 0; i < rollbackFor.length; i++) {
85              RollbackRuleAttribute rollbackRuleAttribute =
86                  new RollbackRuleAttribute(rollbackFor[i]);
87  
88              rollBackAttributes.add(rollbackRuleAttribute);
89          }
90  
91          String[] rollbackForClassName = annotation.rollbackForClassName();
92  
93          for (int i = 0; i < rollbackForClassName.length; i++) {
94              RollbackRuleAttribute rollbackRuleAttribute =
95                  new RollbackRuleAttribute(rollbackForClassName[i]);
96  
97              rollBackAttributes.add(rollbackRuleAttribute);
98          }
99  
100         Class<?>[] noRollbackFor = annotation.noRollbackFor();
101 
102         for (int i = 0; i < noRollbackFor.length; ++i) {
103             NoRollbackRuleAttribute noRollbackRuleAttribute =
104                 new NoRollbackRuleAttribute(noRollbackFor[i]);
105 
106             rollBackAttributes.add(noRollbackRuleAttribute);
107         }
108 
109         String[] noRollbackForClassName = annotation.noRollbackForClassName();
110 
111         for (int i = 0; i < noRollbackForClassName.length; ++i) {
112             NoRollbackRuleAttribute noRollbackRuleAttribute =
113                 new NoRollbackRuleAttribute(noRollbackForClassName[i]);
114 
115             rollBackAttributes.add(noRollbackRuleAttribute);
116         }
117 
118         ruleBasedTransactionAttribute.getRollbackRules().addAll(
119             rollBackAttributes);
120 
121         return ruleBasedTransactionAttribute;
122     }
123 
124 }