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 (true) {
144                            if (!organization.isRoot()) {
145                                    organization = organization.getParentOrganization();
146    
147                                    ancestors.add(organization);
148                            }
149                            else {
150                                    break;
151                            }
152                    }
153    
154                    return ancestors;
155            }
156    
157            @Override
158            public String[] getChildrenTypes() {
159                    return getChildrenTypes(getType());
160            }
161    
162            @Override
163            public List<Organization> getDescendants() throws SystemException {
164                    List<Organization> descendants = new UniqueList<Organization>();
165    
166                    for (Organization suborganization : getSuborganizations()) {
167                            descendants.add(suborganization);
168                            descendants.addAll(suborganization.getDescendants());
169                    }
170    
171                    return descendants;
172            }
173    
174            @Override
175            public Group getGroup() {
176                    if (getOrganizationId() > 0) {
177                            try {
178                                    return GroupLocalServiceUtil.getOrganizationGroup(
179                                            getCompanyId(), getOrganizationId());
180                            }
181                            catch (Exception e) {
182                                    _log.error(e);
183                            }
184                    }
185    
186                    return new GroupImpl();
187            }
188    
189            @Override
190            public long getGroupId() {
191                    Group group = getGroup();
192    
193                    return group.getGroupId();
194            }
195    
196            @Override
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            @Override
224            public Organization getParentOrganization()
225                    throws PortalException, SystemException {
226    
227                    if (getParentOrganizationId() ==
228                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
229    
230                            return null;
231                    }
232    
233                    return OrganizationLocalServiceUtil.getOrganization(
234                            getParentOrganizationId());
235            }
236    
237            @Override
238            public PortletPreferences getPreferences() throws SystemException {
239                    long companyId = getCompanyId();
240                    long ownerId = getOrganizationId();
241                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ORGANIZATION;
242    
243                    return PortalPreferencesLocalServiceUtil.getPreferences(
244                            companyId, ownerId, ownerType);
245            }
246    
247            @Override
248            public int getPrivateLayoutsPageCount() {
249                    try {
250                            Group group = getGroup();
251    
252                            if (group == null) {
253                                    return 0;
254                            }
255                            else {
256                                    return group.getPrivateLayoutsPageCount();
257                            }
258                    }
259                    catch (Exception e) {
260                            _log.error(e);
261                    }
262    
263                    return 0;
264            }
265    
266            @Override
267            public int getPublicLayoutsPageCount() {
268                    try {
269                            Group group = getGroup();
270    
271                            if (group == null) {
272                                    return 0;
273                            }
274                            else {
275                                    return group.getPublicLayoutsPageCount();
276                            }
277                    }
278                    catch (Exception e) {
279                            _log.error(e);
280                    }
281    
282                    return 0;
283            }
284    
285            @Override
286            public Set<String> getReminderQueryQuestions(Locale locale)
287                    throws SystemException {
288    
289                    return getReminderQueryQuestions(LanguageUtil.getLanguageId(locale));
290            }
291    
292            @Override
293            public Set<String> getReminderQueryQuestions(String languageId)
294                    throws SystemException {
295    
296                    PortletPreferences preferences = getPreferences();
297    
298                    String[] questions = StringUtil.splitLines(
299                            LocalizationUtil.getPreferencesValue(
300                                    preferences, "reminderQueries", languageId, false));
301    
302                    return SetUtil.fromArray(questions);
303            }
304    
305            @Override
306            public List<Organization> getSuborganizations() throws SystemException {
307                    return OrganizationLocalServiceUtil.getSuborganizations(
308                            getCompanyId(), getOrganizationId());
309            }
310    
311            @Override
312            public int getSuborganizationsSize() throws SystemException {
313                    return OrganizationLocalServiceUtil.getSuborganizationsCount(
314                            getCompanyId(), getOrganizationId());
315            }
316    
317            @Override
318            public int getTypeOrder() {
319                    String[] types = PropsValues.ORGANIZATIONS_TYPES;
320    
321                    for (int i = 0; i < types.length; i++) {
322                            String type = types[i];
323    
324                            if (type.equals(getType())) {
325                                    return i + 1;
326                            }
327                    }
328    
329                    return 0;
330            }
331    
332            @Override
333            public boolean hasPrivateLayouts() {
334                    if (getPrivateLayoutsPageCount() > 0) {
335                            return true;
336                    }
337                    else {
338                            return false;
339                    }
340            }
341    
342            @Override
343            public boolean hasPublicLayouts() {
344                    if (getPublicLayoutsPageCount() > 0) {
345                            return true;
346                    }
347                    else {
348                            return false;
349                    }
350            }
351    
352            @Override
353            public boolean hasSuborganizations() throws SystemException {
354                    if (getSuborganizationsSize() > 0) {
355                            return true;
356                    }
357                    else {
358                            return false;
359                    }
360            }
361    
362            @Override
363            public boolean isParentable() {
364                    return isParentable(getType());
365            }
366    
367            @Override
368            public boolean isRoot() {
369                    if (getParentOrganizationId() ==
370                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
371    
372                            return true;
373                    }
374                    else {
375                            return false;
376                    }
377            }
378    
379            protected void buildTreePath(StringBundler sb, Organization organization)
380                    throws PortalException, SystemException {
381    
382                    if (organization == null) {
383                            sb.append(StringPool.SLASH);
384                    }
385                    else {
386                            buildTreePath(sb, organization.getParentOrganization());
387    
388                            sb.append(organization.getOrganizationId());
389                            sb.append(StringPool.SLASH);
390                    }
391            }
392    
393            private static Log _log = LogFactoryUtil.getLog(OrganizationImpl.class);
394    
395    }