Thursday, October 22, 2009

Creating the Administrator Module













Creating the Administrator Module

The administrator module of the online technical support application allows the administrator to post a reply to a query. The administrator module contains the AdminPage.jsp and QueryReply.jsp files that provide the Administrator Options and the Query Reply pages.




Creating the Administrator Options Page


The Administrator Options page contains hyperlinks an administrator can select to post a reply to a query and view other query replies. The AdminPage JSP provides the Administrator Options page of the application.



Listing 4-8 shows the content of the AdminPage.jsp file:




Listing 4-8: The AdminPage.jsp File







<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Administrator Options</title>
</head>
<body>
<table width="830" height="71" border="0">
<tr>
<td width="816" height="61" bgcolor="#FFFFFF"><h2 align="center"><font
color="#000080" size="5"><strong><em>Online Technical Support
</em></strong></font></h2></td>
</tr>
</table>
<p align="center"><b></b></p>
<table border="1" width="215" cellspacing="0" cellpadding="0" height="1">
<tr>
<!--Display user ID from session-->
<td width="95" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#000000"><b>User </b></font></div></td>
<td width="104" bgcolor="#FFFFFF" height="1">
<div align="center"><%=session.getAttribute("userid")%></div></td>
</tr>
</table>
<div align="justify">
<!--Display hyperlink to DoLogout.jsp file-->
<table border="1" cellpadding="0" cellspacing="0" width="200" style="position: absolute;
left: 663px; top: 121px; width: 150">
<tr>
<td height="18" bgcolor="#FFFFFF"><div align="center"><font
color="#FFFFFF"><b><a
href="DoLogout.jsp">Logout</a></b></font></div></td>
</tr>
</table>
</div>
<p align="center">&nbsp;</p>
<div align="justify">
<!--Display User Options table-->
<table border="1" cellpadding="0" cellspacing="0" width="300" style="position: absolute;
left: 262px; top: 255px; width: 300">
<tr>
<td bgcolor="#FFFFFF">
<p align="center"><b><font color="#FF0000">Administrator
Options</font></b></p>
</td>
</tr>
<!--Display hyperlink to ViewQueryList.jsp file-->
<tr>
<td bgcolor="#FFFFFF"><div align="center"><i><b><a
href="ViewQueryList.jsp">View query list</a></b></i></div></td>
</tr>
<!--Display hyperlink to QueryReply.jsp file-->
<tr>
<td bgcolor="#FFFFFF"><div align="center"><i><b><a
href="QueryReply.jsp">Post reply to query
</a></b></i></div></td>
</tr>
</table>
</div>
</body>
</html>















Download this listing.


The above code creates the Administrator Options page of the online technical support application. The code displays the hyperlinks to the DoLogout.jsp, NewQuery.jsp, and QueryReply.jsp files.



Figure 4-5 shows the Administrator Options page of the online technical support application:






Figure 4-5: The Administrator Options Page




Creating the Query Reply Page


The Query Reply page allows the administrator to select a query and post a reply to it. This reply is added to the querylist.xml file. The QueryReply JSP provides the Query Reply page of the online technical support application.



Listing 4-9 shows the content of the QueryReply.jsp file:




Listing 4-9: The QueryReply.jsp File







<!--Import the necessary packages-->
<%@page import="javax.xml.parsers.*, java.net.*, java.io.*, java.util.*, org.w3c.dom.*"%>
<%@page
import="javax.xml.transform.Transformer, javax.xml.transform.TransformerFactory, javax.xml.
transform.TransformerException"%>
<%@page
import="javax.xml.transform.TransformerConfigurationException, javax.xml.transform.dom.
DOMSource, javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.OutputKeys, techdesk.*"%>
<%
String tempmsgTitle, tempdate,tempuser, tempmessage, querypath;
/*Use request object to obtain query title, query, and date*/
tempmsgTitle=request.getParameter("txtfieldmessage");
tempmessage=request.getParameter("txtmessage");
tempdate=request.getParameter("txtdate");
/*Create a URL object to load the properties file*/
URL url=new URL("http://127.0.0.1:8080/fourth/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Obtain the querylist path value from path.properties file*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
querypath = prop.getProperty("querylistpath");
/*Check whether all fields are specified*/
if(!(tempmsgTitle == null || tempmessage == null ||tempdate == null))
{
/*Obtain a DocumentBuilder object by calling the newInstance() method*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the querylist XML document*/
Document doc = docb.parse(new FileInputStream(querypath));
/*Retrieve querylist root element*/
NodeList nluserlist = doc.getElementsByTagName("querylist");
/*Obtain a NodeList object that contains child elements of the root element*/
Node doce = nluserlist.item(0);
NodeList nl1 = doce.getChildNodes();
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1< nl1.getLength(); int_1 ++)
{
/*Obtain Node object by calling item() method of NodeList*/
Node n = nl1.item(int_1);
if((n.getNodeName()).equals("query"))
{
/*If node name is query, retrieve value of title attribute*/
NamedNodeMap nnp= n.getAttributes();
String temptitle2=(nnp.getNamedItem("title")).getNodeValue();
/*Check retrieved value with value specified by end user*/
if(tempmsgTitle.equals(temptitle2))
{
/*For matching title, create a reply element*/
Element newe = doc.createElement("reply");
/*Retrieve attribute from session*/
tempuser=session.getAttribute("userid").toString();
/*Check if this is a first reply*/
if(temptitle2.startsWith("RE :"))
{
/*If reply already exist, set a msg attribute with two RE string preceding the reply*/
newe.setAttribute("msg","RE : RE : " + tempmessage);
}
else
{
/*For first reply, set a msg attribute with a RE string preceding the reply*/
newe.setAttribute("msg","RE : " + tempmessage);
}
/*Set user, and postedon attributes to reply element*/
newe.setAttribute("user",tempuser);
newe.setAttribute("postedon",tempdate);
/*Add reply element to query element*/
n.appendChild(newe);
}
}
}
/*Call newInstance() of TransformerFactory class to obtain TransformerFactory object*/
TransformerFactory tFactory = TransformerFactory.newInstance();
/*Call newTransformer() method of TransformerFactory to obtain Transformer object*/
Transformer transformer = tFactory.newTransformer();
/*Set output property of Transformer object*/
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
/*Construct a DOMSource object as source of transformation*/
DOMSource source = new DOMSource(doc);
/*Create a StreamResult object to represent a stream to querylist XML document as result of transformation*/
StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(querypath)));
/*Perform the transformation*/
transformer.transform(source, result);
}
%>
<!--Page design to display fields to add reply-->
<table width="830" height="71" border="0">
<tr>
<td width="816" height="61" bgcolor="#FFFFFF"><h2 align="center"><font
color="#000080" size="5"><strong><em>Online Technical Support
</em></strong></font></h2></td>
</tr>
</table>
<table border="1" width="215" cellspacing="0" cellpadding="0" height="1">
<!--Display user ID retrieved from session object-->
<tr>
<td width="95" bgcolor="#FFFFFF" height="1"><div align="center"><font
color="#000000"><b>User</b></font></div></td>
<td width="104" bgcolor="#FFFFFF" height="1"><div
align="center"><%=session.getAttribute("userid")%></div></td>
</tr>
</table>
<p align="center">&nbsp;</p>
<p align="center"><font size="5"><b>Query Reply </b></font>
</p>
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="200" style="position: absolute;
left: 695px; top: 86px; width: 150">
<!--Display Logout hyperlink-->
<tr>
<td height="18" bgcolor="#FFFFFF"><div align="center"><font
color="#FFFFFF"><b><a
href="DoLogout.jsp">Logout</a></b></font></div></td>
</tr>
<!--Display hyperlink to AdminPage.jsp-->
<tr>
<td height="18" bgcolor="#FFFFFF"><div align="center"><b>
<a href="AdminPage.jsp">
Go to main page</a></b></div></td>
</tr>
</table>
</div>
<!--Display form to add reply. Submit data to same page-->
<form action = QueryReply.jsp method ="post">
<center>
<table width="346">
<!--Display Query title drop-down list-->
<tr>
<td width="179" height="36" bgcolor="#FFFFFF"><div
align="center"><strong>Query title:</strong></div></td>
<td width="153" bgcolor="#FFFFFF">
<select name="txtfieldmessage">
<option value=""></option>
<%
/*Obtain a DocumentBuilder object by calling the newInstance() method*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse the querylist XML document*/
Document doc = docb.parse(new FileInputStream(querypath));
/*Obtain a NodeList element that contains query elements as Node objects*/
NodeList nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1< nl1.getLength(); int_1 ++)
{
/*Obtain the Node object that represent the XML query element*/
Node n=nl1.item(int_1);
/*Retrieve the NamedNodeMap object that contains attributes of query element*/
NamedNodeMap nnp = n.getAttributes();
%>
<!--Populate drop-down list with value of the title attribute-->
<option><%=(nnp.getNamedItem("title")).getNodeValue()%></option>
<%
}
%>
</select></td>
</tr>
<!--Display the Date text field-->
<tr>
<td width="179" bgcolor="#FFFFFF"><div
align="center"><strong>Date:</strong></div></td><td width="153" bgcolor="#FFFFFF">
<input name="txtdate" ></td>
</tr>
<!--Display the Query text area-->
<tr>
<td width="179" bgcolor="#FFFFFF"><div
align="center"><strong>Query:</strong></div></td><td width="353" bgcolor="#FFFFFF">
<textarea name="txtmessage" cols="30" rows="5"></textarea></td>
</tr>
</table>
</center>
<tr>
<td width="179"><center><input type="submit" value="Post
Reply"></input></center></td>
</tr>
</table>
</form>
<!--Table to display existing queries-->
<div align="justify">
<table border="1" cellpadding="0" cellspacing="0" width="300" style="position: absolute;
left: 236px; top: 457px; width: 453px;">
<tr bgcolor="#FFFFFF">
<td colspan="2">
<p align="center"><i><font color="#FF0000"><b> Existing
Queries</b></font></i></p>
</td>
</tr>
<tr>
<td width="147" bgcolor="#FFFFFF"><div align="center"><font
color="#000000"><b>&nbsp; Title</b></font></div></td>
<td width="147" bgcolor="#FFFFFF"><div align="center"><font
color="#333333"><b> Content</b></font></div></td>
</tr>
<%
/*Obtain a DocumentBuilder object by calling the newInstance() method*/
docbfact = DocumentBuilderFactory.newInstance();
/*Call the newDocumentBuilder() method to obtain a DocumentBuilder object*/
docb = docbfact.newDocumentBuilder();
/*Parse the querylist XML document*/
doc = docb.parse(new FileInputStream(querypath ));
/*Obtain a NodeList element that contains query elements as Node objects*/
nl1 = doc.getElementsByTagName("query");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 < nl1.getLength(); int_1 ++)
{
/*Obtain NamedNodeMap hat contains attributes of the query element*/
Node n=nl1.item(int_1);
NamedNodeMap nnp = n.getAttributes();
%>
<tr>
<!--Display values of the title and msg attributes-->
<td width="147" bgcolor="#FFFFFF"><div align="center"><%=
(nnp.getNamedItem("title")).getNodeValue()%></div></td>
<td width="147" bgcolor="#FFFFFF"><div
align="center"><%=(nnp.getNamedItem("msg")).getNodeValue()%></div></td>
</tr>
<%
}
%>
</table>
</div>















Download this listing.


The above code creates the Query Reply page of the online technical support application. The user interface displays the Query title, Date, and Reply fields. The code populates the Query title drop-down list with the values of the title attributes of the querylist XML document. When the administrator selects a query and posts a reply, the code calls the parse() method to parse the querylist XML document. The parse() method creates an object tree in the memory. The code compares the query title specified by the administrator with the values of the title attribute of the query elements. When a matching title is found, the code calls the createElement() method to create an Element object, newe, which represents a reply element. The setAttribute() method sets the msg attribute to the newe object. The appendChild() method adds the newe object to the Element object that represents the query element. Finally, the code transforms the object tree to the XML document by calling the transform() method of the Transformer object.



Figure 4-6 shows the Query Reply page of the online technical support application:






Figure 4-6: The Query Reply Page











No comments: