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.expr.AmberExpr;
033: import com.caucho.amber.expr.EmbeddedExpr;
034: import com.caucho.amber.expr.PathExpr;
035: import com.caucho.amber.query.QueryParser;
036: import com.caucho.amber.table.Column;
037: import com.caucho.amber.table.Table;
038: import com.caucho.amber.type.EmbeddableType;
039: import com.caucho.amber.type.RelatedType;
040: import com.caucho.amber.type.Type;
041: import com.caucho.config.ConfigException;
042: import com.caucho.java.JavaWriter;
043: import com.caucho.log.Log;
044: import com.caucho.util.CharBuffer;
045: import com.caucho.util.L10N;
046:
047: import java.io.IOException;
048: import java.util.ArrayList;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.logging.Logger;
052:
053: /**
054: * Configuration for a bean's embedded field
055: */
056: public class EntityEmbeddedField extends AbstractField {
057: private static final L10N L = new L10N(EntityEmbeddedField.class);
058: protected static final Logger log = Logger
059: .getLogger(EntityEmbeddedField.class.getName());
060:
061: private EmbeddableType _embeddableType;
062:
063: private ArrayList<EmbeddedSubField> _subFields;
064:
065: private boolean _isInsert = true;
066: private boolean _isUpdate = true;
067:
068: public EntityEmbeddedField(RelatedType ownerType,
069: EmbeddableType embeddableType, String name)
070: throws ConfigException {
071: super (ownerType, name);
072:
073: setEmbeddableType(embeddableType);
074: }
075:
076: public EntityEmbeddedField(RelatedType ownerType,
077: EmbeddableType embeddableType) {
078: super (ownerType);
079:
080: setEmbeddableType(embeddableType);
081: }
082:
083: public EmbeddableType getEmbeddableType() {
084: return _embeddableType;
085: }
086:
087: /**
088: * Sets the result type.
089: */
090: public void setEmbeddableType(EmbeddableType type) {
091: _embeddableType = type;
092:
093: _subFields = new ArrayList<EmbeddedSubField>();
094:
095: ArrayList<AmberField> fields = type.getFields();
096: for (int i = 0; i < fields.size(); i++) {
097: _subFields.add(createSubField(fields.get(i), i));
098: }
099: }
100:
101: protected EmbeddedSubField createSubField(AmberField field,
102: int index) {
103: return new EmbeddedSubField(this , field, index);
104: }
105:
106: /**
107: * Sets the result type.
108: */
109: public Type getType() {
110: return _embeddableType;
111: }
112:
113: /**
114: * Returns the subfields.
115: */
116: public ArrayList<EmbeddedSubField> getSubFields() {
117: return _subFields;
118: }
119:
120: /**
121: * Returns true if the property is an @EmbeddedId.
122: */
123: public boolean isEmbeddedId() {
124: return false;
125: }
126:
127: /**
128: * Set true if the property should be saved on an insert.
129: */
130: public void setInsert(boolean isInsert) {
131: _isInsert = isInsert;
132: }
133:
134: /**
135: * Set true if the property should be saved on an update.
136: */
137: public void setUpdate(boolean isUpdate) {
138: _isUpdate = isUpdate;
139: }
140:
141: /**
142: * Initializes the property.
143: */
144: public void init() throws ConfigException {
145: super .init();
146: }
147:
148: /**
149: * Returns the null value.
150: */
151: public String generateNull() {
152: return getType().generateNull();
153: }
154:
155: /**
156: * Generates the set property.
157: */
158: public void generateGetProperty(JavaWriter out) throws IOException {
159: if (!isFieldAccess() && getGetterMethod() == null)
160: return;
161:
162: out.println();
163: out.println("public " + getJavaTypeName() + " "
164: + getGetterName() + "()");
165: out.println("{");
166: out.pushDepth();
167:
168: // XXX: must not load the entity to return the pk. Avoids StackOverflow.
169: if (!(this instanceof EmbeddedIdField)) {
170: out.println("if (__caucho_session != null)");
171: out.println(" __caucho_load_" + getLoadGroupIndex()
172: + "(__caucho_session);");
173: out.println();
174: }
175:
176: out.println("return " + generateSuperGetter() + ";");
177:
178: out.popDepth();
179: out.println("}");
180: }
181:
182: /**
183: * Generates the set property.
184: */
185: public void generateSetProperty(JavaWriter out) throws IOException {
186: if (!isFieldAccess()
187: && (getGetterMethod() == null || getSetterMethod() == null
188: && !isAbstract()))
189: return;
190:
191: out.println();
192: out.println("public void " + getSetterName() + "("
193: + getJavaTypeName() + " v)");
194: out.println("{");
195: out.pushDepth();
196:
197: if (!_isUpdate) {
198: out.println("if (__caucho_session == null)");
199: out.println(" " + generateSuperSetter("v") + ";");
200: } else {
201: out.println(getJavaTypeName() + " oldValue = "
202: + generateSuperGetter() + ";");
203:
204: int maskGroup = getLoadGroupIndex() / 64;
205: String loadVar = "__caucho_loadMask_" + maskGroup;
206:
207: long mask = 1L << (getLoadGroupIndex() % 64);
208:
209: if (getJavaTypeName().equals("java.lang.String")) {
210: out
211: .println("if ((oldValue == v || v != null && v.equals(oldValue)) && ("
212: + loadVar + " & " + mask + "L) != 0L)");
213: out.println(" return;");
214: } else {
215: out.println("if (oldValue == v && (" + loadVar + " & "
216: + mask + "L) != 0)");
217: out.println(" return;");
218: }
219:
220: out.println(generateSuperSetter("v") + ";");
221:
222: int dirtyGroup = getIndex() / 64;
223: String dirtyVar = "__caucho_dirtyMask_" + dirtyGroup;
224:
225: long dirtyMask = 1L << (getIndex() % 64);
226:
227: out.println();
228: out.println("long oldMask = " + dirtyVar + ";");
229: out.println(dirtyVar + " |= " + dirtyMask + "L;");
230: out.println();
231: out
232: .println("if (__caucho_session != null && oldMask == 0)");
233: out.println(" __caucho_session.update(this);");
234: }
235:
236: out.popDepth();
237: out.println("}");
238: }
239:
240: /**
241: * Generates code to copy to an object.
242: */
243: public void generateCopy(JavaWriter out, String dest, String source)
244: throws IOException {
245: // XXX: how to make a new instance?
246:
247: String value = generateGet(source);
248:
249: out.println(generateSet(dest, value) + ";");
250: }
251:
252: /**
253: * Generates the select clause.
254: */
255: public String generateLoadSelect(Table table, String id) {
256: if (getTable() != table)
257: return null;
258: else
259: return generateSelect(id);
260: }
261:
262: /**
263: * Generates the select clause.
264: */
265: public String generateSelect(String id) {
266: StringBuilder sb = new StringBuilder();
267:
268: for (int i = 0; i < _subFields.size(); i++) {
269: if (i > 0)
270: sb.append(", ");
271:
272: sb.append(_subFields.get(i).generateSelect(id));
273: }
274:
275: return sb.toString();
276: }
277:
278: /**
279: * Generates the where clause.
280: */
281: public String generateWhere(String id) {
282: StringBuilder sb = new StringBuilder();
283:
284: for (int i = 0; i < _subFields.size(); i++) {
285: if (i > 0)
286: sb.append(" and ");
287:
288: sb.append(_subFields.get(i).generateWhere(id));
289: }
290:
291: return sb.toString();
292: }
293:
294: /**
295: * Generates the insert.
296: */
297: public void generateInsertColumns(ArrayList<String> columns) {
298: if (_isInsert) {
299: for (int i = 0; i < _subFields.size(); i++) {
300: _subFields.get(i).generateInsertColumns(columns);
301: }
302: }
303: }
304:
305: /**
306: * Generates the update set clause
307: */
308: public void generateUpdate(CharBuffer sql) {
309: if (_isUpdate) {
310: boolean isFirst = true;
311:
312: for (int i = 0; i < _subFields.size(); i++) {
313: if (i > 0)
314: sql.append(", ");
315:
316: _subFields.get(i).generateUpdate(sql);
317: }
318: }
319: }
320:
321: /**
322: * Generates the set clause for the insert clause.
323: */
324: public void generateInsertSet(JavaWriter out, String pstmt,
325: String index, String obj) throws IOException {
326: if (_isInsert)
327: generateSet(out, pstmt, index, obj);
328: else if (getLoadGroupIndex() != 0) {
329: int groupIndex = getLoadGroupIndex();
330: int group = groupIndex / 64;
331: long groupMask = 1L << (groupIndex % 64);
332: out.println("__caucho_loadMask_" + group + " &= ~"
333: + groupMask + "L;");
334: }
335: }
336:
337: /**
338: * Generates the set clause for the insert clause.
339: */
340: public void generateUpdateSet(JavaWriter out, String pstmt,
341: String index, String obj) throws IOException {
342: if (_isUpdate)
343: generateSet(out, pstmt, index, obj);
344: }
345:
346: /**
347: * Generates the set clause.
348: */
349: public void generateSet(JavaWriter out, String pstmt, String index,
350: String obj) throws IOException {
351: if (!isFieldAccess() && getGetterMethod() == null)
352: return;
353:
354: for (int i = 0; i < _subFields.size(); i++) {
355:
356: }
357: /*
358: for (Map.Entry<String, Column> entry : _columns.entrySet()) {
359: Column column = entry.getValue();
360:
361: String getter = _fieldNameByColumn.get(column.getName());
362:
363: EmbeddableType embeddableType = getEmbeddableType();
364:
365: if (! getSourceType().isFieldAccess())
366: getter = "get" + Character.toUpperCase(getter.charAt(0)) +
367: getter.substring(1) + "()";
368:
369: out.println("if (" + generateGet(obj) + " == null) {");
370: out.pushDepth();
371:
372: // embeddableType.generateSetNull(out, pstmt, "index++");
373: column.generateSet(out, pstmt, index, null);
374:
375: out.popDepth();
376: out.println("} else");
377: out.pushDepth();
378: column.generateSet(out, pstmt, index, generateGet(obj)+"."+getter);
379: out.popDepth();
380: }
381: */
382: }
383:
384: /**
385: * Generates get property.
386: */
387: public void generateGetPrimaryKey(CharBuffer cb) {
388: if (!isFieldAccess() && getGetterMethod() == null)
389: return;
390:
391: /*
392: String thisGetter = generateGet("this");
393:
394: EmbeddableType embeddableType = getEmbeddableType();
395:
396: ArrayList<AmberField> fields = embeddableType.getFields();
397: for (int i = 0; i < fields.size(); i++) {
398: if (i != 0)
399: cb.append(", ");
400:
401: AmberField field = fields.get(i);
402:
403: String getter = field.getName();
404:
405: if (! getSourceType().isFieldAccess()) {
406: getter = "get" + Character.toUpperCase(getter.charAt(0)) +
407: getter.substring(1) + "()";
408: }
409:
410: cb.append(thisGetter + "." + getter);
411: }
412: */
413: }
414:
415: /**
416: * Generates loading code
417: */
418: public int generateLoad(JavaWriter out, String rs, String indexVar,
419: int index) throws IOException {
420: /*
421: String var = "amber_ld_embedded" + index;
422:
423: out.print(getJavaTypeName());
424: out.println(" " + var + " = new "+getJavaTypeName()+"();");
425: */
426:
427: // jpa/0w01
428: String value = (_embeddableType.getJavaTypeName()
429: + ".__caucho_make(aConn, rs, " + indexVar + " + "
430: + index + ")");
431:
432: // XXX: should cound
433: index += _subFields.size();
434:
435: out.println(generateSuperSetter(value) + ";");
436:
437: // out.println("__caucho_loadMask |= " + (1L << getIndex()) + "L;");
438:
439: return index;
440: }
441:
442: /**
443: * Creates the expression for the field.
444: */
445:
446: public AmberExpr createExpr(QueryParser parser, PathExpr parent) {
447: return new EmbeddedExpr(parent, _embeddableType, _subFields);
448: }
449: }
|