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