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