1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.tasks.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.language.LanguageUtil;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.service.GroupLocalServiceUtil;
22  import com.liferay.portlet.tasks.model.TasksProposal;
23  import com.liferay.portlet.tasks.model.TasksReview;
24  import com.liferay.portlet.tasks.service.TasksReviewLocalServiceUtil;
25  
26  import java.util.List;
27  import java.util.Locale;
28  
29  /**
30   * <a href="TasksProposalImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class TasksProposalImpl
35      extends TasksProposalModelImpl implements TasksProposal {
36  
37      public TasksProposalImpl() {
38      }
39  
40      public String getStatus(Locale locale)
41          throws PortalException, SystemException {
42  
43          String status = null;
44          int stage = 1;
45  
46          Group group = GroupLocalServiceUtil.getGroup(getGroupId());
47  
48          int stages = group.getWorkflowStages();
49  
50          for (; stage <= stages; stage++) {
51              status = getStatus(stage);
52  
53              if (status.equals(_STATUS_APPROVED)) {
54              }
55              else if (status.equals(_STATUS_PENDING) ||
56                       status.equals(_STATUS_REJECTED)) {
57  
58                  break;
59              }
60              else if ((status.equals(_STATUS_UNASSIGNED)) &&
61                       (stage > 1)) {
62              }
63              else if (stage == 0) {
64                  break;
65              }
66  
67              if (stage == stages) {
68                  break;
69              }
70          }
71  
72          return LanguageUtil.format(locale, status, String.valueOf(stage + 1));
73      }
74  
75      protected String getStatus(int stage) throws SystemException {
76          List<TasksReview> reviews = TasksReviewLocalServiceUtil.getReviews(
77              getProposalId(), stage);
78  
79          if (reviews.size() <= 0) {
80              return _STATUS_UNASSIGNED;
81          }
82  
83          List<TasksReview> completedReviews =
84              TasksReviewLocalServiceUtil.getReviews(
85                  getProposalId(), stage, true);
86  
87          if (completedReviews.size() < reviews.size()) {
88              return _STATUS_PENDING;
89          }
90  
91          List<TasksReview> completedRejectedReviews =
92              TasksReviewLocalServiceUtil.getReviews(
93                  getProposalId(), stage, true, true);
94  
95          if (completedRejectedReviews.size() > 0) {
96              return _STATUS_REJECTED;
97          }
98          else {
99              return _STATUS_APPROVED;
100         }
101     }
102 
103     private static final String _STATUS_APPROVED = "stage-x-review-approved";
104 
105     private static final String _STATUS_PENDING = "stage-x-pending-review";
106 
107     private static final String _STATUS_REJECTED = "stage-x-review-rejected";
108 
109     private static final String _STATUS_UNASSIGNED =
110         "stage-x-review-unassigned";
111 
112 }