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.mirage.aop;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import com.sun.saw.Workflow;
29  import com.sun.saw.WorkflowException;
30  import com.sun.saw.WorkflowFactory;
31  import com.sun.saw.vo.SaveTaskVO;
32  
33  import java.util.Properties;
34  
35  import org.aspectj.lang.ProceedingJoinPoint;
36  
37  /**
38   * <a href="JournalArticleLocalServiceAdvice.java.html"><b><i>View Source</i>
39   * </b></a>
40   *
41   * @author Joshna Reddy
42   * @author Raymond Augé
43   *
44   */
45  public class JournalArticleLocalServiceAdvice extends BaseMirageAdvice {
46  
47      protected Object doInvoke(ProceedingJoinPoint proceedingJoinPoint)
48          throws Throwable {
49  
50          String methodName = proceedingJoinPoint.getSignature().getName();
51          Object[] arguments = proceedingJoinPoint.getArgs();
52  
53          if (methodName.equals("addArticle") ||
54              methodName.equals("deleteArticle") ||
55              methodName.equals("getArticle") ||
56              methodName.equals("getLatestArticle") ||
57              methodName.equals("updateArticle")||
58              methodName.equals("updateContent")) {
59  
60              ContentInvoker contentInvoker = new ContentInvoker(
61                  proceedingJoinPoint);
62  
63              if (methodName.equals("addArticle")) {
64                  contentService.createContent(contentInvoker);
65              }
66              else if (methodName.equals("deleteArticle")) {
67                  contentService.deleteContent(contentInvoker);
68              }
69              else if (methodName.equals("getArticle") ||
70                       methodName.equals("getLatestArticle")) {
71  
72                  contentService.getContent(contentInvoker, null);
73              }
74              else if (methodName.equals("updateArticle")) {
75                  contentService.updateContent(contentInvoker);
76              }
77              else if (methodName.equals("updateContent")) {
78                  contentService.updateContent(contentInvoker, null);
79              }
80  
81              return contentInvoker.getReturnValue();
82          }
83          else if (methodName.equals("approveArticle") ||
84                   methodName.equals("expireArticle")) {
85  
86              processWorkflow();
87  
88              WorkflowInvoker workflowInvoker = new WorkflowInvoker(
89                  proceedingJoinPoint);
90  
91              if (methodName.equals("approveArticle")) {
92                  workflowService.updateWorkflowComplete(workflowInvoker);
93              }
94              else if (methodName.equals("expireArticle")) {
95                  workflowService.updateWorkflowContentRejected(workflowInvoker);
96              }
97  
98              return workflowInvoker.getReturnValue();
99          }
100         else if (methodName.equals("getArticles") ||
101                  methodName.equals("getArticlesBySmallImageId")||
102                  methodName.equals("getArticlesCount") ||
103                  methodName.equals("getDisplayArticle") ||
104                  methodName.equals("getStructureArticles") ||
105                  methodName.equals("getStructureArticlesCount") ||
106                  methodName.equals("getTemplateArticles") ||
107                  methodName.equals("getTemplateArticlesCount") ||
108                  methodName.equals("searchCount") ||
109                  (methodName.equals("search") && (arguments.length > 6))) {
110 
111             SearchCriteriaInvoker searchCriteriaInvoker =
112                 new SearchCriteriaInvoker(proceedingJoinPoint);
113 
114             if (methodName.equals("getArticles") ||
115                 methodName.equals("getArticlesBySmallImageId") ||
116                 methodName.equals("getDisplayArticle")||
117                 methodName.equals("getTemplateArticles")||
118                 methodName.equals("search")) {
119 
120                 contentService.searchContents(searchCriteriaInvoker);
121             }
122             else if (methodName.equals("getStructureArticles")) {
123                 contentService.searchContentsByType(
124                     null, searchCriteriaInvoker);
125             }
126             else if (methodName.equals("getArticlesCount") ||
127                      methodName.equals("searchCount")||
128                      methodName.equals("getTemplateArticlesCount")) {
129 
130                 contentService.contentSearchCount(searchCriteriaInvoker);
131             }
132             else if (methodName.equals("getStructureArticlesCount")) {
133                 contentService.contentSearchCount(null,searchCriteriaInvoker);
134             }
135 
136             return searchCriteriaInvoker.getReturnValue();
137         }
138         else {
139             return proceedingJoinPoint.proceed();
140         }
141     }
142 
143     protected Workflow getWorkflow() throws WorkflowException {
144         Properties properties = new Properties();
145 
146         properties.setProperty(
147             "sawworkflowimplclass", "com.sun.saw.impls.osworkflow.OSWorkflow");
148 
149         WorkflowFactory workflowFactory = WorkflowFactory.getInstance();
150 
151         return workflowFactory.getWorkflowInstance(properties);
152     }
153 
154     protected void processWorkflow() {
155         if (_outputVOError) {
156             return;
157         }
158 
159         try {
160             Workflow workflow = getWorkflow();
161 
162             SaveTaskVO saveTaskVO = new SaveTaskVO();
163 
164             workflow.saveTasks(saveTaskVO);
165         }
166         catch (WorkflowException we) {
167             _outputVOError = true;
168 
169             if (_log.isWarnEnabled()) {
170                 _log.warn(we, we);
171             }
172         }
173     }
174 
175     private static Log _log =
176         LogFactoryUtil.getLog(JournalArticleLocalServiceAdvice.class);
177 
178     private boolean _outputVOError;
179 
180 }