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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.field;
031:
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.amber.table.Column;
034: import com.caucho.amber.table.LinkColumns;
035: import com.caucho.amber.type.RelatedType;
036: import com.caucho.java.JavaWriter;
037: import com.caucho.log.Log;
038: import com.caucho.util.CharBuffer;
039: import com.caucho.util.L10N;
040:
041: import java.io.IOException;
042: import java.util.ArrayList;
043: import java.util.HashSet;
044: import java.util.logging.Logger;
045:
046: /**
047: * Configuration for a bean's field
048: */
049: public class SubId extends Id {
050: private static final L10N L = new L10N(SubId.class);
051: protected static final Logger log = Log.open(SubId.class);
052:
053: private Id _parentId;
054: private LinkColumns _link;
055:
056: public SubId(RelatedType ownerType, RelatedType rootType) {
057: super (ownerType, new ArrayList<IdField>());
058:
059: _parentId = rootType.getId();
060: }
061:
062: /**
063: * Returns the parent keys.
064: */
065: public ArrayList<IdField> getParentKeys() {
066: return _parentId.getKeys();
067: }
068:
069: /**
070: * Returns the keys.
071: */
072: public ArrayList<IdField> getKeys() {
073: return _parentId.getKeys();
074: }
075:
076: /**
077: * Returns the foreign type.
078: */
079: public String getForeignTypeName() {
080: return _parentId.getForeignTypeName();
081: }
082:
083: /**
084: * Generates any prologue.
085: */
086: public void generatePrologue(JavaWriter out,
087: HashSet<Object> completedSet) throws IOException {
088: }
089:
090: /**
091: * Returns the foreign type.
092: */
093: public int generateLoadForeign(JavaWriter out, String rs,
094: String indexVar, int index) throws IOException {
095: return _parentId.generateLoadForeign(out, rs, indexVar, index);
096: }
097:
098: /**
099: * Returns the foreign type.
100: */
101: public int generateLoadForeign(JavaWriter out, String rs,
102: String indexVar, int index, String name) throws IOException {
103: return _parentId.generateLoadForeign(out, rs, indexVar, index,
104: name);
105: }
106:
107: /**
108: * Generates the select clause.
109: */
110: public String generateSelect(String id) {
111: ArrayList<IdField> keys = getParentKeys();
112:
113: CharBuffer cb = CharBuffer.allocate();
114:
115: for (int i = 0; i < keys.size(); i++) {
116: if (i != 0)
117: cb.append(", ");
118:
119: cb.append(keys.get(i).generateSelect(id));
120: }
121:
122: return cb.close();
123: }
124:
125: /**
126: * Generates the select clause.
127: */
128: public String generateLoadSelect(String id) {
129: return null;
130: }
131:
132: /**
133: * Returns the key for the value
134: */
135: public String generateGetProperty(String value) {
136: return _parentId.generateGetProperty(value);
137: }
138:
139: /**
140: * Generates loading cache
141: */
142: public void generateLoadFromObject(JavaWriter out, String obj)
143: throws IOException {
144: _parentId.generateLoadFromObject(out, obj);
145: }
146:
147: /**
148: * Generates loading cache
149: */
150: public void generateSet(JavaWriter out, String obj)
151: throws IOException {
152: _parentId.generateSet(out, obj);
153: }
154:
155: /**
156: * Generates loading cache
157: */
158: public void generateUpdateFromObject(JavaWriter out, String obj)
159: throws IOException {
160: _parentId.generateUpdateFromObject(out, obj);
161: }
162:
163: /**
164: * Generates the where clause.
165: */
166: public String generateMatchArgWhere(String id) {
167: ArrayList<IdField> keys = getParentKeys();
168:
169: CharBuffer cb = CharBuffer.allocate();
170:
171: for (int i = 0; i < keys.size(); i++) {
172: if (i != 0)
173: cb.append(" and ");
174:
175: generateMatchArgWhere(cb, keys.get(i), id);
176: }
177:
178: return cb.close();
179: }
180:
181: /**
182: * Generates the where clause.
183: */
184: private void generateMatchArgWhere(CharBuffer cb, IdField parentId,
185: String id) {
186: LinkColumns link = getOwnerType().getTable()
187: .getDependentIdLink();
188:
189: ArrayList<Column> columns = parentId.getColumns();
190:
191: for (int i = 0; i < columns.size(); i++) {
192: Column column = columns.get(i);
193:
194: if (i != 0)
195: cb.append(" and ");
196:
197: cb.append(id);
198: cb.append('.');
199: cb.append(link.getSourceColumn(column).getName());
200: cb.append("=?");
201: }
202: }
203:
204: /**
205: * Generates the where clause.
206: */
207: public String generateCreateTableSQL(AmberPersistenceUnit manager) {
208: return null;
209: }
210:
211: /**
212: * Generates the set clause.
213: */
214: public void generateSetKey(JavaWriter out, String pstmt,
215: String obj, String index) throws IOException {
216: _parentId.generateSetKey(out, pstmt, obj, index);
217: }
218:
219: /**
220: * Generates the set clause.
221: */
222: public void generateSet(JavaWriter out, String pstmt, String obj,
223: String index) throws IOException {
224: _parentId.generateSet(out, pstmt, obj, index);
225: }
226:
227: /**
228: * Generates the set clause.
229: */
230: public void generateSet(JavaWriter out, String pstmt, String index)
231: throws IOException {
232: _parentId.generateSet(out, pstmt, index);
233: }
234:
235: /**
236: * Generates the set clause.
237: */
238: public void generateSetInsert(JavaWriter out, String pstmt,
239: String index) throws IOException {
240: _parentId.generateSetInsert(out, pstmt, index);
241: }
242:
243: /**
244: * Generates code to convert to the type from the object.
245: */
246: public String generateCastFromObject(String value) {
247: return value;
248: }
249:
250: /**
251: * Generates code for a match.
252: */
253: public void generateMatch(JavaWriter out, String key)
254: throws IOException {
255: // jpa/0l30
256: out
257: .println("return "
258: + generateEquals("__caucho_getPrimaryKey()",
259: key) + ";");
260: }
261:
262: /**
263: * Generates code to test the equals.
264: */
265: public String generateEquals(String leftBase, String value) {
266: return leftBase + ".equals(" + value + ")";
267: }
268:
269: /**
270: * Generates the set clause.
271: */
272: public void generateCheckCreateKey(JavaWriter out)
273: throws IOException {
274: }
275:
276: /**
277: * Generates code to convert to the object.
278: */
279: public String toObject(String value) {
280: return value;
281: }
282: }
|