Author: arminw Date: Mon Oct 29 03:23:40 2007 New Revision: 589577 URL: http://svn.apache.org/viewvc?rev=589577&view=rev Log: improve handling of references on main object store
Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionContainer.java Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionContainer.java URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionContainer.java?rev=589577&view=auto ============================================================================== --- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionContainer.java (added) +++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/CollectionContainer.java Mon Oct 29 03:23:40 2007 @@ -0,0 +1,166 @@ +package org.apache.ojb.broker.core; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.lang.Object; +import java.util.Collection; + +import org.apache.ojb.broker.PersistenceBrokerInternal; +import org.apache.ojb.broker.ManageableCollection; +import org.apache.ojb.broker.core.proxy.CollectionProxy; +import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl; +import org.apache.ojb.broker.metadata.CollectionDescriptor; + +/** + * Helper class to handle a collection reference (1:n or m:n) object with it's + * [EMAIL PROTECTED] org.apache.ojb.broker.metadata.CollectionDescriptor} and the source object + * this reference belongs too. + * + * @version $Id$ + */ +final public class CollectionContainer +{ + private final CollectionDescriptor descriptor; + private final CollectionProxy collectionProxy; + private final Object source; + private final Object reference; + + public CollectionContainer(PersistenceBrokerInternal broker, CollectionDescriptor descriptor, Object source) + { + this.descriptor = descriptor; + this.source = source; + this.reference = descriptor.getPersistentField().get(source); + this.collectionProxy = reference != null ? broker.getProxyFactory().getCollectionProxy(reference) : null; + } + + /** + * Returns the [EMAIL PROTECTED] org.apache.ojb.broker.metadata.CollectionDescriptor} of + * this reference. + * @return The [EMAIL PROTECTED] org.apache.ojb.broker.metadata.CollectionDescriptor} of this reference object. + */ + public CollectionDescriptor getDescriptor() + { + return descriptor; + } + + /** + * Returns <em>true</em> if this collection reference is <em>null</em>. + * @return Whether or not this collection reference is <em>null</em>. + */ + public boolean isNullReference() + { + return reference == null; + } + + /** + * Returns <em>true</em> if this collection reference is a materialized proxy. + * @return Whether or not this collection reference is a materialized proxy. + */ + public boolean isLoadedProxy() + { + return collectionProxy != null && collectionProxy.isLoaded(); + } + + /** + * Returns <em>true</em> if this collection reference is a unmaterialized proxy. + * @return Whether or not this collection reference is a unmaterialized proxy. + */ + public boolean isUnmaterializedProxy() + { + return collectionProxy != null && !collectionProxy.isLoaded(); + } + + /** + * Returns <em>true</em> if this collection reference is a proxy (use lazy loading). + * @return Whether or not this collection reference is a proxy. + */ + public boolean isProxy() + { + return collectionProxy != null; + } + + /** + * Returns the reference object. + * @return The reference object. + */ + public Object getReference() + { + return reference; + } + + /** + * Returns the <em>real</em> (none proxy) reference object. If lazy loading is used + * this collection reference is wrapped by a proxy, this method returns the materialized + * none proxy collection object instead of the proxy reference. + * @return The <em>real</em> (none proxy) collection instance. + */ + public Object getMaterializedReference() + { + return isProxy() ? ((CollectionProxyDefaultImpl) collectionProxy).getData() : reference; + } + + /** + * The source object this reference belongs too. + * + * @return The source object. + */ + public Object getSource() + { + return source; + } + + /** + * Returns <em>true</em> if this collection reference is a + * [EMAIL PROTECTED] org.apache.ojb.broker.ManageableCollection} instance. + * @return Whether or not this collection reference is a manageable collection. + */ + public boolean isManageableCollection() + { + return reference instanceof ManageableCollection; + } + + /** + * Returns <em>true</em> if this collection reference is a collection. + * @return Whether or not this collection reference is a collection. + */ + public boolean isCollection() + { + return reference instanceof Collection; + } + + /** + * Returns <em>true</em> if this collection reference is an array. + * @return Whether or not this collection reference is an array. + */ + public boolean isArrayType() + { + return descriptor.isArrayType(); + } + +// public boolean isTrackingCollection() +// { +// return reference instanceof TrackingCollection; +// } +// +// public TrackingCollection getTrackingCollection() +// { +// return reference instanceof TrackingCollection ? (TrackingCollection) reference : null; +// } +} Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java?rev=589577&r1=589576&r2=589577&view=diff ============================================================================== --- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java (original) +++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java Mon Oct 29 03:23:40 2007 @@ -45,6 +45,7 @@ import org.apache.ojb.broker.accesslayer.sql.SqlExistStatement; import org.apache.ojb.broker.core.PersistenceBrokerImpl; import org.apache.ojb.broker.core.ValueContainer; +import org.apache.ojb.broker.core.CollectionContainer; import org.apache.ojb.broker.core.proxy.IndirectionHandler; import org.apache.ojb.broker.core.proxy.ProxyFactory; import org.apache.ojb.broker.metadata.ClassDescriptor; @@ -108,7 +109,7 @@ if(tok.hasMoreTokens()) { user = tok.nextToken(); - if(user != null && user.trim().equals("")) + if(user != null && user.trim().length() == 0) { user = null; } @@ -999,16 +1000,19 @@ { Object referencedObjects = cod.getPersistentField().get(realObject); if (cod.isMtoNRelation()) + { + if(referencedObjects == null) { - if(referencedObjects == null) - { - referencedObjects = Collections.EMPTY_LIST; - } + referencedObjects = Collections.EMPTY_LIST; + } m_broker.getMtoNBroker().storeMtoN(realObject, cod, referencedObjects, insert); } else { - if(referencedObjects != null) m_broker.storeAndLinkOneToMany(true, realObject, cod, referencedObjects, insert); + if(referencedObjects != null) + { + m_broker.storeAndLinkOneToMany(true, new CollectionContainer(m_broker, cod, realObject), insert); + } } } else --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]