KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 public class MembersPage extends Page
17 {
18    public MembersPage()
19    {
20       super();
21    }
22    
23    public String JavaDoc handleRequest() throws SQLException
24    {
25       boolean simpleSearch = (Boolean.valueOf((String JavaDoc) ServletUtils.getRequestParam(request, "simple"))).booleanValue();
26
27       List found = null;
28       
29       if (simpleSearch)
30       {
31          String JavaDoc searchField = ServletUtils.getRequestParam(request, "searchField");
32          if (StringUtils.isBlank(searchField))
33             return null;
34       
35          found = getSimpleMembers(searchField);
36       }
37       else
38       {
39          found = getComplexMembers();
40       }
41
42       if (found.size()==1 || (found.size()>1 && membersSame(found)) )
43       {
44          Member m = (Member) found.get(0);
45          int id = m.getId(null);
46          request.setAttribute("member_id", new Integer JavaDoc(id));
47          return "member.main";
48          /*
49          String membertype = "method";
50          if (m instanceof ConstructorMember)
51          {
52             membertype = "constructor";
53          }
54          else if (m instanceof FieldMember)
55          {
56             membertype = "field";
57          }
58          String cmd = "member." + membertype;
59          return cmd;
60           */

61       }
62
63       request.setAttribute("display_results", new Boolean JavaDoc(true));
64       request.setAttribute("member_list", found);
65       return null;
66    }
67    
68    /*
69     * call only with members size > 1..
70     */

71    private boolean membersSame(List members)
72    {
73       if (members == null || members.size() <= 1) return true; // safeguard
74

75       String JavaDoc firstQName = ((Member) members.get(0)).getQualifiedName();
76       
77       for (int i=1; i<members.size(); i++)
78       {
79          Member mmb = (Member) members.get(i);
80          if (!firstQName.equals(mmb.getQualifiedName()))
81          {
82             return false;
83          }
84       }
85       return true;
86    }
87    
88    
89    public List getComplexMembers() throws SQLException
90    {
91       String JavaDoc selectors[] = request.getParameterValues("selector");
92       if (selectors == null || selectors.length == 0)
93          return null;
94
95       Map filters = new HashMap(10);
96       String JavaDoc value;
97       for (int i=0; i<selectors.length; i++)
98       {
99          if ("package_name".equals(selectors[i]) || "searchField".equals(selectors[i]))
100          {
101             value = ServletUtils.getRequestParam(request, selectors[i]);
102             value = value.replace('*', '%').toLowerCase();
103             filters.put(selectors[i], value);
104          }
105          else
106          {
107             filters.put(selectors[i], new Integer JavaDoc(ServletUtils.getIntParam(request, selectors[i])));
108          }
109       }
110
111       String JavaDoc sql =
112             " select " +
113             " m.id, m.qualifiedname, m.type, " +
114             " m.isstatic, m.isfinal, m.accessibility, m.modifier, " +
115             " meth.isabstract, meth.returntypeid, meth.returntypename, meth.returntypedimension, " +
116             " em.signature, " +
117             " d.summarydescription, d.since, d.deprecated " +
118             " from METHOD meth, MEMBER m, EXECMEMBER em, DOC d ";
119       
120       if (!DBMgr.getInstance().isOracle())
121       {
122          sql =
123             " select m.id, m.qualifiedname, m.type, " +
124             " m.isstatic, m.isfinal, m.accessibility, m.modifier, " +
125             " meth.isabstract, meth.returntypeid, meth.returntypename, meth.returntypedimension, " +
126             " em.signature, " +
127             " d.summarydescription, d.since, d.deprecated " +
128             " from METHOD meth right outer join MEMBER m on meth.id=m.id " +
129             " left outer join EXECMEMBER em on em.id=m.id, DOC d ";
130       }
131          
132       if (filters.get("package_name")!=null)
133          sql += ", CLASSTYPE c, PACKAGE p ";
134
135       sql += " where ";
136
137       List whereClause = new ArrayList(10);
138       if (filters.get("searchField")!=null)
139       {
140          String JavaDoc selectby = "m.qualifiedname";
141          if (!JDocUtil.isQualified((String JavaDoc) filters.get("searchField")))
142             selectby = "m.name";
143          whereClause.add("lower("+selectby+") like ?");
144       }
145       if (filters.get("member_type")!=null)
146          whereClause.add("m.type=?");
147
148       if (filters.get("static")!=null)
149          whereClause.add("m.isstatic=?");
150       if (filters.get("final")!=null)
151          whereClause.add("m.isfinal=?");
152
153       if (filters.get("deprecated")!=null)
154       {
155          if ( ((Integer JavaDoc)filters.get("deprecated")).intValue() == 1)
156             whereClause.add("d.deprecated != ''");
157          else
158             whereClause.add("d.deprecated = ''");
159       }
160
161       if (filters.get("abstract")!=null)
162          whereClause.add("meth.isabstract=?");
163       if (filters.get("synchronized")!=null)
164          whereClause.add("em.issynchronized=?");
165       if (filters.get("native")!=null)
166          whereClause.add("em.isnative=?");
167       if (filters.get("package_name")!=null)
168          whereClause.add("m.classid=c.id and p.id=c.packageid and lower(p.name) like ?");
169       whereClause.add("m.docid=d.id");
170
171       if (DBMgr.getInstance().isOracle()) // for oracle outer join
172
{
173          whereClause.add(" m.id=em.id (+) ");
174          whereClause.add(" m.id=meth.id (+) ");
175       }
176
177       sql += StringUtils.join(whereClause.toArray(), " and ") +
178              " order by m.qualifiedname ";
179
180       PreparedStatement pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
181             ResultSet.CONCUR_READ_ONLY);
182       pstmt.setFetchSize(FETCH_SIZE);
183       
184       int i=1;
185
186       if (filters.get("searchField")!=null)
187          pstmt.setString(i++, (String JavaDoc) filters.get("searchField"));
188       if (filters.get("member_type")!=null)
189          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("member_type")).intValue());
190       if (filters.get("static")!=null)
191          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("static")).intValue());
192       if (filters.get("final")!=null)
193          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("final")).intValue());
194       if (filters.get("abstract")!=null)
195          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("abstract")).intValue());
196       if (filters.get("synchronized")!=null)
197          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("synchronized")).intValue());
198       if (filters.get("native")!=null)
199          pstmt.setInt(i++, ((Integer JavaDoc) filters.get("native")).intValue());
200       if (filters.get("package_name")!=null)
201          pstmt.setString(i++, (String JavaDoc) filters.get("package_name"));
202
203       Logger.getInstance().debug("sql query for advanced search:\n\t"+sql);
204
205       ResultSet rset = pstmt.executeQuery();
206       int position = position(rset);
207
208       List found = new ArrayList();
209       Member m;
210
211       while (rset.next() && rset.getRow() <= (position + FETCH_SIZE))
212       {
213         m = new Member(rset.getString(2), rset.getInt(3));
214         m.setId(rset.getInt(1));
215         m.setDoc(new DocInfo(rset.getString(13), rset.getString(14), rset.getString(15)));
216         m.setStatic(rset.getBoolean(4));
217         m.setFinal(rset.getBoolean(5));
218         m.setAccessibility(rset.getInt(6));
219         m.setModifiers(rset.getString(7));
220         String JavaDoc signature = rset.getString(12);
221         if (!StringUtils.isBlank(signature))
222         {
223            ExecMember em = new ExecMember(m, signature);
224            String JavaDoc returntypename = rset.getString(10);
225            if (!StringUtils.isBlank(returntypename))
226            {
227                MethodMember method = new MethodMember(m, signature);
228                method.setAbstract(rset.getBoolean(8));
229                method.setReturnTypeName(returntypename);
230                method.setReturnTypeDimension(rset.getInt(11));
231                int returntypeid = rset.getInt(9);
232                if (returntypeid > 0)
233                {
234                   method.setReturnType(new ClassType(returntypename));
235                   method.getReturnType().setId(returntypeid);
236                }
237               found.add(method);
238            }
239            else
240            {
241               found.add(em);
242            }
243         }
244         else
245         {
246            found.add(m);
247         }
248
249       } // end while
250

251       rset.close();
252       return found;
253    }
254       
255    
256    public List getSimpleMembers(String JavaDoc searchField) throws SQLException
257    {
258       searchField = searchField.toLowerCase();
259       String JavaDoc selectby = "m.qualifiedname";
260       if (!JDocUtil.isQualified(searchField))
261       {
262          selectby = "m.name";
263       }
264       
265       Perl5Util util = new Perl5Util();
266       String JavaDoc pattern = "s/\\*/%/g";
267       searchField = util.substitute(pattern, searchField);
268       //searchField = searchField.replaceAll("\\*", "%");
269

270       
271       String JavaDoc sql =
272             " select m.id, m.qualifiedname, m.type, " +
273             " m.isstatic, m.isfinal, m.accessibility, m.modifier, " +
274             " meth.isabstract, meth.returntypeid, meth.returntypename, meth.returntypedimension, " +
275             " em.signature, " +
276             " d.summarydescription, d.since, d.deprecated " +
277             " from METHOD meth right outer join MEMBER m on meth.id=m.id " +
278             " left outer join EXECMEMBER em on em.id=m.id, DOC d " +
279             " where lower("+selectby+") like ? and m.docid=d.id " +
280             " order by m.qualifiedname ";
281       
282       if (DBMgr.getInstance().isOracle())
283       {
284          sql =
285          "select m.id, m.qualifiedname, m.type, " +
286          " m.isstatic, m.isfinal, m.accessibility, m.modifier, " +
287          " meth.isabstract, meth.returntypeid, meth.returntypename, meth.returntypedimension, " +
288          " em.signature, " +
289          " d.summarydescription, d.since, d.deprecated " +
290          " from MEMBER m, DOC d, METHOD meth, EXECMEMBER em " +
291          " where lower("+selectby+") like ? " +
292          " and m.docid = d.id " +
293          " and m.id = meth.id (+) " +
294          " and m.id = em.id (+) " +
295          " order by m.qualifiedname";
296       }
297       
298       PreparedStatement pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
299             ResultSet.CONCUR_READ_ONLY);
300       pstmt.setFetchSize(FETCH_SIZE);
301       
302       pstmt.setString(1, searchField);
303       ResultSet rset = pstmt.executeQuery();
304       int position = position(rset);
305
306       List found = new ArrayList();
307       Member m;
308       
309       while (rset.next() && rset.getRow() <= (position + FETCH_SIZE))
310       {
311         m = new Member(rset.getString(2), rset.getInt(3));
312         m.setId(rset.getInt(1));
313         m.setDoc(new DocInfo(rset.getString(13), rset.getString(14), rset.getString(15)));
314         m.setStatic(rset.getBoolean(4));
315         m.setFinal(rset.getBoolean(5));
316         m.setAccessibility(rset.getInt(6));
317         m.setModifiers(rset.getString(7));
318         String JavaDoc signature = rset.getString(12);
319         if (!StringUtils.isBlank(signature))
320         {
321            ExecMember em = new ExecMember(m, signature);
322            String JavaDoc returntypename = rset.getString(10);
323            if (!StringUtils.isBlank(returntypename))
324            {
325                MethodMember method = new MethodMember(m, signature);
326                method.setAbstract(rset.getBoolean(8));
327                method.setReturnTypeName(returntypename);
328                method.setReturnTypeDimension(rset.getInt(11));
329                int returntypeid = rset.getInt(9);
330                if (returntypeid > 0)
331                {
332                   method.setReturnType(new ClassType(returntypename));
333                   method.getReturnType().setId(returntypeid);
334                }
335               found.add(method);
336            }
337            else
338            {
339               found.add(em);
340            }
341         }
342         else
343         {
344            found.add(m);
345         }
346         
347       } // end while
348

349       rset.close();
350       return found;
351    }
352    
353    
354 }
355
Popular Tags