001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.entity.Entity;
032: import com.caucho.amber.entity.EntityItem;
033: import com.caucho.amber.field.AmberField;
034: import com.caucho.amber.manager.AmberConnection;
035: import com.caucho.amber.query.FromItem;
036: import com.caucho.amber.query.QueryParser;
037: import com.caucho.amber.table.LinkColumns;
038: import com.caucho.amber.table.Table;
039: import com.caucho.amber.type.EntityType;
040: import com.caucho.amber.type.Type;
041: import com.caucho.util.CharBuffer;
042: import com.caucho.util.L10N;
043:
044: import java.lang.reflect.Method;
045: import java.sql.ResultSet;
046: import java.sql.SQLException;
047: import java.util.ArrayList;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.Map;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * An entity expression which should be loaded.
056: */
057: public class LoadEntityExpr extends LoadExpr {
058: private static final L10N L = new L10N(LoadEntityExpr.class);
059: private static final Logger log = Logger
060: .getLogger(LoadEntityExpr.class.getName());
061:
062: LoadEntityExpr(PathExpr expr) {
063: super (expr);
064: }
065:
066: /**
067: * Returns the entity type.
068: */
069: public EntityType getEntityType() {
070: return (EntityType) getType();
071: }
072:
073: /**
074: * Binds the expression as a select item.
075: */
076: public AmberExpr bindSelect(QueryParser parser) {
077: _fromItem = _expr.bindSubPath(parser);
078:
079: if (_fromItem == null)
080: throw new NullPointerException(_expr.getClass().getName()
081: + " " + _expr);
082:
083: EntityType type = getEntityType();
084:
085: if (type.getSecondaryTables().size() > 0) {
086: for (AmberField field : type.getFields()) {
087: Table subTable = field.getTable();
088:
089: if (subTable != null && subTable != type.getTable()) {
090: LinkColumns link = subTable.getDependentIdLink();
091:
092: FromItem item = parser.createDependentFromItem(
093: _fromItem, link);
094:
095: _subItems.add(item);
096: }
097: }
098: }
099:
100: return this ;
101: }
102:
103: /**
104: * Returns the object for the expr.
105: */
106: public Object getObject(AmberConnection aConn, ResultSet rs,
107: int index) throws SQLException {
108: return getEntityType().getLoadObject(aConn, rs, index);
109: }
110:
111: /**
112: * Returns the object for the expr.
113: */
114: public Object getCacheObject(AmberConnection aConn, ResultSet rs,
115: int index) throws SQLException {
116: return getCacheObject(aConn, rs, index, null);
117: }
118:
119: /**
120: * Returns the object for the expr.
121: */
122: public Object getCacheObject(AmberConnection aConn, ResultSet rs,
123: int index, Map<AmberExpr, String> joinFetchMap)
124: throws SQLException {
125: return findItem(aConn, rs, index, joinFetchMap);
126: }
127:
128: /**
129: * Returns the object for the expr.
130: */
131: public EntityItem findItem(AmberConnection aConn, ResultSet rs,
132: int index) throws SQLException {
133: return findItem(aConn, rs, index, null);
134: }
135:
136: /**
137: * Returns the object for the expr.
138: */
139: public EntityItem findItem(AmberConnection aConn, ResultSet rs,
140: int index, Map<AmberExpr, String> joinFetchMap)
141: throws SQLException {
142: // jpa/0h13, jpa/1160
143:
144: EntityType entityType = getEntityType();
145:
146: EntityItem item = entityType.getHome().findItem(aConn, rs,
147: index);
148:
149: if (item == null)
150: return null;
151:
152: int offset = entityType.getId().getKeyCount();
153:
154: Entity entity = item.getEntity();
155:
156: /* XXX: jpa/0s2c
157: if (entityType.getDiscriminator() != null) {
158: // jpa/0l47
159: offset++;
160: }
161: */
162: _index = entity.__caucho_load(aConn, rs, index + offset);
163:
164: item.setNumberOfLoadingColumns(_index);
165:
166: return item;
167: }
168: }
|