blob: cc9da7073c2ddb88306c903aebcadff481909f42 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2012 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Oracle - initial API and implementation
*
******************************************************************************/
package org.eclipse.persistence.tools.gen.internal;
import java.util.Collections;
import java.util.Iterator;
import org.eclipse.persistence.tools.gen.db.Column;
import org.eclipse.persistence.tools.gen.db.ForeignKey;
import org.eclipse.persistence.tools.gen.db.Table;
import org.eclipse.persistence.tools.gen.internal.util.EntityGenTools;
import org.eclipse.persistence.tools.utility.StringUtil;
/**
* Represents the ORM generation properties for a database column.
* <p>
* This is designed to be created/changed by the generation wizard, and generated using Velocity
* templates. The modified properties (if any) are persisted/retrieved using <code>ORMGenCustomizer</code>.
* <p>
* Provisional API: This interface is part of an interim API that is still under development and
* expected to change significantly before reaching stability. It is available at this early stage
* to solicit feedback from pioneering adopters on the understanding that any code that uses this
* API will almost certainly be broken (repeatedly) as the API evolves.
*
* @version 2.6
*/
@SuppressWarnings("nls")
public class ORMGenColumn {
private Table mTable;
private Column mDbColumn;
private ORMGenCustomizer mCustomizer;
private ORMGenTable mGenTable;
private static String JAVA_LANG_PACKAGE = "java.lang.";
public ORMGenColumn(Column dbColumn, ORMGenCustomizer customizer) {
super();
mDbColumn = dbColumn;
mCustomizer = customizer;
mTable = dbColumn.getTable();
}
public ORMGenCustomizer getCustomizer() {
return mCustomizer;
}
public void setGenTable(ORMGenTable ormGenTable) {
mGenTable = ormGenTable;
}
protected String customized(String propName) {
return getCustomizer().getProperty(propName, mTable.getName(), getDbName());
}
protected boolean customizedBoolean(String propName) {
return getCustomizer().getBooleanProperty(propName, mTable.getName(), getDbName());
}
protected void setCustomized(String propName, String value) {
if (value != null && value.length() == 0) {
value = null;
}
getCustomizer().setProperty(propName, value, mTable.getName(), getDbName());
}
protected void setCustomizedBoolean(String propName, boolean value, boolean defaultValue) {
if (defaultValue == value) {
setCustomized(propName, null); //remove the property
} else {
getCustomizer().setBooleanProperty(propName, value, mTable.getName(), getDbName());
}
}
/**
* Returns the column name.
*/
public String getName() {
String annotationName = this.mCustomizer.getDatabaseAnnotationNameBuilder().
buildColumnAnnotationName(mDbColumn.getName(), mDbColumn);
return annotationName != null ? annotationName : mDbColumn.getName();
}
public String getDbName() {
return mDbColumn.getName();
}
public String getJoinColumnName(){
String annotationName = this.mCustomizer.getDatabaseAnnotationNameBuilder().
buildJoinColumnAnnotationName(mDbColumn);
return annotationName != null ? annotationName : mDbColumn.getName();
}
public Column getDbColumn() {
return this.mDbColumn;
}
/**
* Returns the generated bean property name for the given column.
* Returns null.
*/
public String getPropertyName() {
String name = customized(PROPERTY_NAME);
if (name == null) {
//name = StringUtil.columnNameToVarName(getName());
name = EntityGenTools.convertToUniqueJavaStyleAttributeName(getDbName(), Collections.<String>emptySet());
}
return name;
}
public void setPropertyName(String name) {
if (!StringUtil.equalObjects(name, getPropertyName())) {
setCustomized(PROPERTY_NAME, name);
}
}
/**
* Returns true if the values of name element in the @Column is default
* so we can skip generating the annotation
*
* @return true
*/
public boolean isDefault(){
return isDefaultname() && isUpdateable() && isInsertable();
}
/**
* Returns true if the values of name element in the @Column is default
* so we can skip generating the annotation
*
* @return true
*/
public boolean isDefaultname(){
String propName = getPropertyName();
// String dbColumnName = getName();
// return propName.equalsIgnoreCase( dbColumnName );
String annotationName = this.mCustomizer.getDatabaseAnnotationNameBuilder().
buildColumnAnnotationName(propName, this.mDbColumn );
return annotationName==null;
}
/**
* Returns true if the values of name element in the @Column is default
* so we can skip generating the annotation
*
* @return true
*/
public boolean isDefaultJoinColumnName(String associationRolePropName){
if( !this.mDbColumn.isPartOfForeignKey()){
return false;
}
Iterable<ForeignKey> it = mDbColumn.getTable().getForeignKeys();
Iterator<ForeignKey> i = it.iterator();
while( i.hasNext() ){
ForeignKey fk = i.next();
Column c = fk.getBaseColumns().iterator().next();
if( c.equals( this.mDbColumn ) ){
try{
String annotationName = this.mCustomizer.getDatabaseAnnotationNameBuilder().
buildJoinColumnAnnotationName(associationRolePropName, fk );
return annotationName==null;
}catch(Exception e){
//catch the case that referenced table has multiple primary key columns
return false;
}
}
}
return false;
}
/**
* Returns the column type.
* Returns null.
*/
public String getPropertyType() {
String type = customized(PROPERTY_TYPE);
if (type == null) {
type = getCustomizer().getPropertyTypeFromColumn( this.mDbColumn );
}
if( type.startsWith(JAVA_LANG_PACKAGE) ) {
type = type.substring( JAVA_LANG_PACKAGE.length() );
}
if( type.equals("java.sql.Date")){
type = "java.util.Date";
}
return type;
}
public String getSimplePropertyType() {
return mGenTable.getSimplifiedColType( getPropertyType() );
}
public void setPropertyType(String type) {
if (!StringUtil.equalObjects(type, getPropertyType())) {
setCustomized(PROPERTY_TYPE, type);
}
}
/**
* Returns true if the column type is numeric.
*/
public boolean isNumeric() {
boolean ret = this.mDbColumn.isNumeric();
return ret;
}
/**
* Returns the mapping kind, one of {@link #PROPERTY_MAPPING_KIND}|{@link #ID_MAPPING_KIND}
* |{@link #VERSION_MAPPING_KIND}|{@link #TIMESTAMP_MAPPING_KIND}.
*
* Returns null (defaults to basic property type).
*/
public String getMappingKind() {
String kind = customized(MAPPING_KIND);
if (kind == null) {
kind = getCustomizer().getBasicMappingKind();
if ( this.mDbColumn.isPartOfPrimaryKey()
&& this.mDbColumn.getTable().getPrimaryKeyColumnsSize() == 1) {
kind = getCustomizer().getIdMappingKind();
}
}
return kind;
}
public void setMappingKind(String mappingKind) {
if (!StringUtil.equalObjects(mappingKind, getMappingKind())) {
setCustomized(MAPPING_KIND, mappingKind);
}
}
public boolean isNullable() {
return this.mDbColumn.isNullable();
}
public int getSize() {
if ( this.mDbColumn.isNumeric()){
return mDbColumn.getPrecision();
}
return mDbColumn.getLength();
}
public int getDecimalDigits() {
if ( this.mDbColumn.isNumeric() ){
return mDbColumn.getScale();
}
return -1;
}
public boolean isPrimaryKey() {
return this.mDbColumn.isPartOfPrimaryKey();
}
public boolean isPartOfCompositePrimaryKey() {
return this.mDbColumn.isPartOfPrimaryKey() &&
this.mTable.getPrimaryKeyColumnsSize() > 1;
}
public boolean isForeignKey() {
return this.mDbColumn.isPartOfForeignKey();
}
public boolean isUnique() {
return this.mDbColumn.isPartOfUniqueConstraint();
}
public String getPropertyDescription() {
return customized(PROPERTY_DESC);
}
public boolean isDataTypeLOB() {
return this.mDbColumn.isLOB();
}
public boolean isNeedMapTemporalType() {
String propertyType = this.getPropertyType();
return ( propertyType.equals("java.util.Date") || propertyType.equals("java.util.Calendar") ); //$NON-NLS-2$
}
public String getTemporalType() {
String defaultType = getCustomizer().getPropertyTypeFromColumn( this.mDbColumn );
if( defaultType.equals("java.sql.Date")){
return "DATE";
}else if( defaultType.equals("java.sql.Time")){
return "TIME";
}else {
return "TIMESTAMP";
}
}
/**
* Returns the generated property getter scope, one of {@link #PUBLIC_SCOPE}|{@link #PROTECTED_SCOPE}
* |{@link #PRIVATE_SCOPE}.
* This method never returns null (defaults to public).
*/
public String getPropertyGetScope() {
String scope = customized(PROPERTY_GET_SCOPE);
if (scope == null) {
scope = PUBLIC_SCOPE;
}
return scope;
}
public void setPropertyGetScope(String scope) {
if (!StringUtil.equalObjects(scope, getPropertyGetScope())) {
setCustomized(PROPERTY_GET_SCOPE, scope);
}
}
/**
* Returns the generated property setter scope, one of {@link #PUBLIC_SCOPE}|{@link #PROTECTED_SCOPE}
* |{@link #PRIVATE_SCOPE}.
* This method never returns null (defaults to public).
*/
public String getPropertySetScope() {
String scope = customized(PROPERTY_SET_SCOPE);
if (scope == null) {
scope = PUBLIC_SCOPE;
}
return scope;
}
public void setPropertySetScope(String scope) {
if (!StringUtil.equalObjects(scope, getPropertySetScope())) {
setCustomized(PROPERTY_SET_SCOPE, scope);
}
}
/**
* Returns the generated field member scope, one of {@link #PUBLIC_SCOPE}|{@link #PROTECTED_SCOPE}
* |{@link #PRIVATE_SCOPE}.
* This method never returns null (defaults to private).
*/
public String getFieldScope() {
String scope = customized(FIELD_SCOPE);
if (scope == null) {
scope = PRIVATE_SCOPE;
}
return scope;
}
/**
* Returns true if this column should be used in the
* <code>equals</code> method implementation.
*/
public boolean isUseInEquals() {
return customizedBoolean(USE_IN_EQUALS) || isPrimaryKey();
}
public void setUseInEquals(boolean value) {
setCustomizedBoolean(USE_IN_EQUALS, value, false);
}
/**
* Returns true if this column should be used in the
* <code>toString</code> method implementation.
*/
public boolean isUseInToString() {
return customizedBoolean(USE_IN_TO_STRING) || isPrimaryKey();
}
public void setUseInToString(boolean value) {
setCustomizedBoolean(USE_IN_TO_STRING, value, false);
}
public boolean isUpdateable() {
return !"false".equals(customized(UPDATEABLE)); //defaults to true
}
public void setUpdateable(boolean value) {
setCustomizedBoolean(UPDATEABLE, value, true);
}
public boolean isInsertable() {
return !"false".equals(customized(INSERTABLE)); //defaults to true
}
public void setInsertable(boolean value) {
setCustomizedBoolean(INSERTABLE, value, true);
}
public boolean isGenerated() {
return !"false".equals(customized(GENERATED)); //defaults to true
}
public void setGenerated(boolean value) {
setCustomizedBoolean(GENERATED, value, true);
}
@Override
public String toString() {
return "name=" + getName() + "; type=" + getPropertyType() ; //$NON-NLS-2$
}
/*get/set and field scopes*/
public static final String PUBLIC_SCOPE = "public";
public static final String PROTECTED_SCOPE = "protected";
public static final String PRIVATE_SCOPE = "private";
/*customization properties*/
private static final String PROPERTY_NAME = "propertyName";
protected static final String PROPERTY_TYPE = "propertyType";
protected static final String MAPPING_KIND = "mappingKind";
private static final String PROPERTY_DESC = "propertyDesc";
private static final String PROPERTY_GET_SCOPE = "propertyGetScope";
private static final String PROPERTY_SET_SCOPE = "propertySetScope";
private static final String FIELD_SCOPE = "fieldScope";
private static final String USE_IN_EQUALS = "useInEquals";
private static final String USE_IN_TO_STRING = "useInToString";
private static final String UPDATEABLE = "updateable";
private static final String INSERTABLE = "insertable";
private static final String GENERATED = "genProperty";
}