01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
03: * JR Boyens <gnu-jrb[remove] at gmx dot net>
04: * Distributed under the terms of either:
05: * - the common development and distribution license (CDDL), v1.0; or
06: * - the GNU Lesser General Public License, v2.1 or later
07: * $Id: AbstractManyToManyCollection.java 3669 2007-02-26 13:51:23Z gbevin $
08: */
09: package com.uwyn.rife.database.querymanagers.generic;
10:
11: import static com.uwyn.rife.database.querymanagers.generic.GenericQueryManagerRelationalUtils.generateManyToManyJoinColumnName;
12: import static com.uwyn.rife.database.querymanagers.generic.GenericQueryManagerRelationalUtils.generateManyToManyJoinTableName;
13:
14: import java.util.Collection;
15: import java.util.List;
16:
17: import com.uwyn.rife.database.queries.Select;
18:
19: abstract class AbstractManyToManyCollection<E> implements Collection<E> {
20: private AbstractGenericQueryManager mQueryManager;
21: private String mColumnName1;
22: private int mObjectId;
23: private ManyToManyDeclaration mDeclaration;
24:
25: AbstractManyToManyCollection(AbstractGenericQueryManager manager,
26: String columnName1, int objectId,
27: ManyToManyDeclaration declaration) {
28: mQueryManager = manager;
29: mColumnName1 = columnName1;
30: mObjectId = objectId;
31: mDeclaration = declaration;
32: }
33:
34: protected List restoreManyToManyMappings() {
35: GenericQueryManager association_manager = mQueryManager
36: .createNewManager(mDeclaration.getAssociationType());
37: String join_table = generateManyToManyJoinTableName(
38: mDeclaration, mQueryManager, association_manager);
39: final String column2_name = generateManyToManyJoinColumnName(association_manager);
40:
41: RestoreQuery restore_mappings = association_manager
42: .getRestoreQuery().fields(
43: mDeclaration.getAssociationType()).joinInner(
44: join_table,
45: Select.ON,
46: association_manager.getTable()
47: + "."
48: + association_manager
49: .getIdentifierName() + " = "
50: + join_table + "." + column2_name)
51: .where(join_table + "." + mColumnName1, "=", mObjectId);
52:
53: // restore the many to many associations
54: return association_manager.restore(restore_mappings);
55: }
56: }
|