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