Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm?rev=1139579&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencingObjects.vm Sat Jun 25 18:36:03 2011 @@ -0,0 +1,228 @@ +## 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. +## +###### +## +## version $Id: MultiExtendBean.vm 240328 2005-08-26 22:02:48 +0200 (Fr, 26 Aug 2005) tfischer $ +## +## Creates the fillXXX methods for referencing Objects for the base peer +## implementation. +## +## This template expects the current source element to be a "foreign-field" +## element from the schema, processed by the OMTransformer. +## The options and the attributes of the current source element must be set +## as velocity variables. +## +#set ( $foreignKeyElement = $torqueGen.getParent() ) +#set ( $foreignKeyGetter = $foreignKeyElement.getAttribute("foreignKeyGetter") ) +#set ( $referencesPrimaryKey = $foreignKeyElement.getAttribute("referencesPrimaryKey") ) +#set ( $foreignKeyReferences = $foreignKeyElement.getChildren("reference") ) +#set ( $localTableElement = $foreignKeyElement.getChild("table") ) +#set ( $localTableField = $localTableElement.getAttribute("field") ) +#set ( $localDbObjectClassName = $localTableElement.getAttribute("dbObjectClassName") ) +#set ( $localPeerClassName = $localTableElement.getAttribute("peerClassName") ) +#set ( $primaryKeyColumnElements = $localTableElement.getChild("primary-keys").getChildren("column")) +#set ( $foreignTableElement = $foreignKeyElement.getParent() ) +#set ( $foreignTablePeerClassName = $foreignTableElement.getAttribute("peerClassName") ) + /** + * Fills the referenced ${fieldContainedType} objects in the ${field} fields + * of the objects in <code>toFill</code> by reading the database. + * + * @param toFill Contains the objects to fill, not null, + * may not contain null. + * + * @return all read ${fieldType} objects. + * + * @throws TorqueException if an error querying the database occurs. + * @throws NullPointerException if toFill is null or contains null elements. + */ + public List<${fieldContainedType}> ${filler}( + Collection<${localDbObjectClassName}> toFill) + throws TorqueException + { + Connection connection = null; + try + { + connection = Transaction.beginOptional( + ${localPeerClassName}.DATABASE_NAME, + true); + List<${fieldContainedType}> result = ${filler}(toFill, connection); + Transaction.commit(connection); + connection = null; + return result; + } + finally + { + if (connection != null) + { + Transaction.safeRollback(connection); + } + } + } + + /** + * Fills the referenced ${fieldContainedType} objects in the ${field} fields + * of the objects in <code>toFill</code> by reading the database. + * + * @param toFill Contains the objects to fill, not null, + * may not contain null. + * @param connection the database connection to use, not null. + * + * @return all read ${fieldType} objects. + * + * @throws TorqueException if an error querying the database occurs. + * @throws NullPointerException if toFill is null or contains null elements. + */ + public List<${fieldContainedType}> ${filler}( + Collection<${localDbObjectClassName}> toFill, + Connection connection) + throws TorqueException + { + Set<ObjectKey> localKeySet = new HashSet<ObjectKey>(); + for (${localDbObjectClassName} current${localDbObjectClassName} : toFill) + { +#if ($referencesPrimaryKey) + ObjectKey localKey = current${localDbObjectClassName}.getPrimaryKey(); +#else + #if ($foreignKeyReferences.size() == 1) + #set ( $localReferenceColumnElement = $foreignKeyReferences.get(0).getChild("foreign-column").getChild("column") ) + #set ( $localFieldGetter = $localReferenceColumnElement.getAttribute("getter") ) + ObjectKey localKey = SimpleKey.keyFor(current${localDbObjectClassName}.${localFieldGetter}()); + #else + SimpleKey[] keyElementArray = new SimpleKey[$foreignKeyReferences.size()]; + #set ($i = 0) + #foreach ($referenceElement in $foreignKeyReferences) + #set ( $localReferenceColumnElement = $referenceElement.getChild("foreign-column").getChild("column") ) + #set ( $localFieldGetter = $localReferenceColumnElement.getAttribute("getter") ) + keyElementArray[$i] = SimpleKey.keyFor( + current${localDbObjectClassName}.${localFieldGetter}()); + #set ($i = $i + 1) + #end + ObjectKey localKey = new ComboKey(keyElementArray); + #end +#end + localKeySet.add(localKey); + } + + int fillerChunkSize = getFillerChunkSize(); + Iterator<ObjectKey> localKeyIt = localKeySet.iterator(); + List<ObjectKey> localKeyListChunk + = new ArrayList<ObjectKey>(fillerChunkSize); + Map<ObjectKey, List<${fieldContainedType}>> fkTo${fieldContainedType}Map + = new HashMap<ObjectKey, List<${fieldContainedType}>>(); + while (localKeyIt.hasNext()) + { + ObjectKey currentKey = localKeyIt.next(); + localKeyListChunk.add(currentKey); + if (localKeyListChunk.size() < fillerChunkSize + && localKeyIt.hasNext()) + { + continue; + } + + Criteria criteria = new Criteria(); +#if ($foreignKeyReferences.size() == 1) + #set ( $columnElement = $foreignKeyReferences.get(0).getChild("local-column").getChild("column") ) + #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) + criteria.addIn(${foreignTablePeerClassName}.${peerColumnName}, localKeyListChunk); +#else + for (ObjectKey localKey : localKeyListChunk) + { + SimpleKey[] keys = (SimpleKey[]) localKey.getValue(); + #set ( $i = 0 ) + #foreach ($referenceElement in $foreignKeyReferences) + #set ( $columnElement = $referenceElement.getChild("local-column").getChild("column") ) + #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) + Criteria.Criterion c$i = criteria.getNewCriterion( + ${foreignTablePeerClassName}.${peerColumnName}, keys[$i], Criteria.EQUAL); + #set ( $j = $i - 1 ) + #if ($i > 0) + c${j}.and(c${i}); + #end + #set ( $i = $i + 1 ) + #end + criteria.or(c0); + } +#end + List<${fieldContainedType}> referenced${fieldContainedType}List + = ${foreignTablePeerClassName}.doSelect( + criteria, + connection); + for (${fieldContainedType} referencedObject : referenced${fieldContainedType}List) + { + ObjectKey foreignKey = referencedObject.${foreignKeyGetter}(); + List<${fieldContainedType}> objectsWithForeignKey + = fkTo${fieldContainedType}Map.get(foreignKey); + if (objectsWithForeignKey == null) + { + objectsWithForeignKey = new ArrayList<${fieldContainedType}>(); + fkTo${fieldContainedType}Map.put( + foreignKey, + objectsWithForeignKey); + } + objectsWithForeignKey.add(referencedObject); + } + localKeyListChunk.clear(); + } + + List<${fieldContainedType}> result = new ArrayList<${fieldContainedType}>(); + for (${localDbObjectClassName} objectToFill : toFill) + { + objectToFill.${initializer}(); + objectToFill.${getter}().clear(); +#if ($referencesPrimaryKey) + ObjectKey localKey = objectToFill.getPrimaryKey(); +#else + #if ($foreignKeyReferences.size() == 1) + #set ( $localReferenceColumnElement = $foreignKeyReferences.get(0).getChild("foreign-column").getChild("column") ) + #set ( $localFieldGetter = $localReferenceColumnElement.getAttribute("getter") ) + ObjectKey localKey = SimpleKey.keyFor(objectToFill.${localFieldGetter}()); + #else + SimpleKey[] keyElementArray = new SimpleKey[$foreignKeyReferences.size()]; + #set ($i = 0) + #foreach ($referenceElement in $foreignKeyReferences) + #set ( $localReferenceColumnElement = $referenceElement.getChild("foreign-column").getChild("column") ) + #set ( $localFieldGetter = $localReferenceColumnElement.getAttribute("getter") ) + keyElementArray[$i] = SimpleKey.keyFor( + objectToFill.${localFieldGetter}()); + #set ($i = $i + 1) + #end + ObjectKey localKey = new ComboKey(keyElementArray); + #end +#end + List<${fieldContainedType}> referencingList + = fkTo${fieldContainedType}Map.get(localKey); + if (referencingList == null) + { + continue; + } + for (${fieldContainedType} referencing : referencingList) + { + // copy the referencing object for the case that more than one + // toFill object references the same object; in this case + // every toFillObject should have its own instance + // of the referencing object to remain consistent + // with other Torque referencing object treatment + ${fieldContainedType} referencingCopy = referencing.copy(false); + // copy does not set primary key + referencingCopy.setPrimaryKey(referencing.getPrimaryKey()); + objectToFill.${adder}(referencingCopy); + result.add(referencingCopy); + } + } + return result; + }
Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillerChunkSizeGetter.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillerChunkSizeGetter.vm?rev=1139579&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillerChunkSizeGetter.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillerChunkSizeGetter.vm Sat Jun 25 18:36:03 2011 @@ -0,0 +1,37 @@ +## 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. +## +###### +## +## version $Id: MultiExtendBean.vm 240328 2005-08-26 22:02:48 +0200 (Fr, 26 Aug 2005) tfischer $ +## +## Creates the fillXXX methods for the base peer implementation. +## +## This template expects the current source element to be a "foreign-field" +## element from the schema, processed by the OMTransformer. +## The options and the attributes of the current source element must be set +## as velocity variables. +## + /** + * Returns the chunk size for the filler methods. + * + * @return the chunk size for the filler methods. + */ + protected int getFillerChunkSize() + { + return $torqueGen.option("torque.om.complexObjectModel.defaultFillerChunkSize"); + } Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm?rev=1139579&r1=1139578&r2=1139579&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm Sat Jun 25 18:36:03 2011 @@ -42,10 +42,15 @@ import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.HashSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -63,6 +68,7 @@ import org.apache.torque.om.NumberKey; import org.apache.torque.om.StringKey; import org.apache.torque.om.ObjectKey; import org.apache.torque.om.SimpleKey; +import org.apache.torque.om.ComboKey; import org.apache.torque.util.BasePeer; import org.apache.torque.util.Criteria; import org.apache.torque.util.Transaction; Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm?rev=1139579&r1=1139578&r2=1139579&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/retrieveByPKs.vm Sat Jun 25 18:36:03 2011 @@ -36,7 +36,7 @@ * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - public List<${dbObjectClassName}> retrieveByPKs(List<ObjectKey> pks) + public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks) throws TorqueException { Connection db = null; @@ -61,7 +61,7 @@ * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ - public List<${dbObjectClassName}> retrieveByPKs(List<ObjectKey> pks, Connection dbcon) + public List<${dbObjectClassName}> retrieveByPKs(Collection<ObjectKey> pks, Connection dbcon) throws TorqueException { List<${dbObjectClassName}> objs = null; @@ -72,18 +72,17 @@ else { Criteria criteria = new Criteria(); - #set ( $columnElement = $primaryKeyColumnElements.get(0) ) - #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) #if ($primaryKeyColumnElements.size() == 1) + #set ( $columnElement = $primaryKeyColumnElements.get(0) ) + #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName")) criteria.addIn(${peerClassName}.$peerColumnName, pks); #else - Iterator<ObjectKey> iter = pks.iterator(); - while (iter.hasNext()) + for (ObjectKey pk : pks) { - ObjectKey pk = iter.next(); SimpleKey[] keys = (SimpleKey[])pk.getValue(); #set ( $i = 0 ) #foreach ($primaryKeyColumnElement in $primaryKeyColumnElements) + #set ( $peerColumnName = $primaryKeyColumnElement.getAttribute("peerColumnName")) Criteria.Criterion c$i = criteria.getNewCriterion( ${peerClassName}.$peerColumnName, keys[$i], Criteria.EQUAL); #set ( $j = $i - 1 ) --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org