001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocalizationUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.SetUtil;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.model.Address;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.LayoutSet;
034    import com.liferay.portal.model.Organization;
035    import com.liferay.portal.model.OrganizationConstants;
036    import com.liferay.portal.service.AddressLocalServiceUtil;
037    import com.liferay.portal.service.GroupLocalServiceUtil;
038    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
039    import com.liferay.portal.service.OrganizationLocalServiceUtil;
040    import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
041    import com.liferay.portal.util.PortletKeys;
042    import com.liferay.portal.util.PropsUtil;
043    import com.liferay.portal.util.PropsValues;
044    import com.liferay.util.UniqueList;
045    
046    import java.util.ArrayList;
047    import java.util.List;
048    import java.util.Locale;
049    import java.util.Set;
050    
051    import javax.portlet.PortletPreferences;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Jorge Ferrer
056     */
057    public class OrganizationImpl extends OrganizationBaseImpl {
058    
059            public static String[] getChildrenTypes(String type) {
060                    return PropsUtil.getArray(
061                            PropsKeys.ORGANIZATIONS_CHILDREN_TYPES, new Filter(type));
062            }
063    
064            public static String[] getParentTypes(String type) {
065                    String[] types = PropsUtil.getArray(
066                            PropsKeys.ORGANIZATIONS_TYPES, new Filter(type));
067    
068                    List<String> parentTypes = new ArrayList<String>();
069    
070                    for (String curType : types) {
071                            if (ArrayUtil.contains(getChildrenTypes(curType), type)) {
072                                    parentTypes.add(curType);
073                            }
074                    }
075    
076                    return parentTypes.toArray(new String[parentTypes.size()]);
077            }
078    
079            public static boolean isParentable(String type) {
080                    String[] childrenTypes = getChildrenTypes(type);
081    
082                    if (childrenTypes.length > 0) {
083                            return true;
084                    }
085                    else {
086                            return false;
087                    }
088            }
089    
090            public static boolean isRootable(String type) {
091                    return GetterUtil.getBoolean(
092                            PropsUtil.get(PropsKeys.ORGANIZATIONS_ROOTABLE, new Filter(type)));
093            }
094    
095            public OrganizationImpl() {
096            }
097    
098            public String buildTreePath() throws PortalException, SystemException {
099                    StringBundler sb = new StringBundler();
100    
101                    buildTreePath(sb, this);
102    
103                    return sb.toString();
104            }
105    
106            public Address getAddress() {
107                    Address address = null;
108    
109                    try {
110                            List<Address> addresses = getAddresses();
111    
112                            if (addresses.size() > 0) {
113                                    address = addresses.get(0);
114                            }
115                    }
116                    catch (Exception e) {
117                            _log.error(e);
118                    }
119    
120                    if (address == null) {
121                            address = new AddressImpl();
122                    }
123    
124                    return address;
125            }
126    
127            public List<Address> getAddresses() throws SystemException {
128                    return AddressLocalServiceUtil.getAddresses(
129                            getCompanyId(), Organization.class.getName(), getOrganizationId());
130            }
131    
132            public List<Organization> getAncestors()
133                    throws PortalException, SystemException {
134    
135                    List<Organization> ancestors = new ArrayList<Organization>();
136    
137                    Organization organization = this;
138    
139                    while (true) {
140                            if (!organization.isRoot()) {
141                                    organization = organization.getParentOrganization();
142    
143                                    ancestors.add(organization);
144                            }
145                            else {
146                                    break;
147                            }
148                    }
149    
150                    return ancestors;
151            }
152    
153            public String[] getChildrenTypes() {
154                    return getChildrenTypes(getType());
155            }
156    
157            public List<Organization> getDescendants() throws SystemException {
158                    List<Organization> descendants = new UniqueList<Organization>();
159    
160                    for (Organization suborganization : getSuborganizations()) {
161                            descendants.add(suborganization);
162                            descendants.addAll(suborganization.getDescendants());
163                    }
164    
165                    return descendants;
166            }
167    
168            public Group getGroup() {
169                    if (getOrganizationId() > 0) {
170                            try {
171                                    return GroupLocalServiceUtil.getOrganizationGroup(
172                                            getCompanyId(), getOrganizationId());
173                            }
174                            catch (Exception e) {
175                                    _log.error(e);
176                            }
177                    }
178    
179                    return new GroupImpl();
180            }
181    
182            public long getGroupId() {
183                    Group group = getGroup();
184    
185                    return group.getGroupId();
186            }
187    
188            public long getLogoId() {
189                    long logoId = 0;
190    
191                    try {
192                            Group group = getGroup();
193    
194                            LayoutSet publicLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
195                                    group.getGroupId(), false);
196    
197                            logoId = publicLayoutSet.getLogoId();
198    
199                            if (logoId == 0) {
200                                    LayoutSet privateLayoutSet =
201                                            LayoutSetLocalServiceUtil.getLayoutSet(
202                                                    group.getGroupId(), true);
203    
204                                    logoId = privateLayoutSet.getLogoId();
205                            }
206                    }
207                    catch (Exception e) {
208                            _log.error(e);
209                    }
210    
211                    return logoId;
212            }
213    
214            public Organization getParentOrganization()
215                    throws PortalException, SystemException {
216    
217                    if (getParentOrganizationId() ==
218                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
219    
220                            return null;
221                    }
222    
223                    return OrganizationLocalServiceUtil.getOrganization(
224                            getParentOrganizationId());
225            }
226    
227            public PortletPreferences getPreferences() throws SystemException {
228                    long companyId = getCompanyId();
229                    long ownerId = getOrganizationId();
230                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
231    
232                    return PortalPreferencesLocalServiceUtil.getPreferences(
233                            companyId, ownerId, ownerType);
234            }
235    
236            public int getPrivateLayoutsPageCount() {
237                    try {
238                            Group group = getGroup();
239    
240                            if (group == null) {
241                                    return 0;
242                            }
243                            else {
244                                    return group.getPrivateLayoutsPageCount();
245                            }
246                    }
247                    catch (Exception e) {
248                            _log.error(e);
249                    }
250    
251                    return 0;
252            }
253    
254            public int getPublicLayoutsPageCount() {
255                    try {
256                            Group group = getGroup();
257    
258                            if (group == null) {
259                                    return 0;
260                            }
261                            else {
262                                    return group.getPublicLayoutsPageCount();
263                            }
264                    }
265                    catch (Exception e) {
266                            _log.error(e);
267                    }
268    
269                    return 0;
270            }
271    
272            public Set<String> getReminderQueryQuestions(Locale locale)
273                    throws SystemException {
274    
275                    return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
276            }
277    
278            public Set<String> getReminderQueryQuestions(String languageId)
279                    throws SystemException {
280    
281                    PortletPreferences preferences = getPreferences();
282    
283                    String[] questions = StringUtil.splitLines(
284                            LocalizationUtil.getPreferencesValue(
285                                    preferences, "reminderQueries", languageId, false));
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            protected void buildTreePath(StringBundler sb, Organization organization)
359                    throws PortalException, SystemException {
360    
361                    if (organization == null) {
362                            sb.append(StringPool.SLASH);
363                    }
364                    else {
365                            buildTreePath(sb, organization.getParentOrganization());
366    
367                            sb.append(organization.getOrganizationId());
368                            sb.append(StringPool.SLASH);
369                    }
370            }
371    
372            private static Log _log = LogFactoryUtil.getLog(Organization.class);
373    
374    }