| 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 | 0 | public class ResetAuthorSequence extends BibliomaniaServlet { |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
private static final long serialVersionUID = 1L; |
| 17 | 0 | private BibliomaniaDatabase db = null; |
| 18 | |
|
| 19 | |
protected void fillContext(Melati melati, BibContext it) { |
| 20 | 0 | it.setLogicalDatabase("bibliomania"); |
| 21 | 0 | } |
| 22 | |
|
| 23 | |
public int resetAuthorSequence() { |
| 24 | 0 | BookTable books = db.getBookTable(); |
| 25 | 0 | books.readLock(); |
| 26 | 0 | int count = 0; |
| 27 | |
try { |
| 28 | |
|
| 29 | 0 | 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 | 0 | int i = 0; |
| 39 | 0 | Integer book = null; |
| 40 | 0 | Integer author = null; |
| 41 | 0 | Integer previousAuthor = null; |
| 42 | 0 | while (results.next()) { |
| 43 | 0 | book = new Integer(results.getInt(1)); |
| 44 | 0 | author = new Integer(results.getInt(2)); |
| 45 | 0 | if (author.equals(previousAuthor)) { |
| 46 | 0 | i++; |
| 47 | |
} else { |
| 48 | 0 | i = 0; |
| 49 | |
} |
| 50 | 0 | 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 | 0 | previousAuthor = author; |
| 64 | |
} |
| 65 | 0 | } catch (Exception e) { |
| 66 | 0 | throw new SeriousPoemException(e); |
| 67 | 0 | } |
| 68 | 0 | return count; |
| 69 | |
} |
| 70 | |
|
| 71 | |
protected String bibliomaniaHandle(Melati melati, WebContext context) |
| 72 | |
throws WebMacroException { |
| 73 | 0 | db = (BibliomaniaDatabase)melati.getDatabase(); |
| 74 | |
try { |
| 75 | 0 | context.put("count", new Integer(resetAuthorSequence())); |
| 76 | 0 | } catch (Exception e) { |
| 77 | 0 | throw new HandlerException("Problem:" + e.toString()); |
| 78 | 0 | } |
| 79 | 0 | return bibliomaniaTemplate("admin/Results.wm"); |
| 80 | |
} |
| 81 | |
} |