| 1 | |
package org.paneris.bibliomania; |
| 2 | |
|
| 3 | |
import java.util.Enumeration; |
| 4 | |
|
| 5 | |
import org.melati.poem.AccessToken; |
| 6 | |
import org.melati.poem.Database; |
| 7 | |
import org.melati.poem.DefinitionSource; |
| 8 | |
import org.melati.poem.PoemException; |
| 9 | |
import org.melati.poem.PoemTask; |
| 10 | |
import org.melati.poem.PoemThread; |
| 11 | |
import org.paneris.bibliomania.generated.BookFormatTableBase; |
| 12 | |
|
| 13 | |
import org.apache.oro.text.regex.Perl5Matcher; |
| 14 | |
import org.apache.oro.text.regex.Perl5Pattern; |
| 15 | |
|
| 16 | 0 | public class BookFormatTable<T extends BookFormat> extends BookFormatTableBase<BookFormat> { |
| 17 | |
|
| 18 | |
public BookFormatTable( |
| 19 | |
Database database, String name, |
| 20 | |
DefinitionSource definitionSource) throws PoemException { |
| 21 | 2 | super(database, name, definitionSource); |
| 22 | 2 | } |
| 23 | |
|
| 24 | |
public BookFormat ensure(String displayname, String descriptionregexp) { |
| 25 | 0 | BookFormat bf = (BookFormat)newPersistent(); |
| 26 | 0 | bf.setDisplayname(displayname); |
| 27 | 0 | bf.setDescriptionregexp(descriptionregexp); |
| 28 | 0 | create(bf); |
| 29 | 0 | return bf; |
| 30 | |
} |
| 31 | |
|
| 32 | |
public void postInitialise() { |
| 33 | 2 | super.postInitialise(); |
| 34 | 2 | if (count(null) == 0) { |
| 35 | 0 | ensure("Paperback", "paperback"); |
| 36 | 0 | ensure("Hardback", "hardback"); |
| 37 | 0 | ensure("DVD", "dvd|video\\s+dis[ck]"); |
| 38 | 0 | ensure("CD", "\\bcd\\b|compact\\s+dis[ck]"); |
| 39 | 0 | ensure("Video cassette", "video"); |
| 40 | 0 | ensure("Audio cassette", "audio|cassette"); |
| 41 | |
} |
| 42 | 2 | } |
| 43 | |
|
| 44 | |
private BookFormat _formatOfDescription(String description) { |
| 45 | 0 | for (Enumeration f = selection(); f.hasMoreElements();) { |
| 46 | 0 | BookFormat format = (BookFormat)f.nextElement(); |
| 47 | 0 | Perl5Pattern re = format.getDescriptionPerl5Regexp(); |
| 48 | 0 | Perl5Matcher matcher = new Perl5Matcher(); |
| 49 | 0 | if (re != null && matcher.contains(description, re)) |
| 50 | 0 | return format; |
| 51 | 0 | } |
| 52 | |
|
| 53 | 0 | return null; |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public BookFormat formatOfDescription(final String description) { |
| 60 | 0 | if (PoemThread.inSession()) |
| 61 | 0 | return _formatOfDescription(description); |
| 62 | |
else { |
| 63 | 0 | final BookFormat[] it = new BookFormat[1]; |
| 64 | 0 | getDatabase().inSession( |
| 65 | |
AccessToken.root, |
| 66 | 0 | new PoemTask() { |
| 67 | |
public void run() { |
| 68 | 0 | it[0] = _formatOfDescription(description); |
| 69 | 0 | } |
| 70 | |
}); |
| 71 | 0 | return it[0]; |
| 72 | |
} |
| 73 | |
} |
| 74 | |
} |