001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.search;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.PasswordPolicy;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortalPreferences;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    
037    /**
038     * @author Scott Lee
039     */
040    public class PasswordPolicySearch extends SearchContainer<PasswordPolicy> {
041    
042            static List<String> headerNames = new ArrayList<String>();
043            static Map<String, String> orderableHeaders = new HashMap<String, String>();
044    
045            static {
046                    headerNames.add("name");
047                    headerNames.add("description");
048    
049                    orderableHeaders.put("name", "name");
050                    orderableHeaders.put("description", "description");
051            }
052    
053            public static final String EMPTY_RESULTS_MESSAGE =
054                    "no-password-policies-were-found";
055    
056            public PasswordPolicySearch(
057                    PortletRequest portletRequest, PortletURL iteratorURL) {
058    
059                    super(
060                            portletRequest, new PasswordPolicyDisplayTerms(portletRequest),
061                            new PasswordPolicySearchTerms(portletRequest), DEFAULT_CUR_PARAM,
062                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
063    
064                    PasswordPolicyDisplayTerms displayTerms =
065                            (PasswordPolicyDisplayTerms)getDisplayTerms();
066    
067                    iteratorURL.setParameter(
068                            PasswordPolicyDisplayTerms.NAME, displayTerms.getName());
069    
070                    try {
071                            PortalPreferences preferences =
072                                    PortletPreferencesFactoryUtil.getPortalPreferences(
073                                            portletRequest);
074    
075                            String orderByCol = ParamUtil.getString(
076                                    portletRequest, "orderByCol");
077                            String orderByType = ParamUtil.getString(
078                                    portletRequest, "orderByType");
079    
080                            if (Validator.isNotNull(orderByCol) &&
081                                    Validator.isNotNull(orderByType)) {
082    
083                                    preferences.setValue(
084                                            PortletKeys.ENTERPRISE_ADMIN,
085                                            "password-policies-order-by-col", orderByCol);
086                                    preferences.setValue(
087                                            PortletKeys.ENTERPRISE_ADMIN,
088                                            "password-policies-order-by-type", orderByType);
089                            }
090                            else {
091                                    orderByCol = preferences.getValue(
092                                            PortletKeys.ENTERPRISE_ADMIN,
093                                            "password-policies-order-by-col", "name");
094                                    orderByType = preferences.getValue(
095                                            PortletKeys.ENTERPRISE_ADMIN,
096                                            "password-policies-order-by-type", "asc");
097                            }
098    
099                            OrderByComparator orderByComparator =
100                                    EnterpriseAdminUtil.getPasswordPolicyOrderByComparator(
101                                            orderByCol, orderByType);
102    
103                            setOrderableHeaders(orderableHeaders);
104                            setOrderByCol(orderByCol);
105                            setOrderByType(orderByType);
106                            setOrderByComparator(orderByComparator);
107                    }
108                    catch (Exception e) {
109                            _log.error(e);
110                    }
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(PasswordPolicySearch.class);
114    
115    }