01: /**
02: * Copyright (C) 2007 NetMind Consulting Bt.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 3 of the License, or (at your option) any later version.
08: *
09: * 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: */package hu.netmind.persistence;
18:
19: import java.util.List;
20: import hu.netmind.persistence.parser.*;
21: import org.apache.log4j.Logger;
22:
23: /**
24: * Keeps track of a container's items' class.
25: * @author Brautigam Robert
26: * @version CVS Revision: $Revision$
27: */
28: public class ContainerItemClass {
29: private static Logger logger = Logger
30: .getLogger(ContainerItemClass.class);
31:
32: private String itemClassName;
33:
34: public ContainerItemClass(String itemClassName) {
35: this .itemClassName = itemClassName;
36: }
37:
38: public void clear() {
39: itemClassName = null;
40: }
41:
42: public String getItemClassName() {
43: return itemClassName;
44: }
45:
46: public void updateItemClassName(List originalList, Class itemClass,
47: boolean first) {
48: if (logger.isDebugEnabled())
49: logger.debug("updating former class: " + itemClassName
50: + ", with: " + itemClass + ", first: " + first);
51: try {
52: if (itemClassName == null) {
53: // There was no previous class
54: itemClassName = itemClass.getName();
55: } else {
56: // If there was a previous class, then search for
57: // a common superclass (this is at worst java.lang.Object,
58: // but there is one).
59: Class commonClass = Class.forName(itemClassName); // Start from old item class
60: if ((originalList != null)
61: && (originalList instanceof LazyList)
62: && (((LazyList) originalList).getStmts().size() == 1)
63: && (first)) {
64: // This means, that the original list has only one
65: // real statement. If the itemlist class deteriorated
66: // to Object, this is the chance to get back to a
67: // normal class.
68: QueryStatement stmt = (QueryStatement) ((LazyList) originalList)
69: .getStmts().get(0);
70: String tableName = ((TableTerm) stmt
71: .getSelectTerms().get(0)).getTableName();
72: commonClass = ((LazyList) originalList)
73: .getContext().getClassTracker()
74: .getTableClassInfo(tableName)
75: .getSourceEntry().getSourceClass();
76: }
77: while (!commonClass.isAssignableFrom(itemClass))
78: commonClass = commonClass.getSuperclass();
79: itemClassName = commonClass.getName();
80: }
81: } catch (Throwable e) {
82: logger.warn("could not determine item class, previous: "
83: + itemClassName + ", new class: " + itemClass, e);
84: itemClassName = Object.class.getName();
85: }
86: }
87:
88: }
|