001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.transactions;
025:
026: import org.myoodb.objects.*;
027:
028: public class Client {
029: public static int PORT = 54321;
030: public static String USERNAME = "admin";
031: public static String PASSWORD = "admin";
032:
033: public static String NEIGHBORHOOD[][] = { { "John", "Smith" }, // john (a.k.a dad)
034: { "Mary", "Smith" }, // mary (a.k.a mom)
035: { "Mark", "Johson" }, // john's neighbor
036: { "Barb", "Johson" }, // john's neighbor
037: { "Jack", "Smith" }, // john's son
038: { "Kate", "Smith" }, // john's daughter
039: { "Jean", "Smith" }, // john's mother
040: };
041:
042: public static void main(String args[]) throws Exception {
043: org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open(
044: "tcp://localhost:" + PORT, USERNAME, PASSWORD);
045:
046: // XXX: roots cannot be created within a transactions
047: Family family = (Family) db.getRoot("Family");
048: if (family == null) {
049: System.out.println("Create The Smith Family");
050:
051: family = (Family) db.createRoot("Family",
052: "org.myoodb.objects.FamilyDbImpl");
053: //family = (Family) db.createRoot("Family", FamilyDbImpl.class);
054:
055: family.setName("Smith");
056: } else {
057: System.out.println("The Smith Family already created");
058: }
059:
060: for (int i = 0; i < NEIGHBORHOOD.length; i++) {
061: org.myoodb.MyOodbTransaction tx = db.createTransaction();
062: tx.begin(); /* tx-1 */
063:
064: Person person = (Person) db
065: .createObject("org.myoodb.objects.PersonDbImpl");
066: //Person person = (Person) db.createObject(PersonDbImpl.class);
067:
068: person.setName(NEIGHBORHOOD[i][0]);
069:
070: System.out.println(" Checking if Person " + person
071: + " is part of the Smith Family");
072:
073: System.out.println(" Is Person locked: "
074: + person.isLocked());
075:
076: if (NEIGHBORHOOD[i][1].equals("Smith") == false) {
077: System.out.println(" Person " + person
078: + " is not part of the Smith Family");
079:
080: tx.rollback(); /* tx-1 */// make this person disappear!
081:
082: continue;
083: }
084:
085: tx.begin(); /* tx-2 */// show how to nest
086:
087: family.add(person);
088:
089: if (family.size() > 4) {
090: System.out
091: .println(" The Smith Family has reached their Maximum size: "
092: + person);
093:
094: tx.rollback(); /* tx-2 */// undo member add
095:
096: tx.rollback(); /* tx-1 */// make this person disappear!
097:
098: continue;
099: }
100:
101: tx.commit(); /* tx-2 */
102:
103: tx.commit(); /* tx-1 */
104:
105: System.out.println(" Person " + person
106: + " is now offically part of the Smith Family");
107: }
108: }
109: }
|