%@page import="java.util.*, org.w3c.dom.Node, org.w3c.dom.Element, org.w3c.dom.Document, org.w3c.dom.NamedNodeMap, org.xml.sax.*, org.w3c.dom.NodeList, javax.xml.parsers.DocumentBuilder, javax.xml.parsers.DocumentBuilderFactory" %>
<% String findFirstname = request.getParameter("firstName").trim();
String findLastname = request.getParameter("lastName").trim();
boolean lastNameExists = false;
boolean firstNameExists = false;
Element contact;
int nodeType, count;
String nodeName="";
String primaryInfo = "";
String name = "";
String contactFirstName="";
String contactLastName="";
String contactMiddleName="";
String first = "";
String middle = "";
String last = "";
String nickname = "";
String email = "";
String workEmail = "";
String homeEmail = "";
String alternativeEmail = "";
String addresses = "";
String home = "";
String homeAddress = "";
String homeStreet = "";
String homeApt = "";
String homeCity = "";
String homeState = "";
String homeZip = "";
String homeCountry = "";
String homePhoneLandline = "";
String homePhoneCell = "";
String homePhoneFax = "";
String homePhonePager = "";
String homeWebsite = "";
String phones = "";
String phoneNumbers = "";
String work = "";
String workAddress = "";
String workStreet = "";
String workSuite = "";
String workCity = "";
String workState = "";
String workZip = "";
String workCountry = "";
String workPhoneLandline = "";
String workPhoneCell = "";
String workPhoneFax = "";
String workPhonePager = "";
String PhoneNumbers = "";
String workWebsite = "";
String birthday = "";
String anniversary = "";
String notes = "";
String personalInfo = "";
String spouse = "";
String sName = "";
String sBirthday = "";
String sFirstName = "";
String sMiddleName = "";
String sLastName = "";
int numberOfChildren = 0;
int childIdx = 0;
%>
<%@ include file="contactManagementSystemHeader.html" %>
Contact Management System [Contact Information]
<%
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating( false );
DocumentBuilder addressBookParser = dbf.newDocumentBuilder();
Document addressBook = addressBookParser.parse("http://localhost:80/addressbook/AddressBook.xml");
addressBook.getDocumentElement().normalize();
addressBook.normalize();
NodeList contactsAll = addressBook.getElementsByTagName("Contact");
NodeList namesAll = addressBook.getElementsByTagName("Name");
count=contactsAll.getLength();
String firstnameLookup = findFirstname;
String lastnameLookup = findLastname;
String spouseName = "";
for (int ctr = 0; ctr < contactsAll.getLength(); ctr++)
{
Node contactName = namesAll.item(ctr);
Element names = (Element)namesAll.item(ctr);
NodeList lastNameList = names.getElementsByTagName("Last");
Node lastNameElement = lastNameList.item(0);
Node lastNameTextNode = lastNameElement.getFirstChild();
String lastNameText = lastNameTextNode.getNodeValue().trim();
NodeList firstNameList = names.getElementsByTagName("First");
Element firstNameElement = (Element)firstNameList.item(0);
Node firstNameTextNode = firstNameElement.getFirstChild();
String firstNameText = firstNameTextNode.getNodeValue().trim();
if ( lastNameText.equalsIgnoreCase(lastnameLookup) )
{
lastNameExists=true;
if ( firstNameText.equalsIgnoreCase( firstnameLookup ) )
{
firstNameExists=true;
Node foundContactName = contactName;
Element foundContactNameE = (Element)foundContactName;
Node foundPrimaryInfo = foundContactName.getParentNode();
Node foundContactRootNode = foundPrimaryInfo.getParentNode();
foundContactRootNode.normalize();
NodeList foundContactNL = foundContactRootNode.getChildNodes();
Node foundPersonalInfo = foundContactRootNode.getNextSibling();
Element contactDetails = (Element)foundContactRootNode;
NodeList contactNameNL = contactDetails.getElementsByTagName("Name");
NodeList contactNameMiddleNL = contactDetails.getElementsByTagName("Middle");
NodeList contactEmailNL = contactDetails.getElementsByTagName("Email");
NodeList workEmailNL = contactDetails.getElementsByTagName("WorkEmail");
NodeList homeEmailNL = contactDetails.getElementsByTagName("HomeEmail");
NodeList contactWorkInfo = contactDetails.getElementsByTagName("Work");
NodeList contactWebsitePers = contactDetails.getElementsByTagName("Website");
NodeList contactStreetsNL = contactDetails.getElementsByTagName("Street");
NodeList contactWorkAddress = contactDetails.getElementsByTagName("WorkAddress");
NodeList contactWebsiteWork = contactDetails.getElementsByTagName("WorkWebsite");
NodeList contactPhones = contactDetails.getElementsByTagName("Phones");
NodeList contactWorkPhones = contactDetails.getElementsByTagName("WorkPhones");
NodeList contactPhonesLandlineNL = contactDetails.getElementsByTagName("Landline");
NodeList contactPhonesCellNL = contactDetails.getElementsByTagName("Mobile");
NodeList contactPhonesFaxNL = contactDetails.getElementsByTagName("Fax");
NodeList contactPhonesPagerNL = contactDetails.getElementsByTagName("Pager");
NodeList contactBirthday = contactDetails.getElementsByTagName("Birthday");
NodeList contactAnniversary = contactDetails.getElementsByTagName("Anniversary");
NodeList contactNotes = contactDetails.getElementsByTagName("Notes");
NodeList contactSpouseNL = contactDetails.getElementsByTagName("Spouse");
NodeList contactSpouseNameNL = contactDetails.getElementsByTagName("sName");
NodeList contactChildrenNL = contactDetails.getElementsByTagName("Children");
NodeList contactcNames = contactDetails.getElementsByTagName("cName");
numberOfChildren = contactcNames.getLength();
//let's get the nickname and get first, middle, and lastnames for the found contact
//first name and last name are not optional, they have to be here
Element contactNameE =(Element)contactNameNL.item(0); //this is the Name Element
NodeList contactNameNodesNL = foundContactNameE.getChildNodes();
if (foundContactNameE.hasAttribute("Nickname"))
nickname = foundContactNameE.getAttribute("Nickname");
if (contactNameMiddleNL.getLength() > 0)
{
Node contactFirstNameNode = contactNameNodesNL.item(1);
Node contactFirstNameNodeTN = contactFirstNameNode.getFirstChild();
contactFirstName = contactFirstNameNodeTN.getNodeValue().trim();
Node contactMiddleNameNode = contactNameNodesNL.item(3);
Node contactMiddleNameNodeTN = contactMiddleNameNode.getFirstChild();
contactMiddleName = contactMiddleNameNodeTN.getNodeValue().trim();
Node contactLastNameNode = contactNameNodesNL.item(5);
Node contactLastNameNodeTN = contactLastNameNode.getFirstChild();
contactLastName = contactLastNameNodeTN.getNodeValue().trim();
}
else
{
Node contactFirstNameNode = contactNameNodesNL.item(1);
Node contactFirstNameNodeTN = contactFirstNameNode.getFirstChild();
contactFirstName = contactFirstNameNodeTN.getNodeValue().trim();
Node contactLastNameNode = contactNameNodesNL.item(3);
Node contactLastNameNodeTN = contactLastNameNode.getFirstChild();
contactLastName = contactLastNameNodeTN.getNodeValue().trim();
}
//let's get the alternative email address for this contact
if (contactEmailNL.getLength() > 0) //email is optional in our .xsd
{
Element emailNodeE =(Element)contactEmailNL.item(0);
if (emailNodeE.hasAttribute("AlternativeEmail"))
alternativeEmail = emailNodeE.getAttribute("AlternativeEmail");
if (workEmailNL.getLength() > 0)
{
Element workEmailE = (Element)workEmailNL.item(0);
Node workEmailTN = workEmailE.getFirstChild();
workEmail = workEmailTN.getNodeValue().trim();
}
if (homeEmailNL.getLength() > 0)
{
Element homeEmailE = (Element)homeEmailNL.item(0);
Node homeEmailTN = homeEmailE.getFirstChild();
homeEmail = homeEmailTN.getNodeValue().trim();
}
}
//Birthday node is not optional so let's just get it's value now
Element bdayNode = (Element)contactBirthday.item(0);
Node bdayNodeText = bdayNode.getFirstChild();
birthday = bdayNodeText.getNodeValue().trim();
//every Contact will have at least the node structure for a Home Address, so let's get
//nodelists for each of these.
NodeList streetNL = contactDetails.getElementsByTagName("Street");
NodeList apt_or_suiteNL = contactDetails.getElementsByTagName("Suite_or_Apt");
NodeList cityNL = contactDetails.getElementsByTagName("City");
NodeList stateNL = contactDetails.getElementsByTagName("State");
NodeList zipNL = contactDetails.getElementsByTagName("Zip");
NodeList countryNL = contactDetails.getElementsByTagName("Country");
//get the city, state, zip information for work and home
//node structure for Address is always in the contact, even if null values
Element homeStreetE = (Element)streetNL.item(0);
Node homeStreetTN = homeStreetE.getFirstChild();
homeStreet = homeStreetTN.getNodeValue().trim();
//not every addresss has a suite or apartment number
Element homeAptE = (Element)apt_or_suiteNL.item(0);
if (homeAptE.hasChildNodes())
{
Node homeAptTN = homeAptE.getFirstChild();
homeApt = homeAptTN.getNodeValue().trim();
}
Element homeCityE = (Element)cityNL.item(0);
Node homeCityTN = homeCityE.getFirstChild();
homeCity = homeCityTN.getNodeValue().trim();
Element homeStateE = (Element)stateNL.item(0);
Node homeStateTN = homeStateE.getFirstChild();
homeState = homeStateTN.getNodeValue().trim();
Element homeZipE = (Element)zipNL.item(0);
Node homeZipTN = homeZipE.getFirstChild();
homeZip = homeZipTN.getNodeValue().trim();
Element homeCountryE = (Element)countryNL.item(0);
Node homeCountryTN = homeCountryE.getFirstChild();
homeCountry = homeCountryTN.getNodeValue().trim();
if (streetNL.getLength() > 1)
{
Element workStreetE = (Element)streetNL.item(1);
Node workStreetTN = workStreetE.getFirstChild();
workStreet = workStreetTN.getNodeValue().trim();
Element workCityE = (Element)cityNL.item(1);
Node workCityTN = workCityE.getFirstChild();
workCity = workCityTN.getNodeValue().trim();
Element workStateE = (Element)stateNL.item(1);
Node workStateTN = workStateE.getFirstChild();
workState = workStateTN.getNodeValue().trim();
Element workZipE = (Element)zipNL.item(1);
Node workZipTN = workZipE.getFirstChild();
workZip = workZipTN.getNodeValue().trim();
Element workCountryE = (Element)countryNL.item(1);
Node workCountryTN = workCountryE.getFirstChild();
workCountry = workCountryTN.getNodeValue().trim();
Element workSuiteE =(Element)apt_or_suiteNL.item(1);
if (workSuiteE.hasChildNodes())
{
Node workSuiteTN = workSuiteE.getLastChild();
workSuite = workSuiteTN.getNodeValue().trim();
}
}
if (contactPhones.getLength() > 0) //homePhonePhones element is optional and so are all subnodes
{
if (contactPhonesLandlineNL.getLength() > 0)
{
Element homePhoneLandlineE = (Element)contactPhonesLandlineNL.item(0);
Node homePhoneLandlineTN = homePhoneLandlineE.getFirstChild();
homePhoneLandline = homePhoneLandlineTN.getNodeValue().trim();
if (contactPhonesLandlineNL.getLength() > 1) //there's a workphone number
{
Element workPhoneLandlineE = (Element)contactPhonesLandlineNL.item(1);
Node workPhoneLandlineTN = workPhoneLandlineE.getFirstChild();
workPhoneLandline = workPhoneLandlineTN.getNodeValue().trim();
}
}
if (contactPhonesCellNL.getLength() >0)
{
Element homePhoneCellE = (Element)contactPhonesCellNL.item(0);
Node homePhoneCellTN = homePhoneCellE.getFirstChild();
homePhoneCell = homePhoneCellTN.getNodeValue().trim();
if (contactPhonesCellNL.getLength() > 1) //there's a work cell number
{
Element workPhoneCellE = (Element)contactPhonesLandlineNL.item(1);
Node workPhoneCellTN = workPhoneCellE.getFirstChild();
workPhoneCell = workPhoneCellTN.getNodeValue().trim();
}
}
if (contactPhonesFaxNL.getLength() > 0)
{
Element homePhoneFaxE = (Element)contactPhonesFaxNL.item(0);
Node homePhoneFaxTN = homePhoneFaxE.getFirstChild();
homePhoneFax = homePhoneFaxTN.getNodeValue().trim();
if (contactPhonesFaxNL.getLength() > 1) //there's a work fax number
{
Element workPhoneFaxE = (Element)contactPhonesFaxNL.item(1);
Node workPhoneFaxTN = workPhoneFaxE.getFirstChild();
workPhoneFax = workPhoneFaxTN.getNodeValue().trim();
}
}
if (contactPhonesPagerNL.getLength() > 0)
{
Element homePhonePagerE = (Element)contactPhonesPagerNL.item(0);
Node homePhonePagerTN = homePhonePagerE.getFirstChild();
homePhonePager = homePhonePagerTN.getNodeValue().trim();
if (contactPhonesPagerNL.getLength() > 1) //there's a work pager number
{
Element workPhonePagerE = (Element)contactPhonesPagerNL.item(1);
Node workPhonePagerTN = workPhonePagerE.getFirstChild();
workPhonePager = workPhonePagerTN.getNodeValue().trim();
}
}
}
if (contactWebsitePers.getLength() > 0) //website is an optional node in the .xsd
{
Element contactWebsitePersNode = (Element)contactWebsitePers.item(0);
Node websiteNodeText = contactWebsitePersNode.getFirstChild();
homeWebsite = websiteNodeText.getNodeValue().trim();
}
if (contactWebsiteWork.getLength() > 0) //website is an optional node in the .xsd
{
Element contactWebsiteWorkNode = (Element)contactWebsiteWork.item(0);
Node websiteNodeText = contactWebsiteWorkNode.getFirstChild();
workWebsite = websiteNodeText.getNodeValue().trim();
}
if (contactAnniversary.getLength() > 0) //anniversary is optional
{
Node anniversaryNode = contactAnniversary.item(0);
Node anniversaryNodeText = anniversaryNode.getFirstChild();
anniversary = anniversaryNodeText.getNodeValue().trim();
}
if (contactNotes.getLength() > 0) //notes is an optional node in the .xsd
{
Element notesNode = (Element)contactNotes.item(0);
Node notesNodeText = notesNode.getFirstChild();
notes = notesNodeText.getNodeValue().trim();
}
if (contactSpouseNL.getLength() > 0) //spouse is optional node
{
Element spouseE = (Element)contactSpouseNL.item(0);
if (spouseE.hasAttribute("sBirthday"))
sBirthday = spouseE.getAttribute("sBirthday");
Element spouseFirstLastNameE = (Element)contactSpouseNameNL.item(0);
NodeList spouseFirstLastNameNL = spouseFirstLastNameE.getChildNodes();
if (spouseFirstLastNameE.hasChildNodes())
{
Element contactSpouseFirstNameNodeE = (Element)spouseFirstLastNameNL.item(1);
Node contactSpouseFirstNameNodeTN = contactSpouseFirstNameNodeE.getFirstChild();
sFirstName = contactSpouseFirstNameNodeTN.getNodeValue().trim();
Element contactSpouseLastNameNodeE = (Element)spouseFirstLastNameNL.item(3);
Node contactSpouseMiddleNameNodeTN = contactSpouseLastNameNodeE.getFirstChild();
sLastName = contactSpouseMiddleNameNodeTN.getNodeValue().trim();
}
} //spouse loop
numberOfChildren = contactChildrenNL.getLength();
String[] childBirthday = new String[numberOfChildren];
String[] childFirstname = new String[numberOfChildren];
String[] childLastname = new String[numberOfChildren];
if (contactChildrenNL.getLength() > 0) //children optional
{
out.print("| Number of children? | ");
out.print(numberOfChildren);
out.print(" |
| ");
out.print("First name | ");
out.print("Last name | ");
out.print("Birthday | ");
for (childIdx = 0; childIdx < contactcNames.getLength(); childIdx++)
{
Element childrenE = (Element)contactChildrenNL.item(childIdx);
if (childrenE.hasAttribute("cBirthday"))
{
childBirthday[childIdx] = childrenE.getAttribute("cBirthday");
}
Element childNameFirstE = (Element)contactcNames.item(childIdx);
NodeList childNameFirstNodesNL = childNameFirstE.getChildNodes();
Element childNameLastE = (Element)contactcNames.item(childIdx);
NodeList childNameLastNodesNL = childNameLastE.getChildNodes();
if (childNameFirstNodesNL.getLength() > 0)
{
Element childFirstNameNodeE = (Element)childNameFirstNodesNL.item(1);
Node childFirstNameTN = childFirstNameNodeE.getFirstChild();
childFirstname[childIdx] = childFirstNameTN.getNodeValue().trim();
out.print(" |
| " + childFirstname[childIdx] + " | ");
Element childLastNameNodeE = (Element)childNameLastNodesNL.item(3);
Node childLastNameTN = childLastNameNodeE.getFirstChild();
childLastname[childIdx] = childLastNameTN.getNodeValue().trim();
out.print("" + childLastname[childIdx] + " | ");
}
out.print("" + childBirthday[childIdx]);
out.print(" |
");
} //for-loop inside children's loop
} //if for contact's children loop
} // if firstNameText equals firstnameLookup
} // if lastName.equals lastnameLookup
} //for-loop ctr
%>
<% if ((lastNameExists) && (firstNameExists))
{
%>
Contact named <%=findFirstname%> <%=findLastname%> is in the AddressBook:
| First name: |
<%=contactFirstName%> |
Middle name: |
<%=contactMiddleName%>
|
| Last name: | <%=contactLastName%> |
| Birthday: |
<%=birthday%> |
Nickname: | <%=nickname%> |
Alternative email: | <%=alternativeEmail%>
| |
PERSONAL CONTACT INFORMATION |
| Street: |
<%=homeStreet%>
| Apartment #: |
<%=homeApt%>
|
| City: |
<%=homeCity%>
| State: |
<%=homeState%>
| Zip: |
<%=homeZip%>
|
| Country: |
<%=homeCountry%>
|
|
PERSONAL PHONES, EMAIL, WEB |
| Landline: |
<%=homePhoneLandline%>
| Cell phone: |
<%=homePhoneCell%>
|
| Pager: |
<%=homePhonePager%>
| Fax: |
<%=homePhoneFax%>
|
| Email: |
<%=homeEmail%>
| Website: |
<%=homeWebsite%>
|
|
BUSINESS CONTACT INFORMATION |
| Street: |
<%=workStreet%>
|
Suite #: |
<%=workSuite%>
|
| City: |
<%=workCity%>
| State: |
<%=workState%>
| Zip: |
<%=workZip%>
|
| Country: |
<%=workCountry%>
|
|
BUSINESS PHONES, EMAIL, WEB |
|
Landline: |
<%=workPhoneLandline%>
|
Cell phone: |
<%=workPhoneCell%>
|
| Pager: |
<%=workPhonePager%>
| Fax: |
<%=workPhoneFax%>
|
| Email: |
<%=workEmail%>
| Website: |
<%=workWebsite%>
|
|
OTHER PERSONAL INFORMATION |
| SPOUSE'S INFORMATION |
| Anniversary: |
<%=anniversary%>
| Birthday: |
<%=sBirthday%> |
| First name: |
<%=sFirstName%>
| Last name: |
<%=sLastName%>
|
| |
|
NOTES |
|
<%=notes%>
|