KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > constraints > UniqueField


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.constraints;
22
23 import com.db4o.*;
24 import com.db4o.query.*;
25 import com.db4o.test.*;
26
27 public class UniqueField {
28     
29     public int id;
30     public String JavaDoc name;
31     
32     public void store(){
33         UniqueField uf = new UniqueField();
34         uf.name = "Mark I";
35         uf.id = 1;
36         Test.store(uf);
37         uf = new UniqueField();
38         uf.name = "Mark II";
39         uf.id = 2;
40         Test.store(uf);
41         check();
42     }
43     
44     public void test(){
45         UniqueField uf = new UniqueField();
46         uf.name = "Mark I";
47         Test.store(uf);
48         check();
49         Test.commit();
50         check();
51         
52         Query q = Test.query();
53         q.constrain(UniqueField.class);
54         q.descend("id").constrain(new Integer JavaDoc(1));
55         uf = (UniqueField) q.execute().next();
56         uf.name = "Mark II";
57         Test.store(uf);
58         check();
59         Test.commit();
60         check();
61     }
62     
63     private void check(){
64         ObjectContainer oc = Test.objectContainer();
65         Query q = Test.query();
66         q.constrain(UniqueField.class);
67         ObjectSet objectSet = q.execute();
68         Test.ensure(objectSet.size() == 2);
69         while(objectSet.hasNext()){
70             UniqueField uf = (UniqueField)objectSet.next();
71             oc.ext().refresh(uf, Integer.MAX_VALUE);
72             if(uf.id == 1){
73                 Test.ensure(uf.name.equals("Mark I"));
74             }else{
75                 Test.ensure(uf.name.equals("Mark II"));
76             }
77         }
78     }
79     
80     public boolean objectCanNew(ObjectContainer objectContainer){
81         return checkConstraints(objectContainer);
82     }
83
84     public boolean objectCanUpdate(ObjectContainer objectContainer){
85         return checkConstraints(objectContainer);
86     }
87     
88     private boolean checkConstraints(ObjectContainer objectContainer){
89         String JavaDoc semaphoreName ="Unique_User_Name";
90         if(! objectContainer.ext().setSemaphore(semaphoreName, 1000)){
91             return false;
92         }
93         Query q = objectContainer.query();
94         q.constrain(UniqueField.class);
95         q.descend("name").constrain(name);
96         ObjectSet objectSet = q.execute();
97         int size = objectSet.size();
98         
99         boolean canStore = false;
100         if(size == 0){
101             canStore = true;
102         }
103         if(size == 1){
104             UniqueField uf = (UniqueField)objectSet.next();
105             canStore = (uf == this);
106         }
107         objectContainer.ext().releaseSemaphore(semaphoreName);
108         return canStore;
109     }
110     
111 }
112
Popular Tags