1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.spring.aop;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.model.BaseModel;
20  import com.liferay.portal.model.ModelExtensionHandler;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.aspectj.lang.ProceedingJoinPoint;
26  import org.aspectj.lang.Signature;
27  
28  /**
29   * <a href="ModelExtensionAdvice.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author     Raymond Aug�
32   * @deprecated
33   */
34  public class ModelExtensionAdvice<T> {
35  
36      public ModelExtensionHandler<T> getModelExtensionHandler() {
37          return _modelExtensionHandler;
38      }
39  
40      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
41          throws Throwable {
42  
43          Object result = proceedingJoinPoint.proceed();
44  
45          Signature signature = proceedingJoinPoint.getSignature();
46  
47          if (_extensionMethodNames.isEmpty() ||
48              _extensionMethodNames.contains(signature.getName())) {
49  
50              if (_log.isDebugEnabled()) {
51                  StringBuilder sb = new StringBuilder();
52  
53                  sb.append("Extending method ");
54                  sb.append(signature.getDeclaringTypeName());
55                  sb.append(".");
56                  sb.append(signature.getName());
57                  sb.append("(");
58  
59                  Object[] args = proceedingJoinPoint.getArgs();
60  
61                  for (int i = 0; i < args.length; i++) {
62                      if (i > 0) {
63                          sb.append(", ");
64                      }
65  
66                      sb.append(args[i].getClass().getSimpleName());
67                  }
68  
69                  sb.append(")");
70  
71                  _log.debug(sb.toString());
72              }
73  
74              if (result instanceof BaseModel<?>) {
75                  result = _modelExtensionHandler.extendSingle(
76                      (BaseModel<T>)result);
77              }
78              else if (result instanceof List<?>) {
79                  result = _modelExtensionHandler.extendList(
80                      (List<BaseModel<T>>)result);
81              }
82          }
83  
84          return result;
85      }
86  
87      public void setModelExtensionHandler(
88          ModelExtensionHandler<T> modelExtensionHandler) {
89  
90          _modelExtensionHandler = modelExtensionHandler;
91  
92          if (_modelExtensionHandler.getExtensionMethodNames() != null) {
93              _extensionMethodNames =
94                  _modelExtensionHandler.getExtensionMethodNames();
95          }
96          else {
97              _extensionMethodNames = new ArrayList<String>();
98          }
99      }
100 
101     private static final Log _log =
102         LogFactoryUtil.getLog(ModelExtensionAdvice.class);
103 
104     private List<String> _extensionMethodNames;
105     private ModelExtensionHandler<T> _modelExtensionHandler;
106 
107 }