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.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.PropsKeys;
34  import com.liferay.portal.kernel.util.SetUtil;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.Address;
38  import com.liferay.portal.model.Group;
39  import com.liferay.portal.model.LayoutSet;
40  import com.liferay.portal.model.Organization;
41  import com.liferay.portal.model.OrganizationConstants;
42  import com.liferay.portal.service.AddressLocalServiceUtil;
43  import com.liferay.portal.service.GroupLocalServiceUtil;
44  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
45  import com.liferay.portal.service.OrganizationLocalServiceUtil;
46  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
47  import com.liferay.portal.util.PortletKeys;
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  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 List<Organization> getDescendants() throws SystemException {
173         List<Organization> descendants = new UniqueList<Organization>();
174 
175         for (Organization suborganization : getSuborganizations()) {
176             descendants.add(suborganization);
177             descendants.addAll(suborganization.getDescendants());
178         }
179 
180         return descendants;
181     }
182 
183     public Group getGroup() {
184         if (getOrganizationId() > 0) {
185             try {
186                 return GroupLocalServiceUtil.getOrganizationGroup(
187                     getCompanyId(), getOrganizationId());
188             }
189             catch (Exception e) {
190                 _log.error(e);
191             }
192         }
193 
194         return new GroupImpl();
195     }
196 
197     public long getLogoId() {
198         long logoId = 0;
199 
200         try {
201             Group group = getGroup();
202 
203             LayoutSet publicLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
204                 group.getGroupId(), false);
205 
206             logoId = publicLayoutSet.getLogoId();
207 
208             if (logoId == 0) {
209                 LayoutSet privateLayoutSet =
210                     LayoutSetLocalServiceUtil.getLayoutSet(
211                         group.getGroupId(), true);
212 
213                 logoId = privateLayoutSet.getLogoId();
214             }
215         }
216         catch (Exception e) {
217             _log.error(e);
218         }
219 
220         return logoId;
221     }
222 
223     public PortletPreferences getPreferences() throws SystemException {
224         long companyId = getCompanyId();
225         long ownerId = getOrganizationId();
226         int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
227         long plid = PortletKeys.PREFS_PLID_SHARED;
228         String portletId = PortletKeys.LIFERAY_PORTAL;
229 
230         return PortletPreferencesLocalServiceUtil.getPreferences(
231             companyId, ownerId, ownerType, plid, portletId);
232     }
233 
234     public int getPrivateLayoutsPageCount() {
235         try {
236             Group group = getGroup();
237 
238             if (group == null) {
239                 return 0;
240             }
241             else {
242                 return group.getPrivateLayoutsPageCount();
243             }
244         }
245         catch (Exception e) {
246             _log.error(e);
247         }
248 
249         return 0;
250     }
251 
252     public int getPublicLayoutsPageCount() {
253         try {
254             Group group = getGroup();
255 
256             if (group == null) {
257                 return 0;
258             }
259             else {
260                 return group.getPublicLayoutsPageCount();
261             }
262         }
263         catch (Exception e) {
264             _log.error(e);
265         }
266 
267         return 0;
268     }
269 
270     public Set<String> getReminderQueryQuestions(Locale locale)
271         throws SystemException {
272 
273         return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
274     }
275 
276     public Set<String> getReminderQueryQuestions(String languageId)
277         throws SystemException {
278 
279         PortletPreferences preferences = getPreferences();
280 
281         String[] questions = StringUtil.split(
282             LocalizationUtil.getPreferencesValue(
283                 preferences, "reminderQueries", languageId, false),
284             StringPool.NEW_LINE);
285 
286         return SetUtil.fromArray(questions);
287     }
288 
289     public List<Organization> getSuborganizations() throws SystemException {
290         return OrganizationLocalServiceUtil.search(
291             getCompanyId(), getOrganizationId(), null, null, null, null, null,
292             0, getSuborganizationsSize());
293     }
294 
295     public int getSuborganizationsSize() throws SystemException {
296         return OrganizationLocalServiceUtil.searchCount(
297             getCompanyId(), getOrganizationId(), null, null, null, null, null,
298             null, null, null, true);
299     }
300 
301     public int getTypeOrder() {
302         String[] types = PropsValues.ORGANIZATIONS_TYPES;
303 
304         for (int i = 0; i < types.length; i++) {
305             String type = types[i];
306 
307             if (type.equals(getType())) {
308                 return i + 1;
309             }
310         }
311 
312         return 0;
313     }
314 
315     public boolean hasPrivateLayouts() {
316         if (getPrivateLayoutsPageCount() > 0) {
317             return true;
318         }
319         else {
320             return false;
321         }
322     }
323 
324     public boolean hasPublicLayouts() {
325         if (getPublicLayoutsPageCount() > 0) {
326             return true;
327         }
328         else {
329             return false;
330         }
331     }
332 
333     public boolean hasSuborganizations() throws SystemException {
334         if (getSuborganizationsSize() > 0) {
335             return true;
336         }
337         else {
338             return false;
339         }
340     }
341 
342     public boolean isParentable() {
343         return isParentable(getType());
344     }
345 
346     public boolean isRoot() {
347         if (getParentOrganizationId() ==
348                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
349 
350             return true;
351         }
352         else {
353             return false;
354         }
355     }
356 
357     private static Log _log = LogFactoryUtil.getLog(Organization.class);
358 
359 }