1 19 package org.netbeans.modules.j2ee.persistence.entitygenerator; 20 21 import org.netbeans.modules.dbschema.ColumnElement; 22 import org.netbeans.modules.j2ee.persistence.dd.JavaPersistenceQLKeywords; 23 import org.openide.util.*; 24 25 27 35 public abstract class EntityMember { 36 37 40 private String memberName; 41 42 45 private String memberClass; 46 47 56 public static String makeFieldName(String fieldName) { 57 StringBuilder retName = makeName(fieldName); 58 retName.setCharAt(0, Character.toLowerCase(retName.charAt(0))); 59 String returnS = retName.toString(); 60 if (!Utilities.isJavaIdentifier(returnS)) { 61 returnS += '1'; 62 } 63 return returnS; 64 } 65 66 78 public static String makeRelationshipFieldName(String fieldName, 79 boolean isCollection) { 80 if (isCollection){ 81 fieldName += "Collection"; } 83 return makeFieldName(fieldName); 84 } 85 86 private static StringBuilder makeName(String fieldName) { 87 if (fieldName == null || fieldName.length() == 0) { 88 fieldName = "a"; } 90 91 if (!Character.isLetter(fieldName.charAt(0))) { 92 StringBuilder removed = new StringBuilder (fieldName); 93 while (removed.length() > 0 && 94 !Character.isLetter(removed.charAt(0))) { 95 removed.deleteCharAt(0); 96 } 97 return makeName(removed.toString()); 98 } 99 100 String lower = fieldName.toLowerCase(); 101 String upper = fieldName.toUpperCase(); 102 boolean mixedCase = !(fieldName.equals(lower) || 103 fieldName.equals(upper)); 104 105 return mapName(new StringBuilder (mixedCase?fieldName:lower), 106 !mixedCase); 107 } 108 109 116 public static String makeClassName(String className) { 117 StringBuilder fieldName = makeName(className); 118 if (JavaPersistenceQLKeywords.isKeyword(fieldName.toString())) { 119 fieldName.append('1'); 120 } 121 fieldName.setCharAt(0, Character.toUpperCase(fieldName.charAt(0))); 122 return fieldName.toString(); 123 } 124 125 private static StringBuilder mapName(StringBuilder mappedName, 126 boolean convertUpper) { 127 int i = 0; 128 while (i < mappedName.length()) { 129 if (!Character.isLetterOrDigit(mappedName.charAt(i))) { 130 if (convertUpper && ((i+1) < mappedName.length())) { 131 mappedName.setCharAt(i+1, 132 Character.toUpperCase(mappedName.charAt(i+1))); 133 } 134 mappedName.deleteCharAt(i); 135 } else { 136 i++; 137 } 138 } 139 140 return mappedName; 141 } 142 143 148 public static EntityMember create(ColumnElement columnElement) { 149 if (columnElement == null) { 150 throw new IllegalArgumentException ("columnElement == null"); } 152 return new DbSchemaEntityMember(columnElement); 153 } 154 155 159 public String getMemberName() { 160 return memberName; 161 } 162 163 168 public void setMemberName(String name) { 169 if (!Utilities.isJavaIdentifier(name)) { 170 throw new IllegalArgumentException ("isJavaIdentifier()==false"); } 172 173 memberName = name; 174 } 175 176 180 public String getMemberType() { 181 return memberClass; 182 } 183 184 189 public void setMemberType(String aType) { 190 if (aType == null) { 191 throw new IllegalArgumentException ("aType == null"); } 193 memberClass = aType; 194 } 195 196 200 public abstract boolean isLobType(); 201 202 206 public abstract boolean isPrimaryKey(); 207 208 public abstract void setPrimaryKey(boolean isPk, boolean isPkField); 209 210 213 public abstract boolean supportsFinder(); 214 215 218 public boolean equals(Object other) { 219 if (other == null || !(other.getClass().isInstance(getClass()))) { 220 return false; 221 } 222 return ((EntityMember) other).getMemberName().equals(getMemberName()); 223 } 224 225 228 public int hashCode() { 229 return getMemberName().hashCode(); 230 } 231 232 233 public abstract boolean isNullable(); 234 public abstract String getColumnName(); 235 public abstract String getTableName(); 236 } 237 238 239 240 | Popular Tags |