| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.type.impl; |
| 20 | |
| 21 | import java.util.*; |
| 22 | import hu.netmind.beankeeper.parser.*; |
| 23 | import hu.netmind.beankeeper.common.StoreException; |
| 24 | import hu.netmind.beankeeper.model.*; |
| 25 | import hu.netmind.beankeeper.transaction.Transaction; |
| 26 | import hu.netmind.beankeeper.transaction.TransactionTracker; |
| 27 | import hu.netmind.beankeeper.db.Database; |
| 28 | import hu.netmind.beankeeper.schema.SchemaManager; |
| 29 | |
| 30 | /** |
| 31 | * Collection handler implementation. |
| 32 | * @author Brautigam Robert |
| 33 | * @version Revision: $Revision$ |
| 34 | */ |
| 35 | public class CollectionHandler extends ContainerHandler |
| 36 | { |
| 37 | private Class collectionClass; |
| 38 | private TransactionTracker transactionTracker = null; // Injected |
| 39 | private Database database = null; // Injected |
| 40 | private SchemaManager schemaManager = null; // Injected |
| 41 | |
| 42 | /** |
| 43 | * Constructor. |
| 44 | */ |
| 45 | public CollectionHandler(Class collectionClass) |
| 46 | { |
| 47 | this.collectionClass=collectionClass; |
| 48 | } |
| 49 | |
| 50 | public Class getContainerClass() |
| 51 | { |
| 52 | return collectionClass; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Create the subtable for the collection. |
| 57 | */ |
| 58 | public void ensureTableExists(ClassInfo parentInfo, String attributeName, boolean create) |
| 59 | { |
| 60 | Transaction tx = transactionTracker.getTransaction(TransactionTracker.TX_REQUIRED); |
| 61 | tx.begin(); |
| 62 | try |
| 63 | { |
| 64 | // Ensure map helper table |
| 65 | HashMap listAttributeTypes = new HashMap(); |
| 66 | listAttributeTypes.put("persistence_id",Long.class); |
| 67 | listAttributeTypes.put("persistence_start",Long.class); |
| 68 | listAttributeTypes.put("persistence_end",Long.class); |
| 69 | listAttributeTypes.put("persistence_txstartid",Long.class); |
| 70 | listAttributeTypes.put("persistence_txstart",Long.class); |
| 71 | listAttributeTypes.put("persistence_txendid",Long.class); |
| 72 | listAttributeTypes.put("persistence_txend",Long.class); |
| 73 | listAttributeTypes.put("value",Long.class); |
| 74 | ArrayList listKeys = new ArrayList(); |
| 75 | listKeys.add("persistence_id"); |
| 76 | listKeys.add("persistence_txstart"); |
| 77 | listKeys.add("value"); |
| 78 | database.ensureTable( |
| 79 | tx,schemaManager.getTableName( |
| 80 | parentInfo.getAttributeClassEntry(attributeName),attributeName), |
| 81 | listAttributeTypes,listKeys,create); |
| 82 | } catch ( StoreException e ) { |
| 83 | tx.markRollbackOnly(); |
| 84 | throw e; |
| 85 | } catch ( Throwable e ) { |
| 86 | tx.markRollbackOnly(); |
| 87 | throw new StoreException("Unknown exception",e); |
| 88 | } finally { |
| 89 | tx.commit(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create the approriate symbol entry when parsing a query. |
| 95 | */ |
| 96 | public WhereResolver.SymbolTableEntry getSymbolEntry(AttributeSpecifier spec, |
| 97 | WhereResolver.SymbolTableEntry previousEntry, ClassInfo previousInfo, ReferenceTerm previousTerm) |
| 98 | { |
| 99 | String attributeName = spec.getIdentifier(); |
| 100 | // Create entry |
| 101 | WhereResolver.SymbolTableEntry entry = new WhereResolver.SymbolTableEntry(); |
| 102 | entry.specifiedTerm = new SpecifiedTableTerm( |
| 103 | schemaManager.getTableName( |
| 104 | previousInfo.getAttributeClassEntry(attributeName),attributeName), null); |
| 105 | entry.automatic = true; |
| 106 | entry.referenceColumn="value"; |
| 107 | entry.type = WhereResolver.SymbolTableEntry.TYPE_HANDLED; |
| 108 | // Create expression |
| 109 | Expression connectorExpression = new Expression(); |
| 110 | ReferenceTerm leftTerm1 = previousTerm; |
| 111 | if ( previousEntry.automatic ) |
| 112 | previousEntry.termList.add(leftTerm1); |
| 113 | connectorExpression.add(leftTerm1); |
| 114 | connectorExpression.add("="); |
| 115 | ReferenceTerm rightTerm1 = new ReferenceTerm(entry.specifiedTerm,"persistence_id"); |
| 116 | entry.termList.add(rightTerm1); |
| 117 | connectorExpression.add(rightTerm1); |
| 118 | entry.expression=connectorExpression; |
| 119 | // Return |
| 120 | return entry; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Determine the next class info after the given specifier. |
| 125 | */ |
| 126 | public ClassInfo getSymbolInfo(WhereResolver.SymbolTableEntry entry, |
| 127 | AttributeSpecifier spec) |
| 128 | throws ParserException |
| 129 | { |
| 130 | // A list should be the last item, so there should be |
| 131 | // no next specifier. |
| 132 | throw new ParserException(ParserException.ABORT,"list type found, and is not the last item, but it should"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |