Author: vyemialyanchyk
Date: 2009-04-17 13:01:20 -0400 (Fri, 17 Apr 2009)
New Revision: 14808
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefColumnInfo.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/EntitiesList.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/IHibernateJPAWizardParams.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/Country.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/DocumentBase.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java
Log:
JBIDE-3842 - @Column.length - setup non default string length functionality
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -7,6 +7,8 @@
public static String AllEntitiesProcessor_header;
public static String AllEntitiesProcessor_message;
public static String AllEntitiesProcessor_setup_annotation_generation_preference;
+ public static String AllEntitiesProcessor_enable_optimistic_locking;
+ public static String AllEntitiesProcessor_default_string_length;
public static String AllEntitiesProcessor_annotate_fields;
public static String AllEntitiesProcessor_annotate_getters;
public static String AllEntitiesProcessor_auto_select_from_class_preference;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties 2009-04-17
17:01:20 UTC (rev 14808)
@@ -11,6 +11,8 @@
AllEntitiesProcessor_header=Hibernate: add JPA annotations
AllEntitiesProcessor_message=Add JPA annotations to the related set of entities. The
following classes will be changed:
AllEntitiesProcessor_setup_annotation_generation_preference=Setup annotation generation
preference:
+AllEntitiesProcessor_enable_optimistic_locking=Enable optimistic locking:
+AllEntitiesProcessor_default_string_length=Default string length (255 by default):
AllEntitiesProcessor_annotate_fields=Annotate fields
AllEntitiesProcessor_annotate_getters=Annotate getters
AllEntitiesProcessor_auto_select_from_class_preference=Auto select from class preference
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -90,11 +90,11 @@
protected AllEntitiesProcessor processor = new AllEntitiesProcessor();
protected JPAMapToolActor() {
- initAnnotationStylePreference();
+ initPreferences();
}
- public void initAnnotationStylePreference() {
- processor.initAnnotationStylePreference();
+ public void initPreferences() {
+ processor.initPreferences();
}
public static JPAMapToolActor getInstance() {
@@ -183,7 +183,7 @@
JdtUiMessages.JPAMapToolActor_message);
}
}
- processor.saveAnnotationStylePreference();
+ processor.savePreferences();
}
private IWorkbenchWindow getActiveWorkbenchWindow() {
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -187,9 +187,46 @@
entityInfo.setAddVersionFlag(false);
}
}
+ else if (JPAConst.isAnnotationColumn(fullyQualifiedName) && node instanceof
NormalAnnotation) {
+ updateAnnotationColumn((NormalAnnotation)node, mappedBy, fullyQualifiedName);
+ }
return true;
}
+ public void updateAnnotationColumn(NormalAnnotation node, String mappedBy, String
fullyQualifiedName) {
+ ITypeBinding tb = node.resolveTypeBinding();
+ CompilationUnit cu = null;
+ ASTNode astNode = node.getParent();
+ if (astNode instanceof FieldDeclaration) {
+ FieldDeclaration fd = (FieldDeclaration)astNode;
+ Iterator itVarNames = fd.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ entityInfo.updateAnnotationColumn(name, node, true);
+ }
+ cu = getCUFromTypeDeclaration(node);
+ }
+ else if (astNode instanceof MethodDeclaration) {
+ MethodDeclaration md = (MethodDeclaration)astNode;
+ if (md.getName().getIdentifier().startsWith("get")) { //$NON-NLS-1$
+ // the getter - process it
+ String name = getReturnIdentifier(md);
+ // process it like FieldDeclaration
+ entityInfo.updateAnnotationColumn(name, node, true);
+ cu = getCUFromFieldMethod(node);
+ }
+ else {
+ // ignore others
+ }
+ }
+ if (cu != null) {
+ if (tb == null) {
+ entityInfo.addRequiredImport(JPAConst.IMPORT_COLUMN);
+ }
+ }
+ }
+
public void updateAnnotationRelInfo(Annotation node, String mappedBy, String
fullyQualifiedName,
RefType type, String annNameShort, String annNameFull) {
ITypeBinding tb = node.resolveTypeBinding();
@@ -445,6 +482,13 @@
}
}
}
+ if ("java.lang.String".equals(tb.getBinaryName())) { //$NON-NLS-1$
+ Iterator itVarNames = list.iterator();
+ while (itVarNames.hasNext()) {
+ String name = (String)itVarNames.next();
+ entityInfo.updateAnnotationColumn(name, null, false);
+ }
+ }
}
}
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -10,14 +10,16 @@
******************************************************************************/
package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
-import java.util.ArrayList;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+
/**
* Service place to collect information about entity class.
* Then this info has been transformed according annotations
@@ -102,6 +104,11 @@
*/
protected Map<String, RefEntityInfo> references = new TreeMap<String,
RefEntityInfo>();
/*
+ * store declared column information for particular field
+ * field id -> RefColumnInfo
+ */
+ protected Map<String, RefColumnInfo> columns = new TreeMap<String,
RefColumnInfo>();
+ /*
* fully qualified entity name -> Set<RefFieldInfo>
* this is generated from references map for easy information get
*/
@@ -211,6 +218,7 @@
else {
removeRequiredImport(JPAConst.IMPORT_VERSION);
}
+ updateColumnAnnotationImport(false);
Iterator<Map.Entry<String, RefEntityInfo>> referencesIt =
getReferences().entrySet().iterator();
while (referencesIt.hasNext()) {
@@ -254,6 +262,15 @@
primaryIdName = ""; //$NON-NLS-1$
}
}
+
+ public void updateColumnAnnotationImport(boolean nonDefault) {
+ if (isNonColumnAnnotatedStringField() && nonDefault) {
+ addRequiredImport(JPAConst.IMPORT_COLUMN);
+ }
+ else {
+ removeRequiredImport(JPAConst.IMPORT_COLUMN);
+ }
+ }
public String getName() {
String[] arr = fullyQualifiedName.split("\\."); //$NON-NLS-1$
@@ -404,6 +421,52 @@
}
}
+ public void updateAnnotationColumn(String fieldId, NormalAnnotation node, boolean exist)
{
+ if (columns == null || fieldId == null) {
+ return;
+ }
+ RefColumnInfo rci = columns.get(fieldId);
+ if (rci == null) {
+ rci = new RefColumnInfo(fieldId);
+ }
+ if (!rci.isExist()) {
+ rci.setExist(exist);
+ }
+ if (node != null) {
+ Map<String, Expression> rciValues = rci.getValues();
+ Iterator it = node.values().iterator();
+ while (it.hasNext()) {
+ Object obj = it.next();
+ if (obj instanceof MemberValuePair) {
+ MemberValuePair mvp = (MemberValuePair)obj;
+ rciValues.put(mvp.getName().getIdentifier(), mvp.getValue());
+ }
+ }
+ }
+ columns.put(fieldId, rci);
+ }
+
+ public RefColumnInfo getRefColumnInfo(String fieldId) {
+ return columns.get(fieldId);
+ }
+
+ /**
+ * returns true in case of there is String field which is not
+ * annotated with @Column
+ */
+ public boolean isNonColumnAnnotatedStringField() {
+ boolean res = false;
+ Iterator<Map.Entry<String, RefColumnInfo>> it =
columns.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<String, RefColumnInfo> entry = it.next();
+ if (!entry.getValue().isExist()) {
+ res = true;
+ break;
+ }
+ }
+ return res;
+ }
+
public Set<RefFieldInfo> getRefFieldInfoSet(String fullyQualifiedName) {
if (mapRefFieldInfo == null) {
return null;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -21,6 +21,7 @@
public static final String IMPORT_MANY2MANY = "javax.persistence.ManyToMany";
//$NON-NLS-1$
public static final String IMPORT_MAPPEDSUPERCLASS =
"javax.persistence.MappedSuperclass"; //$NON-NLS-1$
public static final String IMPORT_VERSION = "javax.persistence.Version";
//$NON-NLS-1$
+ public static final String IMPORT_COLUMN = "javax.persistence.Column";
//$NON-NLS-1$
public static final String ANNOTATION_SERIALIZABLE = "Serializable";
//$NON-NLS-1$
public static final String ANNOTATION_ENTITY = "Entity"; //$NON-NLS-1$
@@ -33,6 +34,7 @@
public static final String ANNOTATION_MANY2MANY = "ManyToMany"; //$NON-NLS-1$
public static final String ANNOTATION_MAPPEDSUPERCLASS = "MappedSuperclass";
//$NON-NLS-1$
public static final String ANNOTATION_VERSION = "Version"; //$NON-NLS-1$
+ public static final String ANNOTATION_COLUMN = "Column"; //$NON-NLS-1$
public static final Vector<String> ALL_IMPORTS = new Vector<String>();
@@ -48,6 +50,7 @@
ALL_IMPORTS.add(IMPORT_MANY2MANY);
ALL_IMPORTS.add(IMPORT_MAPPEDSUPERCLASS);
ALL_IMPORTS.add(IMPORT_VERSION);
+ ALL_IMPORTS.add(IMPORT_COLUMN);
}
static public String getRefType(RefType refType) {
@@ -137,4 +140,12 @@
}
return false;
}
+
+ static public boolean isAnnotationColumn(String fullyQualifiedName) {
+ if (ANNOTATION_COLUMN.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_COLUMN.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefColumnInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefColumnInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefColumnInfo.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.jdt.core.dom.Expression;
+
+/**
+ * Information about @Column annotation for particular field
+ *
+ * @author Vitali Yemialyanchyk
+ */
+public class RefColumnInfo implements Comparable<RefFieldInfo> {
+ /*
+ * field id
+ */
+ protected String fieldId;
+ /*
+ * is column annotation exist for the field
+ */
+ protected boolean exist = false;
+ /*
+ * column attributes
+ */
+ protected Map<String, Expression> values = new HashMap<String,
Expression>();
+ //
+ public RefColumnInfo(String fieldId) {
+ this.fieldId = fieldId;
+ }
+
+ public int compareTo(RefFieldInfo rfi) {
+ return fieldId.compareTo(rfi.fieldId);
+ }
+
+ public boolean equals(Object obj) {
+ if (super.equals(obj)) {
+ return true;
+ }
+ if (obj == null || !(obj instanceof RefFieldInfo)) {
+ return false;
+ }
+ RefFieldInfo rfi = (RefFieldInfo)obj;
+ return fieldId.equals(rfi.fieldId);
+ }
+
+ public int hashCode() {
+ return fieldId.hashCode();
+ }
+
+ public String getFieldId() {
+ return fieldId;
+ }
+
+ public void setFieldId(String fieldId) {
+ this.fieldId = fieldId;
+ }
+
+ public Map<String, Expression> getValues() {
+ return values;
+ }
+
+ public void setValues(Map<String, Expression> values) {
+ this.values = values;
+ }
+
+ public boolean isExist() {
+ return exist;
+ }
+
+ public void setExist(boolean exist) {
+ this.exist = exist;
+ }
+}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -59,10 +59,32 @@
*/
protected AnnotStyle annotationStylePreference = AnnotStyle.FIELDS;
/**
- * annotation style preference of majority
+ * annotation style preference store name
*/
public final static String storePropertyName =
"hibernate.jpa.generation.AnnotationStyle.preference"; //$NON-NLS-1$
+ /**
+ * @Column length - default value
+ */
+ public final static int columnLength = 255;
+ /**
+ * default length for column which corresponds to String field
+ */
+ protected int defaultStrLength = columnLength;
+ /**
+ * default length for column preference store name
+ */
+ public final static String storeDefaultStrLength =
+ "hibernate.jpa.generation.DefaultStrLength.preference"; //$NON-NLS-1$
+ /**
+ * flag to enable optimistic locking
+ */
+ protected boolean enableOptLock = false;
+ /**
+ * flag to enable optimistic locking preference store name
+ */
+ public final static String storeEnableOptLock =
+ "hibernate.jpa.generation.EnableOptLock.preference"; //$NON-NLS-1$
/**
* change info storage
@@ -83,16 +105,25 @@
this.preferenceStore = preferenceStore;
}
- public void initAnnotationStylePreference() {
+ public void initPreferences() {
IPreferenceStore preferenceStore = getPreferenceStore();
+ //
int value = preferenceStore.getInt(storePropertyName);
if (value >= AnnotStyle.values().length) {
value = 0;
}
annotationStyle = AnnotStyle.values()[value];
+ //
+ value = preferenceStore.getInt(storeDefaultStrLength);
+ if (value <= 0) {
+ value = columnLength;
+ }
+ defaultStrLength = value;
+ //
+ enableOptLock = preferenceStore.getBoolean(storeEnableOptLock);
}
- public void saveAnnotationStylePreference() {
+ public void savePreferences() {
IPreferenceStore preferenceStore = getPreferenceStore();
int value = 0;
while (value < AnnotStyle.values().length) {
@@ -105,6 +136,10 @@
value = 0;
}
preferenceStore.setValue(storePropertyName, value);
+ //
+ preferenceStore.setValue(storeDefaultStrLength, defaultStrLength);
+ //
+ preferenceStore.setValue(storeEnableOptLock, enableOptLock);
}
/**
@@ -244,6 +279,9 @@
public void collectModification(ITextFileBufferManager bufferManager, String
fullyQualifiedName,
EntityInfo entityInfo, Map<String, EntityInfo> entities) throws CoreException {
+ //
+ entityInfo.updateColumnAnnotationImport(defaultStrLength != columnLength);
+ //
ChangeStructure cs = new ChangeStructure();
cs.fullyQualifiedName = fullyQualifiedName;
ICompilationUnit icu = Utils.findCompilationUnit(javaProject, fullyQualifiedName);
@@ -259,6 +297,8 @@
// ... rewrite
ProcessEntityInfo processor = new ProcessEntityInfo();
processor.setAnnotationStyle(annotationStyle);
+ processor.setDefaultStrLength(defaultStrLength);
+ processor.setEnableOptLock(enableOptLock);
processor.setEntityInfo(entityInfo);
processor.setEntities(entities);
processor.setASTRewrite(rewriter);
@@ -315,4 +355,20 @@
public void setAnnotationStylePreference(AnnotStyle annotationStylePreference) {
this.annotationStylePreference = annotationStylePreference;
}
+
+ public int getDefaultStrLength() {
+ return defaultStrLength;
+ }
+
+ public void setDefaultStrLength(int defaultStrLength) {
+ this.defaultStrLength = defaultStrLength;
+ }
+
+ public boolean getEnableOptLock() {
+ return enableOptLock;
+ }
+
+ public void setEnableOptLock(boolean enableOptLock) {
+ this.enableOptLock = enableOptLock;
+ }
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -28,6 +28,7 @@
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
@@ -41,6 +42,7 @@
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.JPAConst;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.OwnerType;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefColumnInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefFieldInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType;
@@ -69,6 +71,14 @@
* annotation style
*/
protected AnnotStyle annotationStyle = AnnotStyle.FIELDS;
+ /**
+ * default length for column which corresponds to String field
+ */
+ protected int defaultStrLength = AllEntitiesProcessor.columnLength;
+ /**
+ * flag to enable optimistic locking
+ */
+ protected boolean enableOptLock = false;
public void setEntityInfo(EntityInfo entityInfo) {
this.entityInfo = entityInfo;
@@ -263,6 +273,35 @@
}
}
}
+ if (type.isSimpleType() && (AllEntitiesProcessor.columnLength !=
defaultStrLength)) {
+ SimpleType simpleType = (SimpleType)type;
+ String typeName = simpleType.getName().getFullyQualifiedName();
+ if ("java.lang.String".equals(typeName) ||
"String".equals(typeName)) { //$NON-NLS-1$ //$NON-NLS-2$
+ String fieldId = null;
+ Iterator itVarNames = node.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ fieldId = var.getName().getIdentifier();
+ if (fieldId != null) {
+ break;
+ }
+ }
+ RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
+ if (rci == null || !rci.isExist()) {
+ // if there is no @Column annotation - create new @Column annotation
+ // with user defined default value length
+ NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
+ natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
+ ListRewrite lrw = rewriter.getListRewrite(node,
FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(natd, null);
+ MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
+ mvp.setName(rewriter.getAST().newSimpleName("length")); //$NON-NLS-1$
+ NumberLiteral nl =
rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
+ mvp.setValue(nl);
+ natd.values().add(mvp);
+ }
+ }
+ }
if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
Iterator itVarNames = node.fragments().iterator();
String fieldId = ""; //$NON-NLS-1$
@@ -377,6 +416,27 @@
}
}
}
+ if (type.isSimpleType() && (AllEntitiesProcessor.columnLength !=
defaultStrLength)) {
+ SimpleType simpleType = (SimpleType)type;
+ String typeName = simpleType.getName().getFullyQualifiedName();
+ if ("java.lang.String".equals(typeName) ||
"String".equals(typeName)) { //$NON-NLS-1$ //$NON-NLS-2$
+ String fieldId = returnIdentifier;
+ RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
+ if (rci == null) {
+ // if there is no @Column annotation - create new @Column annotation
+ // with user defined default value length
+ NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
+ natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
+ ListRewrite lrw = rewriter.getListRewrite(node,
FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(natd, null);
+ MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
+ mvp.setName(rewriter.getAST().newSimpleName("length")); //$NON-NLS-1$
+ NumberLiteral nl =
rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
+ mvp.setValue(nl);
+ natd.values().add(mvp);
+ }
+ }
+ }
if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
String fieldId = ""; //$NON-NLS-1$
RefType refType = RefType.UNDEF;
@@ -521,4 +581,20 @@
public void setAnnotationStyle(AnnotStyle annotationStyle) {
this.annotationStyle = annotationStyle;
}
+
+ public int getDefaultStrLength() {
+ return defaultStrLength;
+ }
+
+ public void setDefaultStrLength(int defaultStrLength) {
+ this.defaultStrLength = defaultStrLength;
+ }
+
+ public boolean getEnableOptLock() {
+ return enableOptLock;
+ }
+
+ public void setEnableOptLock(boolean enableOptLock) {
+ this.enableOptLock = enableOptLock;
+ }
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/EntitiesList.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/EntitiesList.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/EntitiesList.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -24,10 +24,14 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Text;
import org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.process.AnnotStyle;
@@ -135,7 +139,7 @@
idx = 2;
}
generateChoice.select(idx);
- final ModifyListener ml = new ModifyListener() {
+ final ModifyListener mlGenerateChoice = new ModifyListener() {
public void modifyText(ModifyEvent e) {
int idx = ((Combo)e.getSource()).getSelectionIndex();
@@ -155,7 +159,55 @@
}
};
- generateChoice.addModifyListener(ml);
+ generateChoice.addModifyListener(mlGenerateChoice);
+
+ /** /
+ // TODO: implement enable optimistic locking functionality
+ Label labelOptLock = new Label(combolabel, SWT.NULL);
+ labelOptLock.setText(JdtUiMessages.AllEntitiesProcessor_enable_optimistic_locking);
+ Button checkboxOptLock = new Button(combolabel, SWT.CHECK);
+ checkboxOptLock.setSelection(params.getEnableOptLock());
+ final Listener mlOptLock = new Listener() {
+
+ public void handleEvent(Event e) {
+ params.setEnableOptLock(((Button)e.widget).getSelection());
+ params.reCollectModification(data.getBufferManager(), data.getEntities());
+ }
+
+ };
+ checkboxOptLock.addListener(SWT.Selection, mlOptLock);
+ /**/
+
+ Label labelDefaultStrLength = new Label(combolabel, SWT.NULL);
+ labelDefaultStrLength.setText(JdtUiMessages.AllEntitiesProcessor_default_string_length);
+ Text textDefaultStrLength = new Text(combolabel, SWT.SINGLE | SWT.BORDER | SWT.TRAIL);
+ textDefaultStrLength.setText(String.valueOf(params.getDefaultStrLength()));
+ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
+ .grab(true, true).applyTo(textDefaultStrLength);
+ textDefaultStrLength.addListener(SWT.Verify, new Listener() {
+ public void handleEvent(Event e) {
+ for (int i = 0; i < e.text.length(); i++) {
+ char val = e.text.charAt(i);
+ if (!('0' <= val && val <= '9')) {
+ e.doit = false;
+ return;
+ }
+ }
+ }
+ });
+ final ModifyListener mlDefaultStrLength = new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ if (e == null || !(e.getSource() instanceof Text)) {
+ return;
+ }
+ params.setDefaultStrLength(Integer.valueOf(((Text)e.getSource()).getText()));
+ params.reCollectModification(data.getBufferManager(), data.getEntities());
+ }
+
+ };
+ textDefaultStrLength.addModifyListener(mlDefaultStrLength);
+
setControl(container);
}
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/IHibernateJPAWizardParams.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/IHibernateJPAWizardParams.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/wizard/IHibernateJPAWizardParams.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -27,6 +27,14 @@
public void setAnnotationStyle(AnnotStyle annotationStyle);
+ public boolean getEnableOptLock();
+
+ public void setEnableOptLock(boolean enableOptLock);
+
+ public int getDefaultStrLength();
+
+ public void setDefaultStrLength(int defaultStrLength);
+
public AnnotStyle getAnnotationStylePreference();
public void reCollectModification(ITextFileBufferManager bufferManager,
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/Country.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/Country.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/Country.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -10,12 +10,14 @@
******************************************************************************/
package test.annotated.fields;
+import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
@Entity
public class Country {
+ @Column(length=200)
private String name;
@ManyToOne
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/DocumentBase.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/DocumentBase.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/fields/DocumentBase.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -10,12 +10,14 @@
******************************************************************************/
package test.annotated.fields;
+import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
@MappedSuperclass
public abstract class DocumentBase {
+ @Column(length=200)
protected String globalDocumentIdentificator;
@Version
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapMockTests.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -51,15 +51,19 @@
context.checking(new Expectations() {{
oneOf(preferenceStore).setValue(AllEntitiesProcessor.storePropertyName, 2);
+ oneOf(preferenceStore).setValue(AllEntitiesProcessor.storeDefaultStrLength,
255);
+ oneOf(preferenceStore).setValue(AllEntitiesProcessor.storeEnableOptLock,
false);
}});
- allEntitiesProcessor.saveAnnotationStylePreference();
+ allEntitiesProcessor.savePreferences();
context.assertIsSatisfied();
allEntitiesProcessor.setAnnotationStyle(null);
context.checking(new Expectations() {{
oneOf(preferenceStore).setValue(AllEntitiesProcessor.storePropertyName, 0);
+ oneOf(preferenceStore).setValue(AllEntitiesProcessor.storeDefaultStrLength,
255);
+ oneOf(preferenceStore).setValue(AllEntitiesProcessor.storeEnableOptLock,
false);
}});
- allEntitiesProcessor.saveAnnotationStylePreference();
+ allEntitiesProcessor.savePreferences();
context.assertIsSatisfied();
}
@@ -125,7 +129,7 @@
allowing(allEntitiesProcessor).modify(javaProject, null, true);
inSequence(sequence);
- allowing(allEntitiesProcessor).saveAnnotationStylePreference();
+ allowing(allEntitiesProcessor).savePreferences();
inSequence(sequence);
}});
jpaMapToolActor.updateSelected();
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java 2009-04-17
16:31:01 UTC (rev 14807)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java 2009-04-17
17:01:20 UTC (rev 14808)
@@ -79,6 +79,7 @@
public void testTransformerFields() {
testSelection = TEST_FIELDS;
+ processor.setDefaultStrLength(200);
processor.setAnnotationStyle(AnnotStyle.FIELDS);
startTestTransformer();
}