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