1 5 package xdoclet.modules.mvcsoft.ejb; 6 7 import java.util.*; 8 9 import xjavadoc.*; 10 11 import xdoclet.XDocletException; 12 import xdoclet.tagshandler.ClassTagsHandler; 13 14 import xdoclet.util.Translator; 15 16 21 public class MVCSoftTagsHandler extends ClassTagsHandler 22 { 23 protected static XParameter currentQueryMethodParameter; 24 25 protected static StringTokenizer currentAliases; 26 27 protected static String currentRoleName; 28 29 protected static String currentFieldName; 30 31 protected static String currentColName; 32 33 protected static String nested; 34 private final static String CMP_FIELDS = "CMP Fields"; 35 36 42 public String methodParamType() throws XDocletException 43 { 44 StringBuffer type = new StringBuffer (currentQueryMethodParameter.getType().getQualifiedName()); 45 46 for (int iter = 0; iter < currentQueryMethodParameter.getDimension(); iter++) { 47 type.append("[]"); 48 } 49 return type.toString(); 50 } 51 52 58 public void forAllQueryMethodParams(String template) throws XDocletException 59 { 60 List parameters = Arrays.asList(getCurrentMethod().getParameters().toArray()); 61 62 for (int i = 0; i < (parameters.size() - 2); i++) { 63 currentQueryMethodParameter = (XParameter) parameters.get(i); 64 65 generate(template); 66 } 67 } 68 69 75 public String fieldName() throws XDocletException 76 { 77 String token = currentToken(new Properties()); 78 int idx = token.indexOf(' '); 79 80 if (idx == -1) { 81 return token; 82 } 83 else { 84 return token.substring(0, idx).trim(); 85 } 86 } 87 88 94 public String sortDirection() throws XDocletException 95 { 96 String token = currentToken(new Properties()).trim(); 97 int idx = token.indexOf(' '); 98 99 if (idx == -1) { 100 return ""; 101 } 102 else { 103 String sort = token.substring(idx, token.length()).trim(); 104 105 if (sort.equalsIgnoreCase("desc")) { 106 return "Descending"; 107 } 108 else { 109 return "Ascending"; 110 } 111 } 112 } 113 114 120 public void ifHasSortDirection(String template) throws XDocletException 121 { 122 if (!sortDirection().equals("")) { 123 generate(template); 124 } 125 } 126 127 133 public void forEachRoleMapping(String template) throws XDocletException 134 { 135 String aliases = getKeyAliases(); 136 137 if (aliases == null) { 138 return; 139 } 140 141 String leftRole = null; 142 String rightRole = null; 143 int idx = aliases.indexOf(';'); 144 145 if (idx == -1) { 146 leftRole = aliases; 147 } 148 else { 149 leftRole = aliases.substring(0, idx); 150 rightRole = aliases.substring(idx + 1); 151 } 152 doForExtendedRole(leftRole, template); 153 if (rightRole != null) { 154 doForExtendedRole(rightRole, template); 155 } 156 } 157 158 164 public void forSingleRoleMapping(String template) throws XDocletException 165 { 166 String aliases = getKeyAliases(); 167 168 if (aliases == null) { 169 return; 170 } 171 currentAliases = new StringTokenizer(aliases, ",", false); 172 generate(template); 173 } 174 175 181 public void forAllAliases(String template) throws XDocletException 182 { 183 while (currentAliases.hasMoreTokens()) { 184 String token = currentAliases.nextToken(); 185 int idx = token.indexOf('='); 186 187 if (idx == -1) { 188 throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.ALIAS_FORM)); 189 } 190 currentFieldName = token.substring(0, idx).trim(); 191 currentColName = token.substring(idx + 1).trim(); 192 generate(template); 193 } 194 } 195 196 202 public String roleName() throws XDocletException 203 { 204 return currentRoleName; 205 } 206 207 213 public String keyFieldName() throws XDocletException 214 { 215 return currentFieldName; 216 } 217 218 224 public String colName() throws XDocletException 225 { 226 return currentColName; 227 } 228 229 231 public void forNestedFaultGroups(String template) throws XDocletException 232 { 233 Properties prop = new Properties(); 234 235 prop.setProperty("tagName", "mvcsoft:fault-group"); 236 prop.setProperty("paramName", "nested"); 237 nested = classTagValue(prop); 238 generate(template); 240 } 241 242 public String nestedFaultGroups() throws XDocletException 243 { 244 if ((nested == null) || nested.equals("")) 245 return ""; 246 StringBuffer buffer = new StringBuffer (); 247 Map map = parseNested(nested); 248 Iterator iterator = map.keySet().iterator(); 249 250 while (iterator.hasNext()) { 251 String key = (String ) iterator.next(); 252 253 if (key.equals(CMP_FIELDS)) 254 continue; 255 buffer.append(getFaultGroupRelationshipLinks(key, (Map) map.get(key))); 256 } 257 return buffer.toString(); 258 } 259 260 265 private String getKeyAliases() 266 { 267 return getCurrentMethod().getDoc().getTagAttributeValue("mvcsoft:relation", "key-aliases", false); 268 } 269 270 private Map getSubMap(Map mapNested, String cmrPath) 271 { 272 StringTokenizer pathTokenizer = new StringTokenizer(cmrPath, ".", false); 273 274 while (pathTokenizer.hasMoreTokens()) { 275 String token = pathTokenizer.nextToken(); 276 Map mapNew = (Map) mapNested.get(token); 277 278 if (mapNew == null) { 279 mapNew = new HashMap(); 280 mapNested.put(token, mapNew); 281 } 282 mapNested = mapNew; 283 } 284 return mapNested; 285 } 286 287 private String getFaultGroupRelationshipLinks(String cmrField, Map mapNested) 288 { 289 StringBuffer buffer = new StringBuffer ("<fault-group-relationship-link>"); 290 291 buffer.append("<cmr-field>"); 292 buffer.append(cmrField); 293 buffer.append("</cmr-field>"); 294 buffer.append("<loading-strategy>SeparateQuery</loading-strategy>"); 295 296 List cmpFields = (List) mapNested.get(CMP_FIELDS); 297 298 if (cmpFields != null) { 299 for (Iterator iterCmp = cmpFields.iterator(); iterCmp.hasNext(); ) { 300 String cmp = (String ) iterCmp.next(); 301 302 buffer.append("<field-name>"); 303 buffer.append(cmp); 304 buffer.append("</field-name>"); 305 } 306 } 307 308 Iterator iterator = mapNested.keySet().iterator(); 309 310 while (iterator.hasNext()) { 311 String key = (String ) iterator.next(); 312 313 if (key.equals(CMP_FIELDS)) 314 continue; 315 buffer.append(getFaultGroupRelationshipLinks(key, (Map) mapNested.get(key))); 316 } 317 buffer.append("</fault-group-relationship-link>"); 318 return buffer.toString(); 319 } 320 321 328 private void doForExtendedRole(String token, String template) throws XDocletException 329 { 330 int idx = token.indexOf(':'); 331 332 if (idx == -1) { 333 throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.KEY_ALIAS_FORM)); 334 } 335 336 currentRoleName = token.substring(0, idx); 337 currentAliases = new StringTokenizer(token.substring(idx + 1), ",", false); 338 generate(template); 339 } 340 341 private Map parseNested(String nestedFaultGroups) 342 throws XDocletException 343 { 344 HashMap mapNested = new HashMap(); 345 StringTokenizer tokenizer = new StringTokenizer(nestedFaultGroups, ";", false); 346 347 while (tokenizer.hasMoreTokens()) { 348 String token = tokenizer.nextToken(); 349 int idx = token.indexOf(":"); 350 351 if (idx == -1) { 352 throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.NESTED_FAULT_GROUP_FORM)); 353 } 354 355 String cmrPath = token.substring(0, idx); 356 StringTokenizer cmpFields = new StringTokenizer(token.substring(idx + 1), ",", false); 357 Map subMap = getSubMap(mapNested, cmrPath); 358 List fields = new LinkedList(); 359 360 while (cmpFields.hasMoreTokens()) { 361 fields.add(cmpFields.nextToken()); 362 } 363 subMap.put(CMP_FIELDS, fields); 364 } 365 return mapNested; 366 } 367 } 368 | Popular Tags |