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 companyId = getCompanyId();
235                    long ownerId = getOrganizationId();
236                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
237    
238                    return PortalPreferencesLocalServiceUtil.getPreferences(
239                            companyId, ownerId, ownerType);
240            }
241    
242            @Override
243            public int getPrivateLayoutsPageCount() {
244                    try {
245                            Group group = getGroup();
246    
247                            if (group == null) {
248                                    return 0;
249                            }
250                            else {
251                                    return group.getPrivateLayoutsPageCount();
252                            }
253                    }
254                    catch (Exception e) {
255                            _log.error(e);
256                    }
257    
258                    return 0;
259            }
260    
261            @Override
262            public int getPublicLayoutsPageCount() {
263                    try {
264                            Group group = getGroup();
265    
266                            if (group == null) {
267                                    return 0;
268                            }
269                            else {
270                                    return group.getPublicLayoutsPageCount();
271                            }
272                    }
273                    catch (Exception e) {
274                            _log.error(e);
275                    }
276    
277                    return 0;
278            }
279    
280            @Override
281            public Set<String> getReminderQueryQuestions(Locale locale)
282                    throws SystemException {
283    
284                    return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
285            }
286    
287            @Override
288            public Set<String> getReminderQueryQuestions(String languageId)
289                    throws SystemException {
290    
291                    PortletPreferences preferences = getPreferences();
292    
293                    String[] questions = StringUtil.splitLines(
294                            LocalizationUtil.getPreferencesValue(
295                                    preferences, "reminderQueries", languageId, false));
296    
297                    return SetUtil.fromArray(questions);
298            }
299    
300            @Override
301            public List<Organization> getSuborganizations() throws SystemException {
302                    return OrganizationLocalServiceUtil.getSuborganizations(
303                            getCompanyId(), getOrganizationId());
304            }
305    
306            @Override
307            public int getSuborganizationsSize() throws SystemException {
308                    return OrganizationLocalServiceUtil.getSuborganizationsCount(
309                            getCompanyId(), getOrganizationId());
310            }
311    
312            @Override
313            public int getTypeOrder() {
314                    String[] types = PropsValues.ORGANIZATIONS_TYPES;
315    
316                    for (int i = 0; i < types.length; i++) {
317                            String type = types[i];
318    
319                            if (type.equals(getType())) {
320                                    return i + 1;
321                            }
322                    }
323    
324                    return 0;
325            }
326    
327            @Override
328            public boolean hasPrivateLayouts() {
329                    if (getPrivateLayoutsPageCount() > 0) {
330                            return true;
331                    }
332                    else {
333                            return false;
334                    }
335            }
336    
337            @Override
338            public boolean hasPublicLayouts() {
339                    if (getPublicLayoutsPageCount() > 0) {
340                            return true;
341                    }
342                    else {
343                            return false;
344                    }
345            }
346    
347            @Override
348            public boolean hasSuborganizations() throws SystemException {
349                    if (getSuborganizationsSize() > 0) {
350                            return true;
351                    }
352                    else {
353                            return false;
354                    }
355            }
356    
357            @Override
358            public boolean isParentable() {
359                    return isParentable(getType());
360            }
361    
362            @Override
363            public boolean isRoot() {
364                    if (getParentOrganizationId() ==
365                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
366    
367                            return true;
368                    }
369    
370                    return false;
371            }
372    
373            protected void buildTreePath(StringBundler sb, Organization organization)
374                    throws PortalException, SystemException {
375    
376                    if (organization == null) {
377                            sb.append(StringPool.SLASH);
378                    }
379                    else {
380                            buildTreePath(sb, organization.getParentOrganization());
381    
382                            sb.append(organization.getOrganizationId());
383                            sb.append(StringPool.SLASH);
384                    }
385            }
386    
387            private static Log _log = LogFactoryUtil.getLog(OrganizationImpl.class);
388    
389    }