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.model.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.configuration.Filter;
28  import com.liferay.portal.kernel.language.LanguageUtil;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.ArrayUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.Address;
36  import com.liferay.portal.model.Group;
37  import com.liferay.portal.model.Organization;
38  import com.liferay.portal.model.OrganizationConstants;
39  import com.liferay.portal.service.AddressLocalServiceUtil;
40  import com.liferay.portal.service.GroupLocalServiceUtil;
41  import com.liferay.portal.service.OrganizationLocalServiceUtil;
42  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
43  import com.liferay.portal.util.PortletKeys;
44  import com.liferay.portal.util.PropsKeys;
45  import com.liferay.portal.util.PropsUtil;
46  import com.liferay.portal.util.PropsValues;
47  import com.liferay.util.LocalizationUtil;
48  import com.liferay.util.SetUtil;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Set;
54  
55  import javax.portlet.PortletPreferences;
56  
57  /**
58   * <a href="OrganizationImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   * @author Jorge Ferrer
62   *
63   */
64  public class OrganizationImpl
65      extends OrganizationModelImpl implements Organization {
66  
67      public List<Organization> getAncestors()
68          throws PortalException, SystemException {
69  
70          List<Organization> ancestors = new ArrayList<Organization>();
71  
72          Organization organization = this;
73  
74          while (true) {
75              if (!organization.isRoot()) {
76                  organization = organization.getParentOrganization();
77  
78                  ancestors.add(organization);
79              }
80              else {
81                  break;
82              }
83          }
84  
85          return ancestors;
86      }
87  
88      public static String[] getChildrenTypes(String type) {
89          return PropsUtil.getArray(
90              PropsKeys.ORGANIZATIONS_CHILDREN_TYPES, new Filter(type));
91      }
92  
93      public Organization getParentOrganization()
94          throws PortalException, SystemException {
95  
96          if (getParentOrganizationId() ==
97                  OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
98  
99              return null;
100         }
101 
102         return OrganizationLocalServiceUtil.getOrganization(
103             getParentOrganizationId());
104     }
105 
106     public static String[] getParentTypes(String type) {
107         String[] types = PropsUtil.getArray(
108             PropsKeys.ORGANIZATIONS_TYPES, new Filter(type));
109 
110         List<String> parentTypes = new ArrayList<String>();
111 
112         for (String curType : types) {
113             if (ArrayUtil.contains(getChildrenTypes(curType), type)) {
114                 parentTypes.add(curType);
115             }
116         }
117 
118         return parentTypes.toArray(new String[parentTypes.size()]);
119     }
120 
121     public static boolean isParentable(String type) {
122         String[] childrenTypes = getChildrenTypes(type);
123 
124         if (childrenTypes.length > 0) {
125             return true;
126         }
127         else {
128             return false;
129         }
130     }
131 
132     public static boolean isRootable(String type) {
133         return GetterUtil.getBoolean(
134             PropsUtil.get(PropsKeys.ORGANIZATIONS_ROOTABLE, new Filter(type)));
135     }
136 
137     public OrganizationImpl() {
138     }
139 
140     public Address getAddress() {
141         Address address = null;
142 
143         try {
144             List<Address> addresses = getAddresses();
145 
146             if (addresses.size() > 0) {
147                 address = addresses.get(0);
148             }
149         }
150         catch (Exception e) {
151             _log.error(e);
152         }
153 
154         if (address == null) {
155             address = new AddressImpl();
156         }
157 
158         return address;
159     }
160 
161     public List<Address> getAddresses() throws SystemException {
162         return AddressLocalServiceUtil.getAddresses(
163             getCompanyId(), Organization.class.getName(), getOrganizationId());
164     }
165 
166     public String[] getChildrenTypes() {
167         return getChildrenTypes(getType());
168     }
169 
170     public Group getGroup() {
171         if (getOrganizationId() > 0) {
172             try {
173                 return GroupLocalServiceUtil.getOrganizationGroup(
174                     getCompanyId(), getOrganizationId());
175             }
176             catch (Exception e) {
177                 _log.error(e);
178             }
179         }
180 
181         return new GroupImpl();
182     }
183 
184     public PortletPreferences getPreferences() throws SystemException {
185         long companyId = getCompanyId();
186         long ownerId = getOrganizationId();
187         int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
188         long plid = PortletKeys.PREFS_PLID_SHARED;
189         String portletId = PortletKeys.LIFERAY_PORTAL;
190 
191         return PortletPreferencesLocalServiceUtil.getPreferences(
192             companyId, ownerId, ownerType, plid, portletId);
193     }
194 
195     public int getPrivateLayoutsPageCount() {
196         try {
197             Group group = getGroup();
198 
199             if (group == null) {
200                 return 0;
201             }
202             else {
203                 return group.getPrivateLayoutsPageCount();
204             }
205         }
206         catch (Exception e) {
207             _log.error(e);
208         }
209 
210         return 0;
211     }
212 
213     public int getPublicLayoutsPageCount() {
214         try {
215             Group group = getGroup();
216 
217             if (group == null) {
218                 return 0;
219             }
220             else {
221                 return group.getPublicLayoutsPageCount();
222             }
223         }
224         catch (Exception e) {
225             _log.error(e);
226         }
227 
228         return 0;
229     }
230 
231     public Set<String> getReminderQueryQuestions(Locale locale)
232         throws SystemException {
233 
234         return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
235     }
236 
237     public Set<String> getReminderQueryQuestions(String languageId)
238         throws SystemException {
239 
240         PortletPreferences preferences = getPreferences();
241 
242         String[] questions = StringUtil.split(
243             LocalizationUtil.getPreferencesValue(
244                 preferences, "reminderQueries", languageId, false),
245             StringPool.NEW_LINE);
246 
247         return SetUtil.fromArray(questions);
248     }
249 
250     public List<Organization> getSuborganizations() throws SystemException {
251         return OrganizationLocalServiceUtil.search(
252             getCompanyId(), getOrganizationId(), null, null, null, null, null,
253             0, getSuborganizationsSize());
254     }
255 
256     public int getSuborganizationsSize() throws SystemException {
257         return OrganizationLocalServiceUtil.searchCount(
258             getCompanyId(), getOrganizationId(), null, null, null, null, null,
259             null, null, null, true);
260     }
261 
262     public int getTypeOrder() {
263         String[] types = PropsValues.ORGANIZATIONS_TYPES;
264 
265         for (int i = 0; i < types.length; i++) {
266             String type = types[i];
267 
268             if (type.equals(getType())) {
269                 return i + 1;
270             }
271         }
272 
273         return 0;
274     }
275 
276     public boolean hasPrivateLayouts() {
277         if (getPrivateLayoutsPageCount() > 0) {
278             return true;
279         }
280         else {
281             return false;
282         }
283     }
284 
285     public boolean hasPublicLayouts() {
286         if (getPublicLayoutsPageCount() > 0) {
287             return true;
288         }
289         else {
290             return false;
291         }
292     }
293 
294     public boolean hasSuborganizations() throws SystemException {
295         if (getSuborganizationsSize() > 0) {
296             return true;
297         }
298         else {
299             return false;
300         }
301     }
302 
303     public boolean isParentable() {
304         return isParentable(getType());
305     }
306 
307     public boolean isRoot() {
308         if (getParentOrganizationId() ==
309                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
310 
311             return true;
312         }
313         else {
314             return false;
315         }
316     }
317 
318     private static Log _log = LogFactoryUtil.getLog(Organization.class);
319 
320 }