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.portlet.passwordpoliciesadmin.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.passwordpoliciesadmin.util.PasswordPoliciesAdminUtil;
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            public static final String EMPTY_RESULTS_MESSAGE =
043                    "no-password-policies-were-found";
044    
045            public static List<String> headerNames = new ArrayList<String>();
046            public static Map<String, String> orderableHeaders =
047                    new HashMap<String, String>();
048    
049            static {
050                    headerNames.add("name");
051                    headerNames.add("description");
052    
053                    orderableHeaders.put("name", "name");
054                    orderableHeaders.put("description", "description");
055            }
056    
057            public PasswordPolicySearch(
058                    PortletRequest portletRequest, PortletURL iteratorURL) {
059    
060                    super(
061                            portletRequest, new PasswordPolicyDisplayTerms(portletRequest),
062                            new PasswordPolicyDisplayTerms(portletRequest), DEFAULT_CUR_PARAM,
063                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
064    
065                    PasswordPolicyDisplayTerms displayTerms =
066                            (PasswordPolicyDisplayTerms)getDisplayTerms();
067    
068                    iteratorURL.setParameter(
069                            PasswordPolicyDisplayTerms.NAME, displayTerms.getName());
070    
071                    try {
072                            PortalPreferences preferences =
073                                    PortletPreferencesFactoryUtil.getPortalPreferences(
074                                            portletRequest);
075    
076                            String orderByCol = ParamUtil.getString(
077                                    portletRequest, "orderByCol");
078                            String orderByType = ParamUtil.getString(
079                                    portletRequest, "orderByType");
080    
081                            if (Validator.isNotNull(orderByCol) &&
082                                    Validator.isNotNull(orderByType)) {
083    
084                                    preferences.setValue(
085                                            PortletKeys.PASSWORD_POLICIES_ADMIN,
086                                            "password-policies-order-by-col", orderByCol);
087                                    preferences.setValue(
088                                            PortletKeys.PASSWORD_POLICIES_ADMIN,
089                                            "password-policies-order-by-type", orderByType);
090                            }
091                            else {
092                                    orderByCol = preferences.getValue(
093                                            PortletKeys.PASSWORD_POLICIES_ADMIN,
094                                            "password-policies-order-by-col", "name");
095                                    orderByType = preferences.getValue(
096                                            PortletKeys.PASSWORD_POLICIES_ADMIN,
097                                            "password-policies-order-by-type", "asc");
098                            }
099    
100                            OrderByComparator<PasswordPolicy> orderByComparator =
101                                    PasswordPoliciesAdminUtil.getPasswordPolicyOrderByComparator(
102                                            orderByCol, orderByType);
103    
104                            setOrderableHeaders(orderableHeaders);
105                            setOrderByCol(orderByCol);
106                            setOrderByType(orderByType);
107                            setOrderByComparator(orderByComparator);
108                    }
109                    catch (Exception e) {
110                            _log.error(e);
111                    }
112            }
113    
114            private static final Log _log = LogFactoryUtil.getLog(
115                    PasswordPolicySearch.class);
116    
117    }