001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.directory.util;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.HitsOpenSearchImpl;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.SearchContext;
021    import com.liferay.portal.kernel.search.Summary;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.User;
026    import com.liferay.portlet.expando.model.ExpandoBridge;
027    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
028    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
029    import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
030    
031    import java.io.Serializable;
032    
033    import java.util.Enumeration;
034    import java.util.HashMap;
035    import java.util.LinkedHashMap;
036    import java.util.Map;
037    
038    import javax.portlet.PortletURL;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Marcellus Tavares
043     */
044    public class DirectoryOpenSearchImpl extends HitsOpenSearchImpl {
045    
046            public static final String SEARCH_PATH = "/c/directory/open_search";
047    
048            public static final String TITLE = "Liferay Directory Search: ";
049    
050            public String getPortletId() {
051                    return DirectoryIndexer.PORTLET_ID;
052            }
053    
054            public String getSearchPath() {
055                    return SEARCH_PATH;
056            }
057    
058            public Summary getSummary(
059                    Indexer indexer, Document document, String snippet,
060                    PortletURL portletURL) {
061    
062                    Summary summary = super.getSummary(
063                            indexer, document, snippet, portletURL);
064    
065                    portletURL = summary.getPortletURL();
066    
067                    portletURL.setParameter("struts_action", "/directory/view_user");
068    
069                    return summary;
070            }
071    
072            public String getTitle(String keywords) {
073                    return TITLE + keywords;
074            }
075    
076            protected void addSearchAttributes(
077                    long companyId, SearchContext searchContext, String keywords) {
078    
079                    if (Validator.isNotNull(keywords)) {
080                            Map<String, Serializable> attributes =
081                                    new HashMap<String, Serializable>();
082    
083                            attributes.put("emailAddress", keywords);
084                            attributes.put("firstName", keywords);
085                            attributes.put("lastName", keywords);
086                            attributes.put("middleName", keywords);
087                            attributes.put("params", getUserParams(companyId, keywords));
088                            attributes.put("screenName", keywords);
089    
090                            searchContext.setAttributes(attributes);
091                    }
092            }
093    
094            protected LinkedHashMap<String, Object> getUserParams(
095                    long companyId, String keywords) {
096    
097                    LinkedHashMap<String, Object> userParams =
098                            new LinkedHashMap<String, Object>();
099    
100                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
101                            companyId, User.class.getName());
102    
103                    Enumeration<String> enu = expandoBridge.getAttributeNames();
104    
105                    while (enu.hasMoreElements()) {
106                            String attributeName = enu.nextElement();
107    
108                            UnicodeProperties properties = expandoBridge.getAttributeProperties(
109                                    attributeName);
110    
111                            String indexable = properties.getProperty(
112                                    ExpandoBridgeIndexer.INDEXABLE);
113    
114                            if (GetterUtil.getBoolean(indexable)) {
115                                    int type = expandoBridge.getAttributeType(attributeName);
116    
117                                    if (type == ExpandoColumnConstants.STRING) {
118                                            userParams.put(attributeName, keywords);
119                                    }
120                            }
121                    }
122    
123                    return userParams;
124            }
125    
126    }