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.portlet.enterpriseadmin.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.service.OrganizationLocalServiceUtil;
30  import com.liferay.portal.util.comparator.GroupNameComparator;
31  import com.liferay.portal.util.comparator.GroupTypeComparator;
32  import com.liferay.portal.util.comparator.OrganizationNameComparator;
33  import com.liferay.portal.util.comparator.OrganizationTypeComparator;
34  import com.liferay.portal.util.comparator.PasswordPolicyDescriptionComparator;
35  import com.liferay.portal.util.comparator.PasswordPolicyNameComparator;
36  import com.liferay.portal.util.comparator.RoleDescriptionComparator;
37  import com.liferay.portal.util.comparator.RoleNameComparator;
38  import com.liferay.portal.util.comparator.RoleTypeComparator;
39  import com.liferay.portal.util.comparator.UserEmailAddressComparator;
40  import com.liferay.portal.util.comparator.UserFirstNameComparator;
41  import com.liferay.portal.util.comparator.UserGroupDescriptionComparator;
42  import com.liferay.portal.util.comparator.UserGroupNameComparator;
43  import com.liferay.portal.util.comparator.UserJobTitleComparator;
44  import com.liferay.portal.util.comparator.UserLastNameComparator;
45  import com.liferay.portal.util.comparator.UserScreenNameComparator;
46  
47  import java.util.List;
48  
49  /**
50   * <a href="EnterpriseAdminUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class EnterpriseAdminUtil {
56  
57      public static OrderByComparator getGroupOrderByComparator(
58          String orderByCol, String orderByType) {
59  
60          boolean orderByAsc = false;
61  
62          if (orderByType.equals("asc")) {
63              orderByAsc = true;
64          }
65  
66          OrderByComparator orderByComparator = null;
67  
68          if (orderByCol.equals("name")) {
69              orderByComparator = new GroupNameComparator(orderByAsc);
70          }
71          else if (orderByCol.equals("type")) {
72              orderByComparator = new GroupTypeComparator(orderByAsc);
73          }
74          else {
75              orderByComparator = new GroupNameComparator(orderByAsc);
76          }
77  
78          return orderByComparator;
79      }
80  
81      public static Long[][] getLeftAndRightOrganizationIds(long organizationId)
82          throws PortalException, SystemException {
83  
84          Organization organization =
85              OrganizationLocalServiceUtil.getOrganization(organizationId);
86  
87          return getLeftAndRightOrganizationIds(organization);
88      }
89  
90      public static Long[][] getLeftAndRightOrganizationIds(
91          Organization organization) {
92  
93          return new Long[][] {
94              new Long[] {
95                  organization.getLeftOrganizationId(),
96                  organization.getRightOrganizationId()
97              }
98          };
99      }
100 
101     public static Long[][] getLeftAndRightOrganizationIds(
102         List<Organization> organizations) {
103 
104         Long[][] leftAndRightOrganizationIds = new Long[organizations.size()][];
105 
106         for (int i = 0; i < organizations.size(); i++) {
107             Organization organization = organizations.get(i);
108 
109             leftAndRightOrganizationIds[i] =
110                 new Long[] {
111                     organization.getLeftOrganizationId(),
112                     organization.getRightOrganizationId()
113                 };
114         }
115 
116         return leftAndRightOrganizationIds;
117     }
118 
119     public static Long[] getOrganizationIds(List<Organization> organizations) {
120         if ((organizations == null) || organizations.isEmpty()) {
121             return new Long[0];
122         }
123 
124         Long[] organizationIds = new Long[organizations.size()];
125 
126         for (int i = 0; i < organizations.size(); i++) {
127             Organization organization = organizations.get(i);
128 
129             organizationIds[i] = new Long(organization.getOrganizationId());
130         }
131 
132         return organizationIds;
133     }
134 
135     public static OrderByComparator getOrganizationOrderByComparator(
136         String orderByCol, String orderByType) {
137 
138         boolean orderByAsc = false;
139 
140         if (orderByType.equals("asc")) {
141             orderByAsc = true;
142         }
143 
144         OrderByComparator orderByComparator = null;
145 
146         if (orderByCol.equals("name")) {
147             orderByComparator = new OrganizationNameComparator(orderByAsc);
148         }
149         else if (orderByCol.equals("type")) {
150             orderByComparator = new OrganizationTypeComparator(orderByAsc);
151         }
152         else {
153             orderByComparator = new OrganizationNameComparator(orderByAsc);
154         }
155 
156         return orderByComparator;
157     }
158 
159     public static OrderByComparator getPasswordPolicyOrderByComparator(
160         String orderByCol, String orderByType) {
161 
162         boolean orderByAsc = false;
163 
164         if (orderByType.equals("asc")) {
165             orderByAsc = true;
166         }
167 
168         OrderByComparator orderByComparator = null;
169 
170         if (orderByCol.equals("name")) {
171             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
172         }
173         else if (orderByCol.equals("description")) {
174             orderByComparator = new PasswordPolicyDescriptionComparator(
175                 orderByAsc);
176         }
177         else {
178             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
179         }
180 
181         return orderByComparator;
182     }
183 
184     public static OrderByComparator getRoleOrderByComparator(
185         String orderByCol, String orderByType) {
186 
187         boolean orderByAsc = false;
188 
189         if (orderByType.equals("asc")) {
190             orderByAsc = true;
191         }
192 
193         OrderByComparator orderByComparator = null;
194 
195         if (orderByCol.equals("name")) {
196             orderByComparator = new RoleNameComparator(orderByAsc);
197         }
198         else if (orderByCol.equals("description")) {
199             orderByComparator = new RoleDescriptionComparator(orderByAsc);
200         }
201         else if (orderByCol.equals("type")) {
202             orderByComparator = new RoleTypeComparator(orderByAsc);
203         }
204         else {
205             orderByComparator = new RoleNameComparator(orderByAsc);
206         }
207 
208         return orderByComparator;
209     }
210 
211     public static OrderByComparator getUserGroupOrderByComparator(
212         String orderByCol, String orderByType) {
213 
214         boolean orderByAsc = false;
215 
216         if (orderByType.equals("asc")) {
217             orderByAsc = true;
218         }
219 
220         OrderByComparator orderByComparator = null;
221 
222         if (orderByCol.equals("name")) {
223             orderByComparator = new UserGroupNameComparator(orderByAsc);
224         }
225         else if (orderByCol.equals("description")) {
226             orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
227         }
228         else {
229             orderByComparator = new UserGroupNameComparator(orderByAsc);
230         }
231 
232         return orderByComparator;
233     }
234 
235     public static OrderByComparator getUserOrderByComparator(
236         String orderByCol, String orderByType) {
237 
238         boolean orderByAsc = false;
239 
240         if (orderByType.equals("asc")) {
241             orderByAsc = true;
242         }
243 
244         OrderByComparator orderByComparator = null;
245 
246         if (orderByCol.equals("email-address")) {
247             orderByComparator = new UserEmailAddressComparator(orderByAsc);
248         }
249         else if (orderByCol.equals("first-name")) {
250             orderByComparator = new UserFirstNameComparator(orderByAsc);
251         }
252         else if (orderByCol.equals("job-title")) {
253             orderByComparator = new UserJobTitleComparator(orderByAsc);
254         }
255         else if (orderByCol.equals("last-name")) {
256             orderByComparator = new UserLastNameComparator(orderByAsc);
257         }
258         else if (orderByCol.equals("screen-name")) {
259             orderByComparator = new UserScreenNameComparator(orderByAsc);
260         }
261         else {
262             orderByComparator = new UserLastNameComparator(orderByAsc);
263         }
264 
265         return orderByComparator;
266     }
267 
268 }