001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.constraints;
022:
023: import com.db4o.*;
024: import com.db4o.query.*;
025: import com.db4o.test.*;
026:
027: public class UniqueField {
028:
029: public int id;
030: public String name;
031:
032: public void store() {
033: UniqueField uf = new UniqueField();
034: uf.name = "Mark I";
035: uf.id = 1;
036: Test.store(uf);
037: uf = new UniqueField();
038: uf.name = "Mark II";
039: uf.id = 2;
040: Test.store(uf);
041: check();
042: }
043:
044: public void test() {
045: UniqueField uf = new UniqueField();
046: uf.name = "Mark I";
047: Test.store(uf);
048: check();
049: Test.commit();
050: check();
051:
052: Query q = Test.query();
053: q.constrain(UniqueField.class);
054: q.descend("id").constrain(new Integer(1));
055: uf = (UniqueField) q.execute().next();
056: uf.name = "Mark II";
057: Test.store(uf);
058: check();
059: Test.commit();
060: check();
061: }
062:
063: private void check() {
064: ObjectContainer oc = Test.objectContainer();
065: Query q = Test.query();
066: q.constrain(UniqueField.class);
067: ObjectSet objectSet = q.execute();
068: Test.ensure(objectSet.size() == 2);
069: while (objectSet.hasNext()) {
070: UniqueField uf = (UniqueField) objectSet.next();
071: oc.ext().refresh(uf, Integer.MAX_VALUE);
072: if (uf.id == 1) {
073: Test.ensure(uf.name.equals("Mark I"));
074: } else {
075: Test.ensure(uf.name.equals("Mark II"));
076: }
077: }
078: }
079:
080: public boolean objectCanNew(ObjectContainer objectContainer) {
081: return checkConstraints(objectContainer);
082: }
083:
084: public boolean objectCanUpdate(ObjectContainer objectContainer) {
085: return checkConstraints(objectContainer);
086: }
087:
088: private boolean checkConstraints(ObjectContainer objectContainer) {
089: String semaphoreName = "Unique_User_Name";
090: if (!objectContainer.ext().setSemaphore(semaphoreName, 1000)) {
091: return false;
092: }
093: Query q = objectContainer.query();
094: q.constrain(UniqueField.class);
095: q.descend("name").constrain(name);
096: ObjectSet objectSet = q.execute();
097: int size = objectSet.size();
098:
099: 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: }
|