| 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.List; |
| 22 | import hu.netmind.beankeeper.parser.*; |
| 23 | import hu.netmind.beankeeper.query.LazyList; |
| 24 | import hu.netmind.beankeeper.schema.SchemaManager; |
| 25 | import org.apache.log4j.Logger; |
| 26 | |
| 27 | /** |
| 28 | * Keeps track of a container's items' class. |
| 29 | * @author Brautigam Robert |
| 30 | * @version CVS Revision: $Revision$ |
| 31 | */ |
| 32 | public class ContainerItemClass |
| 33 | { |
| 34 | private static Logger logger = Logger.getLogger(ContainerItemClass.class); |
| 35 | |
| 36 | private String itemClassName; |
| 37 | private SchemaManager schemaManager = null; |
| 38 | |
| 39 | public ContainerItemClass(SchemaManager schemaManager, String itemClassName) |
| 40 | { |
| 41 | this.schemaManager=schemaManager; |
| 42 | this.itemClassName=itemClassName; |
| 43 | } |
| 44 | |
| 45 | public void clear() |
| 46 | { |
| 47 | itemClassName=null; |
| 48 | } |
| 49 | |
| 50 | public String getItemClassName() |
| 51 | { |
| 52 | return itemClassName; |
| 53 | } |
| 54 | |
| 55 | public void updateItemClassName(List originalList, Class itemClass, boolean first) |
| 56 | { |
| 57 | if ( logger.isDebugEnabled() ) |
| 58 | logger.debug("updating former class: "+itemClassName+", with: "+itemClass+", first: "+first); |
| 59 | try |
| 60 | { |
| 61 | if ( itemClassName == null ) |
| 62 | { |
| 63 | // There was no previous class |
| 64 | itemClassName = itemClass.getName(); |
| 65 | } else { |
| 66 | // If there was a previous class, then search for |
| 67 | // a common superclass (this is at worst java.lang.Object, |
| 68 | // but there is one). |
| 69 | Class commonClass = Class.forName(itemClassName); // Start from old item class |
| 70 | if ( (originalList!=null) && (originalList instanceof LazyList) && |
| 71 | (((LazyList)originalList).getStmts().size()==1) && |
| 72 | (first) ) |
| 73 | { |
| 74 | // This means, that the original list has only one |
| 75 | // real statement. If the itemlist class deteriorated |
| 76 | // to Object, this is the chance to get back to a |
| 77 | // normal class. |
| 78 | QueryStatement stmt = (QueryStatement) ((LazyList)originalList).getStmts().get(0); |
| 79 | String tableName = ((TableTerm)stmt.getSelectTerms().get(0)).getTableName(); |
| 80 | commonClass = schemaManager.getClassEntry(tableName).getSourceClass(); |
| 81 | } |
| 82 | while ( ! commonClass.isAssignableFrom(itemClass) ) |
| 83 | commonClass = commonClass.getSuperclass(); |
| 84 | itemClassName = commonClass.getName(); |
| 85 | } |
| 86 | } catch ( Throwable e ) { |
| 87 | logger.warn("could not determine item class, previous: "+itemClassName+", new class: "+itemClass,e); |
| 88 | itemClassName=Object.class.getName(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | |