1 package org.paneris.bibliomania;
2
3 import java.sql.Timestamp;
4 import java.util.Enumeration;
5 import java.util.Vector;
6
7 import org.melati.poem.Database;
8 import org.melati.poem.DefinitionSource;
9 import org.melati.poem.PoemException;
10 import org.melati.poem.PoemThread;
11 import org.melati.poem.util.EnumUtils;
12 import org.paneris.bibliomania.generated.StockingsSearchTableBase;
13
14 public class StockingsSearchTable<T extends StockingsSearch> extends StockingsSearchTableBase<StockingsSearch> {
15
16
17 public BibliomaniaDatabase getBibliomaniaDatabase () {
18 return (BibliomaniaDatabase)getDatabase();
19 }
20
21
22 public StockingsSearchTable(
23 Database database, String name,
24 DefinitionSource definitionSource) throws PoemException {
25 super(database, name, definitionSource);
26 }
27
28
29
30
31
32
33 private void trim() {
34 int excess =
35 count(null) - getBibliomaniaDatabase().getBookStockingsCacheSizeMax();
36
37 if (excess > 5) {
38 Vector togo = EnumUtils.vectorOf(
39 selection(null, "lastaccessed LIMIT " + excess, false));
40 try {
41 for (Enumeration s = togo.elements(); s.hasMoreElements();)
42 ((StockingsSearch)s.nextElement()).delete_unsafe();
43 }
44 finally {
45 PoemThread.commit();
46 }
47 }
48 }
49
50 public StockingsSearch searchFor(String author, String title) {
51 if (author == null) author = "";
52 if (title == null) title = "";
53
54 StockingsSearch it;
55 boolean created;
56
57 synchronized (this) {
58 it = (StockingsSearch)firstSelection(
59 getTitletermColumn().eqClause(title) + " AND " +
60 getAuthortermColumn().eqClause(author));
61
62 if (it == null) {
63 it = (StockingsSearch)newPersistent();
64 it.setTitleterm(title);
65 it.setAuthorterm(author);
66 it.makePersistent();
67 created = true;
68 }
69 else
70 created = false;
71 }
72
73 if (created)
74 trim();
75
76 it.setLastaccessed(new Timestamp(System.currentTimeMillis()));
77 PoemThread.commit();
78
79 return it;
80 }
81
82 public StockingsSearch searchFor(Book book) {
83 return searchFor(book.getAuthor().getLongname(), book.getFulltitle());
84 }
85
86 public StockingsSearch searchFor(Author author) {
87 return searchFor(author.getLongname(), "");
88 }
89 }