KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > QuerySnipStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.storage;
27
28 import org.snipsnap.app.Application;
29 import org.snipsnap.snip.Snip;
30 import org.snipsnap.snip.SnipPostNameComparator;
31 import org.snipsnap.snip.storage.query.QueryKit;
32 import org.snipsnap.snip.storage.query.SnipComparator;
33 import org.snipsnap.snip.storage.query.SnipQuery;
34
35 import java.sql.Timestamp JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Comparator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Arrays JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * Wrapper with finders for in-memory searching. Can
44  * be used with JDBC...Storage when all Snips are
45  * kept in memory (via MemorySnipStorage).
46  *
47  * @author Stephan J. Schmidt
48  * @version $Id: QuerySnipStorage.java 1606 2004-05-17 10:56:18Z leo $
49  */

50
51 public class QuerySnipStorage implements SnipStorage {
52   // Count comparators, make the comparator by default
53
// with most usages. Make this dynamic
54
private static Comparator nameComparator = new SnipComparator() {
55     public int compare(Snip s1, Snip s2) {
56       return s1.getName().compareTo(s2.getName());
57     }
58   };
59
60   private static Comparator snipPostNameComparator = new SnipPostNameComparator();
61
62   private static Comparator nameWithoutPathComparator = new SnipComparator() {
63     public int compare(Snip s1, Snip s2) {
64       return getName(s1).compareTo(getName(s2));
65     }
66
67     public String JavaDoc getName(Snip snip) {
68       String JavaDoc name = snip.getName();
69       int index = name.lastIndexOf("/");
70       if (-1 != index) {
71         return name.substring(index + 1);
72       } else {
73         return name;
74       }
75     }
76   };
77
78   private static Comparator nameComparatorDesc = new SnipComparator() {
79     public int compare(Snip s1, Snip s2) {
80       return s2.getName().compareTo(s1.getName());
81     }
82   };
83   private static Comparator mTimeComparatorDesc = new SnipComparator() {
84     public int compare(Snip s1, Snip s2) {
85       return s2.getMTime().compareTo(s1.getMTime());
86     }
87   };
88   private static Comparator cTimeComparator = new SnipComparator() {
89     public int compare(Snip s1, Snip s2) {
90       return s1.getCTime().compareTo(s2.getCTime());
91     }
92   };
93   private static Comparator hotnessComparator = new SnipComparator() {
94     public int compare(Snip s1, Snip s2) {
95       return s1.getAccess().getViewCount()
96           < s2.getAccess().getViewCount() ? 1 : -1;
97     }
98   };
99
100   //@TODO replace with dynamic proxy.
101
private SnipStorage storage;
102
103   public QuerySnipStorage(SnipStorage storage) {
104     this.storage = storage;
105   }
106
107   // Basic manipulation methods Load,Store,Create,Remove
108
public Snip[] match(String JavaDoc pattern) {
109     return storage.match(pattern);
110   }
111
112   public Snip[] match(String JavaDoc start, String JavaDoc end) {
113     return storage.match(start, end);
114   }
115
116   public Snip storageLoad(String JavaDoc name) {
117     return storage.storageLoad(name);
118   }
119
120   public void storageStore(Snip snip) {
121     storage.storageStore(snip);
122   }
123
124   public Snip storageCreate(String JavaDoc name, String JavaDoc content) {
125     return storage.storageCreate(name, content);
126   }
127
128   public void storageRemove(Snip snip) {
129     storage.storageRemove(snip);
130   }
131
132   // Finder methods
133
public int storageCount() {
134     return storage.storageAll().size();
135   }
136
137   public List JavaDoc storageAll() {
138     return storage.storageAll();
139   }
140
141   public List JavaDoc storageAll(String JavaDoc applicationOid) {
142     return storage.storageAll(applicationOid);
143   }
144
145
146   public List JavaDoc storageByHotness(int size) {
147     return QueryKit.querySorted(storage.storageAll(), hotnessComparator, size);
148   }
149
150   public List JavaDoc storageByUser(final String JavaDoc login) {
151     return QueryKit.querySorted(storage.storageAll(), new SnipQuery() {
152       public boolean fit(Snip snip) {
153         return (login.equals(snip.getCUser()));
154       }
155     }, nameWithoutPathComparator);
156   }
157
158   public List JavaDoc storageByDateSince(final Timestamp JavaDoc date) {
159     return QueryKit.query(storage.storageAll(), new SnipQuery() {
160       public boolean fit(Snip snip) {
161         return (date.before(snip.getMTime()));
162       }
163     });
164   }
165
166   public List JavaDoc storageByRecent(String JavaDoc applicationOid, int size) {
167     return QueryKit.querySorted(storage.storageAll(applicationOid),
168         mTimeComparatorDesc, size);
169   }
170
171   public List JavaDoc storageByRecent(int size) {
172     return QueryKit.querySorted(storage.storageAll(),
173         mTimeComparatorDesc, size);
174   }
175
176   public List JavaDoc storageByComments(final Snip parent) {
177     return QueryKit.querySorted(storage.storageAll(), new SnipQuery() {
178       public boolean fit(Snip snip) {
179         return (parent == snip.getCommentedSnip());
180       }
181     }, cTimeComparator);
182   }
183
184   public List JavaDoc storageByParent(final Snip parent) {
185     return QueryKit.query(storage.storageAll(), new SnipQuery() {
186       public boolean fit(Snip snip) {
187         return (parent == snip.getParent());
188       }
189     });
190   }
191
192   public List JavaDoc storageByParentNameOrder(final Snip parent, int count) {
193 // Logger.debug("all = " + storage.storageAll());
194
// Logger.debug("childs for=" + parent);
195

196     List JavaDoc list = QueryKit.querySorted(storage.storageAll(), new SnipQuery() {
197       public boolean fit(Snip snip) {
198 // Logger.debug("snip.parent = "+snip.getParent());
199
// Logger.debug("snip.name = "+snip.getName());
200
// if (snip.getParent() != null) {
201
// Logger.debug("snip.parent.name = "+snip.getParent().getName());
202
// }
203
// Logger.debug("snip="+snip.getParent());
204
// Logger.debug("? "+(parent == snip.getParent()));
205
return (parent == snip.getParent());
206       }
207     }, nameComparatorDesc, count);
208 // Logger.debug("result = " + list.toString());
209
return list;
210   }
211
212   public List JavaDoc storageByParentModifiedOrder(Snip parent, int count) {
213     List JavaDoc result = storageByParent(parent);
214     Collections.sort(result, mTimeComparatorDesc);
215     return result.subList(0, Math.min(count, result.size()));
216   }
217
218   public List JavaDoc storageByDateInName(final String JavaDoc nameSpace, final String JavaDoc start, final String JavaDoc end) {
219     final String JavaDoc queryStart = nameSpace + "/" + start + "/";
220     final String JavaDoc queryEnd = nameSpace + "/" + end + "/";
221     List JavaDoc blogWithParent = QueryKit.querySorted(storage.storageAll(), new SnipQuery() {
222       public boolean fit(Snip snip) {
223         // Return all Snips with the parent matching like
224
// 2001-01-12 ... and parent "start"
225
String JavaDoc name = snip.getName();
226         Snip parent = snip.getParent();
227         boolean blogWithParent = (start.compareTo(name) <= 0 && end.compareTo(name) >= 0 &&
228             null != parent && nameSpace.equals(parent.getName()));
229         return blogWithParent;
230       }
231     }, nameComparatorDesc);
232 // System.err.println("Parent="+blogWithParent);
233

234     List JavaDoc blogWithNameSpace = new ArrayList JavaDoc(Arrays.asList(match(queryStart, queryEnd)));
235     Collections.sort(blogWithNameSpace, snipPostNameComparator);
236
237 // System.err.println("NameSpace="+blogWithNameSpace);
238

239     blogWithNameSpace.addAll(blogWithParent);
240     return blogWithNameSpace;
241   }
242 }
243
Popular Tags