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