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