1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.type.RelatedType; 33 import com.caucho.config.ConfigException; 34 import com.caucho.java.JavaWriter; 35 import com.caucho.log.Log; 36 import com.caucho.util.L10N; 37 38 import javax.persistence.CascadeType; 39 import java.io.IOException ; 40 import java.util.logging.Logger ; 41 42 46 abstract public class CascadableField extends AbstractField { 47 private static final L10N L = new L10N(CascadableField.class); 48 private static final Logger log = Log.open(CascadableField.class); 49 50 private CascadeType[] _cascadeTypes; 51 52 CascadableField(RelatedType sourceType) 53 { 54 super(sourceType); 55 } 56 57 CascadableField(RelatedType sourceType, 58 String name, 59 CascadeType[] cascadeTypes) 60 throws ConfigException 61 { 62 super(sourceType, name); 63 64 _cascadeTypes = cascadeTypes; 65 } 66 67 71 public boolean isCascade(CascadeType cascade) 72 { 73 if (_cascadeTypes == null) 74 return false; 75 76 for (int i = 0; i < _cascadeTypes.length; i++) { 77 if (_cascadeTypes[i] == CascadeType.ALL) 78 return true; 79 80 if (_cascadeTypes[i] == cascade) 81 return true; 82 } 83 84 return false; 85 } 86 87 91 public void setCascadeType(CascadeType[] cascadeTypes) 92 { 93 _cascadeTypes = cascadeTypes; 94 } 95 96 107 public void generatePreCascade(JavaWriter out, 108 String aConn, 109 CascadeType cascadeType) 110 throws IOException 111 { 112 if (cascadeType != CascadeType.PERSIST) 113 return; 114 115 if (isCascade(cascadeType)) { 116 117 String getter = generateSuperGetter(); 118 119 out.println("if (" + getter + " != null) {"); 120 out.pushDepth(); 121 122 out.println(aConn + ".persistNoChecks("+ getter + ");"); 123 124 out.popDepth(); 125 out.println("}"); 126 } 127 } 128 129 142 public void generatePostCascade(JavaWriter out, 143 String aConn, 144 CascadeType cascadeType) 145 throws IOException 146 { 147 if (cascadeType == CascadeType.PERSIST) 148 return; 149 150 if (isCascade(cascadeType)) { 151 152 String getter = generateSuperGetter(); 153 154 out.println("if (" + getter + " != null) {"); 155 out.pushDepth(); 156 157 out.print(aConn + "."); 158 159 switch (cascadeType) { 160 case MERGE: 161 out.print("merge"); 162 break; 163 164 case REMOVE: 165 out.print("remove"); 166 break; 167 168 case REFRESH: 169 out.print("refresh"); 170 break; 171 } 172 173 out.println("("+ getter + ");"); 174 175 out.popDepth(); 176 out.println("}"); 177 } 178 } 179 180 183 public boolean isCascadable() 184 { 185 return true; 186 } 187 188 192 public boolean generateFlushCheck(JavaWriter out) 193 throws IOException 194 { 195 return false; 196 } 197 } 198 | Popular Tags |