001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.example.tutorial;
028:
029: import java.util.List;
030:
031: import fr.ign.cogit.geoxygene.datatools.Geodatabase;
032: import fr.ign.cogit.geoxygene.datatools.ojb.GeodatabaseOjbFactory;
033:
034: /**
035: * Exemple.
036: *
037: * @author Thierry Badard & Arnaud Braun
038: * @version 1.0
039: *
040: */
041:
042: public class TestMyClass {
043:
044: ///////////////////////////////////////////////////////////////////////////
045: ///////////////////////////////////////////////////////////////////////////
046: /* represente une connection au SGBD via le mappeur objet-relationnel */
047: private Geodatabase db;
048:
049: ///////////////////////////////////////////////////////////////////////////
050: ///////////////////////////////////////////////////////////////////////////
051: /* constructeur : initialise l'attribut Geodatabse*/
052: public TestMyClass() {
053: // SGBD = oracle ; mappeur = OJB
054: // initialise la connection au SGBD et le mapping
055: db = GeodatabaseOjbFactory.newInstance();
056: }
057:
058: ///////////////////////////////////////////////////////////////////////////
059: ///////////////////////////////////////////////////////////////////////////
060: /* main */
061: public static void main(String args[]) {
062:
063: // on construit un nouvel objet "TestMaclasse"
064: TestMyClass test = new TestMyClass();
065:
066: // on appelle les methodes de TestMaclasse
067: test.cree();
068: test.charge();
069: test.interroge();
070: }
071:
072: ///////////////////////////////////////////////////////////////////////////
073: ///////////////////////////////////////////////////////////////////////////
074: /* methode cree() : on cree de nouveaux objets et on les rends persistants */
075: public void cree() {
076:
077: // declaration des variables
078: MyClass monobjet;
079:
080: // ouverture d'une transaction
081: db.begin();
082: System.out.println("begin");
083:
084: // creation de 2 objets
085: // le "db.makePersistent" les rend persistents
086: monobjet = new MyClass();
087: monobjet.bonjour();
088: db.makePersistent(monobjet);
089: monobjet.setField0(123);
090: monobjet.setField1("M. et Mme. PIC ont une fille");
091: monobjet.setField2(true);
092: monobjet.setField3(3.14159);
093:
094: monobjet = new MyClass();
095: monobjet.bonjour();
096: db.makePersistent(monobjet);
097: monobjet.setField0(4567890);
098: monobjet.setField1("Aïcha");
099: monobjet.setField2(false);
100: monobjet.setField3(0.123456);
101:
102: // remarque : on affecte aucun identifiant
103: // il est affecte automatiquement par OJB
104:
105: // commit et fermeture de la transaction
106: // on rend valide toutes les creations et modifications
107: // une modif meme apres le "db.makePersistent" est repercutee
108: db.commit();
109: System.out.println("commit ok");
110: }
111:
112: ///////////////////////////////////////////////////////////////////////////
113: ///////////////////////////////////////////////////////////////////////////
114: /* methode charge() : on va charger des objets de la base */
115: public void charge() {
116:
117: // declaration des variables
118: MyClass monobjet;
119:
120: // ouverture d'une transaction
121: db.begin();
122: System.out.println("begin");
123:
124: // on charge tous les objets de Maclasse
125: List list = db.loadAll(MyClass.class);
126:
127: // on passe en revue ces objets
128: System.out.println("nombre d'objets : " + list.size());
129: for (int i = 0; i < list.size(); i++) {
130: monobjet = (MyClass) list.get(i);
131: System.out.println("id : " + monobjet.getId());
132: System.out.println("classe : "
133: + monobjet.getClass().getName());
134: System.out.println("field0 : " + monobjet.getField0());
135: System.out.println("field1 : " + monobjet.getField1());
136: System.out.println("field2 : " + monobjet.getField2());
137: System.out.println("field3 : " + monobjet.getField3());
138: }
139:
140: // commit et fermeture de la transaction
141: db.commit();
142: System.out.println("commit ok");
143: }
144:
145: ///////////////////////////////////////////////////////////////////////////
146: ///////////////////////////////////////////////////////////////////////////
147: /* methode interroge() : exemple de requete oriente-objet OQL */
148: public void interroge() {
149:
150: // declaration des variables
151: MyClass monobjet;
152:
153: // ouverture d'une transaction
154: db.begin();
155: System.out.println("begin");
156:
157: // creation de la requete
158: String query = "select x from fr.ign.cogit.geoxygene.example.tutorial.MyClass where field0 > $1";
159: // $1 represente un parametre (200 ici)
160: List list = db.loadOQL(query, new Integer(200));
161:
162: // on passe en revue ces objets
163: System.out.println("nombre d'objets : " + list.size());
164: for (int i = 0; i < list.size(); i++) {
165: monobjet = (MyClass) list.get(i);
166: System.out.println("id : " + monobjet.getId());
167: }
168:
169: // commit et fermeture de la transaction
170: db.commit();
171: System.out.println("commit ok");
172: }
173:
174: }
|