1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
49  public class PortalTransactionAnnotationParser
50      implements TransactionAnnotationParser, Serializable {
51  
52      public TransactionAttribute parseTransactionAnnotation(
53          AnnotatedElement annotatedElement) {
54  
55          Transactional annotation = annotatedElement.getAnnotation(
56              Transactional.class);
57  
58          if (annotation == null) {
59              return null;
60          }
61  
62          RuleBasedTransactionAttribute ruleBasedTransactionAttribute =
63              new RuleBasedTransactionAttribute();
64  
65          int isolationLevel = annotation.isolation().value();
66  
67          if (isolationLevel == TransactionDefinition.ISOLATION_PORTAL) {
68              ruleBasedTransactionAttribute.setIsolationLevel(
69                  PropsValues.TRANSACTION_ISOLATION_PORTAL);
70          }
71          else {
72              ruleBasedTransactionAttribute.setIsolationLevel(isolationLevel);
73          }
74  
75          ruleBasedTransactionAttribute.setPropagationBehavior(
76              annotation.propagation().value());
77          ruleBasedTransactionAttribute.setReadOnly(annotation.readOnly());
78          ruleBasedTransactionAttribute.setTimeout(annotation.timeout());
79  
80          List<RollbackRuleAttribute> rollBackAttributes =
81              new ArrayList<RollbackRuleAttribute>();
82  
83          Class<?>[] rollbackFor = annotation.rollbackFor();
84  
85          for (int i = 0; i < rollbackFor.length; i++) {
86              RollbackRuleAttribute rollbackRuleAttribute =
87                  new RollbackRuleAttribute(rollbackFor[i]);
88  
89              rollBackAttributes.add(rollbackRuleAttribute);
90          }
91  
92          String[] rollbackForClassName = annotation.rollbackForClassName();
93  
94          for (int i = 0; i < rollbackForClassName.length; i++) {
95              RollbackRuleAttribute rollbackRuleAttribute =
96                  new RollbackRuleAttribute(rollbackForClassName[i]);
97  
98              rollBackAttributes.add(rollbackRuleAttribute);
99          }
100 
101         Class<?>[] noRollbackFor = annotation.noRollbackFor();
102 
103         for (int i = 0; i < noRollbackFor.length; ++i) {
104             NoRollbackRuleAttribute noRollbackRuleAttribute =
105                 new NoRollbackRuleAttribute(noRollbackFor[i]);
106 
107             rollBackAttributes.add(noRollbackRuleAttribute);
108         }
109 
110         String[] noRollbackForClassName = annotation.noRollbackForClassName();
111 
112         for (int i = 0; i < noRollbackForClassName.length; ++i) {
113             NoRollbackRuleAttribute noRollbackRuleAttribute =
114                 new NoRollbackRuleAttribute(noRollbackForClassName[i]);
115 
116             rollBackAttributes.add(noRollbackRuleAttribute);
117         }
118 
119         ruleBasedTransactionAttribute.getRollbackRules().addAll(
120             rollBackAttributes);
121 
122         return ruleBasedTransactionAttribute;
123     }
124 
125 }