1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EnterpriseAdminUtil {
55  
56      public static OrderByComparator getGroupOrderByComparator(
57          String orderByCol, String orderByType) {
58  
59          boolean orderByAsc = false;
60  
61          if (orderByType.equals("asc")) {
62              orderByAsc = true;
63          }
64  
65          OrderByComparator orderByComparator = null;
66  
67          if (orderByCol.equals("name")) {
68              orderByComparator = new GroupNameComparator(orderByAsc);
69          }
70          else if (orderByCol.equals("type")) {
71              orderByComparator = new GroupTypeComparator(orderByAsc);
72          }
73          else {
74              orderByComparator = new GroupNameComparator(orderByAsc);
75          }
76  
77          return orderByComparator;
78      }
79  
80      public static Long[][] getLeftAndRightOrganizationIds(long organizationId)
81          throws PortalException, SystemException {
82  
83          Organization organization =
84              OrganizationLocalServiceUtil.getOrganization(organizationId);
85  
86          return getLeftAndRightOrganizationIds(organization);
87      }
88  
89      public static Long[][] getLeftAndRightOrganizationIds(
90          Organization organization) {
91  
92          return new Long[][] {
93              new Long[] {
94                  organization.getLeftOrganizationId(),
95                  organization.getRightOrganizationId()
96              }
97          };
98      }
99  
100     public static Long[][] getLeftAndRightOrganizationIds(
101         List<Organization> organizations) {
102 
103         Long[][] leftAndRightOrganizationIds = new Long[organizations.size()][];
104 
105         for (int i = 0; i < organizations.size(); i++) {
106             Organization organization = organizations.get(i);
107 
108             leftAndRightOrganizationIds[i] =
109                 new Long[] {
110                     organization.getLeftOrganizationId(),
111                     organization.getRightOrganizationId()
112                 };
113         }
114 
115         return leftAndRightOrganizationIds;
116     }
117 
118     public static Long[] getOrganizationIds(List<Organization> organizations) {
119         if ((organizations == null) || organizations.isEmpty()) {
120             return new Long[0];
121         }
122 
123         Long[] organizationIds = new Long[organizations.size()];
124 
125         for (int i = 0; i < organizations.size(); i++) {
126             Organization organization = organizations.get(i);
127 
128             organizationIds[i] = new Long(organization.getOrganizationId());
129         }
130 
131         return organizationIds;
132     }
133 
134     public static OrderByComparator getOrganizationOrderByComparator(
135         String orderByCol, String orderByType) {
136 
137         boolean orderByAsc = false;
138 
139         if (orderByType.equals("asc")) {
140             orderByAsc = true;
141         }
142 
143         OrderByComparator orderByComparator = null;
144 
145         if (orderByCol.equals("name")) {
146             orderByComparator = new OrganizationNameComparator(orderByAsc);
147         }
148         else if (orderByCol.equals("type")) {
149             orderByComparator = new OrganizationTypeComparator(orderByAsc);
150         }
151         else {
152             orderByComparator = new OrganizationNameComparator(orderByAsc);
153         }
154 
155         return orderByComparator;
156     }
157 
158     public static OrderByComparator getPasswordPolicyOrderByComparator(
159         String orderByCol, String orderByType) {
160 
161         boolean orderByAsc = false;
162 
163         if (orderByType.equals("asc")) {
164             orderByAsc = true;
165         }
166 
167         OrderByComparator orderByComparator = null;
168 
169         if (orderByCol.equals("name")) {
170             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
171         }
172         else if (orderByCol.equals("description")) {
173             orderByComparator = new PasswordPolicyDescriptionComparator(
174                 orderByAsc);
175         }
176         else {
177             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
178         }
179 
180         return orderByComparator;
181     }
182 
183     public static OrderByComparator getRoleOrderByComparator(
184         String orderByCol, String orderByType) {
185 
186         boolean orderByAsc = false;
187 
188         if (orderByType.equals("asc")) {
189             orderByAsc = true;
190         }
191 
192         OrderByComparator orderByComparator = null;
193 
194         if (orderByCol.equals("name")) {
195             orderByComparator = new RoleNameComparator(orderByAsc);
196         }
197         else if (orderByCol.equals("description")) {
198             orderByComparator = new RoleDescriptionComparator(orderByAsc);
199         }
200         else if (orderByCol.equals("type")) {
201             orderByComparator = new RoleTypeComparator(orderByAsc);
202         }
203         else {
204             orderByComparator = new RoleNameComparator(orderByAsc);
205         }
206 
207         return orderByComparator;
208     }
209 
210     public static OrderByComparator getUserGroupOrderByComparator(
211         String orderByCol, String orderByType) {
212 
213         boolean orderByAsc = false;
214 
215         if (orderByType.equals("asc")) {
216             orderByAsc = true;
217         }
218 
219         OrderByComparator orderByComparator = null;
220 
221         if (orderByCol.equals("name")) {
222             orderByComparator = new UserGroupNameComparator(orderByAsc);
223         }
224         else if (orderByCol.equals("description")) {
225             orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
226         }
227         else {
228             orderByComparator = new UserGroupNameComparator(orderByAsc);
229         }
230 
231         return orderByComparator;
232     }
233 
234     public static OrderByComparator getUserOrderByComparator(
235         String orderByCol, String orderByType) {
236 
237         boolean orderByAsc = false;
238 
239         if (orderByType.equals("asc")) {
240             orderByAsc = true;
241         }
242 
243         OrderByComparator orderByComparator = null;
244 
245         if (orderByCol.equals("email-address")) {
246             orderByComparator = new UserEmailAddressComparator(orderByAsc);
247         }
248         else if (orderByCol.equals("first-name")) {
249             orderByComparator = new UserFirstNameComparator(orderByAsc);
250         }
251         else if (orderByCol.equals("job-title")) {
252             orderByComparator = new UserJobTitleComparator(orderByAsc);
253         }
254         else if (orderByCol.equals("last-name")) {
255             orderByComparator = new UserLastNameComparator(orderByAsc);
256         }
257         else if (orderByCol.equals("screen-name")) {
258             orderByComparator = new UserScreenNameComparator(orderByAsc);
259         }
260         else {
261             orderByComparator = new UserLastNameComparator(orderByAsc);
262         }
263 
264         return orderByComparator;
265     }
266 
267 }