KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > pages > Page


1 package org.ashkelon.pages;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import org.ashkelon.*;
7 import org.ashkelon.util.*;
8 import org.ashkelon.db.*;
9
10 import java.sql.*;
11 import java.util.*;
12
13 /**
14  * @author Eitan Suez
15  */

16 public abstract class Page
17 {
18    Logger log;
19    Connection conn;
20    HttpServletRequest request;
21    ServletContext app;
22    HttpSession session;
23    
24    public static int FETCH_SIZE = 60;
25    
26    public Page()
27    {
28       log = Logger.getInstance();
29       log.setPrefix("Page");
30    }
31    
32    public Page(Connection conn, HttpServletRequest request)
33    {
34       this();
35       setConnection(conn);
36       setRequest(request);
37       session = request.getSession();
38    }
39    
40    /**
41     * handleRequest method can optionally return the name of a valid page
42     * which the flowcontroller will use to forward the request to
43     * instead of the default associated page name.
44     */

45    public abstract String JavaDoc handleRequest() throws Exception JavaDoc;
46    
47    public void setConnection(Connection conn)
48    {
49       this.conn = conn;
50    }
51    public void setRequest(HttpServletRequest request)
52    {
53       this.request = request;
54       this.session = request.getSession();
55    }
56    public void setApplication(ServletContext context)
57    {
58       this.app = context;
59    }
60    
61    protected void finalize() throws Throwable JavaDoc
62    {
63       DBMgr.getInstance().releaseConnection(conn);
64       conn = null;
65       request = null;
66       log.debug("page finalized");
67       log = null;
68    }
69  
70    
71    public static String JavaDoc printTree(TreeNode node, String JavaDoc pkgname)
72    {
73       Object JavaDoc o = node.getValue();
74       if (o==null) return "";
75       ClassType cls = null;
76       String JavaDoc name = "";
77       String JavaDoc indent = "";
78       int level = 0;
79       String JavaDoc cmd = "cls.main.do?cls_id=";
80       String JavaDoc parent = "";
81       String JavaDoc typename = "package";
82       String JavaDoc modifiers = "";
83       String JavaDoc descr;
84       boolean nohref = false;
85       if (o instanceof JPackage)
86       {
87          JPackage pkg = (JPackage) o;
88          name = pkg.getName();
89          descr = pkg.getSummaryDescription();
90          cmd = "pkg.main.do?pkg_id="+pkg.getId();
91       }
92       else
93       {
94          cls = (ClassType) o;
95          name = cls.getQualifiedName();
96          descr = cls.getSummaryDescription();
97          typename = cls.getClassTypeName();
98          modifiers = cls.getModifiers();
99          level = cls.getLevel();
100          indent = StringUtils.join(" ","",cls.getLevel()*3);
101          cmd += cls.getId();
102          parent = (level == 1) ? pkgname : cls.getSuperClassName();
103          if (cls.getId()==0)
104          {
105             nohref = true;
106          }
107       }
108       
109       descr = HtmlUtils.cleanAttributeText(descr);
110       
111       String JavaDoc output = indent + "<UL ID=\""+parent+".child\">\n";
112       
113       String JavaDoc collapsible = node.isEmpty() ? "CLASS=\"leafnode\"" : "CLASS=\"collapsible\"";
114       output += indent + " <LI "+collapsible+" CHILD=\""+name+".child\">\n";
115       if (nohref) // unknown or unavailable
116
{
117          output += indent + " <SPAN CLASS=\""+typename+" "+modifiers+"\">" + name + "</SPAN>\n";
118          output += indent + " </LI>\n";
119       }
120       else
121       {
122          output += indent + " <A HREF=\""+cmd+"\"><SPAN CLASS=\""+typename+" "+modifiers+"\" TITLE=\""+descr+"\">" + name + "</SPAN></A>\n";
123          output += indent + " </LI>\n";
124       }
125       
126       Map children = (Map) node.getChildren();
127       Collection values = children.values();
128       if (values != null)
129       {
130          Iterator itr = values.iterator();
131          TreeNode childNode;
132          while (itr.hasNext())
133          {
134             childNode = (TreeNode) itr.next();
135             if (childNode != null)
136                output += printTree(childNode, pkgname);
137          }
138       }
139       
140       output += indent + "</UL>\n";
141       return output;
142    }
143
144    protected int position(ResultSet rset) throws SQLException
145    {
146       rset.last();
147       int totalResults = rset.getRow();
148       request.setAttribute("total-results", new Integer JavaDoc(totalResults));
149
150       int position = ServletUtils.getIntParam(request, "cursor-position");
151       if (position == 0)
152          rset.beforeFirst();
153       else
154          rset.absolute(position);
155
156       if (position + FETCH_SIZE < totalResults)
157             request.setAttribute("next-cursor-position", new Integer JavaDoc(position
158                   + FETCH_SIZE));
159       if (position - FETCH_SIZE >= 0)
160             request.setAttribute("prev-cursor-position", new Integer JavaDoc(position
161                   - FETCH_SIZE));
162
163       return position;
164    }
165
166    
167    
168 }
169
170
Popular Tags