📄 addressmaintenancebean.java
字号:
package mvc.addressmaintenance;/* Standard Java Classes */import java.rmi.RemoteException;import java.util.Collection;import java.util.Iterator;import java.io.File;/* Imported from the J2EE or JBoss ejb.jar and servlet.jar files */import javax.ejb.SessionBean;import javax.ejb.SessionContext;import javax.naming.InitialContext;import javax.servlet.http.HttpServletRequest;/* Imported from the xerces.jar file */import org.w3c.dom.Document; /* W3C Interfaces */import org.w3c.dom.Element;import org.apache.xerces.dom.DocumentImpl; /* Xerces DOM Classes *//* Class files for the chapter */import mvc.templates.*;import mvc.MVCHelper;import mvc.entitybeans.customers.*;import mvc.entitybeans.postalcodes.*;import mvc.custlist.*;public class AddressMaintenanceBean implements MVCBean { /* * The retrieve method for the AddressMaintenanceBean is using the Customers * and PostalCodes entity beans to retrieve a customer address record and build an XML document to return * back to the calling application. */ public Document retrieve(HttpServletRequest _request) throws RemoteException, Exception { Integer customerId = new Integer(_request.getParameter("customerId")); InitialContext ctx = new InitialContext(); CustomersHome customersHome = (CustomersHome) ctx.lookup("Customers"); Customers customers = customersHome.findByPrimaryKey(customerId); PostalCodesHome postalCodesHome = (PostalCodesHome) ctx.lookup("PostalCodes"); PostalCodes postalCodes = postalCodesHome.findByPrimaryKey(customers.getCustomerPostalCode()); return buildXML(customers, postalCodes, ctx); } /* * The buildXML method will build an XML document based on the values * stored in the internal class variables. It will then query the * music record database and retrieve a list of all of the Postal Codes * stored within the postal codes table. These postal codes are used to * populate the dropdown box seen in the address maintenance screen. */ private Document buildXML(Customers _customers, PostalCodes _postalCodes, InitialContext _ctx) throws Exception { Document xmlDoc = new DocumentImpl(); /* Creating the root element */ Element rootElement = xmlDoc.createElement("CUSTOMERS"); Element customer = xmlDoc.createElement("CUSTOMER"); xmlDoc.appendChild(rootElement); customer.setAttribute("customerid", _customers.getCustomerId().toString()); /* Adding my different elements to the customer record */ customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERFIRSTNAME", _customers.getCustomerFirstName())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERLASTNAME", _customers.getCustomerLastName())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERADDRESS1", _customers.getCustomerAddress1())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERADDRESS2", _customers.getCustomerAddress2())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERCITY", _customers.getCustomerCity())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERSTATE", _postalCodes.getStateProv())); customer.appendChild(MVCHelper.buildDBElement(xmlDoc, "CUSTOMERZIP", _postalCodes.getPostalCode())); rootElement.appendChild(customer); /* * Retrieving all of the postal codes stored within the music record database and * appending this onto the customer's document root object. */ rootElement.appendChild(buildPostalCodes(xmlDoc, _ctx)); return xmlDoc; } /* * The buildPostalCodes method will build the <POSTALCODES></POSTALCODES> * piece of the <CUSTOMERS>...</CUSTOMERS> XML DOM that is returned to * the code calling the toXML() method on this class. */ private Element buildPostalCodes(Document _xmlDoc, InitialContext _ctx) throws Exception { PostalCodesHome postalCodesHome = (PostalCodesHome) _ctx.lookup("PostalCodes"); Collection postalCodeCollections = postalCodesHome.findAll(); /* Appending individual <POSTALCODE> elements to to <POSTALCODES> */ Iterator iterator = postalCodeCollections.iterator(); /* Building the <POSTALCODES> XML Element */ Element postalCodesElement = _xmlDoc.createElement("POSTALCODES"); Element postalCodeElement = null; while (iterator.hasNext()) { PostalCodes postalCodes = (PostalCodes) iterator.next(); postalCodeElement = _xmlDoc.createElement("POSTALCODE"); postalCodeElement.setAttribute("postalcodeid", postalCodes.getPostalCode()); postalCodesElement.appendChild(postalCodeElement); } return postalCodesElement; } /* * The post method populates an AddressMaintenanceHelper class with data returned * from the music record database. Since this screen returns to the CustomerList * screen after updating the record, it instantiates the CustomerList EJB and * performs a retrieve. */ public Document post(HttpServletRequest _request) throws RemoteException, Exception { Integer customerId = new Integer(_request.getParameter("customerId")); InitialContext ctx = new InitialContext(); CustomersHome customersHome = (CustomersHome) ctx.lookup("Customers"); Customers customers = customersHome.findByPrimaryKey(customerId); /* * Populating the customers object with data posted when the * user hit the Save button on the Address Maintenance screen. */ customers .setCustomerAddress1(_request.getParameter("customerAddress1")); customers .setCustomerAddress2(_request.getParameter("customerAddress2")); customers.setCustomerCity(_request.getParameter("customerCity")); customers.setCustomerPostalCode(_request.getParameter("postalCode")); /* * Instantiating the CustomerList class and using it to return a refreshed * list of the Customer Records. */ MVCHome customerListHome = (MVCHome) ctx.lookup("CustomerList"); MVCObject mvcComponent = customerListHome.create(); return mvcComponent.retrieve(_request); } /* * Standard EJB Methods. For simplicity's sake these methods have * been implemented as empty methods. */ public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionCtx) {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -