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