1 package org.paneris.bibliomania;
2
3 import java.sql.ResultSet;
4
5 import org.melati.Melati;
6 import org.melati.poem.SeriousPoemException;
7 import org.webmacro.WebMacroException;
8 import org.webmacro.servlet.HandlerException;
9 import org.webmacro.servlet.WebContext;
10
11 public class ResetAuthorSequence extends BibliomaniaServlet {
12
13
14
15
16 private static final long serialVersionUID = 1L;
17 private BibliomaniaDatabase db = null;
18
19 protected void fillContext(Melati melati, BibContext it) {
20 it.setLogicalDatabase("bibliomania");
21 }
22
23 public int resetAuthorSequence() {
24 BookTable books = db.getBookTable();
25 books.readLock();
26 int count = 0;
27 try {
28
29 ResultSet results =
30 db.sqlQuery(
31 "SELECT "
32 + books.getColumn("id").quotedName()
33 + ", "
34 + books.getColumn("author").quotedName()
35 + " FROM "
36 + db.quotedName(books.getName())
37 + " ORDER BY author, authorsequence");
38 int i = 0;
39 Integer book = null;
40 Integer author = null;
41 Integer previousAuthor = null;
42 while (results.next()) {
43 book = new Integer(results.getInt(1));
44 author = new Integer(results.getInt(2));
45 if (author.equals(previousAuthor)) {
46 i++;
47 } else {
48 i = 0;
49 }
50 count =
51 count
52 + db.sqlUpdate(
53 "UPDATE "
54 + db.quotedName(books.getName())
55 + " SET "
56 + books.getColumn("authorsequence").quotedName()
57 + " = "
58 + new Integer(i).toString()
59 + " WHERE "
60 + books.getColumn("id").quotedName()
61 + " = "
62 + book.toString());
63 previousAuthor = author;
64 }
65 } catch (Exception e) {
66 throw new SeriousPoemException(e);
67 }
68 return count;
69 }
70
71 protected String bibliomaniaHandle(Melati melati, WebContext context)
72 throws WebMacroException {
73 db = (BibliomaniaDatabase)melati.getDatabase();
74 try {
75 context.put("count", new Integer(resetAuthorSequence()));
76 } catch (Exception e) {
77 throw new HandlerException("Problem:" + e.toString());
78 }
79 return bibliomaniaTemplate("admin/Results.wm");
80 }
81 }