KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > Author


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

7
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.sql.Connection JavaDoc;
13 import java.sql.PreparedStatement JavaDoc;
14 import java.sql.ResultSet JavaDoc;
15 import java.sql.SQLException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24
25 import org.ashkelon.db.DBMgr;
26 import org.ashkelon.db.DBUtils;
27 import org.ashkelon.db.PKManager;
28 import org.ashkelon.util.StringUtils;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * Part of Persistable javadoc object model known as 'ashkelon'
36  * An author represents a java class or interface author
37  *
38  * @author Eitan Suez
39  * @version 2.0
40  */

41 public class Author implements JDoc, Serializable JavaDoc
42 {
43    private String JavaDoc name;
44    private String JavaDoc email;
45    private List JavaDoc classes;
46
47    private static String JavaDoc SEQUENCE = "AUTHOR_SEQ";
48    private static String JavaDoc TABLENAME = "AUTHOR";
49
50    private int id;
51    private boolean idSet = false;
52    
53    public Author(String JavaDoc name)
54    {
55       parseName(name);
56       setClasses(new ArrayList JavaDoc());
57    }
58
59    /**
60     * add logic to properly parse out an <a href..> author tag into a name and an email
61     * address
62     */

63    private void parseName(String JavaDoc data)
64    {
65      String JavaDoc name = data;
66      String JavaDoc email = "";
67      try
68      {
69        DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
70        InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(data.getBytes());
71        Document JavaDoc d = builder.parse(is);
72        Element JavaDoc root = d.getDocumentElement();
73        NamedNodeMap JavaDoc attr = root.getAttributes();
74        for (int i=0; i<attr.getLength(); i++)
75        {
76          email = attr.item(i).getNodeValue();
77          int idx = email.indexOf("mailto:");
78          email = email.substring(idx+7);
79        }
80        name = root.getFirstChild().getNodeValue();
81      }
82      catch (ParserConfigurationException JavaDoc pconfex) { }
83      catch (SAXException JavaDoc saxex) { }
84      catch (IOException JavaDoc ioex) { }
85
86     setName(name);
87     setEmail(email);
88    }
89    
90    private void parseNameAlternative()
91    {
92     // regular expression:
93
// to match email address: (\w+\@\w+\.\w+)
94
// to match caption: >((\w+\s*)+)<
95
// tbd.
96
}
97    
98    public void store(Connection JavaDoc conn) throws SQLException JavaDoc
99    {
100       if (exists(conn))
101          return;
102       Map JavaDoc fieldInfo = new HashMap JavaDoc(5);
103       fieldInfo.put("ID", new Integer JavaDoc(getId(conn)));
104       fieldInfo.put("NAME", StringUtils.truncate(getName(), 120));
105       fieldInfo.put("EMAIL", StringUtils.truncate(getEmail(), 120));
106       DBUtils.insert(conn, TABLENAME, fieldInfo);
107       conn.commit();
108    }
109
110    public int getId(Connection JavaDoc conn) throws SQLException JavaDoc
111    {
112       if (!idSet)
113       {
114          //id = DBUtils.getNextVal(conn, SEQUENCE);
115
id = PKManager.getInstance().nextVal(SEQUENCE);
116          idSet = true;
117       }
118       return id;
119    }
120
121    public int getId()
122    {
123       return id;
124    }
125    
126    public void setId(int id)
127    {
128       this.id = id;
129       idSet = true;
130    }
131    
132    
133    private boolean exists(Connection JavaDoc conn) throws SQLException JavaDoc
134    {
135       Map JavaDoc constraints = new HashMap JavaDoc();
136       constraints.put("NAME", getName());
137       Object JavaDoc obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints);
138       if (obj == null)
139          return false;
140       
141       if (!idSet)
142       {
143          id = ((Number JavaDoc) obj).intValue();
144          idSet = true;
145       }
146       return true;
147    }
148    
149    // accessors
150
public String JavaDoc getName() { return name; }
151    public void setName(String JavaDoc name) { this.name = name; }
152    
153    public String JavaDoc getEmail() { return email; }
154    public void setEmail(String JavaDoc email) { this.email = email; }
155
156    public void setClasses(List JavaDoc classes) { this.classes = classes; }
157    public List JavaDoc getClasses() { return classes; }
158    
159    public void addClass(ClassType classtype) { classes.add(classtype); }
160    
161    
162    public String JavaDoc toString()
163    {
164       return getName();
165    }
166    
167    
168    public void fetchClasses(Connection JavaDoc conn) throws SQLException JavaDoc
169    {
170       String JavaDoc sql = DBMgr.getInstance().getStatement("fetch_authorclasses");
171       
172       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
173       pstmt.setInt(1, getId());
174       ResultSet JavaDoc rset = pstmt.executeQuery();
175       
176       ClassType ct;
177       while (rset.next())
178       {
179          ct = new ClassType(rset.getString(2));
180          ct.setId(rset.getInt(1));
181          addClass(ct);
182       }
183       
184       rset.close();
185       pstmt.close();
186    }
187    
188    public static Author fetchAuthor(Connection JavaDoc conn, int id) throws SQLException JavaDoc
189    {
190       String JavaDoc sql = DBMgr.getInstance().getStatement("fetchauthor");
191       
192       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
193       pstmt.setInt(1, id);
194       ResultSet JavaDoc rset = pstmt.executeQuery();
195       
196       ClassType ct;
197       Author author = null;
198       
199       if (rset.next())
200       {
201          author = new Author(rset.getString(1));
202          author.setId(id);
203          author.setEmail(rset.getString(5));
204          
205          ct = new ClassType(rset.getString(3));
206          ct.setId(rset.getInt(2));
207          ct.setClassType(rset.getInt(4));
208          author.addClass(ct);
209       }
210       
211       while (rset.next())
212       {
213          ct = new ClassType(rset.getString(3));
214          ct.setId(rset.getInt(2));
215          ct.setClassType(rset.getInt(4));
216          author.addClass(ct);
217       }
218       
219       rset.close();
220       pstmt.close();
221       
222       return author;
223    }
224    
225    public DocInfo getDoc() { return null; } // this is crummy. TODO: fix
226
public String JavaDoc getStyle() { return "author"; } // maybe getstyle should have a separate interface
227

228    
229 }
230
Popular Tags