001package org.apache.fulcrum.security.torque.om;
002
003import java.sql.Connection;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.List;
007import java.util.Map;
008import java.util.Set;
009import java.util.HashSet;
010import java.util.Objects;
011import java.util.stream.Collectors;
012
013import org.apache.torque.NoRowsException;
014import org.apache.torque.TooManyRowsException;
015import org.apache.torque.TorqueException;
016import org.apache.torque.criteria.Criteria;
017import org.apache.torque.criteria.Criterion;
018import org.apache.torque.om.mapper.RecordMapper;
019import org.apache.torque.om.ObjectKey;
020import org.apache.torque.om.SimpleKey;
021import org.apache.torque.map.TableMap;
022import org.apache.torque.util.TorqueConnection;
023import org.apache.torque.util.Transaction;
024import org.apache.torque.util.ColumnValues;
025import org.apache.torque.util.JdbcTypedValue;
026
027
028
029/**
030 * The skeleton for this class was autogenerated by Torque on:
031 *
032 * [Thu Nov 04 13:34:23 CET 2021]
033 *
034 * You should not use this class directly.  It should not even be
035 * extended; all references should be to TorqueDynamicUserPeer
036 */
037
038@SuppressWarnings("unused")
039public abstract class BaseTorqueDynamicUserPeerImpl
040    extends org.apache.torque.util.AbstractPeerImpl<TorqueDynamicUser>
041{
042    /** Serial version */
043    private static final long serialVersionUID = 1636029263235L;
044
045
046
047    /**
048     * Constructor.
049     * The recordMapper, tableMap and databaseName fields are correctly
050     * initialized.
051     */
052    public BaseTorqueDynamicUserPeerImpl()
053    {
054        this(new TorqueDynamicUserRecordMapper(),
055            TorqueDynamicUserPeer.TABLE,
056            TorqueDynamicUserPeer.DATABASE_NAME);
057    }
058
059    /**
060     * Constructor providing the objects to be injected as parameters.
061     *
062     * @param recordMapper a record mapper to map JDBC result sets to objects
063     * @param tableMap the default table map
064     * @param databaseName the name of the database
065     */
066    public BaseTorqueDynamicUserPeerImpl(
067            RecordMapper<TorqueDynamicUser> recordMapper, 
068            TableMap tableMap,
069            String databaseName)
070    {
071        super(recordMapper, tableMap, databaseName);
072    }
073
074
075    /**
076     * Returns a new instance of the Data object class
077     */
078    public TorqueDynamicUser getDbObjectInstance()
079    {
080        return new TorqueDynamicUser();
081    }
082
083
084    /**
085     * Method to do updates.  This method is to be used during a transaction,
086     * otherwise use the doUpdate(Criteria) method.
087     *
088     * @param columnValues the values to update plus the primary key
089     *        identifying the row to update.
090     * @param con the connection to use, not null.
091     *
092     * @return the number of affected rows.
093     *
094     * @throws TorqueException Any exceptions caught during processing will be
095     *         rethrown wrapped into a TorqueException.
096     */
097    public int doUpdate(ColumnValues columnValues, Connection con)
098        throws TorqueException
099    {
100        Criteria selectCriteria = new Criteria(getDatabaseName());
101        correctBooleans(columnValues);
102
103        {
104            JdbcTypedValue pkValue
105                = columnValues.remove(TorqueDynamicUserPeer.USER_ID);
106            if (pkValue == null)
107            {
108                throw new TorqueException(
109                        "The value for the primary key column "
110                        + "TorqueDynamicUserPeer.USER_ID"
111                        + " must be set");
112            }
113            if (pkValue.getSqlExpression() == null)
114            {
115                selectCriteria.where(
116                        TorqueDynamicUserPeer.USER_ID,
117                        pkValue.getValue());
118            }
119            else
120            {
121                selectCriteria.where(
122                        TorqueDynamicUserPeer.USER_ID,
123                        pkValue.getSqlExpression());
124            }
125        }
126
127
128        int rowCount = doUpdate(selectCriteria, columnValues, con);
129        return rowCount;
130    }
131
132    /**
133     * Deletes a data object, i.e. a row in a table, in the database.
134     *
135     * @param obj the data object to delete in the database, not null.
136     *
137     * @return the number of deleted rows.
138     *
139     * @throws TorqueException Any exceptions caught during processing will be
140     *         rethrown wrapped into a TorqueException.
141     */
142    public int doDelete(TorqueDynamicUser obj) throws TorqueException
143    {
144        int result = doDelete(buildCriteria(obj.getPrimaryKey()));
145        obj.setDeleted(true);
146        return result;
147    }
148
149    /**
150     * Deletes a data object, i.e. a row in a table, in the database.
151     * This method is to be used during a transaction, otherwise use the
152     * doDelete(TorqueDynamicUser) method.
153     *
154     * @param obj the data object to delete in the database, not null.
155     * @param con the connection to use, not null.
156     *
157     * @return the number of deleted rows.
158     *
159     * @throws TorqueException Any exceptions caught during processing will be
160     *         rethrown wrapped into a TorqueException.
161     */
162    public int doDelete(TorqueDynamicUser obj, Connection con)
163        throws TorqueException
164    {
165        int result = doDelete(buildCriteria(obj.getPrimaryKey()), con);
166        obj.setDeleted(true);
167        return result;
168    }
169
170    /**
171     * Deletes data objects, i.e. rows in a table, in the database.
172     *
173     * @param objects the data object to delete in the database, not null,
174     *        may not contain null.
175     *
176     * @return the number of deleted rows.
177     *
178     * @throws TorqueException Any exceptions caught during processing will be
179     *         rethrown wrapped into a TorqueException.
180     */
181    public int doDelete(Collection<TorqueDynamicUser> objects)
182            throws TorqueException
183    {
184        int result = doDelete(buildPkCriteria(objects));
185        objects.forEach(object -> object.setDeleted(true));
186        return result;
187    }
188
189    /**
190     * Deletes data objects, i.e. rows in a table, in the database.
191     * This method uses the passed connection to delete the rows;
192     * if a transaction is open in the connection, the deletion happens inside
193     * this transaction.
194     *
195     * @param objects the data objects to delete in the database, not null,
196     *        may not contain null.
197     * @param con the connection to use for deleting, not null.
198     *
199     * @return the number of deleted rows.
200     *
201     * @throws TorqueException Any exceptions caught during processing will be
202     *         rethrown wrapped into a TorqueException.
203     */
204    public int doDelete(
205            Collection<TorqueDynamicUser> objects,
206            Connection con)
207        throws TorqueException
208    {
209        int result = doDelete(buildPkCriteria(objects), con);
210        objects.forEach(object -> object.setDeleted(true));
211        return result;
212    }
213
214    /** 
215     * Build a Criteria object which selects all objects which have a given
216     * primary key.
217     *
218     * @param pk the primary key value to build the criteria from, not null.
219     */
220    public Criteria buildCriteria(ObjectKey<?> pk)
221    {
222        Criteria criteria = new Criteria();
223        criteria.and(TorqueDynamicUserPeer.USER_ID, pk);
224        return criteria;
225     }
226
227    /** 
228     * Build a Criteria object which selects all objects which primary keys
229     * are contained in the passed collection.
230     *
231     * @param pks the primary key values to build the criteria from, not null,
232     *        may not contain null.
233     */
234    public Criteria buildCriteria(Collection<ObjectKey<?>> pks)
235    {
236        Criteria criteria = new Criteria();
237        criteria.andIn(TorqueDynamicUserPeer.USER_ID, pks);
238        return criteria;
239     }
240
241
242    /** 
243     * Build a Criteria object which selects all passed objects using their
244     * primary key. Objects which do not yet have a primary key are ignored.
245     *
246     * @param objects the objects to build the criteria from, not null,
247     *        may not contain null.
248     */
249    public Criteria buildPkCriteria(
250            Collection<TorqueDynamicUser> objects)
251    {
252        return buildCriteria(objects.stream()
253                .map(object -> object.getPrimaryKey())
254                .collect(Collectors.toList()));
255    }
256
257    /** 
258     * Build a Criteria object from the data object for this peer.
259     * The primary key columns are only added if the object is not new.
260     *
261     * @param obj the object to build the criteria from, not null.
262     */
263    public Criteria buildCriteria(TorqueDynamicUser obj)
264    {
265        Criteria criteria = new Criteria(getDatabaseName());
266        if (!obj.isNew())
267        {
268            criteria.and(TorqueDynamicUserPeer.USER_ID, obj.getEntityId());
269        }
270        criteria.and(TorqueDynamicUserPeer.LOGIN_NAME, obj.getEntityName());
271        criteria.and(TorqueDynamicUserPeer.PASSWORD_VALUE, obj.getPassword());
272        return criteria;
273    }
274
275    /** 
276     * Build a Criteria object from the data object for this peer,
277     * skipping all binary columns.
278     *
279     * @param obj the object to build the criteria from, not null.
280     */
281    public Criteria buildSelectCriteria(TorqueDynamicUser obj)
282    {
283        Criteria criteria = new Criteria(getDatabaseName());
284        if (!obj.isNew())
285        {
286            criteria.and(TorqueDynamicUserPeer.USER_ID, obj.getEntityId());
287        }
288        criteria.and(TorqueDynamicUserPeer.LOGIN_NAME, obj.getEntityName());
289        criteria.and(TorqueDynamicUserPeer.PASSWORD_VALUE, obj.getPassword());
290        return criteria;
291    }
292
293    /** 
294     * Returns the contents of the object as ColumnValues object.
295     * Primary key columns which are generated on insertion are not
296     * added to the returned object if they still have their initial
297     * value. Also, columns which have the useDatabaseDefaultValue
298     * flag set to true are also not added to the returned object
299     * if they still have their initial value.
300     *
301     * @throws TorqueException if the table map cannot be retrieved
302     *         (should not happen).
303     */
304    public ColumnValues buildColumnValues(TorqueDynamicUser torqueDynamicUser)
305            throws TorqueException
306    {
307        ColumnValues columnValues = new ColumnValues();
308        if (!torqueDynamicUser.isNew() 
309            || torqueDynamicUser.getEntityId() != null)
310        {
311            columnValues.put(
312                    TorqueDynamicUserPeer.USER_ID,
313                    new JdbcTypedValue(
314                        torqueDynamicUser.getEntityId(),
315                        4));
316        }
317        columnValues.put(
318                TorqueDynamicUserPeer.LOGIN_NAME,
319                new JdbcTypedValue(
320                    torqueDynamicUser.getEntityName(),
321                    12));
322        columnValues.put(
323                TorqueDynamicUserPeer.PASSWORD_VALUE,
324                new JdbcTypedValue(
325                    torqueDynamicUser.getPassword(),
326                    12));
327        return columnValues;
328    }
329
330    /**
331     * Retrieve a single object by pk
332     *
333     * @param pk the primary key
334     * @throws TorqueException Any exceptions caught during processing will be
335     *         rethrown wrapped into a TorqueException.
336     * @throws NoRowsException Primary key was not found in database.
337     * @throws TooManyRowsException Primary key was not found in database.
338     */
339    public TorqueDynamicUser retrieveByPK(Integer pk)
340        throws TorqueException, NoRowsException, TooManyRowsException
341    {
342        return retrieveByPK(SimpleKey.keyFor(pk));
343    }
344
345    /**
346     * Retrieve a single object by pk
347     *
348     * @param pk the primary key
349     * @param con the connection to use
350     * @throws TorqueException Any exceptions caught during processing will be
351     *         rethrown wrapped into a TorqueException.
352     * @throws NoRowsException Primary key was not found in database.
353     * @throws TooManyRowsException Primary key was not found in database.
354     */
355    public TorqueDynamicUser retrieveByPK(Integer pk, Connection con)
356        throws TorqueException, NoRowsException, TooManyRowsException
357    {
358        return retrieveByPK(SimpleKey.keyFor(pk), con);
359    }
360    
361    
362    
363
364    /**
365     * Retrieve a single object by pk
366     *
367     * @param pk the primary key
368     * @throws TorqueException Any exceptions caught during processing will be
369     *         rethrown wrapped into a TorqueException.
370     * @throws NoRowsException Primary key was not found in database.
371     * @throws TooManyRowsException Primary key was not found in database.
372     */
373    public TorqueDynamicUser retrieveByPK(ObjectKey<?> pk)
374        throws TorqueException, NoRowsException, TooManyRowsException
375    {
376        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
377        {
378            TorqueDynamicUser result = retrieveByPK(pk, connection);
379            Transaction.commit(connection);
380            return result;
381        }
382    }
383
384    /**
385     * Retrieve a single object by pk
386     *
387     * @param pk the primary key
388     * @param con the connection to use
389     * @throws TorqueException Any exceptions caught during processing will be
390     *         rethrown wrapped into a TorqueException.
391     * @throws NoRowsException Primary key was not found in database.
392     * @throws TooManyRowsException Primary key was not found in database.
393     */
394    public TorqueDynamicUser retrieveByPK(ObjectKey<?> pk, Connection con)
395        throws TorqueException, NoRowsException, TooManyRowsException
396    {
397        Criteria criteria = buildCriteria(pk);
398        TorqueDynamicUser v = doSelectSingleRecord(criteria, con);
399        if (v == null)
400        {
401            throw new NoRowsException("Failed to select a row.");
402        }
403
404        return v;
405    }
406
407
408    /**
409     * Retrieve multiple objects by pk.
410     *
411     * @param pks List of primary keys.
412     *        Entries in pks which do not match entries in the database are ignored.
413     *
414     * @return the list of matching objects, not null.
415     *
416     * @throws TorqueException Any exceptions caught during processing will be
417     *         rethrown wrapped into a TorqueException.
418     */
419    public List<TorqueDynamicUser> retrieveByTypedPKs(Collection<Integer> pks)
420        throws TorqueException
421    {
422        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
423        {
424            List<TorqueDynamicUser> result = retrieveByTypedPKs(pks, connection);
425            Transaction.commit(connection);
426            return result;
427        }
428    }
429
430    /**
431     * Retrieve multiple objects by pk.
432     *
433     * @param pks List of primary keys.
434     *        Entries in pks which do not match entries in the database are ignored.
435     * @param dbcon the connection to use
436     *
437     * @return the list of matching objects, not null.
438     *
439     * @throws TorqueException Any exceptions caught during processing will be
440     *         rethrown wrapped into a TorqueException.
441     */
442    public List<TorqueDynamicUser> retrieveByTypedPKs(
443                Collection<Integer> pks,
444                Connection dbcon)
445            throws TorqueException
446    {
447        if (pks == null || pks.size() == 0)
448        {
449            return new ArrayList<TorqueDynamicUser>();
450        }
451        List<ObjectKey<?>> objectKeyList = new ArrayList<ObjectKey<?>>();
452        for (Integer pk : pks)
453        {
454            objectKeyList.add(SimpleKey.keyFor(pk));
455        }
456        Criteria criteria = buildCriteria(objectKeyList);
457        List<TorqueDynamicUser> result = doSelect(criteria, dbcon);
458        return result;
459    }
460
461    /**
462     * Retrieve multiple objects by pk.
463     *
464     * @param pks List of primary keys.
465     *        Entries in pks which do not match entries in the database are ignored.
466     *
467     * @return the list of matching objects, not null.
468     *
469     * @throws TorqueException Any exceptions caught during processing will be
470     *         rethrown wrapped into a TorqueException.
471     */
472    public List<TorqueDynamicUser> retrieveByObjectKeys(Collection<ObjectKey<?>> pks)
473        throws TorqueException
474    {
475        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
476        {
477            List<TorqueDynamicUser> result = retrieveByObjectKeys(pks, connection);
478            Transaction.commit(connection);
479            return result;
480        }
481    }
482
483    /**
484     * Retrieve multiple objects by pk.
485     *
486     * @param pks List of primary keys.
487     *        Entries in pks which do not match entries in the database are ignored.
488     * @param dbcon the connection to use
489     *
490     * @return the list of matching objects, not null.
491     *
492     * @throws TorqueException Any exceptions caught during processing will be
493     *         rethrown wrapped into a TorqueException.
494     */
495    public List<TorqueDynamicUser> retrieveByObjectKeys(
496                Collection<ObjectKey<?>> pks,
497                Connection dbcon)
498            throws TorqueException
499    {
500        if (pks == null || pks.size() == 0)
501        {
502            return new ArrayList<TorqueDynamicUser>();
503        }
504        Criteria criteria = buildCriteria(pks);
505        List<TorqueDynamicUser> result = doSelect(criteria, dbcon);
506        return result;
507    }
508
509
510
511
512    /** 
513     * Saves the passed collection as linked objects.
514     * This means the following is done:
515     * <ul>
516     *  <li>
517     *    The current collection of objects which are linked to toLinkTo
518     *    and which are also in the list toSave is read from the database
519     *    into the list intersection.
520     *  </li>
521     *  <li>
522     *    All objects in toSave which are not in intersection are inserted.
523     *  </li>
524     *  <li>
525     *    All objects in intersection are updated with the 
526     *    corresponding values in toSave and saved.
527     *  </li>
528     *  <li>
529     *    All objects in the database which are linked to toLinkTo but are 
530     *    not in toSave are deleted from the database.
531     *  </li>
532     * </ul>
533     *
534     * @param toLinkTo the object which collections should be set with the
535     *        values in toSave.
536     * @param toSave Contains the objects to save, not null, 
537     *        may not contain null.
538     *
539     * @throws TorqueException if an error accessing the database occurs.
540     * @throws NullPointerException if toSave is null or contains null elements.
541     */
542    public void setAndSaveTorqueDynamicUserGroups(
543            TorqueDynamicUser toLinkTo,
544            Collection<TorqueDynamicUserGroup> toSave) 
545        throws TorqueException
546    {
547        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
548        {
549            setAndSaveTorqueDynamicUserGroups(toLinkTo, toSave, connection);
550            Transaction.commit(connection);
551        }
552    }
553
554    /** 
555     * Saves the passed collection as linked objects.
556     * This means the following is done:
557     * <ul>
558     *  <li>
559     *    The current collection of objects which are linked to toLinkTo
560     *    and which are also in the list toSave is read from the database
561     *    into the list intersection.
562     *  </li>
563     *  <li>
564     *    All objects in toSave which are not in intersection are inserted.
565     *  </li>
566     *  <li>
567     *    All objects in intersection are updated with the 
568     *    corresponding values in toSave and saved.
569     *  </li>
570     *  <li>
571     *    All objects in the database which are linked to toLinkTo but are 
572     *    not in toSave are deleted from the database.
573     *  </li>
574     * </ul>
575     *
576     * @param toLinkTo the object which collections should be set with the
577     *        values in toSave.
578     * @param toSave Contains the objects to save, not null, 
579     *        may not contain null.
580     * @param connection the database connection to use.
581     *
582     * @throws TorqueException if an error accessing the database occurs.
583     * @throws NullPointerException if toSave is null or contains null elements.
584     */
585    public void setAndSaveTorqueDynamicUserGroups(
586            TorqueDynamicUser toLinkTo,
587            Collection<TorqueDynamicUserGroup> toSave,
588            Connection connection) 
589        throws TorqueException
590    {
591        // make sure the collection cache in toLinkTo represents the current
592        // database state
593        toLinkTo.resetTorqueDynamicUserGroup();
594        toLinkTo.getTorqueDynamicUserGroups(connection);
595        ObjectKey<?> localKey = toLinkTo.getPrimaryKey();
596
597        Criteria criteria = new Criteria();
598        Criterion onlyReferencingCriterion = new Criterion(
599                TorqueDynamicUserGroupPeer.USER_ID, 
600                localKey);
601        criteria.where(onlyReferencingCriterion);
602        Set<ObjectKey<?>> toSaveKeys = new HashSet<ObjectKey<?>>();
603        for (TorqueDynamicUserGroup toSaveElement : toSave)
604        {
605            ObjectKey<?> toSaveKey = toSaveElement.getPrimaryKey();
606            if (toSaveKey.getValue() != null)
607            {
608                toSaveKeys.add(toSaveKey);
609            }
610        }
611        
612        // calculate intersection between objects in the database
613        // and objects in the list
614        List<TorqueDynamicUserGroup> intersection;
615        if (toSaveKeys.isEmpty())
616        {
617            intersection = new ArrayList<TorqueDynamicUserGroup>();
618        }
619        else
620        {
621            Criterion toSaveKeyCriterion = null;
622            for (ObjectKey<?> toSaveKey : toSaveKeys)
623            {
624                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
625                 Criterion p0 = new Criterion(
626                        TorqueDynamicUserGroupPeer.USER_ID,
627                        toSavePrimaryKeys[0]);
628                 Criterion p1 = new Criterion(
629                        TorqueDynamicUserGroupPeer.GROUP_ID,
630                        toSavePrimaryKeys[1]);
631                p0.and(p1);
632                if (toSaveKeyCriterion == null)
633                {
634                    toSaveKeyCriterion = p0;
635                }
636                else
637                {
638                    toSaveKeyCriterion.or(p0);
639                }
640            }
641            criteria.and(toSaveKeyCriterion);
642            intersection = TorqueDynamicUserGroupPeer.doSelect(
643                    criteria,
644                    connection);
645        }
646        if (toLinkTo.isTorqueDynamicUserGroupsInitialized())
647        {
648            toLinkTo.getTorqueDynamicUserGroups().clear();
649        }
650        else
651        {
652           toLinkTo.initTorqueDynamicUserGroups();
653        }
654        for (TorqueDynamicUserGroup toSaveElement : toSave)
655        {
656            int listIndex = intersection.indexOf(toSaveElement);
657            if (listIndex == -1)
658            {
659                toLinkTo.addTorqueDynamicUserGroup(toSaveElement);
660                toSaveElement.save(connection);
661            }
662            else
663            {
664                toLinkTo.addTorqueDynamicUserGroup(toSaveElement);
665                toSaveElement.setNew(false);
666                if (!toSaveElement.valueEquals(intersection.get(listIndex)))
667                {
668                    //force saving if content differs
669                    toSaveElement.setModified(true);
670                }
671                toSaveElement.save(connection);
672            }
673            toSaveKeys.add(toSaveElement.getPrimaryKey());
674        }
675        
676        // delete elements not in intersection
677        Criteria deleteCriteria = new Criteria();
678        deleteCriteria.where(onlyReferencingCriterion);
679        if (!toSaveKeys.isEmpty())
680        {
681            Criterion toSaveKeyCriterion = null;
682            for (ObjectKey<?> toSaveKey : toSaveKeys)
683            {
684                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
685                  Criterion p0 = new Criterion(
686                         TorqueDynamicUserGroupPeer.USER_ID,
687                         toSavePrimaryKeys[0],
688                         Criteria.NOT_EQUAL);
689                  Criterion p1 = new Criterion(
690                         TorqueDynamicUserGroupPeer.GROUP_ID,
691                         toSavePrimaryKeys[1],
692                         Criteria.NOT_EQUAL);
693                p0.or(p1);
694                if (toSaveKeyCriterion == null)
695                {
696                    toSaveKeyCriterion = p0;
697                }
698                else
699                {
700                    toSaveKeyCriterion.and(p0);
701                }
702            }
703            deleteCriteria.and(toSaveKeyCriterion);
704        }
705        TorqueDynamicUserGroupPeer.doDelete(deleteCriteria, connection);
706    }
707    /** 
708     * Saves the passed collection as linked objects.
709     * This means the following is done:
710     * <ul>
711     *  <li>
712     *    The current collection of objects which are linked to toLinkTo
713     *    and which are also in the list toSave is read from the database
714     *    into the list intersection.
715     *  </li>
716     *  <li>
717     *    All objects in toSave which are not in intersection are inserted.
718     *  </li>
719     *  <li>
720     *    All objects in intersection are updated with the 
721     *    corresponding values in toSave and saved.
722     *  </li>
723     *  <li>
724     *    All objects in the database which are linked to toLinkTo but are 
725     *    not in toSave are deleted from the database.
726     *  </li>
727     * </ul>
728     *
729     * @param toLinkTo the object which collections should be set with the
730     *        values in toSave.
731     * @param toSave Contains the objects to save, not null, 
732     *        may not contain null.
733     *
734     * @throws TorqueException if an error accessing the database occurs.
735     * @throws NullPointerException if toSave is null or contains null elements.
736     */
737    public void setAndSaveTorqueDynamicUserDelegatesRelatedByDelegatorUserIds(
738            TorqueDynamicUser toLinkTo,
739            Collection<TorqueDynamicUserDelegates> toSave) 
740        throws TorqueException
741    {
742        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
743        {
744            setAndSaveTorqueDynamicUserDelegatesRelatedByDelegatorUserIds(toLinkTo, toSave, connection);
745            Transaction.commit(connection);
746        }
747    }
748
749    /** 
750     * Saves the passed collection as linked objects.
751     * This means the following is done:
752     * <ul>
753     *  <li>
754     *    The current collection of objects which are linked to toLinkTo
755     *    and which are also in the list toSave is read from the database
756     *    into the list intersection.
757     *  </li>
758     *  <li>
759     *    All objects in toSave which are not in intersection are inserted.
760     *  </li>
761     *  <li>
762     *    All objects in intersection are updated with the 
763     *    corresponding values in toSave and saved.
764     *  </li>
765     *  <li>
766     *    All objects in the database which are linked to toLinkTo but are 
767     *    not in toSave are deleted from the database.
768     *  </li>
769     * </ul>
770     *
771     * @param toLinkTo the object which collections should be set with the
772     *        values in toSave.
773     * @param toSave Contains the objects to save, not null, 
774     *        may not contain null.
775     * @param connection the database connection to use.
776     *
777     * @throws TorqueException if an error accessing the database occurs.
778     * @throws NullPointerException if toSave is null or contains null elements.
779     */
780    public void setAndSaveTorqueDynamicUserDelegatesRelatedByDelegatorUserIds(
781            TorqueDynamicUser toLinkTo,
782            Collection<TorqueDynamicUserDelegates> toSave,
783            Connection connection) 
784        throws TorqueException
785    {
786        // make sure the collection cache in toLinkTo represents the current
787        // database state
788        toLinkTo.resetTorqueDynamicUserDelegatesRelatedByDelegatorUserId();
789        toLinkTo.getTorqueDynamicUserDelegatesRelatedByDelegatorUserIds(connection);
790        ObjectKey<?> localKey = toLinkTo.getPrimaryKey();
791
792        Criteria criteria = new Criteria();
793        Criterion onlyReferencingCriterion = new Criterion(
794                TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID, 
795                localKey);
796        criteria.where(onlyReferencingCriterion);
797        Set<ObjectKey<?>> toSaveKeys = new HashSet<ObjectKey<?>>();
798        for (TorqueDynamicUserDelegates toSaveElement : toSave)
799        {
800            ObjectKey<?> toSaveKey = toSaveElement.getPrimaryKey();
801            if (toSaveKey.getValue() != null)
802            {
803                toSaveKeys.add(toSaveKey);
804            }
805        }
806        
807        // calculate intersection between objects in the database
808        // and objects in the list
809        List<TorqueDynamicUserDelegates> intersection;
810        if (toSaveKeys.isEmpty())
811        {
812            intersection = new ArrayList<TorqueDynamicUserDelegates>();
813        }
814        else
815        {
816            Criterion toSaveKeyCriterion = null;
817            for (ObjectKey<?> toSaveKey : toSaveKeys)
818            {
819                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
820                 Criterion p0 = new Criterion(
821                        TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID,
822                        toSavePrimaryKeys[0]);
823                 Criterion p1 = new Criterion(
824                        TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID,
825                        toSavePrimaryKeys[1]);
826                p0.and(p1);
827                if (toSaveKeyCriterion == null)
828                {
829                    toSaveKeyCriterion = p0;
830                }
831                else
832                {
833                    toSaveKeyCriterion.or(p0);
834                }
835            }
836            criteria.and(toSaveKeyCriterion);
837            intersection = TorqueDynamicUserDelegatesPeer.doSelect(
838                    criteria,
839                    connection);
840        }
841        if (toLinkTo.isTorqueDynamicUserDelegatesRelatedByDelegatorUserIdsInitialized())
842        {
843            toLinkTo.getTorqueDynamicUserDelegatesRelatedByDelegatorUserIds().clear();
844        }
845        else
846        {
847           toLinkTo.initTorqueDynamicUserDelegatesRelatedByDelegatorUserIds();
848        }
849        for (TorqueDynamicUserDelegates toSaveElement : toSave)
850        {
851            int listIndex = intersection.indexOf(toSaveElement);
852            if (listIndex == -1)
853            {
854                toLinkTo.addTorqueDynamicUserDelegatesRelatedByDelegatorUserId(toSaveElement);
855                toSaveElement.save(connection);
856            }
857            else
858            {
859                toLinkTo.addTorqueDynamicUserDelegatesRelatedByDelegatorUserId(toSaveElement);
860                toSaveElement.setNew(false);
861                if (!toSaveElement.valueEquals(intersection.get(listIndex)))
862                {
863                    //force saving if content differs
864                    toSaveElement.setModified(true);
865                }
866                toSaveElement.save(connection);
867            }
868            toSaveKeys.add(toSaveElement.getPrimaryKey());
869        }
870        
871        // delete elements not in intersection
872        Criteria deleteCriteria = new Criteria();
873        deleteCriteria.where(onlyReferencingCriterion);
874        if (!toSaveKeys.isEmpty())
875        {
876            Criterion toSaveKeyCriterion = null;
877            for (ObjectKey<?> toSaveKey : toSaveKeys)
878            {
879                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
880                  Criterion p0 = new Criterion(
881                         TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID,
882                         toSavePrimaryKeys[0],
883                         Criteria.NOT_EQUAL);
884                  Criterion p1 = new Criterion(
885                         TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID,
886                         toSavePrimaryKeys[1],
887                         Criteria.NOT_EQUAL);
888                p0.or(p1);
889                if (toSaveKeyCriterion == null)
890                {
891                    toSaveKeyCriterion = p0;
892                }
893                else
894                {
895                    toSaveKeyCriterion.and(p0);
896                }
897            }
898            deleteCriteria.and(toSaveKeyCriterion);
899        }
900        TorqueDynamicUserDelegatesPeer.doDelete(deleteCriteria, connection);
901    }
902    /** 
903     * Saves the passed collection as linked objects.
904     * This means the following is done:
905     * <ul>
906     *  <li>
907     *    The current collection of objects which are linked to toLinkTo
908     *    and which are also in the list toSave is read from the database
909     *    into the list intersection.
910     *  </li>
911     *  <li>
912     *    All objects in toSave which are not in intersection are inserted.
913     *  </li>
914     *  <li>
915     *    All objects in intersection are updated with the 
916     *    corresponding values in toSave and saved.
917     *  </li>
918     *  <li>
919     *    All objects in the database which are linked to toLinkTo but are 
920     *    not in toSave are deleted from the database.
921     *  </li>
922     * </ul>
923     *
924     * @param toLinkTo the object which collections should be set with the
925     *        values in toSave.
926     * @param toSave Contains the objects to save, not null, 
927     *        may not contain null.
928     *
929     * @throws TorqueException if an error accessing the database occurs.
930     * @throws NullPointerException if toSave is null or contains null elements.
931     */
932    public void setAndSaveTorqueDynamicUserDelegatesRelatedByDelegateeUserIds(
933            TorqueDynamicUser toLinkTo,
934            Collection<TorqueDynamicUserDelegates> toSave) 
935        throws TorqueException
936    {
937        try (TorqueConnection connection = Transaction.begin(getDatabaseName()))
938        {
939            setAndSaveTorqueDynamicUserDelegatesRelatedByDelegateeUserIds(toLinkTo, toSave, connection);
940            Transaction.commit(connection);
941        }
942    }
943
944    /** 
945     * Saves the passed collection as linked objects.
946     * This means the following is done:
947     * <ul>
948     *  <li>
949     *    The current collection of objects which are linked to toLinkTo
950     *    and which are also in the list toSave is read from the database
951     *    into the list intersection.
952     *  </li>
953     *  <li>
954     *    All objects in toSave which are not in intersection are inserted.
955     *  </li>
956     *  <li>
957     *    All objects in intersection are updated with the 
958     *    corresponding values in toSave and saved.
959     *  </li>
960     *  <li>
961     *    All objects in the database which are linked to toLinkTo but are 
962     *    not in toSave are deleted from the database.
963     *  </li>
964     * </ul>
965     *
966     * @param toLinkTo the object which collections should be set with the
967     *        values in toSave.
968     * @param toSave Contains the objects to save, not null, 
969     *        may not contain null.
970     * @param connection the database connection to use.
971     *
972     * @throws TorqueException if an error accessing the database occurs.
973     * @throws NullPointerException if toSave is null or contains null elements.
974     */
975    public void setAndSaveTorqueDynamicUserDelegatesRelatedByDelegateeUserIds(
976            TorqueDynamicUser toLinkTo,
977            Collection<TorqueDynamicUserDelegates> toSave,
978            Connection connection) 
979        throws TorqueException
980    {
981        // make sure the collection cache in toLinkTo represents the current
982        // database state
983        toLinkTo.resetTorqueDynamicUserDelegatesRelatedByDelegateeUserId();
984        toLinkTo.getTorqueDynamicUserDelegatesRelatedByDelegateeUserIds(connection);
985        ObjectKey<?> localKey = toLinkTo.getPrimaryKey();
986
987        Criteria criteria = new Criteria();
988        Criterion onlyReferencingCriterion = new Criterion(
989                TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, 
990                localKey);
991        criteria.where(onlyReferencingCriterion);
992        Set<ObjectKey<?>> toSaveKeys = new HashSet<ObjectKey<?>>();
993        for (TorqueDynamicUserDelegates toSaveElement : toSave)
994        {
995            ObjectKey<?> toSaveKey = toSaveElement.getPrimaryKey();
996            if (toSaveKey.getValue() != null)
997            {
998                toSaveKeys.add(toSaveKey);
999            }
1000        }
1001        
1002        // calculate intersection between objects in the database
1003        // and objects in the list
1004        List<TorqueDynamicUserDelegates> intersection;
1005        if (toSaveKeys.isEmpty())
1006        {
1007            intersection = new ArrayList<TorqueDynamicUserDelegates>();
1008        }
1009        else
1010        {
1011            Criterion toSaveKeyCriterion = null;
1012            for (ObjectKey<?> toSaveKey : toSaveKeys)
1013            {
1014                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
1015                 Criterion p0 = new Criterion(
1016                        TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID,
1017                        toSavePrimaryKeys[0]);
1018                 Criterion p1 = new Criterion(
1019                        TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID,
1020                        toSavePrimaryKeys[1]);
1021                p0.and(p1);
1022                if (toSaveKeyCriterion == null)
1023                {
1024                    toSaveKeyCriterion = p0;
1025                }
1026                else
1027                {
1028                    toSaveKeyCriterion.or(p0);
1029                }
1030            }
1031            criteria.and(toSaveKeyCriterion);
1032            intersection = TorqueDynamicUserDelegatesPeer.doSelect(
1033                    criteria,
1034                    connection);
1035        }
1036        if (toLinkTo.isTorqueDynamicUserDelegatesRelatedByDelegateeUserIdsInitialized())
1037        {
1038            toLinkTo.getTorqueDynamicUserDelegatesRelatedByDelegateeUserIds().clear();
1039        }
1040        else
1041        {
1042           toLinkTo.initTorqueDynamicUserDelegatesRelatedByDelegateeUserIds();
1043        }
1044        for (TorqueDynamicUserDelegates toSaveElement : toSave)
1045        {
1046            int listIndex = intersection.indexOf(toSaveElement);
1047            if (listIndex == -1)
1048            {
1049                toLinkTo.addTorqueDynamicUserDelegatesRelatedByDelegateeUserId(toSaveElement);
1050                toSaveElement.save(connection);
1051            }
1052            else
1053            {
1054                toLinkTo.addTorqueDynamicUserDelegatesRelatedByDelegateeUserId(toSaveElement);
1055                toSaveElement.setNew(false);
1056                if (!toSaveElement.valueEquals(intersection.get(listIndex)))
1057                {
1058                    //force saving if content differs
1059                    toSaveElement.setModified(true);
1060                }
1061                toSaveElement.save(connection);
1062            }
1063            toSaveKeys.add(toSaveElement.getPrimaryKey());
1064        }
1065        
1066        // delete elements not in intersection
1067        Criteria deleteCriteria = new Criteria();
1068        deleteCriteria.where(onlyReferencingCriterion);
1069        if (!toSaveKeys.isEmpty())
1070        {
1071            Criterion toSaveKeyCriterion = null;
1072            for (ObjectKey<?> toSaveKey : toSaveKeys)
1073            {
1074                SimpleKey<?>[] toSavePrimaryKeys = (SimpleKey[]) toSaveKey.getValue();
1075                  Criterion p0 = new Criterion(
1076                         TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID,
1077                         toSavePrimaryKeys[0],
1078                         Criteria.NOT_EQUAL);
1079                  Criterion p1 = new Criterion(
1080                         TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID,
1081                         toSavePrimaryKeys[1],
1082                         Criteria.NOT_EQUAL);
1083                p0.or(p1);
1084                if (toSaveKeyCriterion == null)
1085                {
1086                    toSaveKeyCriterion = p0;
1087                }
1088                else
1089                {
1090                    toSaveKeyCriterion.and(p0);
1091                }
1092            }
1093            deleteCriteria.and(toSaveKeyCriterion);
1094        }
1095        TorqueDynamicUserDelegatesPeer.doDelete(deleteCriteria, connection);
1096    }
1097
1098
1099}