1 package org.ashkelon; 2 3 import java.io.*; 4 import java.net.URL ; 5 import java.sql.Connection ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import org.ashkelon.db.DBMgr; 17 import org.ashkelon.db.DBUtils; 18 import org.ashkelon.db.PKManager; 19 import org.ashkelon.util.Logger; 20 import org.ashkelon.util.StringUtils; 21 import org.exolab.castor.mapping.Mapping; 22 import org.exolab.castor.xml.MarshalException; 23 import org.exolab.castor.xml.Unmarshaller; 24 import org.exolab.castor.xml.ValidationException; 25 26 31 public class API implements JDoc, Serializable 32 { 33 private String name; 34 private String summaryDescription; 35 private String description; 36 private String publisher; 37 private String downloadURL; 38 private java.util.Date releaseDate; 39 private String version; 40 private Collection packagenames; 41 42 private List packages; 43 44 private static String SEQUENCE = "API_SEQ"; 45 private static String TABLENAME = "API"; 46 47 private int id; 48 private boolean idSet = false; 49 50 private transient Logger log; 51 private Unmarshaller ums = null; 52 53 54 public API() 55 { 56 log = Logger.getInstance(); 57 setPackagenames(new ArrayList ()); 58 setPackages(new ArrayList ()); 59 60 try 61 { 62 ClassLoader loader = this.getClass().getClassLoader(); 63 URL resource = loader.getResource("org/ashkelon/apimapping.xml"); 64 Mapping mapping = new Mapping(); 65 mapping.loadMapping(resource); 66 67 ums = new Unmarshaller(API.class); 68 ums.setMapping(mapping); 69 } 70 catch (Exception ex) 71 { 72 System.err.println("exception: "+ex.getMessage()); 73 ex.printStackTrace(); 74 } 75 } 76 77 public API(String name) 78 { 79 this(); 80 setName(name); 81 } 82 83 public API load(String filename, String sourcepath) 84 throws FileNotFoundException, MarshalException, ValidationException 85 { 86 try 87 { 88 return load(new FileReader(filename)); 89 } 90 catch (MarshalException ex) 91 { 92 System.out.println("let's see if file matches maven pom format.."); 94 return MavenPOMAdapter.read(new File(filename), sourcepath); 95 } 96 } 97 98 public API load(Reader reader) throws MarshalException, ValidationException 99 { 100 return (API) ums.unmarshal(reader); 101 } 102 103 private API readMavenPOM(Reader reader) throws MarshalException 104 { 105 return null; 106 } 107 108 public int getId(Connection conn) throws SQLException 109 { 110 if (!idSet) 111 setId(PKManager.getInstance().nextVal(SEQUENCE)); 112 return id; 113 } 114 115 public int getId() 116 { 117 return id; 118 } 119 120 public void setId(int id) 121 { 122 this.id = id; 123 idSet = true; 124 } 125 126 public void store(Connection conn) throws SQLException 127 { 128 String prefix = log.getPrefix(); 129 log.setPrefix("API"); 130 if (exists(conn)) 131 { 132 log.traceln("Skipping API " + getName() + " (already in repository)"); 133 return; 134 } 135 136 Map fieldInfo = new HashMap (5); 137 fieldInfo.put("ID", new Integer (getId(conn))); 138 fieldInfo.put("NAME", StringUtils.truncate(name, 60)); 139 fieldInfo.put("SUMMARYDESCRIPTION", StringUtils.truncate(summaryDescription, 250)); 140 fieldInfo.put("PUBLISHER", StringUtils.truncate(publisher, 150)); 141 fieldInfo.put("DOWNLOAD_URL", StringUtils.truncate(downloadURL, 150)); 142 fieldInfo.put("RELEASE_DATE", new java.sql.Date (releaseDate.getTime())); 143 fieldInfo.put("VERSION", StringUtils.truncate(version, 30)); 144 fieldInfo.put("DESCRIPTION", description); 145 146 try 147 { 148 DBUtils.insert(conn, TABLENAME, fieldInfo); 149 } 150 catch (SQLException ex) 151 { 152 fieldInfo.put("DESCRIPTION", StringUtils.truncate(description, 3600)); 153 DBUtils.insert(conn, TABLENAME, fieldInfo); 154 } 155 156 157 Iterator itr = getPackagenames().iterator(); 158 String packagename = null; 159 String sql = "update PACKAGE set api_id=? where name=?"; 160 PreparedStatement pstmt = conn.prepareStatement(sql); 161 pstmt.setInt(1, getId()); 162 while (itr.hasNext()) 163 { 164 packagename = (String ) itr.next(); 165 pstmt.setString(2, packagename); 166 pstmt.executeUpdate(); 167 } 168 pstmt.close(); 169 log.setPrefix(prefix); 170 } 171 172 public boolean exists(Connection conn) throws SQLException 173 { 174 Map constraints = new HashMap (); 175 constraints.put("NAME", getName()); 176 constraints.put("VERSION", getVersion()); 177 Object obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints); 178 if (obj == null) 179 return false; 180 181 if (!idSet) 182 { 183 id = ((Number ) obj).intValue(); 184 idSet = true; 185 } 186 return true; 187 } 188 189 public boolean delete(Connection conn) throws SQLException 190 { 191 Iterator itr = packagenames.iterator(); 192 while (itr.hasNext()) 193 { 194 JPackage.delete(conn, (String ) itr.next()); 195 } 196 197 HashMap constraint = new HashMap (); 199 constraint.put("NAME", name); 200 DBUtils.delete(conn, TABLENAME, constraint); 201 202 return true; 203 } 204 205 206 207 208 public String getName() { return name; } 210 public void setName(String name) { this.name = name; } 211 212 public String getSummaryDescription() { return summaryDescription; } 213 public void setSummaryDescription(String summaryDescription) { this.summaryDescription = summaryDescription; } 214 215 public String getDescription() { return description; } 216 public void setDescription(String description) { this.description = description; } 217 218 public String getPublisher() { return publisher; } 219 public void setPublisher(String publisher) { this.publisher = publisher; } 220 221 public String getDownloadURL() { return downloadURL; } 222 public void setDownloadURL(String downloadURL) { this.downloadURL = downloadURL; } 223 224 public java.util.Date getReleaseDate() { return releaseDate; } 225 public void setReleaseDate(java.util.Date releaseDate) { this.releaseDate = releaseDate; } 226 227 public String getVersion() { return version; } 228 public void setVersion(String version) { this.version = version; } 229 230 public Collection getPackagenames() { return packagenames; } 231 public void setPackagenames(Collection packagenames) { this.packagenames = packagenames; } 232 233 public List getPackages() { return packages; } 234 public void setPackages(List packages) { this.packages = packages; } 235 236 public void addPackage(JPackage pkg) 237 { 238 packages.add(pkg); 239 } 240 public void addPackagename(String packagename) 241 { 242 packagenames.add(packagename); 243 } 244 245 public String toString() 246 { 247 return getName() + " v" + getVersion(); 248 } 249 250 public String longToString() 251 { 252 StringBuffer text = new StringBuffer (toString()); 253 text.append("; Packages: "); 254 Iterator itr = packagenames.iterator(); 255 if (itr.hasNext()) 256 text.append((String ) itr.next()); 257 while (itr.hasNext()) 258 { 259 text.append(", ").append((String ) itr.next()); 260 } 261 return text.toString(); 262 } 263 264 public static API makeAPIFor(Connection conn, int apiId) throws SQLException 265 { 266 String sql = DBMgr.getInstance().getStatement("makeapi"); 267 268 PreparedStatement pstmt = conn.prepareStatement(sql); 269 pstmt.setInt(1, apiId); 270 ResultSet rset = pstmt.executeQuery(); 271 272 if (!rset.next()) 273 return null; 274 275 API api = new API(rset.getString(2)); 276 api.setId(rset.getInt(1)); 277 api.setSummaryDescription(rset.getString(3)); 278 api.setDescription(rset.getString(4)); 279 api.setPublisher(rset.getString(5)); 280 api.setDownloadURL(rset.getString(6)); 281 api.setReleaseDate(rset.getDate(7)); 282 api.setVersion(rset.getString(8)); 283 284 rset.close(); 285 pstmt.close(); 286 287 api.getPackageInfo(conn); 288 289 return api; 290 } 291 292 public void getPackageInfo(Connection conn) throws SQLException 293 { 294 String sql = DBMgr.getInstance().getStatement("packageinfo"); 295 301 302 PreparedStatement pstmt = conn.prepareStatement(sql); 303 pstmt.setInt(1, getId()); 304 ResultSet rset = pstmt.executeQuery(); 305 306 JPackage pkg; 307 while (rset.next()) 308 { 309 pkg = new JPackage(rset.getString(2)); 310 pkg.setId(rset.getInt(1)); 311 DocInfo doc = new DocInfo(); 312 doc.setSummaryDescription(rset.getString(3)); 313 pkg.setDoc(doc); 314 315 addPackage(pkg); 316 } 317 318 rset.close(); 319 pstmt.close(); 320 } 321 322 public static String getTableName() { return TABLENAME; } 323 324 public DocInfo getDoc() { return null; } public String getStyle() { return "api"; } 327 } 328 | Popular Tags |