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.type.*;
035: import com.caucho.config.ConfigException;
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.sql.ResultSet;
043: import java.sql.SQLException;
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.HashSet;
047: import java.util.Map;
048: import java.util.logging.Logger;
049:
050: /**
051: * Configuration for a bean's field
052: */
053: public class EmbeddedId extends CompositeId {
054: private static final L10N L = new L10N(EmbeddedId.class);
055: protected static final Logger log = Logger
056: .getLogger(EmbeddedId.class.getName());
057:
058: private EmbeddedIdField _embeddedIdField;
059:
060: public EmbeddedId(RelatedType ownerType, EmbeddedIdField key) {
061: super (ownerType);
062:
063: _embeddedIdField = key;
064:
065: for (EmbeddedSubField subField : key.getSubFields()) {
066: IdField subKey = (IdField) subField;
067:
068: addKey(subKey);
069: }
070: }
071:
072: /**
073: * Returns true for an identity key.
074: */
075: public boolean isIdentityGenerator() {
076: return false;
077: }
078:
079: /**
080: * Returns the embedded id field
081: */
082: public EmbeddedIdField getEmbeddedIdField() {
083: return _embeddedIdField;
084: }
085:
086: /**
087: * Returns true if this is an @EmbeddedId
088: */
089: public boolean isEmbeddedId() {
090: return _embeddedIdField != null;
091: }
092:
093: //
094: // Java code generation
095: //
096:
097: /**
098: * Generates code to copy to an object.
099: */
100: public void generateCopy(JavaWriter out, String dest, String source)
101: throws IOException {
102: // XXX: how to make a new instance?
103:
104: String value = _embeddedIdField.generateGet(source);
105:
106: out.println(_embeddedIdField.generateSet(dest, value) + ";");
107: }
108:
109: /**
110: * Returns the foreign type.
111: */
112: public int generateLoadForeign(JavaWriter out, String rs,
113: String indexVar, int index, String name) throws IOException {
114: out.print(_embeddedIdField.getEmbeddableType()
115: .getJavaTypeName());
116: out.print(".__caucho_make");
117: out.print("(aConn, " + rs + ", " + indexVar + " + " + index
118: + ")");
119:
120: ArrayList<IdField> keys = getKeys();
121:
122: index += keys.size();
123:
124: return index;
125: }
126:
127: /**
128: * Generates any class prologue.
129: */
130: public void generatePrologue(JavaWriter out,
131: HashSet<Object> completedSet) throws IOException {
132: }
133:
134: /**
135: * Generates any prologue.
136: */
137: public void generatePrologueMake(JavaWriter out,
138: HashSet<Object> completedSet) throws IOException {
139: }
140:
141: /**
142: * Returns the key for the value
143: */
144: public String generateGetProperty(String objThis) {
145: return _embeddedIdField.generateGet(objThis);
146: }
147: }
|