001: /* Copyright 2001-2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.groups.local.searchers;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.util.ArrayList;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.jasig.portal.EntityIdentifier;
017: import org.jasig.portal.RDBMServices;
018: import org.jasig.portal.groups.GroupsException;
019: import org.jasig.portal.groups.local.ITypedEntitySearcher;
020:
021: /**
022: * Searches the portal DB for people. Used by EntitySearcherImpl
023: *
024: * @author Alex Vigdor
025: * @version $Revision: 36810 $
026: */
027:
028: public class RDBMPersonSearcher implements ITypedEntitySearcher {
029: private static final Log log = LogFactory
030: .getLog(RDBMPersonSearcher.class);
031: private static final String user_is_search = "select USER_NAME from UP_USER where UPPER(USER_NAME)=UPPER(?)";
032: private static final String user_partial_search = "select USER_NAME from UP_USER where UPPER(USER_NAME) like UPPER(?)";
033: private static final String person_partial_search = "select USER_NAME from UP_PERSON_DIR where (UPPER(FIRST_NAME) like UPPER(?) or UPPER(LAST_NAME) like UPPER(?))";
034: private static final String person_is_search = "select USER_NAME from UP_PERSON_DIR where (UPPER(FIRST_NAME) = UPPER(?) or UPPER(LAST_NAME) = UPPER(?))";
035:
036: private Class personDef;
037:
038: public RDBMPersonSearcher() {
039: personDef = org.jasig.portal.security.IPerson.class;
040: }
041:
042: public EntityIdentifier[] searchForEntities(String query, int method)
043: throws GroupsException {
044: //System.out.println("searching for channel");
045: EntityIdentifier[] r = new EntityIdentifier[0];
046: ArrayList ar = new ArrayList();
047: Connection conn = null;
048: PreparedStatement ps = null;
049: PreparedStatement ups = null;
050: PreparedStatement uis = null;
051: ResultSet rs = null;
052: ResultSet urs = null;
053: ResultSet uprs = null;
054:
055: try {
056: conn = RDBMServices.getConnection();
057: uis = conn
058: .prepareStatement(RDBMPersonSearcher.user_is_search);
059: switch (method) {
060: case IS:
061: ps = conn
062: .prepareStatement(RDBMPersonSearcher.person_is_search);
063: ups = uis;
064: break;
065: case STARTS_WITH:
066: query = query + "%";
067: ps = conn
068: .prepareStatement(RDBMPersonSearcher.person_partial_search);
069: ups = conn
070: .prepareStatement(RDBMPersonSearcher.user_partial_search);
071: break;
072: case ENDS_WITH:
073: query = "%" + query;
074: ps = conn
075: .prepareStatement(RDBMPersonSearcher.person_partial_search);
076: ups = conn
077: .prepareStatement(RDBMPersonSearcher.user_partial_search);
078: break;
079: case CONTAINS:
080: query = "%" + query + "%";
081: ps = conn
082: .prepareStatement(RDBMPersonSearcher.person_partial_search);
083: ups = conn
084: .prepareStatement(RDBMPersonSearcher.user_partial_search);
085: break;
086: default:
087: throw new GroupsException("Unknown search type");
088: }
089: ps.clearParameters();
090: ps.setString(1, query);
091: ps.setString(2, query);
092: rs = ps.executeQuery();
093: //System.out.println(ps.toString());
094: while (rs.next()) {
095: //System.out.println("result");
096: uis.clearParameters();
097: uis.setString(1, rs.getString(1));
098: urs = uis.executeQuery();
099: if (urs.next()) {
100: ar.add(new EntityIdentifier(urs.getString(1),
101: personDef));
102: }
103: }
104:
105: ups.clearParameters();
106: ups.setString(1, query);
107: uprs = ups.executeQuery();
108: while (uprs.next()) {
109: ar.add(new EntityIdentifier(uprs.getString(1),
110: personDef));
111: }
112: } catch (SQLException e) {
113: throw new GroupsException(
114: "RDBMChannelDefSearcher.searchForEntities(): " + ps,
115: e);
116: } finally {
117: if (rs != null)
118: RDBMServices.closeResultSet(rs);
119: if (urs != null)
120: RDBMServices.closeResultSet(urs);
121: if (uprs != null)
122: RDBMServices.closeResultSet(uprs);
123: if (ps != null)
124: RDBMServices.closeStatement(ps);
125: if (uis != null)
126: RDBMServices.closeStatement(uis);
127: if (ups != null)
128: RDBMServices.closeStatement(ups);
129: if (conn != null)
130: RDBMServices.releaseConnection(conn);
131: }
132: return (EntityIdentifier[]) ar.toArray(r);
133: }
134:
135: public Class getType() {
136: return personDef;
137: }
138: }
|