1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JMethod; 34 import com.caucho.ejb.cfg.CmrManyToMany; 35 import com.caucho.ejb.cfg.CmrManyToOne; 36 import com.caucho.ejb.cfg.CmrRelation; 37 import com.caucho.ejb.cfg.EjbEntityBean; 38 import com.caucho.java.JavaWriter; 39 import com.caucho.java.gen.BaseClass; 40 import com.caucho.util.L10N; 41 42 import java.io.IOException ; 43 import java.util.Set ; 44 45 48 public class CollectionClass extends BaseClass { 49 private final static L10N L = new L10N(CollectionClass.class); 50 51 private CmrRelation _oneToMany; 52 53 public CollectionClass(CmrRelation oneToMany, 54 String className) 55 { 56 _oneToMany = oneToMany; 57 58 setClassName(className); 59 60 JClass retType = oneToMany.getGetter().getReturnType(); 61 if (retType.isAssignableTo(Set .class)) 62 setSuperClassName("com.caucho.ejb.entity.CmpSetImpl"); 63 else 64 setSuperClassName("com.caucho.ejb.entity.CmpCollectionImpl"); 65 } 66 67 70 public void generateClassContent(JavaWriter out) 71 throws IOException 72 { 73 generateConstructor(out); 74 75 generateAdd(out); 76 generateRemove(out); 77 78 super.generateClassContent(out); 79 } 80 81 84 public void generateConstructor(JavaWriter out) 85 throws IOException 86 { 87 EjbEntityBean sourceBean = _oneToMany.getBean(); 88 String sourceType = sourceBean.getLocal().getName(); 89 90 out.println(); 91 out.println("Bean _bean;"); 92 out.println(sourceType + " _beanLocal;"); 93 94 out.println(); 95 out.println("public " + getClassName() + "(Bean bean, com.caucho.amber.AmberQuery query)"); 96 out.println("{"); 97 out.pushDepth(); 98 out.println("_bean = bean;"); 99 out.println("_beanLocal = bean._ejb_context._viewLocal;"); 100 out.println("fill(query);"); 101 out.popDepth(); 102 out.println("}"); 103 } 104 105 108 public void generateAdd(JavaWriter out) 109 throws IOException 110 { 111 out.println(); 112 out.println("public boolean addImpl(Object v)"); 113 out.println("{"); 114 out.pushDepth(); 115 116 EjbEntityBean targetBean = _oneToMany.getTargetBean(); 117 String targetType = targetBean.getLocal().getName(); 118 119 out.println("if (v == null)"); 120 out.println(" return false;"); 121 out.println("else if (! (v instanceof " + targetType + "))"); 122 out.println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);"); 123 124 CmrRelation targetRelation = _oneToMany.getTargetRelation(); 125 126 if (targetRelation instanceof CmrManyToOne) { 127 CmrManyToOne manyToOne = (CmrManyToOne) targetRelation; 128 129 JMethod setter = manyToOne.getSetter(); 130 131 if (setter != null) { 132 out.println(targetType + " bean = (" + targetType + ") v;"); 133 134 JMethod localDstSetter = targetBean.getMethod(targetBean.getLocal(), 135 setter); 136 137 if (localDstSetter != null) { 138 out.print("bean"); 139 } 140 else { 141 out.print("((" + targetBean.getEJBClass().getName() + ") "); 142 out.print("((com.caucho.ejb.entity.EntityObject) bean)._caucho_getBean(_ejb_trans, true))"); 143 } 144 145 out.println("." + setter.getName() + "(_beanLocal);"); 146 } 147 } 148 else if (_oneToMany instanceof CmrManyToMany) { 149 CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany; 150 151 JMethod getter = manyToMany.getGetter(); 152 153 if (manyToMany.isTargetUnique()) 154 out.println("_bean.__amber_" + getter.getName() + "_remove_target(v);"); 155 156 out.println("_bean.__amber_" + getter.getName() + "_add(v);"); 157 } 158 159 out.println("return true;"); 160 161 out.popDepth(); 162 out.println("}"); 163 } 164 165 168 public void generateRemove(JavaWriter out) 169 throws IOException 170 { 171 out.println(); 172 out.println("protected boolean removeImpl(Object v)"); 173 out.println("{"); 174 out.pushDepth(); 175 176 EjbEntityBean targetBean = _oneToMany.getTargetBean(); 177 String targetType = targetBean.getLocal().getName(); 178 179 out.println("if (v == null)"); 180 out.println(" return false;"); 181 out.println("else if (! (v instanceof " + targetType + "))"); 182 out.println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);"); 183 184 CmrRelation targetRelation = _oneToMany.getTargetRelation(); 185 186 if (targetRelation instanceof CmrManyToOne) { 187 CmrManyToOne manyToOne = (CmrManyToOne) targetRelation; 188 189 JMethod setter = manyToOne.getSetter(); 190 JMethod getter = manyToOne.getGetter(); 191 192 if (setter != null) { 193 out.println("if (_bean != null) {"); 194 out.pushDepth(); 195 196 out.println(targetType + " bean = (" + targetType + ") v;"); 197 198 out.println("if (_beanLocal != null) {"); 199 out.pushDepth(); 200 201 JMethod localDstSetter = targetBean.getMethod(targetBean.getLocal(), 202 setter); 203 String bean = "bean"; 204 205 if (localDstSetter == null) { 206 String beanClass = targetBean.getEJBClass().getName(); 207 208 out.print(beanClass + " bean1 = ((" + beanClass + ") "); 209 out.print("((com.caucho.ejb.entity.EntityObject) bean)._caucho_getBean(_ejb_trans, true));"); 210 211 bean = "bean1"; 212 } 213 214 out.println("if (_beanLocal.equals(" + bean + "." + getter.getName() + "())) {"); 215 out.pushDepth(); 216 217 out.println(bean + "." + setter.getName() + "(null);"); 218 219 out.popDepth(); 220 out.println("}"); 221 222 out.popDepth(); 223 out.println("}"); 224 225 out.popDepth(); 226 out.println("}"); 227 out.println(); 228 } 229 } 230 else if (_oneToMany instanceof CmrManyToMany) { 231 CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany; 232 233 JMethod getter = manyToMany.getGetter(); 234 235 out.println("_bean.__amber_" + getter.getName() + "_remove(v);"); 236 } 237 238 out.println("return true;"); 239 240 out.popDepth(); 241 out.println("}"); 242 } 243 } 244 | Popular Tags |