001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: FriendManager.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package tutorial.friends.backend;
009:
010: import com.uwyn.rife.config.Config;
011: import com.uwyn.rife.database.Datasources;
012: import com.uwyn.rife.database.DbPreparedStatement;
013: import com.uwyn.rife.database.DbPreparedStatementHandler;
014: import com.uwyn.rife.database.DbQueryManager;
015: import com.uwyn.rife.database.DbRowProcessor;
016: import com.uwyn.rife.database.queries.CreateTable;
017: import com.uwyn.rife.database.queries.DropTable;
018: import com.uwyn.rife.database.queries.Insert;
019: import com.uwyn.rife.database.queries.Select;
020:
021: /**
022: * Manages a list of visitors that are stored in a database.
023: *
024: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
025: * @version $Revision: 3634 $
026: */
027: public class FriendManager extends DbQueryManager {
028: private static String TABLE_NAME_FRIEND = "friend";
029:
030: public FriendManager() {
031: super (Datasources.getRepInstance().getDatasource(
032: Config.getRepInstance().getString("DATASOURCE")));
033: }
034:
035: /**
036: * Installs and populates the database structure.
037: *
038: * @throws DatabaseException when a database access error occurs.
039: */
040: public void install() {
041: CreateTable create = new CreateTable(getDatasource()).table(
042: TABLE_NAME_FRIEND).columns(Friend.class);
043: executeUpdate(create);
044:
045: add(new Friend("JR", "Boyens", "Partner in RIFE crime",
046: "http://rifers.org/blogs/jboyens"));
047: add(new Friend(
048: "Steph",
049: "Meslin-Weber",
050: "Zaurus Java FAQ, Wonka Java VM, QT/embedded AWT Java bindings",
051: "http://adorphuye.com"));
052: add(new Friend(
053: "Eugene",
054: "Ciurana",
055: "Author, Sushi expert, Muay Thai, #java IRC channel pastebin, variety of tools",
056: "http://eugeneciurana.com"));
057: add(new Friend("Mikael", "Hallendal",
058: "GNOME, Loudmoudth, Gossip, MrPoject, Devhelp, Yelp",
059: "http://www.imendio.com"));
060: add(new Friend("Paul", "Fleischer",
061: "PapuaWM - a minimalistic windowmanager",
062: "http://www.papuaos.org/papuawm"));
063: add(new Friend("Mike", "Newman",
064: "gnome-pkgview, gtkdial, gwvedit, gpuce, LemmingChat",
065: "http://www.greatnorthern.demon.co.uk/packages/"));
066: add(new Friend(
067: "Lasse",
068: "Bang Mikkelsen",
069: "PapuaWEB - a php page publishing environment with authentication.",
070: "http://www.papuaos.org/papuaweb"));
071: }
072:
073: /**
074: * Adds data of a <code>Friend</code> instance to the database.
075: *
076: * @param friend the <code>Friend</code> that will be added to the database
077: *
078: * @throws DatabaseException when a database access error occurs
079: *
080: */
081: public void add(final Friend friend) {
082: Insert insert = new Insert(getDatasource()).into(
083: TABLE_NAME_FRIEND).fieldsParameters(Friend.class);
084:
085: executeUpdate(insert, new DbPreparedStatementHandler() {
086: public void setParameters(DbPreparedStatement statement) {
087: statement.setBean(friend);
088: }
089: });
090: }
091:
092: /**
093: * Display the data in the database.
094: *
095: * @param processor the <code>DbRowProcessor</code> that will process the
096: * results
097: *
098: * @throws DatabaseException when a database access error occurs
099: */
100: public void display(DbRowProcessor processor) {
101: Select select = new Select(getDatasource()).from(
102: TABLE_NAME_FRIEND).fields(Friend.class).orderBy(
103: "firstname");
104:
105: // fetch every row in the resultset and forward the processing
106: // of the data to the DisplayProcessor
107: executeFetchAll(select, processor);
108: }
109:
110: /**
111: * Removes the database structure.
112: *
113: * @throws DatabaseException when a database access error occurs.
114: */
115: public void remove() {
116: DropTable drop = new DropTable(getDatasource())
117: .table(TABLE_NAME_FRIEND);
118: executeUpdate(drop);
119: }
120: }
|