1 18 19 package org.objectweb.jac.ide; 20 21 import java.util.Collection ; 22 import java.util.Vector ; 23 import org.objectweb.jac.core.ObjectRepository; 24 import org.objectweb.jac.core.rtti.ClassItem; 25 import org.objectweb.jac.core.rtti.ClassRepository; 26 import org.objectweb.jac.core.rtti.FieldItem; 27 import org.objectweb.jac.core.rtti.NamingConventions; 28 import org.objectweb.jac.util.Strings; 29 30 public class RelationRole extends Role implements Typed { 31 32 protected static final String UNDEFINED = "<UNDEFINED END>"; 33 34 public RelationRole(Link link) { 35 super(link); 36 } 37 38 public RelationRole() { 39 } 40 41 String cardinality = "0-1"; 42 public String getCardinality() { 43 return cardinality; 44 } 45 public void setCardinality(String cardinality) { 46 this.cardinality = cardinality; 47 } 48 49 52 public String getGenerationName() { 53 if (end==null) 54 return UNDEFINED; 55 return Strings.toUSAscii(getRoleName()); 56 } 57 58 public String getGenerationFullName() { 59 return start.getGenerationFullName()+"."+getGenerationName(); 60 } 61 62 public String getRoleName() { 63 if (name==null || name.equals("")) { 64 if (end==null) { 65 return UNDEFINED; 66 } else { 67 if (isMultiple()) { 68 return NamingConventions.maybeLowerFirst( 69 Projects.plurals.getPlural(end.getName())); 70 } else { 71 return NamingConventions.maybeLowerFirst(end.getName()); 72 } 73 } 74 } else { 75 return name; 76 } 77 } 78 79 public Type getType() { 80 if (end == null) 81 return null; 82 if (isMultiple()) { 83 if (primaryKey!=null) 84 return Projects.types.resolveType("HashMap","java.util"); 85 else 86 return Projects.types.resolveType("Vector","java.util"); 87 } else { 88 return (Type)end; 89 } 90 } 91 92 public Type getAbstractType() { 93 if (end == null) 94 return null; 95 if (isMultiple()) { 96 if (primaryKey!=null) 97 return Projects.types.resolveType("Map","java.util"); 98 else 99 return Projects.types.resolveType("List","java.util"); 100 } else { 101 return (Type)end; 102 } 103 } 104 105 public boolean isMultiple() { 106 if( cardinality!=null && ( 107 cardinality.equals("*") || cardinality.equals("0-*")|| 108 cardinality.equals("1-*"))) { 109 return true; 110 } else { 111 return false; 112 } 113 } 114 115 public String getPrototype() { 116 if (end==null) 117 return UNDEFINED; 118 String role = getGenerationName(); 119 String prototype = (getAbstractType().getGenerationFullName())+" "+role; 120 if (isMultiple()) { 121 return prototype+" = new "+getType().getGenerationFullName()+"()"; 122 } else { 123 return prototype; 124 } 125 } 126 127 Method adder; 128 public Method getAdder() { 129 return adder; 130 } 131 public void setAdder(Method method) { 132 this.adder = method; 133 if (method!=null) 134 method.setParent((Class )start); 135 } 136 137 142 public void initAdder(Method adder) { 143 adder.setType(Projects.types.resolveType("void")); 144 adder.addParameter(new Parameter(NamingConventions.lowerFirst(end.getName()),(Type)end)); 145 adder.setName(CodeGeneration.getAdderName(getGenerationName())); 146 } 147 148 Method remover; 149 public Method getRemover() { 150 return remover; 151 } 152 public void setRemover(Method method) { 153 this.remover = method; 154 if (method!=null) 155 method.setParent((Class )start); 156 } 157 162 public void initRemover(Method remover) { 163 remover.setType(Projects.types.resolveType("void")); 164 remover.addParameter(new Parameter(NamingConventions.lowerFirst(end.getName()),(Type)end)); 165 remover.setName(CodeGeneration.getRemoverName(getGenerationName())); 166 } 167 168 Method clearer; 169 public Method getClearer() { 170 return clearer; 171 } 172 public void setClearer(Method method) { 173 this.clearer = method; 174 if (method!=null) 175 method.setParent((Class )start); 176 } 177 182 public void initClearer(Method clearer) { 183 clearer.setType(Projects.types.resolveType("void")); 184 clearer.setName(CodeGeneration.getClearerName(getGenerationName())); 185 } 186 187 Method getter; 188 public Method getGetter() { 189 return getter; 190 } 191 public void setGetter(Method method) { 192 this.getter = method; 193 if (method!=null) 194 method.setParent((Class )start); 195 } 196 201 public void initGetter(Method getter) { 202 getter.setType(getAbstractType()); 203 getter.setName(CodeGeneration.getGetterName(getGenerationName())); 204 } 205 206 public boolean isNavigable() { 207 RelationLink rel = (RelationLink)link; 208 if (rel.getOrientation()==RelationLink.ORIENTATION_BOTH) { 209 return true; 210 } else { 211 if (link.getStartRole()==this) { 212 return rel.getOrientation()==RelationLink.ORIENTATION_STRAIGHT; 213 } else { 214 return rel.getOrientation()==RelationLink.ORIENTATION_REVERSE; 215 } 216 } 217 } 218 219 public boolean isAggregation() { 220 RelationLink rel = (RelationLink)link; 221 return rel.isAggregation() && isStartRole(); 222 } 223 224 225 Typed primaryKey; 226 public Typed getPrimaryKey() { 227 return primaryKey; 228 } 229 public void setPrimaryKey(Typed primaryKey) { 230 this.primaryKey = primaryKey; 231 } 232 233 236 public String getRemoverName() { 237 if (remover!=null) { 238 return remover.getGenerationName(); 239 } else { 240 return CodeGeneration.getRemoverName(getGenerationName()); 241 } 242 } 243 244 public Collection primaryKeyChoices() { 245 if (end==null) { 246 return new Vector (0); 247 } else { 248 Class cl = (Class )end; 249 Vector result = new Vector (); 250 result.addAll(cl.getReferenceRoles()); 251 result.addAll(cl.getAllFields()); 252 return result; 253 } 254 } 255 256 public Collection methodChoices() { 257 ClassItem cl = ClassRepository.get().getClass(Method.class); 258 FieldItem field = cl.getField("parent"); 259 return ObjectRepository.getObjectsWhere(cl,field,null); 260 } 261 } 262 263 | Popular Tags |