View Javadoc

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  public class BookFormatTable<T extends BookFormat> extends BookFormatTableBase<BookFormat> {
17  
18    public BookFormatTable(
19        Database database, String name,
20        DefinitionSource definitionSource) throws PoemException {
21      super(database, name, definitionSource);
22    }
23  
24    public BookFormat ensure(String displayname, String descriptionregexp) {
25      BookFormat bf = (BookFormat)newPersistent();
26      bf.setDisplayname(displayname);
27      bf.setDescriptionregexp(descriptionregexp);
28      create(bf);
29      return bf;
30    }
31  
32    public void postInitialise() {
33      super.postInitialise();
34      if (count(null) == 0) {
35        ensure("Paperback", "paperback");
36        ensure("Hardback", "hardback");
37        ensure("DVD", "dvd|video\\s+dis[ck]");
38        ensure("CD", "\\bcd\\b|compact\\s+dis[ck]");
39        ensure("Video cassette", "video");
40        ensure("Audio cassette", "audio|cassette");
41      }
42    }
43  
44    private BookFormat _formatOfDescription(String description) {
45      for (Enumeration f = selection(); f.hasMoreElements();) {
46        BookFormat format = (BookFormat)f.nextElement();
47        Perl5Pattern re = format.getDescriptionPerl5Regexp();
48        Perl5Matcher matcher = new Perl5Matcher();
49        if (re != null && matcher.contains(description, re))
50          return format;
51      }
52  
53      return null;
54    }
55  
56    // FIXME megahack, because this is called from a non-POEM search
57    // thread!!
58  
59    public BookFormat formatOfDescription(final String description) {
60      if (PoemThread.inSession())
61        return _formatOfDescription(description);
62      else {
63        final BookFormat[] it = new BookFormat[1];
64        getDatabase().inSession(
65            AccessToken.root,
66            new PoemTask() {
67              public void run() {
68                it[0] = _formatOfDescription(description);
69              }
70            });
71        return it[0];
72      }
73    }
74  }