Listing B. RequestToEventTranslator.java.

Enterprise Java
Writing a Reusable Implementation of the MVC Design Pattern
Prashant Sarode
Listing B. RequestToEventTranslator.java.


package com.framework;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;

public class RequestToEventTranslator{

private String debugMsg="EventTranslator--0";
private DOMParser XMLParser;
private Document eventMappingdoc;
private String eventMappingFile;

public RequestToEventTranslator(){
try {

//Initiatize The DOM Parser
    XMLParser = new DOMParser();
    XMLParser.setIncludeIgnorableWhitespace(false);
} catch (Exception e) {
    debugMsg=debugMsg + "Problem In Constructor";

}

}

public Event processRequest( HttpServletRequest request,
                                    ServletConfig servletConfig){
Event event =null;
Node eventXmlNode;
try {

//Get BusinessEventMapping.xml's complete file name.
eventMappingFile = getEventMappingFile(request);
XMLParser.parse(eventMappingFile);
eventMappingdoc = XMLParser.getDocument();

//Get the event Url Path & then the event in XML node format
String eventPath= request.getPathInfo();
eventXmlNode=getEvent(eventPath.substring(1,eventPath.length()));

if (eventXmlNode !=null){

//Now create an Event Object..from Event XML Node
event = new Event(eventXmlNode);
if (event.getType().toUpperCase().equals("NOREDIRECT") ){
    if (event.getEventParams() != null ){
    //---Do the Population of Param Values
    if (populateParamValues(event,request,servletConfig) != true){
                        event=null;
    }
    }else{
    debugMsg="EventTranslator--event was of type 'NOREDIRECT'";
   event = null;

}
}
return event;
}else{
debugMsg = "Event Not found in BusinessEventMapping.XML";
return event;
}


} catch (Exception e) {
debugMsg="EventTranslator--problem in processRequest()";
return null;
}

}

public String getDebugMsg() {
return debugMsg;
}

private String getEventMappingFile( HttpServletRequest request) {
String fileName;
fileName= request.getPathTranslated().replace('\\','/');
fileName = fileName.substring(0,fileName.lastIndexOf
                                        ( request.getPathInfo()));
fileName= fileName +"/WEB-INF/BusinessEventMapping.xml";
fileName= "file:///" + fileName;

return fileName;
}

private Node getEvent(String eventID) {
Node retEventNode=null;
retEventNode = getMatchingNode("Event", "ID", eventID,
                                                eventMappingdoc);
return  retEventNode;
}

private Node getMatchingNode(String tagName, String attName,
                                    String attVal, Document srcDoc) {
Node retNode=null;
.
.
//XML parsing, traversing to get right node based on method parameters
.
.
    return  retNode;
 }

private Node getMatchingNode(String tagName,Node srcNode) {
    Node retNode=null;
.
.
//Overloded --XML parsing, traversing to get right node based on method parameters
.
.
    return  retNode;
 }


private boolean populateParamValues(Event event ,
                                    HttpServletRequest request,
                                    ServletConfig servletConfig){
EventParam[] eventParams=event.getEventParams();
String paramName;
String paramType;
String paramScope;
boolean paramReadOnly;
boolean retVal=true;
int k;

for (k=0; k <eventParams.length; k++) {
if (eventParams[k] != null ){
paramName = eventParams[k].getName();
paramType  = eventParams[k].getType();
paramScope= eventParams[k].getScope();
paramReadOnly= eventParams[k].getReadOnly();

if (paramScope.toUpperCase().equals("SESSION")){
retVal = populateParamValueWithSessionScope(request,
                                            eventParams[k],
                                            paramName,
                                            paramReadOnly);

}else if (paramScope.toUpperCase().equals("APPLICATION")) {
retVal = populateParamValueWithAppScope(request,
                                        servletConfig,
                                        eventParams[k],
                                        paramName,paramReadOnly);

}else if (paramScope.toUpperCase().equals("REQUEST")) {
retVal = populateParamValueWithReqScope(request,
                                        eventParams[k],
                                        paramName,
                                        paramReadOnly);

}else if (paramScope.toUpperCase().equals("NULL")) {
retVal = populateParamValueWithNullScope(request,
                                         eventParams[k],
                                         paramName);
}
}
}
return retVal;
}

private boolean populateParamValueWithSessionScope(
                                        HttpServletRequest request,
                                        EventParam eventParam,
                                        String paramName,
                                        boolean paramReadOnly){
.
.
//Deal with Session scope.
.
.

}


private boolean populateParamValueWithAppScope(
                                        HttpServletRequest request,
                                        EventParam eventParam,
                                        String paramName,
                                        boolean paramReadOnly){
//Deal with Application scope.
.
.
 }

private boolean populateParamValueWithReqScope(
                                        HttpServletRequest request,
                                        EventParam eventParam,
                                        String paramName,
                                        boolean paramReadOnly){
//Deal with Request scope.
.
.

}

private boolean populateParamValueWithNullScope(
                                        HttpServletRequest request,
                                        EventParam eventParam,
                                        String paramName){
//Deal with Null scope.
.
}
}