KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > DocInfo


1 package org.ashkelon;
2 /**
3  * Ashkelon
4  * Copyright UptoData Inc. 2001
5  * March 2001
6  */

7
8 import java.io.Serializable JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.ashkelon.db.DBUtils;
19 import org.ashkelon.db.PKManager;
20 import org.ashkelon.util.JDocUtil;
21 import org.ashkelon.util.Logger;
22 import org.ashkelon.util.StringUtils;
23
24 import com.sun.javadoc.ClassDoc;
25 import com.sun.javadoc.Doc;
26 import com.sun.javadoc.MemberDoc;
27 import com.sun.javadoc.PackageDoc;
28 import com.sun.javadoc.SeeTag;
29
30 /**
31  * Part of Persistable javadoc object model known as ashkelon
32  * Analog of com.sun.javadoc.Doc
33  *
34  * @author Eitan Suez
35  * @version 2.0
36  */

37 public class DocInfo implements Serializable JavaDoc
38 {
39    private String JavaDoc since;
40    private String JavaDoc deprecated;
41    private String JavaDoc summaryDescription;
42    private String JavaDoc description;
43    private int docType;
44    private List JavaDoc references;
45    
46    private int id;
47    private boolean idSet = false;
48
49    private static String JavaDoc SEQUENCE = "DOC_SEQ";
50    private static String JavaDoc TABLENAME = "DOC";
51
52    /** constants representing different primary types of doc elements */
53    public static final int PACKAGE_TYPE = 1;
54    /** constants representing different types of doc elements */
55    public static final int CLASS_TYPE = 2;
56    /** constants representing different types of doc elements */
57    public static final int MEMBER_TYPE = 3;
58    /** constants representing different types of doc elements */
59    public static final int EXECMEMBER_TYPE = 4;
60    
61    public static final String JavaDoc[] DOCTYPES = {"pkg", "cls", "member", "member"};
62    
63    
64    public DocInfo()
65    {
66       setSummaryDescription("");
67       setDescription("");
68       setSince("");
69       setDeprecated("");
70       setReferences(new ArrayList JavaDoc());
71    }
72    
73    public DocInfo(String JavaDoc summaryDescription, String JavaDoc since, String JavaDoc deprecated)
74    {
75       this(summaryDescription, since, deprecated, "");
76    }
77    
78    public DocInfo(String JavaDoc summaryDescription, String JavaDoc since, String JavaDoc deprecated, String JavaDoc description)
79    {
80       setSummaryDescription(summaryDescription);
81       setDescription(description);
82       setSince(since);
83       setDeprecated(deprecated);
84       setReferences(new ArrayList JavaDoc());
85    }
86    
87    public DocInfo(Doc doc)
88    {
89       this();
90       setSince(JDocUtil.getTagText(doc.tags("@since")));
91       setDeprecated(JDocUtil.resolveDescription(this, doc.tags("@deprecated")));
92       
93       addReferences(doc.seeTags());
94       setDocType(getDocType(doc));
95
96       setSummaryDescription(JDocUtil.resolveDescription(this, doc.firstSentenceTags()));
97       setDescription(JDocUtil.resolveDescription(this, doc.inlineTags()));
98       /*
99       setSummaryDescription(JDocUtil.getTagText(doc.firstSentenceTags()));
100       setDescription(doc.commentText());
101        */

102    }
103    
104    public void store(Connection JavaDoc conn) throws SQLException JavaDoc
105    {
106       Map JavaDoc fieldInfo = new HashMap JavaDoc(10);
107       fieldInfo.put("ID", new Integer JavaDoc(getId(conn)));
108       fieldInfo.put("DOC_TYPE", new Integer JavaDoc(getDocType()));
109
110       fieldInfo.put("SINCE", StringUtils.truncate(getSince(), 75));
111       fieldInfo.put("DEPRECATED", StringUtils.truncate(getDeprecated(), 100));
112       fieldInfo.put("SUMMARYDESCRIPTION", getSummaryDescription());
113       fieldInfo.put("DESCRIPTION", getDescription());
114       
115       try
116       {
117          DBUtils.insert(conn, TABLENAME, fieldInfo);
118       }
119       catch (SQLException JavaDoc ex)
120       {
121          fieldInfo.put("SUMMARYDESCRIPTION", StringUtils.truncate(getSummaryDescription(), 400));
122          fieldInfo.put("DESCRIPTION", StringUtils.truncate(getDescription(), 3600));
123          DBUtils.insert(conn, TABLENAME, fieldInfo);
124       }
125       
126       
127       for (int i=0; i<getReferences().size(); i++)
128       {
129          ((Reference) references.get(i)).store(conn);
130       }
131    }
132    
133    public static void delete(Connection JavaDoc conn, int docid) throws SQLException JavaDoc
134    {
135       HashMap JavaDoc constraint = new HashMap JavaDoc();
136       constraint.put("SOURCEDOC_ID", new Integer JavaDoc(docid));
137       DBUtils.delete(conn, "REFERENCE", constraint);
138       
139       constraint.clear();
140       constraint.put("id", new Integer JavaDoc(docid));
141       DBUtils.delete(conn, TABLENAME, constraint);
142    }
143    
144    public int getId(Connection JavaDoc conn) throws SQLException JavaDoc
145    {
146       if (!idSet)
147       {
148          //id = DBUtils.getNextVal(conn, SEQUENCE);
149
id = PKManager.getInstance().nextVal(SEQUENCE);
150          idSet = true;
151       }
152       return id;
153    }
154    
155    public int getId()
156    {
157       return id;
158    }
159    
160    public void setId(int id)
161    {
162       this.id = id;
163       idSet = true;
164    }
165    
166    // accessor methods
167
public String JavaDoc getSince() { return since; }
168    public void setSince(String JavaDoc since)
169    {
170       this.since = StringUtils.avoidNull(since);
171    }
172    
173    public String JavaDoc getCleanSince()
174    {
175       String JavaDoc since = getSince();
176       //Perl5Util perlutil = new Perl5Util();
177
//perlutil.replace(since, pattern);
178
int i = since.indexOf("1.");
179       if (i > -1)
180          return since.substring(i);
181       else
182          return since;
183    }
184    
185    
186    public String JavaDoc getDeprecated() { return deprecated; }
187    public void setDeprecated(String JavaDoc deprecated)
188    {
189       this.deprecated = StringUtils.avoidNull(deprecated);
190    }
191    
192    public String JavaDoc getSummaryDescription() { return summaryDescription; }
193    public void setSummaryDescription(String JavaDoc summaryDescription)
194    {
195       if (StringUtils.isBlank(summaryDescription))
196          this.summaryDescription = "No description available.";
197       else
198          this.summaryDescription = summaryDescription;
199    }
200    
201    public String JavaDoc getDescription() { return description; }
202    public void setDescription(String JavaDoc description)
203    {
204       if (StringUtils.isBlank(description))
205          this.description = "No description available.";
206       else
207          this.description = description;
208    }
209    
210    public int getDocType() { return docType; }
211    public void setDocType(int docType) { this.docType = docType; }
212
213    public String JavaDoc getDocTypeName()
214    {
215       return DOCTYPES[getDocType()-1];
216    }
217    
218    
219    public List JavaDoc getReferences() { return references; }
220    public void setReferences(List JavaDoc references) { this.references = references; }
221    
222    public void addReferences(SeeTag[] seetags)
223    {
224       Reference ref = null;
225       for (int i=0; i<seetags.length; i++)
226       {
227          ref = new Reference(this, seetags[i]);
228          references.add(ref);
229       }
230    }
231
232    /**
233     * @return the doc element type
234     */

235    public static int getDocType(Doc doc)
236    {
237       if(doc instanceof PackageDoc)
238       {
239          return PACKAGE_TYPE;
240       }
241       else if(doc instanceof ClassDoc)
242       {
243          return CLASS_TYPE;
244       }
245       else if(doc instanceof MemberDoc)
246       {
247          return MEMBER_TYPE;
248       }
249       else
250       {
251          // throw an exception
252
return JDocUtil.UNKNOWN_TYPE;
253       }
254    }
255    
256    public boolean hasAnythingToShow()
257    {
258       return (!
259          (StringUtils.isBlank(getSince()) && StringUtils.isBlank(getDeprecated()) &&
260          getReferences().isEmpty())
261       );
262    }
263    
264    public void fetchRefs(Connection JavaDoc conn) throws SQLException JavaDoc
265    {
266       String JavaDoc sql =
267        " select r.label, r.refdoc_name, r.refdoc_type, r.refdoc_id " +
268        " from REFERENCE r " +
269        " where r.sourcedoc_id = ? " +
270        " order by r.label ";
271
272       Logger log = Logger.getInstance();
273       
274       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
275       pstmt.setInt(1, getId());
276       ResultSet JavaDoc rset = pstmt.executeQuery();
277       Reference ref;
278       
279       while (rset.next())
280       {
281          ref = new Reference(rset.getString(2));
282          ref.setLabel(rset.getString(1));
283          ref.setRefDocType(rset.getInt(3));
284          ref.getRefDoc().setId(rset.getInt(4));
285          //ref.setQualifiedSourceName(rset.getString(ref.getRefDocType()+8));
286
references.add(ref);
287          log.debug("added a reference to " + ref.getRefDocName());
288       }
289       
290       rset.close();
291       pstmt.close();
292    }
293    
294    public void fetchRefsOld(Connection JavaDoc conn) throws SQLException JavaDoc
295    {
296       String JavaDoc sql =
297         " select r.label, r.refdoc_name, r.refdoc_type, r.sourcedoc_id, " +
298         " p.id , c.id, m.id, e.id, " +
299         " p.name, c.qualifiedname, m.qualifiedname, e.fullyqualifiedname " +
300         " from REFERENCE r, EXECMEMBER e, MEMBER m, CLASSTYPE c, PACKAGE p " +
301         " where r.sourcedoc_id=? " +
302         " and r.refdoc_name=e.fullyqualifiedname (+) " +
303         " and r.refdoc_name=m.qualifiedname (+) " +
304         " and r.refdoc_name=c.qualifiedname (+) " +
305         " and r.refdoc_name=p.name (+) " +
306         " order by r.label ";
307
308       Logger log = Logger.getInstance();
309       
310       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
311       pstmt.setInt(1, getId());
312       ResultSet JavaDoc rset = pstmt.executeQuery();
313       Reference ref;
314       
315       while (rset.next())
316       {
317          ref = new Reference(rset.getString(2));
318          ref.setLabel(rset.getString(1));
319          ref.setRefDocType(rset.getInt(3));
320          ref.getRefDoc().setId(rset.getInt(ref.getRefDocType()+4));
321          ref.setQualifiedSourceName(rset.getString(ref.getRefDocType()+8));
322          references.add(ref);
323          log.debug("added a reference to " + ref.getRefDocName());
324       }
325       
326       rset.close();
327       pstmt.close();
328    }
329    
330    
331    public boolean isDeprecated()
332    {
333       return (getDeprecated().trim().length() > 0);
334    }
335    
336    public String JavaDoc toString() { return summaryDescription; }
337    
338    
339 }
340
Popular Tags