001    /**
002     * Copyright (c) 2000-2010 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.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.model.Address;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.Organization;
034    import com.liferay.portal.model.OrganizationConstants;
035    import com.liferay.portal.service.AddressLocalServiceUtil;
036    import com.liferay.portal.service.GroupLocalServiceUtil;
037    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
038    import com.liferay.portal.service.OrganizationLocalServiceUtil;
039    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
040    import com.liferay.portal.util.PortletKeys;
041    import com.liferay.portal.util.PropsUtil;
042    import com.liferay.portal.util.PropsValues;
043    import com.liferay.util.UniqueList;
044    
045    import java.util.ArrayList;
046    import java.util.List;
047    import java.util.Locale;
048    import java.util.Set;
049    
050    import javax.portlet.PortletPreferences;
051    
052    /**
053     * @author Brian Wing Shun Chan
054     * @author Jorge Ferrer
055     */
056    public class OrganizationImpl
057            extends OrganizationModelImpl implements Organization {
058    
059            public List<Organization> getAncestors()
060                    throws PortalException, SystemException {
061    
062                    List<Organization> ancestors = new ArrayList<Organization>();
063    
064                    Organization organization = this;
065    
066                    while (true) {
067                            if (!organization.isRoot()) {
068                                    organization = organization.getParentOrganization();
069    
070                                    ancestors.add(organization);
071                            }
072                            else {
073                                    break;
074                            }
075                    }
076    
077                    return ancestors;
078            }
079    
080            public static String[] getChildrenTypes(String type) {
081                    return PropsUtil.getArray(
082                            PropsKeys.ORGANIZATIONS_CHILDREN_TYPES, new Filter(type));
083            }
084    
085            public Organization getParentOrganization()
086                    throws PortalException, SystemException {
087    
088                    if (getParentOrganizationId() ==
089                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
090    
091                            return null;
092                    }
093    
094                    return OrganizationLocalServiceUtil.getOrganization(
095                            getParentOrganizationId());
096            }
097    
098            public static String[] getParentTypes(String type) {
099                    String[] types = PropsUtil.getArray(
100                            PropsKeys.ORGANIZATIONS_TYPES, new Filter(type));
101    
102                    List<String> parentTypes = new ArrayList<String>();
103    
104                    for (String curType : types) {
105                            if (ArrayUtil.contains(getChildrenTypes(curType), type)) {
106                                    parentTypes.add(curType);
107                            }
108                    }
109    
110                    return parentTypes.toArray(new String[parentTypes.size()]);
111            }
112    
113            public static boolean isParentable(String type) {
114                    String[] childrenTypes = getChildrenTypes(type);
115    
116                    if (childrenTypes.length > 0) {
117                            return true;
118                    }
119                    else {
120                            return false;
121                    }
122            }
123    
124            public static boolean isRootable(String type) {
125                    return GetterUtil.getBoolean(
126                            PropsUtil.get(PropsKeys.ORGANIZATIONS_ROOTABLE, new Filter(type)));
127            }
128    
129            public OrganizationImpl() {
130            }
131    
132            public Address getAddress() {
133                    Address address = null;
134    
135                    try {
136                            List<Address> addresses = getAddresses();
137    
138                            if (addresses.size() > 0) {
139                                    address = addresses.get(0);
140                            }
141                    }
142                    catch (Exception e) {
143                            _log.error(e);
144                    }
145    
146                    if (address == null) {
147                            address = new AddressImpl();
148                    }
149    
150                    return address;
151            }
152    
153            public List<Address> getAddresses() throws SystemException {
154                    return AddressLocalServiceUtil.getAddresses(
155                            getCompanyId(), Organization.class.getName(), getOrganizationId());
156            }
157    
158            public String[] getChildrenTypes() {
159                    return getChildrenTypes(getType());
160            }
161    
162            public List<Organization> getDescendants() throws SystemException {
163                    List<Organization> descendants = new UniqueList<Organization>();
164    
165                    for (Organization suborganization : getSuborganizations()) {
166                            descendants.add(suborganization);
167                            descendants.addAll(suborganization.getDescendants());
168                    }
169    
170                    return descendants;
171            }
172    
173            public Group getGroup() {
174                    if (getOrganizationId() > 0) {
175                            try {
176                                    return GroupLocalServiceUtil.getOrganizationGroup(
177                                            getCompanyId(), getOrganizationId());
178                            }
179                            catch (Exception e) {
180                                    _log.error(e);
181                            }
182                    }
183    
184                    return new GroupImpl();
185            }
186    
187            public long getLogoId() {
188                    long logoId = 0;
189    
190                    try {
191                            Group group = getGroup();
192    
193                            LayoutSet publicLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
194                                    group.getGroupId(), false);
195    
196                            logoId = publicLayoutSet.getLogoId();
197    
198                            if (logoId == 0) {
199                                    LayoutSet privateLayoutSet =
200                                            LayoutSetLocalServiceUtil.getLayoutSet(
201                                                    group.getGroupId(), true);
202    
203                                    logoId = privateLayoutSet.getLogoId();
204                            }
205                    }
206                    catch (Exception e) {
207                            _log.error(e);
208                    }
209    
210                    return logoId;
211            }
212    
213            public PortletPreferences getPreferences() throws SystemException {
214                    long companyId = getCompanyId();
215                    long ownerId = getOrganizationId();
216                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
217                    long plid = PortletKeys.PREFS_PLID_SHARED;
218                    String portletId = PortletKeys.LIFERAY_PORTAL;
219    
220                    return PortletPreferencesLocalServiceUtil.getPreferences(
221                            companyId, ownerId, ownerType, plid, portletId);
222            }
223    
224            public int getPrivateLayoutsPageCount() {
225                    try {
226                            Group group = getGroup();
227    
228                            if (group == null) {
229                                    return 0;
230                            }
231                            else {
232                                    return group.getPrivateLayoutsPageCount();
233                            }
234                    }
235                    catch (Exception e) {
236                            _log.error(e);
237                    }
238    
239                    return 0;
240            }
241    
242            public int getPublicLayoutsPageCount() {
243                    try {
244                            Group group = getGroup();
245    
246                            if (group == null) {
247                                    return 0;
248                            }
249                            else {
250                                    return group.getPublicLayoutsPageCount();
251                            }
252                    }
253                    catch (Exception e) {
254                            _log.error(e);
255                    }
256    
257                    return 0;
258            }
259    
260            public Set<String> getReminderQueryQuestions(Locale locale)
261                    throws SystemException {
262    
263                    return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
264            }
265    
266            public Set<String> getReminderQueryQuestions(String languageId)
267                    throws SystemException {
268    
269                    PortletPreferences preferences = getPreferences();
270    
271                    String[] questions = StringUtil.split(
272                            LocalizationUtil.getPreferencesValue(
273                                    preferences, "reminderQueries", languageId, false),
274                            StringPool.NEW_LINE);
275    
276                    return SetUtil.fromArray(questions);
277            }
278    
279            public List<Organization> getSuborganizations() throws SystemException {
280                    return OrganizationLocalServiceUtil.search(
281                            getCompanyId(), getOrganizationId(), null, null, null, null, null,
282                            0, getSuborganizationsSize());
283            }
284    
285            public int getSuborganizationsSize() throws SystemException {
286                    return OrganizationLocalServiceUtil.searchCount(
287                            getCompanyId(), getOrganizationId(), null, null, null, null, null,
288                            null, null, null, true);
289            }
290    
291            public int getTypeOrder() {
292                    String[] types = PropsValues.ORGANIZATIONS_TYPES;
293    
294                    for (int i = 0; i < types.length; i++) {
295                            String type = types[i];
296    
297                            if (type.equals(getType())) {
298                                    return i + 1;
299                            }
300                    }
301    
302                    return 0;
303            }
304    
305            public boolean hasPrivateLayouts() {
306                    if (getPrivateLayoutsPageCount() > 0) {
307                            return true;
308                    }
309                    else {
310                            return false;
311                    }
312            }
313    
314            public boolean hasPublicLayouts() {
315                    if (getPublicLayoutsPageCount() > 0) {
316                            return true;
317                    }
318                    else {
319                            return false;
320                    }
321            }
322    
323            public boolean hasSuborganizations() throws SystemException {
324                    if (getSuborganizationsSize() > 0) {
325                            return true;
326                    }
327                    else {
328                            return false;
329                    }
330            }
331    
332            public boolean isParentable() {
333                    return isParentable(getType());
334            }
335    
336            public boolean isRoot() {
337                    if (getParentOrganizationId() ==
338                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
339    
340                            return true;
341                    }
342                    else {
343                            return false;
344                    }
345            }
346    
347            private static Log _log = LogFactoryUtil.getLog(Organization.class);
348    
349    }