1 2 package de.jwi.jgallery.db; 3 4 22 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.text.NumberFormat ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.sql.DataSource ; 35 36 40 public class Statistics 41 { 42 private static NumberFormat numberInstance = NumberFormat.getNumberInstance(); 43 static 44 { 45 numberInstance.setMaximumFractionDigits(1); 46 } 47 48 public class FolderInfo 49 { 50 51 52 public String toString() 53 { 54 55 return name + " " + hits + " " + max + " " + avg; 56 } 57 58 private String name; 59 60 private String id; 61 62 private String hits; 63 64 private String max; 65 66 private String min; 67 68 private String avg; 69 70 private String baseUrl; 71 72 private String servletURL; 73 74 public FolderInfo(String name, String id, String hits, String max, String min, 75 String avg, String baseUrl, String 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 getURL() 95 { 96 return baseUrl + name + "index.html?nocount=true"; 97 } 98 99 public String getDetailsTO() 100 { 101 return servletURL + "/to/"+id; 102 } 103 104 public String getDetailsTN() 105 { 106 return servletURL + "/tn/"+id; 107 } 108 109 110 public String getAvg() 111 { 112 return avg; 113 } 114 115 public String getName() 116 { 117 return name; 118 } 119 120 public String getId() 121 { 122 return id; 123 } 124 125 public String getHits() 126 { 127 return hits; 128 } 129 130 131 public String getMax() 132 { 133 return max; 134 } 135 136 public String getMin() 137 { 138 return min; 139 } 140 141 } 142 143 public class ImageInfo 144 { 145 private String name; 146 private String hits; 147 private String url; 148 public ImageInfo(String name, String hits, String url) 149 { 150 this.name = name; 151 this.hits = hits; 152 this.url = url; 153 } 154 public String getHits() 155 { 156 return hits; 157 } 158 public String getName() 159 { 160 return name; 161 } 162 public String getUrl() 163 { 164 return url; 165 } 166 } 167 168 private DataSource dataSource; 169 170 public Statistics(DataSource dataSource) 171 { 172 this.dataSource = dataSource; 173 } 174 175 public List getStatistics(HttpServletRequest request, String self) throws SQLException 176 { 177 String query; 178 Connection conn = dataSource.getConnection(); 179 int counter = 0; 180 181 List l = new ArrayList (); 182 FolderInfo fi; 183 184 if (conn != null) 185 { 186 Statement 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 rs = stmt.executeQuery(query); 195 196 String 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 getFolderStatistics(String id, HttpServletRequest request, String self) throws SQLException 214 { 215 String query; 216 Connection conn = dataSource.getConnection(); 217 int counter = 0; 218 219 List l = new ArrayList (); 220 Map h = new HashMap (); 221 ImageInfo ii; 222 223 if (conn != null) 224 { 225 Statement stmt = conn.createStatement(); 226 227 query = "select folder " + 228 "from folders " + 229 "where id='" + id + 230 "';"; 231 232 ResultSet rs = stmt.executeQuery(query); 233 234 String 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 baseUrl = request.getContextPath(); 250 251 while (rs.next()) 252 { 253 String 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 |