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