Wednesday, November 4, 2009

Creating the Delete Video Album Page













Creating the Delete Video Album Page

An administrator can use the Delete Video Album page to delete information about a video album. This page contains a video album name drop-down list and a Delete Video Album button. The page also displays information about video albums stored in the videoalbumlist.xml file. The DeleteVideoAlbum.jsp file provides the Delete Video Album page of the application, as shown in Listing 2-14:




Listing 2-14: The DeleteVideoAlbum.jsp File







<!--
JSP Page that allows an end user to delete a video album
-->
<!--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.transf
orm.TransformerException"%>
<%@page
import="javax.xml.transform.TransformerConfigurationException,javax.xml.transform.dom.DOMSource
,javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.OutputKeys"%>
<%!String videopath;%>
<%
/*Create a URL object to load properties file*/
URL url=new URL("http://127.0.0.1:8080/second/path.properties");
/*Open URL connection*/
URLConnection ucon=url.openConnection();
/*Retrieve videoalbumlistpath property*/
Properties prop = new Properties();
prop.load(ucon.getInputStream());
videopath = prop.getProperty("videoalbumlistpath");
/*Retrieve album name to delete from HttpRequest object*/
String tempuser;
tempuser=request.getParameter("txtfieldalbum");
/*Check if album name to delete is provided*/
if(!(tempuser == null ))
{
/*Create object of DocumentBuildeFactory, using newInstance() method of
DocumentBuilderFactory*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Create object of DocumentBuilder using the newDocumentBuilder() method of
DocumentBuilderFactory*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse musicpurchaselist.xml and obtain a Document object*/
Document doc = docb.parse(new FileInputStream(videopath));
/*retrieve NodeList object corresponding to musicalbumlist element*/
NodeList nluserlist = doc.getElementsByTagName("videoalbumlist");
/*Retrieve NodeList for child elements*/
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 ++)
{
/*Check for the album element*/
Node n = nl1.item(int_1);
if((n.getNodeName()).equals("album"))
{
/*Retrieve value of name attribute for the album element*/
NamedNodeMap nnp= n.getAttributes();
String tempuser2=(nnp.getNamedItem("name")).getNodeValue();
/*Check retrieved value with end user specified value to delete*/
if(tempuser.equals(tempuser2))
{
/*Delete element from XML file*/
doce.removeChild(n);
}
}
}
/*Create a TransformerFactory by calling the newInstance() method of TransformerFactory class*/
TransformerFactory tFactory = TransformerFactory.newInstance();
/*Obtain a Transformer object by calling the newTransformer() method of TransformerFactory class*/
Transformer transformer = tFactory.newTransformer();
/*Set output property of Transformer object*/
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
/*Specify the source of the transformation*/
DOMSource source = new DOMSource(doc);
/*Create the StreamResult holder for transformation to the videoalbumlist.xml file*/
StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(videopath)));
/*Perform the transformation*/
transformer.transform(source, result);
}
%>
<!--Page design to display Delete Video Album page-->
<table width="767" height="80" border="3">
<tr>
<td bgcolor="#0099FF"><h2 align="center"><font color="#FFFFFF"
size="5"><strong><em>Online Music Store
</em></strong></font></h2></td>
</tr>
</table>
<p align="center"><font size="5"><b>Delete Video Album
</b></font></p>
<!--Retrieve user name and role from session and display it in tabular format-->
<table border="0" width="215" cellspacing="0" cellpadding="0" height="1">
<tr>
<td width="95" bgcolor="#0099FF" height="1"><div align="center"><font
color="#000000"><b>User </b></font></div></td>
<td width="104" bgcolor="#0099FF" height="1">
<div align="center"><%=session.getAttribute("userid")%></div></td>
</tr>
<tr>
<td width="95" bgcolor="#0099FF" height="13"><div
align="center"><b>Role</b></div></td>
<td width="104" bgcolor="#0099FF" height="13"><div
align="center"><%=session.getAttribute("role")%></div></td>
</tr>
</table>
<!--Display hyperlink to DoLogout page-->
<div align="justify">
<table border="0" cellpadding="0" cellspacing="0" width="200" style="position: absolute;
left: 496px; top: 154px; width: 266px" >
<tr>
<td height="18" bgcolor="#0099FF"><div align="center"><font
color="#FFFFFF"><b><a
href="DoLogout.jsp">Logout</a></b></font></div></td>
</tr>
<!--Check current role and display the Go to main page hyperlink accordingly-->
<tr>
<td height="18" bgcolor="#0099FF"><div align="center"><b>
<%if("administrator".equals(session.getAttribute("role")))
{%>
<a href="AdministratorPage.jsp">
<%}
else
{%>
<a href="PurchaseItem.jsp">
<%}%>
Go to main page</a></b></div></td>
</tr>
</table>
</div>
<!--Display HTML form with drop-down option field to obtain information on album to delete.
Submit Form details to same page-->
<form action = DeleteVideoAlbum.jsp method ="post">
<Center>
<table width="507">
<tr>
<td width="179" bgcolor="#0099FF"><div align="center"><strong>Select Video
Album Name</strong></div></td>
<td width="153" bgcolor="#FFFFFF"><select name="txtfieldalbum">
<option value=""></option>
<%
/*Create object of DocumentBuildeFactory, using newInstance() method of DocumentBuilderFactory*/
DocumentBuilderFactory docbfact = DocumentBuilderFactory.newInstance();
/*Create object of DocumentBuilder using the newDocumentBuilder() method of
DocumentBuilderFactory*/
DocumentBuilder docb = docbfact.newDocumentBuilder();
/*Parse videoalbumlist.xml and obtain a Document object*/
Document doc = docb.parse(new FileInputStream(videopath));
/*Obtain a NodeList object that represents the elements of the XML file with name album*/
NodeList nl1 = doc.getElementsByTagName("album");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1<nl1.getLength(); int_1 ++)
{
/*Retrieve NamedNodeMap that represents attributes of the album element*/
Node n=nl1.item(int_1);
NamedNodeMap nnp = n.getAttributes();
%>
/*Populate drop-down list with attribute values*/
<option
value=<%=(nnp.getNamedItem("name")).getNodeValue()%>><%=(nnp.getNamedItem("name"))
.getNodeValue()%></option>
<%
}
%>
</select></td>
<td width="153" bgcolor="#FFFFFF"><input type="submit" value="Delete Video
Album"></td>
</tr>
<tr>
<td width="179"></input></td>
</tr>
</table>
</Center>
</form>
<!--Page design to display available video albums in tabular format-->
<div align="justify">
<table border="1" cellpadding="0" cellspacing="0" width="300" style="position: absolute;
left: 202px; top: 312px">
<tr bgcolor="#0099FF">
<td colspan="2">
<p align="center"> <font color="#000000"><b>Available Video
Albums</b></font></p>
</td>
</tr>
<tr>
<td width="147" bgcolor="#0099FF"><div align="center"><font
color="#333333"><b>&nbsp;Album Name</b></font></div></td>
<td width="147" bgcolor="#0099FF"><div align="center"><font
color="#000000"><b>Company Name</b></font></div></td>
</tr>
<%
/*Create object of DocumentBuilderFactory, using newInstance() method of DocumentBuilderFactory*/
docbfact = DocumentBuilderFactory.newInstance();
/*Create object of DocumentBuilder using the newDocumentBuilder() method of DocumentBuilderFactory*/
docb = docbfact.newDocumentBuilder();
/*Parse the XML document, videoalbumlist.xml to retrieve the Document object*/
doc = docb.parse(new FileInputStream(videopath));
/*Obtain a NodeList object that represents the elements of the XML file with name album*/
nl1 = doc.getElementsByTagName("album");
/*Iterate through the NodeList object*/
for(int int_1 = 0; int_1 <nl1.getLength(); int_1 ++)
{
/*Obtain the Node object by calling the item() method of NodeList*/
Node n=nl1.item(int_1);
/*Obtain the NamedNodeMap object that represents the attributes of the album element*/
NamedNodeMap nnp = n.getAttributes();
%>
<!--Retrieve the name attribute value of the album element of the XML file, and display it -->
<tr>
<td width="147" bgcolor="#FFFFFF"><div
align="center"><%=(nnp.getNamedItem("name")).getNodeValue()%></div></td>
<%
/*Obtain the NodeLIst object for the sub element company of album element*/
NodeList nl2 = n.getChildNodes();
/*Obtain the attributes for the sub element as NamedNodeMap object*/
Node n2=nl2.item(1);
NamedNodeMap nnp1 = n2.getAttributes();
%>
<!--Retrieve the name attribute value of the company sub element of the XML file, and
display it -->
<td width="147" bgcolor="#FFFFFF"><div
align="center"><%=(nnp1.getNamedItem("name")).getNodeValue()%></div></td>
</tr>
<%
}
%>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div></html>















Download this listing.


The above code creates the Delete Video Album page of the online music store application. This page displays a drop-down list and a Delete Video Album button. The code populates the drop-down list with the names retrieved from the videoalbumlist.xml file after parsing the file. When an end user clicks the Delete Video Album button, the code uses DOM to parse the videoalbumlist XML document to create an object tree. The code iterates through the object tree to locate the node that corresponds to the video album name to be deleted. After locating the node, the code removes it from the object tree and transforms the object tree back to the videoalbumlist XML document.



Figure 2-11 shows the Delete Video Album page of the online music store application:






Figure 2-11: Delete Video Album Page










No comments: