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