View Javadoc

1   package org.paneris.bibliomania.loadtest;
2   
3   import java.io.FileOutputStream;
4   import java.io.ObjectOutputStream;
5   import java.io.Serializable;
6   import java.util.Enumeration;
7   import java.util.Vector;
8   
9   import org.melati.poem.AccessToken;
10  import org.melati.poem.Persistent;
11  import org.melati.poem.PoemTask;
12  import org.melati.poem.util.EnumUtils;
13  import org.melati.util.UnexpectedExceptionException;
14  import org.paneris.bibliomania.BibliomaniaDatabase;
15  import org.paneris.bibliomania.Book;
16  import org.paneris.bibliomania.Chapter;
17  import org.paneris.bibliomania.Unit;
18  
19  public class ExtractTree implements Serializable {
20  
21    private static final long serialVersionUID = 847223626240878960L;
22  
23    public DiscreteDistribution books = new DiscreteDistribution();
24    public int searchsectiongroup;
25  
26    public void readDB() {
27      final BibliomaniaDatabase bib = new BibliomaniaDatabase(false);
28      bib.connect("bibliomania", "org.melati.poem.dbms.Postgresql",
29                  "jdbc:postgresql:bibliomania", "postgres", "*",8);
30      bib.inSession(
31          AccessToken.root,
32          new PoemTask() {
33            @SuppressWarnings("unchecked")
34            public void run() {
35              try {
36                searchsectiongroup =
37                    bib.getSearchSectionGroup().troid().intValue();
38  
39                dobooks: for (Enumeration<Unit> e = bib.getBookTable().selection();
40                              e.hasMoreElements();) {
41                  Book book = (Book)e.nextElement();
42  
43                  Vector<Chapter> chapters = EnumUtils.vectorOf(book.getChapters());
44                  if (chapters.size() > 0) {
45                    ChapterDesc[] chapterDescs = new ChapterDesc[chapters.size()];
46                    for (int i = 0; i < chapterDescs.length; ++i) {
47                      Chapter chapter = (Chapter)chapters.elementAt(i);
48                      int p = chapter.totalPages();
49                      if (p == 0) continue dobooks;
50                      chapterDescs[i] = new ChapterDesc(chapter.troid().intValue(),
51                                                    chapter.totalPages());
52                    }
53  
54                    books.add(
55                        new BookDesc(
56                            book.getSection().getGroupTroid().intValue(),
57                            book.getSectionTroid().intValue(),
58                            book.getAuthorTroid().intValue(),
59                            book.troid().intValue(),
60                            chapterDescs));
61                  }
62                }
63              }
64              catch (Exception e) {
65                throw new UnexpectedExceptionException(e);
66              }
67            }
68          });
69    }
70  
71    public static void main(String[] args) throws Exception {
72      ExtractTree t = new ExtractTree();
73      t.readDB();
74      ObjectOutputStream o =
75          new ObjectOutputStream(new FileOutputStream("books.ser"));
76      o.writeObject(t);
77      o.close();
78    }
79  }