FileDocCategorySizeDatePackage
XMLPersonDAO.javaAPI DocExample7845Thu Dec 15 22:15:00 GMT 2005com.oreilly.jent.people.xml

XMLPersonDAO

public class XMLPersonDAO extends com.oreilly.jent.people.PersonDAO
Class: XMLPersonDAO An implementation of a PersonDAO that reads all of its person data out of an XML file, located using the "xml-file" element in the parameters section of the config file.
author
Jim Farley

Fields Summary
private static Logger
sLog
private Collection
mPeople
Constructors Summary
Methods Summary
public java.util.CollectionfindPeople(java.util.Map searchParams)
Search the people loaded from the XML file.

        // Extract the search parameters from the map
        String emailArg = (String)searchParams.get(PersonDAO.EMAIL);
        String fnameArg = (String)searchParams.get(PersonDAO.FIRST_NAME);
        String lnameArg = (String)searchParams.get(PersonDAO.LAST_NAME);
        
        // Check for no search arguments, which is an invalid search
        if ((emailArg == null || emailArg.length() == 0) &&
            (fnameArg == null || fnameArg.length() == 0) &&
            (lnameArg == null || lnameArg.length() == 0)) {
            throw new InvalidSearchException("No arguments provided");
        }
        // Check each person for a match
        ArrayList people = new ArrayList();
        Iterator pIter = mPeople.iterator();
        while (pIter.hasNext()) {
            Person p = (Person)pIter.next();
            boolean match = false;
            // First check the names
            if ((fnameArg != null && fnameArg.length() > 0 &&
                 p.getFirstName() != null &&
                 p.getFirstName().indexOf(fnameArg) >= 0) ||
                (lnameArg != null && lnameArg.length() > 0 &&
                 p.getLastName() != null &&
                 p.getLastName().indexOf(lnameArg) >= 0)) { 
                match = true;
                people.add(p);
            }
            // If names didn't match, check email addresses
            if (!match && emailArg != null && emailArg.length() > 0) {
                String[] addrs = p.getEmailAddresses();
                for (int i = 0; i < addrs.length && !match; i++) {
                    if (addrs[i].indexOf(emailArg) >= 0) {
                        match = true;
                        people.add(p);
                    }
                }
            }
        }
        return people;
    
public com.oreilly.jent.people.PersonfindPerson(java.lang.String id)

        // TODO Auto-generated method stub
        return null;
    
public com.oreilly.jent.people.PersonfindPersonByAcct(java.lang.String acctID)

        // TODO Auto-generated method stub
        return null;
    
public voidinit(com.oreilly.jent.people.util.ConfigSet config)
Initialize the DAO from the config info. This DAO implementation requires the JNDI name of a DataSource that points to the person database. This name has to be given as the parameter "ds-name" in the "parameters" element of the config file.


                                                    
        
        try {
            // Try to parse the XML file referenced by the xml-file element 
            String xmlFileName = config.getParameter("dao.parameters.xml-file");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            File xmlFile = new File(xmlFileName);
            Document personData = builder.parse(xmlFile);
            mPeople = parseData(personData);
        }
        catch (ParserConfigurationException pce) {
            sLog.severe("Failed to parse XML file: " + pce.getMessage());
        }
        catch (SAXException se) {
            sLog.severe("Failed to parse XML file: " + se.getMessage());
        }
        catch (IOException ie) {
            sLog.severe("Failed to load XML file: " + ie.getMessage());
            ie.printStackTrace();
        }
    
private java.util.CollectionparseData(org.w3c.dom.Document data)

        ArrayList people = new ArrayList();
        NodeList pNodes = data.getElementsByTagName("person");
        for (int i = 0; i < pNodes.getLength(); i++) {
            try {
                // Wrap each person's node in the XML doc with a ConfigSet
                Node pn = pNodes.item(i);
                Document d = 
                    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
                Node pnc = d.importNode(pn, true);
                d.appendChild(pnc);
                
                ConfigSet ps = new ConfigSet(d);
                // Make a person from the node data
                Person p = new Person();
                p.setFirstName(ps.getParameter("person.first-name"));
                p.setLastName(ps.getParameter("person.last-name"));
                p.addEmailAddress(ps.getParameter("person.email-address"));
                people.add(p);
            }
            catch (DOMException de) {
               sLog.severe("XML issue while parsing people data file: " +
                           de.getMessage());
            }
            catch (ParserConfigurationException pce) {
                sLog.severe("XML issue while parsing people data file: " +
                            pce.getMessage());
            }
            catch (FactoryConfigurationError fce) {
                sLog.severe("Error creating document builder: " +
                            fce.getMessage());
            }
        }
        return people;