KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > API


1 package org.ashkelon;
2
3 import java.io.*;
4 import java.net.URL JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
27  * Represents a Java API (a collection of related packages)
28  *
29  * @author Eitan Suez
30  */

31 public class API implements JDoc, Serializable
32 {
33     private String JavaDoc name;
34     private String JavaDoc summaryDescription;
35     private String JavaDoc description;
36     private String JavaDoc publisher;
37     private String JavaDoc downloadURL;
38     private java.util.Date JavaDoc releaseDate;
39     private String JavaDoc version;
40     private Collection JavaDoc packagenames;
41     
42     private List JavaDoc packages;
43
44     private static String JavaDoc SEQUENCE = "API_SEQ";
45     private static String JavaDoc 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 JavaDoc());
58        setPackages(new ArrayList JavaDoc());
59
60        try
61        {
62           ClassLoader JavaDoc loader = this.getClass().getClassLoader();
63           URL JavaDoc 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 JavaDoc ex)
71        {
72            System.err.println("exception: "+ex.getMessage());
73            ex.printStackTrace();
74        }
75     }
76     
77     public API(String JavaDoc name)
78     {
79        this();
80        setName(name);
81     }
82     
83     public API load(String JavaDoc filename, String JavaDoc sourcepath)
84       throws FileNotFoundException, MarshalException, ValidationException
85     {
86        try
87        {
88           return load(new FileReader(filename));
89        }
90        catch (MarshalException ex)
91        {
92           // see if xml file matches maven POM format..
93
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 JavaDoc conn) throws SQLException JavaDoc
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 JavaDoc conn) throws SQLException JavaDoc
127    {
128       String JavaDoc 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 JavaDoc fieldInfo = new HashMap JavaDoc(5);
137       fieldInfo.put("ID", new Integer JavaDoc(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 JavaDoc(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 JavaDoc ex)
151       {
152          fieldInfo.put("DESCRIPTION", StringUtils.truncate(description, 3600));
153          DBUtils.insert(conn, TABLENAME, fieldInfo);
154       }
155
156
157       Iterator JavaDoc itr = getPackagenames().iterator();
158       String JavaDoc packagename = null;
159       String JavaDoc sql = "update PACKAGE set api_id=? where name=?";
160       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
161       pstmt.setInt(1, getId());
162       while (itr.hasNext())
163       {
164          packagename = (String JavaDoc) 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 JavaDoc conn) throws SQLException JavaDoc
173    {
174       Map JavaDoc constraints = new HashMap JavaDoc();
175       constraints.put("NAME", getName());
176       constraints.put("VERSION", getVersion());
177       Object JavaDoc obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints);
178       if (obj == null)
179          return false;
180       
181       if (!idSet)
182       {
183          id = ((Number JavaDoc) obj).intValue();
184          idSet = true;
185       }
186       return true;
187    }
188    
189    public boolean delete(Connection JavaDoc conn) throws SQLException JavaDoc
190    {
191       Iterator JavaDoc itr = packagenames.iterator();
192       while (itr.hasNext())
193       {
194          JPackage.delete(conn, (String JavaDoc) itr.next());
195       }
196
197       // delete self
198
HashMap JavaDoc constraint = new HashMap JavaDoc();
199       constraint.put("NAME", name);
200       DBUtils.delete(conn, TABLENAME, constraint);
201
202       return true;
203    }
204
205    
206    
207    
208     // accessors
209
public String JavaDoc getName() { return name; }
210     public void setName(String JavaDoc name) { this.name = name; }
211
212     public String JavaDoc getSummaryDescription() { return summaryDescription; }
213     public void setSummaryDescription(String JavaDoc summaryDescription) { this.summaryDescription = summaryDescription; }
214
215     public String JavaDoc getDescription() { return description; }
216     public void setDescription(String JavaDoc description) { this.description = description; }
217
218     public String JavaDoc getPublisher() { return publisher; }
219     public void setPublisher(String JavaDoc publisher) { this.publisher = publisher; }
220
221     public String JavaDoc getDownloadURL() { return downloadURL; }
222     public void setDownloadURL(String JavaDoc downloadURL) { this.downloadURL = downloadURL; }
223
224     public java.util.Date JavaDoc getReleaseDate() { return releaseDate; }
225     public void setReleaseDate(java.util.Date JavaDoc releaseDate) { this.releaseDate = releaseDate; }
226
227     public String JavaDoc getVersion() { return version; }
228     public void setVersion(String JavaDoc version) { this.version = version; }
229
230     public Collection JavaDoc getPackagenames() { return packagenames; }
231     public void setPackagenames(Collection JavaDoc packagenames) { this.packagenames = packagenames; }
232     
233     public List JavaDoc getPackages() { return packages; }
234     public void setPackages(List JavaDoc packages) { this.packages = packages; }
235     
236     public void addPackage(JPackage pkg)
237     {
238        packages.add(pkg);
239     }
240     public void addPackagename(String JavaDoc packagename)
241     {
242        packagenames.add(packagename);
243     }
244     
245     public String JavaDoc toString()
246     {
247        return getName() + " v" + getVersion();
248     }
249     
250     public String JavaDoc longToString()
251     {
252        StringBuffer JavaDoc text = new StringBuffer JavaDoc(toString());
253        text.append("; Packages: ");
254        Iterator JavaDoc itr = packagenames.iterator();
255        if (itr.hasNext())
256           text.append((String JavaDoc) itr.next());
257        while (itr.hasNext())
258        {
259           text.append(", ").append((String JavaDoc) itr.next());
260        }
261        return text.toString();
262     }
263     
264    public static API makeAPIFor(Connection JavaDoc conn, int apiId) throws SQLException JavaDoc
265    {
266       String JavaDoc sql = DBMgr.getInstance().getStatement("makeapi");
267
268       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
269       pstmt.setInt(1, apiId);
270       ResultSet JavaDoc 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 JavaDoc conn) throws SQLException JavaDoc
293    {
294       String JavaDoc sql = DBMgr.getInstance().getStatement("packageinfo");
295       /*
296        select p.id, p.name, d.summarydescription
297         from package p, doc d
298         where p.api_id=? and p.docid=d.id
299         order by p.name
300        */

301       
302       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
303       pstmt.setInt(1, getId());
304       ResultSet JavaDoc 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 JavaDoc getTableName() { return TABLENAME; }
323    
324    public DocInfo getDoc() { return null; } // this is crummy. TODO: fix
325
public String JavaDoc getStyle() { return "api"; } // maybe getstyle should have a separate interface
326

327 }
328
Popular Tags