001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.UniqueList;
032    import com.liferay.portal.model.Address;
033    import com.liferay.portal.model.Group;
034    import com.liferay.portal.model.LayoutSet;
035    import com.liferay.portal.model.Organization;
036    import com.liferay.portal.model.OrganizationConstants;
037    import com.liferay.portal.service.AddressLocalServiceUtil;
038    import com.liferay.portal.service.GroupLocalServiceUtil;
039    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
040    import com.liferay.portal.service.OrganizationLocalServiceUtil;
041    import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
042    import com.liferay.portal.util.PortletKeys;
043    import com.liferay.portal.util.PropsUtil;
044    import com.liferay.portal.util.PropsValues;
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            @Override
099            public String buildTreePath() throws PortalException, SystemException {
100                    StringBundler sb = new StringBundler();
101    
102                    buildTreePath(sb, this);
103    
104                    return sb.toString();
105            }
106    
107            @Override
108            public Address getAddress() {
109                    Address address = null;
110    
111                    try {
112                            List<Address> addresses = getAddresses();
113    
114                            if (addresses.size() > 0) {
115                                    address = addresses.get(0);
116                            }
117                    }
118                    catch (Exception e) {
119                            _log.error(e);
120                    }
121    
122                    if (address == null) {
123                            address = new AddressImpl();
124                    }
125    
126                    return address;
127            }
128    
129            @Override
130            public List<Address> getAddresses() throws SystemException {
131                    return AddressLocalServiceUtil.getAddresses(
132                            getCompanyId(), Organization.class.getName(), getOrganizationId());
133            }
134    
135            @Override
136            public List<Organization> getAncestors()
137                    throws PortalException, SystemException {
138    
139                    List<Organization> ancestors = new ArrayList<Organization>();
140    
141                    Organization organization = this;
142    
143                    while (!organization.isRoot()) {
144                            organization = organization.getParentOrganization();
145    
146                            ancestors.add(organization);
147                    }
148    
149                    return ancestors;
150            }
151    
152            @Override
153            public String[] getChildrenTypes() {
154                    return getChildrenTypes(getType());
155            }
156    
157            @Override
158            public List<Organization> getDescendants() throws SystemException {
159                    List<Organization> descendants = new UniqueList<Organization>();
160    
161                    for (Organization suborganization : getSuborganizations()) {
162                            descendants.add(suborganization);
163                            descendants.addAll(suborganization.getDescendants());
164                    }
165    
166                    return descendants;
167            }
168    
169            @Override
170            public Group getGroup() {
171                    if (getOrganizationId() > 0) {
172                            try {
173                                    return GroupLocalServiceUtil.getOrganizationGroup(
174                                            getCompanyId(), getOrganizationId());
175                            }
176                            catch (Exception e) {
177                                    _log.error(e);
178                            }
179                    }
180    
181                    return new GroupImpl();
182            }
183    
184            @Override
185            public long getGroupId() {
186                    Group group = getGroup();
187    
188                    return group.getGroupId();
189            }
190    
191            @Override
192            public long getLogoId() {
193                    long logoId = 0;
194    
195                    try {
196                            Group group = getGroup();
197    
198                            LayoutSet publicLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
199                                    group.getGroupId(), false);
200    
201                            logoId = publicLayoutSet.getLogoId();
202    
203                            if (logoId == 0) {
204                                    LayoutSet privateLayoutSet =
205                                            LayoutSetLocalServiceUtil.getLayoutSet(
206                                                    group.getGroupId(), true);
207    
208                                    logoId = privateLayoutSet.getLogoId();
209                            }
210                    }
211                    catch (Exception e) {
212                            _log.error(e);
213                    }
214    
215                    return logoId;
216            }
217    
218            @Override
219            public Organization getParentOrganization()
220                    throws PortalException, SystemException {
221    
222                    if (getParentOrganizationId() ==
223                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
224    
225                            return null;
226                    }
227    
228                    return OrganizationLocalServiceUtil.getOrganization(
229                            getParentOrganizationId());
230            }
231    
232            @Override
233            public PortletPreferences getPreferences() throws SystemException {
234                    long ownerId = getOrganizationId();
235                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
236    
237                    return PortalPreferencesLocalServiceUtil.getPreferences(
238                            ownerId, ownerType);
239            }
240    
241            @Override
242            public int getPrivateLayoutsPageCount() {
243                    try {
244                            Group group = getGroup();
245    
246                            if (group == null) {
247                                    return 0;
248                            }
249                            else {
250                                    return group.getPrivateLayoutsPageCount();
251                            }
252                    }
253                    catch (Exception e) {
254                            _log.error(e);
255                    }
256    
257                    return 0;
258            }
259    
260            @Override
261            public int getPublicLayoutsPageCount() {
262                    try {
263                            Group group = getGroup();
264    
265                            if (group == null) {
266                                    return 0;
267                            }
268                            else {
269                                    return group.getPublicLayoutsPageCount();
270                            }
271                    }
272                    catch (Exception e) {
273                            _log.error(e);
274                    }
275    
276                    return 0;
277            }
278    
279            @Override
280            public Set<String> getReminderQueryQuestions(Locale locale)
281                    throws SystemException {
282    
283                    return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
284            }
285    
286            @Override
287            public Set<String> getReminderQueryQuestions(String languageId)
288                    throws SystemException {
289    
290                    PortletPreferences preferences = getPreferences();
291    
292                    String[] questions = StringUtil.splitLines(
293                            LocalizationUtil.getPreferencesValue(
294                                    preferences, "reminderQueries", languageId, false));
295    
296                    return SetUtil.fromArray(questions);
297            }
298    
299            @Override
300            public List<Organization> getSuborganizations() throws SystemException {
301                    return OrganizationLocalServiceUtil.getSuborganizations(
302                            getCompanyId(), getOrganizationId());
303            }
304    
305            @Override
306            public int getSuborganizationsSize() throws SystemException {
307                    return OrganizationLocalServiceUtil.getSuborganizationsCount(
308                            getCompanyId(), getOrganizationId());
309            }
310    
311            @Override
312            public int getTypeOrder() {
313                    String[] types = PropsValues.ORGANIZATIONS_TYPES;
314    
315                    for (int i = 0; i < types.length; i++) {
316                            String type = types[i];
317    
318                            if (type.equals(getType())) {
319                                    return i + 1;
320                            }
321                    }
322    
323                    return 0;
324            }
325    
326            @Override
327            public boolean hasPrivateLayouts() {
328                    if (getPrivateLayoutsPageCount() > 0) {
329                            return true;
330                    }
331                    else {
332                            return false;
333                    }
334            }
335    
336            @Override
337            public boolean hasPublicLayouts() {
338                    if (getPublicLayoutsPageCount() > 0) {
339                            return true;
340                    }
341                    else {
342                            return false;
343                    }
344            }
345    
346            @Override
347            public boolean hasSuborganizations() throws SystemException {
348                    if (getSuborganizationsSize() > 0) {
349                            return true;
350                    }
351                    else {
352                            return false;
353                    }
354            }
355    
356            @Override
357            public boolean isParentable() {
358                    return isParentable(getType());
359            }
360    
361            @Override
362            public boolean isRoot() {
363                    if (getParentOrganizationId() ==
364                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
365    
366                            return true;
367                    }
368    
369                    return false;
370            }
371    
372            protected void buildTreePath(StringBundler sb, Organization organization)
373                    throws PortalException, SystemException {
374    
375                    if (organization == null) {
376                            sb.append(StringPool.SLASH);
377                    }
378                    else {
379                            buildTreePath(sb, organization.getParentOrganization());
380    
381                            sb.append(organization.getOrganizationId());
382                            sb.append(StringPool.SLASH);
383                    }
384            }
385    
386            private static Log _log = LogFactoryUtil.getLog(OrganizationImpl.class);
387    
388    }