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