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.portlet.usersadmin.search;
016    
017    import com.liferay.portal.NoSuchCountryException;
018    import com.liferay.portal.NoSuchRegionException;
019    import com.liferay.portal.kernel.dao.search.DAOParamUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Country;
027    import com.liferay.portal.model.Region;
028    import com.liferay.portal.service.CountryServiceUtil;
029    import com.liferay.portal.service.RegionServiceUtil;
030    
031    import javax.portlet.PortletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Hugo Huijser
036     */
037    public class OrganizationSearchTerms extends OrganizationDisplayTerms {
038    
039            public OrganizationSearchTerms(PortletRequest portletRequest) {
040                    super(portletRequest);
041    
042                    city = DAOParamUtil.getString(portletRequest, CITY);
043                    countryId = ParamUtil.getLong(portletRequest, COUNTRY_ID);
044                    name = DAOParamUtil.getString(portletRequest, NAME);
045                    parentOrganizationId = ParamUtil.getLong(
046                            portletRequest, PARENT_ORGANIZATION_ID);
047                    regionId = ParamUtil.getLong(portletRequest, REGION_ID);
048                    street = DAOParamUtil.getString(portletRequest, STREET);
049                    type = DAOParamUtil.getString(portletRequest, TYPE);
050                    zip = DAOParamUtil.getString(portletRequest, ZIP);
051            }
052    
053            public Long getCountryIdObj() {
054                    if (countryId == 0) {
055                            return null;
056                    }
057                    else {
058                            return new Long(countryId);
059                    }
060            }
061    
062            public String getCountryName() throws PortalException, SystemException {
063                    String countryName = null;
064    
065                    if (countryId != 0) {
066                            try {
067                                    Country country = CountryServiceUtil.getCountry(countryId);
068    
069                                    countryName = country.getName().toLowerCase();
070                            }
071                            catch (NoSuchCountryException nsce) {
072                                    if (_log.isWarnEnabled()) {
073                                            _log.warn(nsce.getMessage());
074                                    }
075                            }
076                    }
077    
078                    return countryName;
079            }
080    
081            public Long getRegionIdObj() {
082                    if (regionId == 0) {
083                            return null;
084                    }
085                    else {
086                            return new Long(regionId);
087                    }
088            }
089    
090            public String getRegionName() throws PortalException, SystemException {
091                    String regionName = null;
092    
093                    if (regionId != 0) {
094                            try {
095                                    Region region = RegionServiceUtil.getRegion(regionId);
096    
097                                    regionName = region.getName().toLowerCase();
098                            }
099                            catch (NoSuchRegionException nsre) {
100                                    if (_log.isWarnEnabled()) {
101                                            _log.warn(nsre.getMessage());
102                                    }
103                            }
104                    }
105    
106                    return regionName;
107            }
108    
109            public boolean hasSearchTerms() {
110                    if (isAdvancedSearch()) {
111                            if (Validator.isNotNull(city) || (countryId > 0) ||
112                                    Validator.isNotNull(name) || (regionId > 0) ||
113                                    Validator.isNotNull(street) || Validator.isNotNull(type) ||
114                                    Validator.isNotNull(zip)) {
115    
116                                    return true;
117                            }
118                    }
119                    else {
120                            if (Validator.isNotNull(keywords)) {
121                                    return true;
122                            }
123                    }
124    
125                    return false;
126            }
127    
128            private static Log _log = LogFactoryUtil.getLog(
129                    OrganizationSearchTerms.class);
130    
131    }