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