KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > db > Statistics


1
2 package de.jwi.jgallery.db;
3
4 /*
5  * jGallery - Java web application to display image galleries
6  *
7  * Copyright (C) 2004 Juergen Weber
8  *
9  * This file is part of jGallery.
10  *
11  * jGallery is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * jGallery is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with jGallery; if not, write to the Free
20  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston
21  */

22
23 import java.sql.Connection JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.text.NumberFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35
36 /**
37  * @author Jürgen Weber Source file created on 08.08.2004
38  *
39  */

40 public class Statistics
41 {
42     private static NumberFormat JavaDoc numberInstance = NumberFormat.getNumberInstance();
43     static
44     {
45         numberInstance.setMaximumFractionDigits(1);
46     }
47     
48     public class FolderInfo
49     {
50         
51         
52         public String JavaDoc toString()
53         {
54
55             return name + " " + hits + " " + max + " " + avg;
56         }
57
58         private String JavaDoc name;
59         
60         private String JavaDoc id;
61
62         private String JavaDoc hits;
63
64         private String JavaDoc max;
65
66         private String JavaDoc min;
67
68         private String JavaDoc avg;
69         
70         private String JavaDoc baseUrl;
71
72         private String JavaDoc servletURL;
73         
74         public FolderInfo(String JavaDoc name, String JavaDoc id, String JavaDoc hits, String JavaDoc max, String JavaDoc min,
75                 String JavaDoc avg, String JavaDoc baseUrl, String JavaDoc servletURL)
76         {
77             this.name = name;
78             this.id = id;
79             this.hits = hits;
80             this.max = (max != null) ? max : "0";
81             this.min = (min != null) ? min : "0";
82             this.avg = (avg != null) ? avg : "0";
83             
84             
85             
86             float f = Float.parseFloat(this.avg);
87             this.avg = numberInstance.format(f);
88             
89             this.baseUrl = baseUrl;
90             this.servletURL = servletURL;
91         }
92
93
94         public String JavaDoc getURL()
95         {
96             return baseUrl + name + "index.html?nocount=true";
97         }
98         
99         public String JavaDoc getDetailsTO()
100         {
101             return servletURL + "/to/"+id;
102         }
103
104         public String JavaDoc getDetailsTN()
105         {
106             return servletURL + "/tn/"+id;
107         }
108
109         
110         public String JavaDoc getAvg()
111         {
112             return avg;
113         }
114
115         public String JavaDoc getName()
116         {
117             return name;
118         }
119
120         public String JavaDoc getId()
121         {
122             return id;
123         }
124
125         public String JavaDoc getHits()
126         {
127             return hits;
128         }
129
130
131         public String JavaDoc getMax()
132         {
133             return max;
134         }
135
136         public String JavaDoc getMin()
137         {
138             return min;
139         }
140
141     }
142     
143     public class ImageInfo
144     {
145         private String JavaDoc name;
146         private String JavaDoc hits;
147         private String JavaDoc url;
148         public ImageInfo(String JavaDoc name, String JavaDoc hits, String JavaDoc url)
149         {
150             this.name = name;
151             this.hits = hits;
152             this.url = url;
153         }
154         public String JavaDoc getHits()
155         {
156             return hits;
157         }
158         public String JavaDoc getName()
159         {
160             return name;
161         }
162         public String JavaDoc getUrl()
163         {
164             return url;
165         }
166     }
167
168     private DataSource JavaDoc dataSource;
169
170     public Statistics(DataSource JavaDoc dataSource)
171     {
172         this.dataSource = dataSource;
173     }
174
175     public List JavaDoc getStatistics(HttpServletRequest JavaDoc request, String JavaDoc self) throws SQLException JavaDoc
176     {
177         String JavaDoc query;
178         Connection JavaDoc conn = dataSource.getConnection();
179         int counter = 0;
180
181         List JavaDoc l = new ArrayList JavaDoc();
182         FolderInfo fi;
183
184         if (conn != null)
185         {
186             Statement JavaDoc stmt = conn.createStatement();
187
188             query = "select folder, f.id, f.hits as folderhits, " +
189                     "max(i.hits) as maxhits, min(i.hits) as minhits, " +
190                     "avg(i.hits) as avghits " +
191                     "from folders f left join images i on (f.id = i.folderid) " +
192                     "group by f.id order by f.hits desc;";
193             
194             ResultSet JavaDoc rs = stmt.executeQuery(query);
195
196             String JavaDoc baseUrl = request.getContextPath();
197                 
198             while (rs.next())
199             {
200                 fi = new FolderInfo(rs.getString("folder"), rs.getString("id"),
201                         rs.getString("folderhits"), rs.getString("maxhits"), rs
202                         .getString("minhits"), rs.getString("avghits"),baseUrl,self);
203                 
204                 l.add(fi);
205             }
206
207             conn.close();
208         }
209
210         return l;
211     }
212
213     public Map JavaDoc getFolderStatistics(String JavaDoc id, HttpServletRequest JavaDoc request, String JavaDoc self) throws SQLException JavaDoc
214     {
215         String JavaDoc query;
216         Connection JavaDoc conn = dataSource.getConnection();
217         int counter = 0;
218
219         List JavaDoc l = new ArrayList JavaDoc();
220         Map JavaDoc h = new HashMap JavaDoc();
221         ImageInfo ii;
222
223         if (conn != null)
224         {
225             Statement JavaDoc stmt = conn.createStatement();
226
227             query = "select folder " +
228             "from folders " +
229             "where id='" + id +
230             "';";
231     
232             ResultSet JavaDoc rs = stmt.executeQuery(query);
233
234             String JavaDoc folder = null;
235             if (rs.next())
236             {
237                 folder = rs.getString("folder");
238             }
239             
240             h.put("folder",folder);
241             
242             query = "select image as name, hits " +
243                     "from images " +
244                     "where folderid='" + id +
245                     "' order by hits desc;";
246             
247             rs = stmt.executeQuery(query);
248
249             String JavaDoc baseUrl = request.getContextPath();
250                 
251             while (rs.next())
252             {
253                 String JavaDoc name = rs.getString("name");
254                 int p=-1;
255                 if ((p=name.indexOf('.'))>-1)
256                 {
257                     name = name.substring(0,p);
258                 }
259                 ii = new ImageInfo(name, rs.getString("hits"),baseUrl+folder+name+".html?nocount=true");
260                 
261                 l.add(ii);
262             }
263
264             conn.close();
265         }
266
267         h.put("images",l);
268         
269         return h;
270     }
271
272 }
Popular Tags