001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.j2ee.persistence.entitygenerator;
042:
043: /**
044: * This class represents an instance of an member in an entity bean class which
045: * is backed by a ColumnElement representing a relational database.
046: * @author Christopher Webster
047: */
048:
049: import org.netbeans.modules.dbschema.ColumnElement;
050: import org.netbeans.modules.dbschema.util.SQLTypeUtil;
051:
052: class DbSchemaEntityMember extends EntityMember {
053:
054: /**
055: * is this member part of primary key
056: */
057: private boolean isPrimaryKey;
058:
059: /**
060: * Original mapping to sql type
061: */
062: private SQLType sqlType;
063:
064: /**
065: * Column Element providing metadata
066: */
067: private ColumnElement columnElement;
068:
069: public DbSchemaEntityMember(ColumnElement element) {
070: columnElement = element;
071: sqlType = SQLType.getSQLType(element.getType());
072: setMemberName(makeFieldName(element.getName().getName()));
073: isPrimaryKey = false;
074: setMemberType(sqlType.getMemberType(element));
075: }
076:
077: public boolean isNullable() {
078: return columnElement.isNullable();
079: }
080:
081: public boolean isPrimaryKey() {
082: return isPrimaryKey;
083: }
084:
085: public void setPrimaryKey(boolean isPk, boolean isPkField) {
086: isPrimaryKey = isPk;
087:
088: // this is relevant for CMP 2.1 and earlier where a pk field is not
089: // allowed to be of a primitive type, so the first corresponding class
090: // if extracted from the sqlType
091: if (isPkField) {
092: setMemberType(getRespectiveNonPrimitiveType());
093: }
094: }
095:
096: /**
097: * Tries to get the respective non-primitive type for the type of
098: * this member. In other words, gets the respective wrapper class for
099: * the member type if it is a primitive <code>int, long, short, byte,
100: * double, float or char</code>, otherwise
101: * returns <code>sqlType#getFirstNonPrimitiveType</code>.
102: */
103: private String getRespectiveNonPrimitiveType() {
104: String type = getMemberType();
105: if ("int".equals(type)) {//NOI18N
106: return Integer.class.getName();
107: } else if ("long".equals(type)) {//NOI18N
108: return Long.class.getName();
109: } else if ("short".equals(type)) {//NOI18N
110: return Short.class.getName();
111: } else if ("byte".equals(type)) {//NOI18N
112: return Byte.class.getName();
113: } else if ("double".equals(type)) {//NOI18N
114: return Double.class.getName();
115: } else if ("float".equals(type)) {//NOI18N
116: return Float.class.getName();
117: } else if ("char".equals(type)) {//NOI18N
118: return Character.class.getName();
119: }
120: return sqlType.getFirstNonPrimitiveType();
121: }
122:
123: private ColumnElement getColumnElement() {
124: return columnElement;
125: }
126:
127: public boolean supportsFinder() {
128: return sqlType.supportsFinder();
129: }
130:
131: public String getColumnName() {
132: return getColumnElement().getName().getName();
133: }
134:
135: public String getTableName() {
136: return getColumnElement().getDeclaringTable().getName()
137: .getName();
138: }
139:
140: public boolean isLobType() {
141: return SQLTypeUtil.isLob(getColumnElement().getType());
142: }
143:
144: }
|