1    package com.instantbank.lettertemplate.control.taglib;
2    
3    import javax.servlet.jsp.JspTagException;
4    import javax.servlet.jsp.tagext.TagSupport;
5    
6    import javax.servlet.http.HttpSession;
7    import javax.servlet.http.HttpServletRequest;
8    import com.instantbank.lettertemplate.control.util.WebKeys;
9    import com.instantbank.lettertemplate.control.web.Parameter;
10   import com.instantbank.lettertemplate.control.web.ScreenFlowManager;
11   import com.instantbank.common.utilcomponents.Debug;
12   import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
13   
14   
15   /**
16    *  This class is an easy interface to the JSP template or other text that needs
17    *  to be inserted.
18    *
19    * @created August 2002
20    */
21   public class InsertTag extends TagSupport {
22   
23     private boolean directInclude = false;
24     private String parameter = null;
25     private Parameter parameterRef = null;
26     private ScreenFlowManager screenManager;
27     private Debug debug = null;
28   
29   
30     /**
31      *  default constructor
32      */
33     public InsertTag() {
34       super();  // gets paramenter String (invokes setParameter())
35       debug = new Debug();
36       debug.setDebugginOn(true);
37       debug.setPreMessage("** InsertTag: ");
38     }
39   
40   
41     /**
42      *  Sets the parameter attribute of the InsertTag object
43      *
44      * @param parameter The new parameter value
45      */
46     public void setParameter(String parameter) {
47       this.parameter = parameter;
48     }
49   
50   
51     /**
52      *  Process beginning and body of tag <ib:insert parameter="..."
53      *
54      * @return SKIP_BODY
55      * @exception JspTagException
56      */
57     public int doStartTag() throws JspTagException {
58       try {
59         pageContext.getOut().flush();
60       }
61       catch(Exception e) {
62         // do nothing
63       }
64   
65       // load the ScreenFlowManager
66       try {
67         screenManager
68           = (ScreenFlowManager)pageContext.getServletContext()
69           .getAttribute(WebKeys.ScreenManagerKey);
70       }
71       catch(NullPointerException e) {
72         throw new JspTagException
73           (LetterTemplateExceptionMessage.NOT_SCREENMANAGER + e.getMessage());
74       }
75       if((screenManager != null) && (parameter != null)) {
76         // gets parameterRef: Parameter structure associated to parameter string
77         // of output screen;
78         // name of output screen is obtained from a web session variable;
79         // structure of all screens and its parameters are maintained
80         // by ScreenFlowManager javaBean
81         parameterRef
82           = (Parameter)screenManager.getParameter(parameter, pageContext.getSession());
83       }
84       else {
85         debug.println("InsertTag: screenManager is null");
86       }
87       if(parameterRef != null) {
88         directInclude = parameterRef.isDirect();
89       }
90       return SKIP_BODY;
91     }
92   
93   
94   
95     /**
96      *  Process end  of tag <ib:insert parameter="..."
97      *
98      * @return EVAL_PAGE
99      * @exception JspTagException
100     */
101    public int doEndTag() throws JspTagException {
102      try {
103        if(directInclude && parameterRef != null) {
104          // case "HtmlTitle" parameter: string value of parameterRef
105          // is directly included
106          pageContext.getOut().print(parameterRef.getValue());
107  
108        }
109        else if(parameterRef != null) {
110          // case  "HtmlBody", "HtmlBanner", ... parameters:
111          // associated JSP is included
112          if(parameterRef.getValue() != null) {
113            debug.println("JSP to insert:" + parameterRef.getValue());
114  
115            pageContext.getRequest()
116              .getRequestDispatcher(parameterRef.getValue())
117              .include(pageContext.getRequest(), pageContext.getResponse());
118          }
119          else {
120            debug.println("parameterRef.getValue() is null");
121          }
122        }
123      }
124      catch(Exception ex) {
125        debug.println("InsertTag:doEndTag caught: " + ex);
126      }
127      return EVAL_PAGE;
128    }
129  
130  }
131  
132