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.constraints;
022:
023: import com.db4o.config.*;
024: import com.db4o.events.*;
025: import com.db4o.ext.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.btree.*;
029: import com.db4o.reflect.*;
030:
031: /**
032: * configures a field of a class to allow unique values only.
033: */
034: public class UniqueFieldValueConstraint implements ConfigurationItem {
035:
036: protected final Object _clazz;
037: protected final String _fieldName;
038:
039: /**
040: * constructor to create a UniqueFieldValueConstraint.
041: * @param clazz can be a class (Java) / Type (.NET) / instance of the class / fully qualified class name
042: * @param fieldName the name of the field that is to be unique.
043: */
044: public UniqueFieldValueConstraint(Object clazz, String fieldName) {
045: _clazz = clazz;
046: _fieldName = fieldName;
047: }
048:
049: public void prepare(Configuration configuration) {
050: // Nothing to do...
051: }
052:
053: /**
054: * internal method, public for implementation reasons.
055: */
056: public void apply(final InternalObjectContainer objectContainer) {
057:
058: EventRegistryFactory.forObjectContainer(objectContainer)
059: .committing().addListener(new EventListener4() {
060:
061: private FieldMetadata _fieldMetaData;
062:
063: private void ensureSingleOccurence(
064: Transaction trans, ObjectInfoCollection col) {
065: Iterator4 i = col.iterator();
066: while (i.moveNext()) {
067: ObjectInfo info = (ObjectInfo) i.current();
068: int id = (int) info.getInternalID();
069:
070: // TODO: check if the object is of the appropriate
071: // type before going further?
072:
073: HardObjectReference ref = HardObjectReference
074: .peekPersisted(trans, id, 1);
075: Object fieldValue = fieldMetadata().getOn(
076: trans, ref._object);
077: if (fieldValue == null) {
078: continue;
079: }
080: BTreeRange range = fieldMetadata().search(
081: trans, fieldValue);
082: if (range.size() > 1) {
083: throw new UniqueFieldValueConstraintViolationException(
084: classMetadata().getName(),
085: fieldMetadata().getName());
086: }
087: }
088: }
089:
090: private boolean isClassMetadataAvailable() {
091: return null != classMetadata();
092: }
093:
094: private FieldMetadata fieldMetadata() {
095: if (_fieldMetaData != null) {
096: return _fieldMetaData;
097: }
098: _fieldMetaData = classMetadata()
099: .fieldMetadataForName(_fieldName);
100: return _fieldMetaData;
101: }
102:
103: private ClassMetadata classMetadata() {
104: return objectContainer
105: .classMetadataForReflectClass(reflectClass());
106: }
107:
108: private ReflectClass reflectClass() {
109: return ReflectorUtils.reflectClassFor(
110: objectContainer.reflector(), _clazz);
111: }
112:
113: public void onEvent(Event4 e, EventArgs args) {
114: if (!isClassMetadataAvailable()) {
115: return;
116: }
117: CommitEventArgs commitEventArgs = (CommitEventArgs) args;
118: Transaction trans = (Transaction) commitEventArgs
119: .transaction();
120: ensureSingleOccurence(trans, commitEventArgs
121: .added());
122: ensureSingleOccurence(trans, commitEventArgs
123: .updated());
124: }
125: });
126:
127: }
128: }
|