Source code: org/vrspace/server/DBObject.java
1 package org.vrspace.server;
2 import java.lang.reflect.Field;
3 import org.vrspace.util.Logger;
4
5 /** DBObject defines methodes for database storage.
6 ** When processing a request, sets the field value and stores itself into
7 ** database, if one is specified.
8 */
9 public class DBObject extends VRObject {
10 protected DB db;
11
12 public DBObject () {}
13
14 /** This constructor implies autocommit, that is, sendEvent will
15 ** immediately store changed object into the database.
16 */
17 public DBObject ( DB db ) {
18 this.db = db;
19 }
20
21 /** Stores (insert/update) the object into database <b>db</b>
22 */
23 public void put( DB db ) throws Exception
24 {
25 db.put( this );
26 }
27
28 /**
29 store this into the database
30 */
31 protected void put() {
32 try {
33 db.put( this );
34 } catch ( Throwable t ) {
35 Logger.logError( getID()+": error during put()", t );
36 }
37 }
38
39 /** Fetches the object having db_id = <b>id</b> from the database <b>db</b>
40 */
41 public Object get( DB db, long id )
42 throws Exception
43 {
44 return db.get( this.getClassName(), id );
45 }
46
47 /** Deletes itself from the database.
48 */
49 public void delete( DB db ) throws Exception
50 {
51 db.delete( this );
52 }
53
54 /** Forwards request <b>r</b> to other clients, listeners added by Dispatcher.
55 ** Sets the field <b>r.eventName</b> to <b>r.eventValue</b>.
56 ** I field does not exist, forwards event anyway.
57 ** Stores itself into the database if one is set.
58 ** Throws RequestException if request target is another object, or if
59 ** database exception occured.
60 ** Calls setValue() method which actualy forwards the event.
61 */
62 public void sendEvent( Request r ) throws RequestException
63 {
64 if ( ! r.getClassName().equals( getClassName() ) ) {
65 RequestException e = new RequestException( r, "Object of " + getClass().getName() + " class got event sent to " + r.getClassName() + " class" );
66 r.exception = e;
67 throw (e);
68 }
69 if ( r.getId() != db_id ) {
70 RequestException e = new RequestException( r, "DBObject " + db_id + " got event sent to " + r.getId() );
71 r.exception = e;
72 throw( e );
73 }
74 try {
75 setField( r );
76 } catch ( Exception e ) {
77 Logger.logError(getID()+": error during setField()", e);
78 //RequestException re = new RequestException( e.toString() );
79 //r.exception = re;
80 //throw( re );
81 } finally {
82 // public db field changed
83 if ( db != null ) {
84 try {
85 db.update( r );
86 } catch ( Exception e ) {
87 RequestException re = new RequestException( r, e.toString() );
88 r.exception = re;
89 throw( re );
90 }
91 }
92 }
93 setValue( r );
94 }
95 }