1 package org.ashkelon; 2 7 8 import java.io.ByteArrayInputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.Serializable ; 12 import java.sql.Connection ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.SQLException ; 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.ParserConfigurationException ; 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 ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.xml.sax.SAXException ; 33 34 41 public class Author implements JDoc, Serializable 42 { 43 private String name; 44 private String email; 45 private List classes; 46 47 private static String SEQUENCE = "AUTHOR_SEQ"; 48 private static String TABLENAME = "AUTHOR"; 49 50 private int id; 51 private boolean idSet = false; 52 53 public Author(String name) 54 { 55 parseName(name); 56 setClasses(new ArrayList ()); 57 } 58 59 63 private void parseName(String data) 64 { 65 String name = data; 66 String email = ""; 67 try 68 { 69 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 70 InputStream is = new ByteArrayInputStream (data.getBytes()); 71 Document d = builder.parse(is); 72 Element root = d.getDocumentElement(); 73 NamedNodeMap 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 pconfex) { } 83 catch (SAXException saxex) { } 84 catch (IOException ioex) { } 85 86 setName(name); 87 setEmail(email); 88 } 89 90 private void parseNameAlternative() 91 { 92 } 97 98 public void store(Connection conn) throws SQLException 99 { 100 if (exists(conn)) 101 return; 102 Map fieldInfo = new HashMap (5); 103 fieldInfo.put("ID", new Integer (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 conn) throws SQLException 111 { 112 if (!idSet) 113 { 114 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 conn) throws SQLException 134 { 135 Map constraints = new HashMap (); 136 constraints.put("NAME", getName()); 137 Object obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints); 138 if (obj == null) 139 return false; 140 141 if (!idSet) 142 { 143 id = ((Number ) obj).intValue(); 144 idSet = true; 145 } 146 return true; 147 } 148 149 public String getName() { return name; } 151 public void setName(String name) { this.name = name; } 152 153 public String getEmail() { return email; } 154 public void setEmail(String email) { this.email = email; } 155 156 public void setClasses(List classes) { this.classes = classes; } 157 public List getClasses() { return classes; } 158 159 public void addClass(ClassType classtype) { classes.add(classtype); } 160 161 162 public String toString() 163 { 164 return getName(); 165 } 166 167 168 public void fetchClasses(Connection conn) throws SQLException 169 { 170 String sql = DBMgr.getInstance().getStatement("fetch_authorclasses"); 171 172 PreparedStatement pstmt = conn.prepareStatement(sql); 173 pstmt.setInt(1, getId()); 174 ResultSet 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 conn, int id) throws SQLException 189 { 190 String sql = DBMgr.getInstance().getStatement("fetchauthor"); 191 192 PreparedStatement pstmt = conn.prepareStatement(sql); 193 pstmt.setInt(1, id); 194 ResultSet 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; } public String getStyle() { return "author"; } 228 229 } 230 | Popular Tags |