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.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.enterpriseadmin.util.UserIndexer;
027    import com.liferay.portlet.expando.model.ExpandoBridge;
028    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
029    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
030    import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
031    
032    import java.io.Serializable;
033    
034    import java.util.Enumeration;
035    import java.util.HashMap;
036    import java.util.LinkedHashMap;
037    import java.util.Map;
038    
039    import javax.portlet.PortletURL;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Marcellus Tavares
044     */
045    public class DirectoryOpenSearchImpl extends HitsOpenSearchImpl {
046    
047            public static final String SEARCH_PATH = "/c/directory/open_search";
048    
049            public static final String TITLE = "Liferay Directory Search: ";
050    
051            public String getPortletId() {
052                    return UserIndexer.PORTLET_ID;
053            }
054    
055            public String getSearchPath() {
056                    return SEARCH_PATH;
057            }
058    
059            public Summary getSummary(
060                    Indexer indexer, Document document, String snippet,
061                    PortletURL portletURL) {
062    
063                    Summary summary = super.getSummary(
064                            indexer, document, snippet, portletURL);
065    
066                    portletURL = summary.getPortletURL();
067    
068                    portletURL.setParameter("struts_action", "/directory/view_user");
069    
070                    return summary;
071            }
072    
073            public String getTitle(String keywords) {
074                    return TITLE + keywords;
075            }
076    
077            protected void addSearchAttributes(
078                    long companyId, SearchContext searchContext, String keywords) {
079    
080                    if (Validator.isNotNull(keywords)) {
081                            Map<String, Serializable> attributes =
082                                    new HashMap<String, Serializable>();
083    
084                            attributes.put("emailAddress", keywords);
085                            attributes.put("firstName", keywords);
086                            attributes.put("lastName", keywords);
087                            attributes.put("middleName", keywords);
088                            attributes.put("params", getUserParams(companyId, keywords));
089                            attributes.put("screenName", keywords);
090    
091                            searchContext.setAttributes(attributes);
092                    }
093            }
094    
095            protected LinkedHashMap<String, Object> getUserParams(
096                    long companyId, String keywords) {
097    
098                    LinkedHashMap<String, Object> userParams =
099                            new LinkedHashMap<String, Object>();
100    
101                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
102                            companyId, User.class.getName());
103    
104                    Enumeration<String> enu = expandoBridge.getAttributeNames();
105    
106                    while (enu.hasMoreElements()) {
107                            String attributeName = enu.nextElement();
108    
109                            UnicodeProperties properties = expandoBridge.getAttributeProperties(
110                                    attributeName);
111    
112                            String indexable = properties.getProperty(
113                                    ExpandoBridgeIndexer.INDEXABLE);
114    
115                            if (GetterUtil.getBoolean(indexable)) {
116                                    int type = expandoBridge.getAttributeType(attributeName);
117    
118                                    if (type == ExpandoColumnConstants.STRING) {
119                                            userParams.put(attributeName, keywords);
120                                    }
121                            }
122                    }
123    
124                    return userParams;
125            }
126    
127    }