FileDocCategorySizeDatePackage
SearchTag.javaAPI DocExample7971Thu Dec 15 21:40:40 GMT 2005com.oreilly.jent.people.tags

SearchTag

public class SearchTag extends javax.servlet.jsp.tagext.BodyTagSupport
SearchTag: A custom tag that performs a search against a person database, given one or more search fields. The results of the search consist of a Collection of Person beans, used to populate a page variable with page scope, whose variable name is set on the tag using the "varName" attribute. The tag supports four attributes in total: - varName : Name to be assigned to the page variable holding the results of the search. If any error is generated while processing the search, an error message will be place into a page variable named "-error". - firstNamePattern : Pattern to search for in the first name field. - lastNamePattern : Pattern to search for in the last name field. - emailPattern : Pattern to search for in the email field. All search patterns are "AND"ed together in the search. Each search field is optional, but a search must include at least one search pattern. Search patterns can include the '*' character as a wildcard. If no wildcards are included in the pattern, then a "contains" search is used for that field.

Fields Summary
private Logger
mLog
private String
mFirstNamePattern
private String
mLastNamePattern
private String
mEmailPattern
private String
mVarName
Constructors Summary
public SearchTag()


      
    
Methods Summary
public intdoStartTag()
Take the provided search parameters, translate them into a search against the person database. Populate a page variable with a collection of the people found, represented as Person beans.

        mLog.info("doStartTag invoked");
        // List of people found
        Person[] people = new Person[0];
        ArrayList pList = new ArrayList();
        String error = null;
        
        // Convert the tag's attributes into a set of SearchArgs
        SearchArg[] sargs = new SearchArg[3];
        sargs[0] = new SearchArg();
        sargs[0].setName(PersonDAO.FIRST_NAME);
        sargs[0].setValue(this.getFirstNamePattern());
        sargs[1] = new SearchArg();
        sargs[1].setName(PersonDAO.LAST_NAME);
        sargs[1].setValue(this.getLastNamePattern());
        sargs[2] = new SearchArg();
        sargs[2].setName(PersonDAO.EMAIL);
        sargs[2].setValue(this.getEmailPattern());
        
        // Get the PeopleFinder EJB home
        try {
            Context ctx = new InitialContext();
            Object ref = ctx.lookup("ejb/ws-PeopleFinderLocal");
            PeopleFinderLocalHome home = 
                (PeopleFinderLocalHome)ref;//PortableRemoteObject.narrow(ref, PeopleFinderLocalHome.class);
            PeopleFinderLocal finder = home.create();
            people = finder.findPeople(sargs);
            // Add the people to our list
            for (int i = 0; i < people.length; i++) {
                pList.add(people[i]);
            }
        }
        catch (NamingException ne) {
            error = "Failed to locate/access EJB component: " + ne.getMessage();
            mLog.severe(error);
        }
        catch (CreateException ce) {
            error = "Failed to create EJB component: " + ce.getMessage();
            mLog.severe(error);
        }
        catch (InvalidSearchException ise) {
            error = "Search parameters were invalid: " + ise.getMessage();
            mLog.severe(error);
        }
        catch (PersistenceException pe) {
            error = "Persistence error during search: " + pe.getMessage();
            mLog.severe(error);
        }
        
        // Set the page variable (if the search failed, it will be empty)
        this.pageContext.setAttribute(getVarName(), pList, 
                                      PageContext.SESSION_SCOPE);
        // Set the error page variable if there is an error.
        if (error != null) {
            this.pageContext.setAttribute(getVarName() + "-error", error,
                                          PageContext.REQUEST_SCOPE);
        }
        return Tag.SKIP_BODY;
    
public java.lang.StringgetEmailPattern()
Get the search pattern for email

        return mEmailPattern;
    
public java.lang.StringgetFirstNamePattern()
Get the search pattern for first name

        return mFirstNamePattern;
    
public java.lang.StringgetLastNamePattern()
Get the search pattern for last name

        return mLastNamePattern;
    
public java.lang.StringgetVarName()
Get the name of the page variable for the results

        return mVarName;
    
public booleanhasEmailPattern()
Predicate, to detemine whether an email pattern has been set

        return (this.getEmailPattern() != null && 
                this.getEmailPattern().length() > 0);
    
public booleanhasFirstNamePattern()
Predicate, to detemine whether a first name pattern has been set

        return (this.getFirstNamePattern() != null && 
                this.getFirstNamePattern().length() > 0);
    
public booleanhasLastNamePattern()
Predicate, to detemine whether a last name pattern has been set

        return (this.getLastNamePattern() != null &&
                this.getLastNamePattern().length() > 0);
    
public voidsetEmailPattern(java.lang.String mEmailPattern)
Set the search pattern for email

        this.mEmailPattern = mEmailPattern;
    
public voidsetFirstNamePattern(java.lang.String mFirstNamePattern)
Set the search pattern for first name

        this.mFirstNamePattern = mFirstNamePattern;
    
public voidsetLastNamePattern(java.lang.String mLastNamePattern)
Set the search pattern for last name

        this.mLastNamePattern = mLastNamePattern;
    
public voidsetVarName(java.lang.String mVarName)
Set the name of the page variable for the results

        this.mVarName = mVarName;