001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.search.identity;
023:
024: import org.jboss.portal.common.i18n.LocalizedString;
025: import org.jboss.portal.identity.IdentityException;
026: import org.jboss.portal.identity.NoSuchUserException;
027: import org.jboss.portal.identity.User;
028: import org.jboss.portal.identity.UserModule;
029: import org.jboss.portal.search.Query;
030: import org.jboss.portal.search.QueryConverter;
031: import org.jboss.portal.search.SearchingException;
032: import org.jboss.portal.search.impl.AbstractFederatedSearcher;
033: import org.jboss.portal.search.impl.SimpleQueryConverter;
034: import org.jboss.portal.search.query.BooleanClause;
035: import org.jboss.portal.search.query.BooleanQuery;
036: import org.jboss.portal.search.query.Term;
037: import org.jboss.portal.search.query.TermQuery;
038: import org.jboss.portal.search.result.ResultSet;
039: import org.jboss.portal.search.result.ResultURL;
040: import org.jboss.portal.search.result.impl.PortletResultURL;
041: import org.jboss.portal.search.result.impl.SimpleResult;
042:
043: import javax.naming.InitialContext;
044: import javax.naming.NamingException;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.Map;
048:
049: /**
050: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
051: * @version $Revision: 8784 $
052: */
053: public class UserSearcher extends AbstractFederatedSearcher {
054:
055: private UserModule userModule;
056:
057: public UserSearcher() {
058: setId("user_searcher");
059: setDisplayName(new LocalizedString("User Searcher"));
060: try {
061: userModule = (UserModule) new InitialContext()
062: .lookup("java:/portal/UserModule");
063: } catch (NamingException e) {
064: // FIXME
065: e.printStackTrace();
066: }
067: }
068:
069: public ResultSet search(Query query) throws SearchingException {
070: ResultSet resultSet = new ResultSet(this );
071:
072: /*
073: StandardQueryConverter queryConverter = new StandardQueryConverter();
074:
075: try
076: {
077: search(queryConverter.convert(query), resultSet);
078: }
079: catch (QueryConversionException e)
080: {
081: throw new SearchingException("An error occured while searching", e);
082: }
083: */
084: search(query, resultSet);
085:
086: return resultSet;
087: }
088:
089: private void search(Query query, ResultSet resultSet) {
090: if (query instanceof TermQuery) {
091: searchTerm(((TermQuery) query).getTerm(), resultSet);
092: } else if (query instanceof BooleanQuery) {
093: BooleanQuery booleanQuery = (BooleanQuery) query;
094: Iterator it = booleanQuery.getClauses().iterator();
095: while (it.hasNext()) {
096: BooleanClause clause = (BooleanClause) it.next();
097: if (!clause.isProhibited()) {
098: search(booleanQuery, resultSet);
099: }
100: }
101: }
102: }
103:
104: private void searchTerm(Term term, ResultSet resultSet) {
105: try {
106: User user = userModule.findUserByUserName(term.getText());
107: SimpleResult result = new SimpleResult(getId(), user);
108: result.setTitle(new LocalizedString(user.getUserName()));
109: result.setType(new LocalizedString("User"));
110: Map renderParameters = new HashMap();
111: renderParameters.put("userid", user.getId().toString());
112: renderParameters.put("op", "viewProfile");
113: ResultURL url = new PortletResultURL(
114: "default.default.UserPortletWindow",
115: renderParameters);
116: result.setURL(url);
117: resultSet.add(result);
118: } catch (IllegalArgumentException e) {
119: // FIXME
120: e.printStackTrace();
121: } catch (NoSuchUserException e) {
122: // Ignore
123: } catch (IdentityException e) {
124: // FIXME
125: e.printStackTrace();
126: }
127: }
128:
129: public QueryConverter getQueryConverter() {
130: return new SimpleQueryConverter();
131: }
132:
133: }
|