| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.assembly.data; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.Date; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |   | 
  | 21 |  |  import org.kuali.student.common.validator.DateParser; | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 |  |  public class MetadataInterrogator { | 
  | 28 |  |   | 
  | 29 | 0 |          public enum ConstraintIds { | 
  | 30 | 0 |                  MULTI_LINE_TEXT("multi.line.text"), RICH_TEXT("rich.text"); | 
  | 31 |  |                  private final String id; | 
  | 32 |  |   | 
  | 33 | 0 |                  private ConstraintIds(final String id) { | 
  | 34 | 0 |                          this.id = id; | 
  | 35 | 0 |                  } | 
  | 36 |  |   | 
  | 37 |  |                  public String getId() { | 
  | 38 | 0 |                          return this.id; | 
  | 39 |  |                  } | 
  | 40 |  |          } | 
  | 41 |  |   | 
  | 42 |  |          private Metadata meta; | 
  | 43 |  |   | 
  | 44 | 25 |          public MetadataInterrogator(Metadata meta) { | 
  | 45 | 25 |                  this.meta = meta; | 
  | 46 | 25 |          } | 
  | 47 |  |   | 
  | 48 |  |           | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |          public boolean hasConstraint(String id) { | 
  | 52 | 0 |                  return hasConstraint(meta, id); | 
  | 53 |  |          } | 
  | 54 |  |   | 
  | 55 |  |   | 
  | 56 |  |           | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 |  |   | 
  | 60 |  |   | 
  | 61 |  |          public boolean isRequired() { | 
  | 62 | 0 |                  return isRequired(meta); | 
  | 63 |  |          } | 
  | 64 |  |   | 
  | 65 |  |           | 
  | 66 |  |   | 
  | 67 |  |   | 
  | 68 |  |   | 
  | 69 |  |   | 
  | 70 |  |          public Integer getLargestMinOccurs() { | 
  | 71 | 0 |                  return getLargestMinOccurs(meta); | 
  | 72 |  |          } | 
  | 73 |  |   | 
  | 74 |  |           | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |          public boolean isRepeating() { | 
  | 80 | 0 |                  return isRepeating(meta); | 
  | 81 |  |          } | 
  | 82 |  |   | 
  | 83 |  |           | 
  | 84 |  |   | 
  | 85 |  |   | 
  | 86 |  |   | 
  | 87 |  |   | 
  | 88 |  |          public Integer getSmallestMaxOccurs() { | 
  | 89 | 0 |                  return getSmallestMaxOccurs(meta); | 
  | 90 |  |          } | 
  | 91 |  |   | 
  | 92 |  |           | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |   | 
  | 96 |  |   | 
  | 97 |  |          public Integer getLargestMinLength() { | 
  | 98 | 91 |                  return getLargestMinLength(meta); | 
  | 99 |  |          } | 
  | 100 |  |   | 
  | 101 |  |          public Integer getSmallestMaxLength() { | 
  | 102 | 69 |                  return getSmallestMaxLength(meta); | 
  | 103 |  |          } | 
  | 104 |  |   | 
  | 105 |  |          public boolean isMultilined() { | 
  | 106 | 0 |                  return isMultilined(meta); | 
  | 107 |  |          } | 
  | 108 |  |   | 
  | 109 |  |           | 
  | 110 |  |           | 
  | 111 |  |   | 
  | 112 |  |   | 
  | 113 |  |          public static boolean hasConstraint(Metadata meta, ConstraintIds id) { | 
  | 114 | 0 |                  return hasConstraint(meta, id.getId()); | 
  | 115 |  |          } | 
  | 116 |  |           | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |          public static boolean hasConstraint(Metadata meta, String id) { | 
  | 120 | 0 |                  boolean result = false; | 
  | 121 | 0 |                  if (meta != null && meta.getConstraints() != null) { | 
  | 122 | 0 |                          result = containsConstraint(meta.getConstraints(), id); | 
  | 123 |  |                  } | 
  | 124 | 0 |                  return result; | 
  | 125 |  |          } | 
  | 126 |  |   | 
  | 127 |  |          private static boolean containsConstraint(List<ConstraintMetadata> constraints, | 
  | 128 |  |                          String id) { | 
  | 129 | 0 |                  boolean result = false; | 
  | 130 | 0 |                  if (constraints != null) { | 
  | 131 | 0 |                          for (ConstraintMetadata con : constraints) { | 
  | 132 | 0 |                                  if ((con.getId() != null && con.getId().equals(id)) | 
  | 133 |  |                                                  || containsConstraint(con.getChildConstraints(), id)) { | 
  | 134 | 0 |                                          result = true; | 
  | 135 | 0 |                                          break; | 
  | 136 |  |                                  } | 
  | 137 |  |                          } | 
  | 138 |  |                  } | 
  | 139 | 0 |                  return result; | 
  | 140 |  |          } | 
  | 141 |  |   | 
  | 142 |  |           | 
  | 143 |  |   | 
  | 144 |  |   | 
  | 145 |  |   | 
  | 146 |  |   | 
  | 147 |  |          public static boolean isRequired(Metadata meta) { | 
  | 148 | 0 |                  if (meta == null) { | 
  | 149 | 0 |                          return false; | 
  | 150 |  |                  } | 
  | 151 | 0 |                  Integer largestMinOccurs = getLargestMinOccurs(meta); | 
  | 152 |  |                   | 
  | 153 | 0 |                  if (largestMinOccurs == null) { | 
  | 154 | 0 |                          return false; | 
  | 155 |  |                  } | 
  | 156 | 0 |                  if (largestMinOccurs >= 1) { | 
  | 157 | 0 |                          return true; | 
  | 158 |  |                  } | 
  | 159 | 0 |                  return false; | 
  | 160 |  |          } | 
  | 161 |  |   | 
  | 162 |  |           | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |   | 
  | 167 |  |   | 
  | 168 |  |          public static boolean isRequiredForNextState(Metadata meta){ | 
  | 169 | 0 |                  boolean required = false; | 
  | 170 |  |                   | 
  | 171 | 0 |                  if(meta == null){ | 
  | 172 | 0 |                          return false; | 
  | 173 |  |                  } | 
  | 174 |  |                   | 
  | 175 |  |                   | 
  | 176 | 0 |                  if (meta.getConstraints() != null && meta.getConstraints().size() == 1){ | 
  | 177 | 0 |                          ConstraintMetadata constraint =  meta.getConstraints().get(0); | 
  | 178 | 0 |                          required = constraint.isRequiredForNextState(); | 
  | 179 |  |                  } | 
  | 180 |  |                   | 
  | 181 | 0 |                  return required; | 
  | 182 |  |          } | 
  | 183 |  |           | 
  | 184 |  |           | 
  | 185 |  |   | 
  | 186 |  |   | 
  | 187 |  |   | 
  | 188 |  |   | 
  | 189 |  |   | 
  | 190 |  |          public static String getNextState(Metadata meta){ | 
  | 191 | 0 |                  String state = null; | 
  | 192 |  |                   | 
  | 193 | 0 |                  if(meta == null){ | 
  | 194 | 0 |                          return state; | 
  | 195 |  |                  } | 
  | 196 |  |                   | 
  | 197 |  |                   | 
  | 198 | 0 |                  if (meta.getConstraints() != null && meta.getConstraints().size() == 1){ | 
  | 199 | 0 |                          ConstraintMetadata constraint =  meta.getConstraints().get(0); | 
  | 200 | 0 |                          state = constraint.getNextState(); | 
  | 201 |  |                  } | 
  | 202 |  |                   | 
  | 203 | 0 |                  return state; | 
  | 204 |  |          } | 
  | 205 |  |           | 
  | 206 |  |           | 
  | 207 |  |   | 
  | 208 |  |   | 
  | 209 |  |   | 
  | 210 |  |   | 
  | 211 |  |          public static Integer getLargestMinOccurs(Metadata meta) { | 
  | 212 | 0 |                  if (meta == null) { | 
  | 213 | 0 |                          return null; | 
  | 214 |  |                  } | 
  | 215 | 0 |                  Integer largestMinOccurs = null; | 
  | 216 |  |                   | 
  | 217 |  |                   | 
  | 218 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 219 | 0 |                          if (cons.getMinOccurs() != null) { | 
  | 220 | 0 |                                  if (largestMinOccurs == null) { | 
  | 221 | 0 |                                          largestMinOccurs = cons.getMinOccurs(); | 
  | 222 | 0 |                                  } else if (cons.getMinOccurs() > largestMinOccurs) { | 
  | 223 | 0 |                                          largestMinOccurs = cons.getMinOccurs(); | 
  | 224 |  |                                  } | 
  | 225 |  |                          } | 
  | 226 |  |                  } | 
  | 227 | 0 |                  return largestMinOccurs; | 
  | 228 |  |          } | 
  | 229 |  |   | 
  | 230 |  |           | 
  | 231 |  |   | 
  | 232 |  |   | 
  | 233 |  |   | 
  | 234 |  |   | 
  | 235 |  |          public static boolean isRepeating(Metadata meta) { | 
  | 236 | 0 |                  if (meta == null) { | 
  | 237 | 0 |                          return false; | 
  | 238 |  |                  } | 
  | 239 | 0 |                  Integer smallestMaxOccurs = getSmallestMaxOccurs(meta); | 
  | 240 | 0 |                  if (hasRepeatingConstraint(meta) || | 
  | 241 |  |                                  (smallestMaxOccurs != null && smallestMaxOccurs > 1)) { | 
  | 242 | 0 |                          return true; | 
  | 243 |  |                  } | 
  | 244 | 0 |                  return false; | 
  | 245 |  |          } | 
  | 246 |  |           | 
  | 247 |  |          private static boolean hasRepeatingConstraint(Metadata meta) { | 
  | 248 | 0 |                  boolean isRepeating = false; | 
  | 249 |  |   | 
  | 250 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 251 | 0 |                          if ("repeating".equals(cons.getId())){ | 
  | 252 | 0 |                                  isRepeating = true; | 
  | 253 |  |                          } | 
  | 254 |  |                  } | 
  | 255 | 0 |                  return isRepeating; | 
  | 256 |  |          } | 
  | 257 |  |   | 
  | 258 |  |           | 
  | 259 |  |   | 
  | 260 |  |   | 
  | 261 |  |   | 
  | 262 |  |   | 
  | 263 |  |   | 
  | 264 |  |   | 
  | 265 |  |   | 
  | 266 |  |   | 
  | 267 |  |          public static Integer getSmallestMaxOccurs(Metadata meta) { | 
  | 268 | 0 |                  if (meta == null) { | 
  | 269 | 0 |                          return null; | 
  | 270 |  |                  } | 
  | 271 | 0 |                  Integer smallestMaxOccurs = null; | 
  | 272 | 0 |                  boolean isRepeating = false; | 
  | 273 |  |   | 
  | 274 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 275 | 0 |                          if ("repeating".equals(cons.getId())){ | 
  | 276 | 0 |                                  isRepeating = true; | 
  | 277 |  |                          } | 
  | 278 | 0 |                          if (cons.getMaxOccurs() != null) { | 
  | 279 | 0 |                                  if (smallestMaxOccurs == null) { | 
  | 280 | 0 |                                          smallestMaxOccurs = cons.getMaxOccurs(); | 
  | 281 | 0 |                                  } else if (cons.getMaxOccurs() < smallestMaxOccurs) { | 
  | 282 | 0 |                                          smallestMaxOccurs = cons.getMaxOccurs(); | 
  | 283 |  |                                  } | 
  | 284 |  |                          } | 
  | 285 |  |                  } | 
  | 286 |  |                   | 
  | 287 | 0 |                  if (isRepeating && smallestMaxOccurs == null){ | 
  | 288 | 0 |                          smallestMaxOccurs = -1; | 
  | 289 |  |                  } | 
  | 290 |  |                   | 
  | 291 | 0 |                  return smallestMaxOccurs; | 
  | 292 |  |          } | 
  | 293 |  |   | 
  | 294 |  |           | 
  | 295 |  |   | 
  | 296 |  |   | 
  | 297 |  |   | 
  | 298 |  |   | 
  | 299 |  |          public static Integer getLargestMinLength(Metadata meta) { | 
  | 300 | 91 |                  if (meta == null) { | 
  | 301 | 0 |                          return null; | 
  | 302 |  |                  } | 
  | 303 | 91 |                  Integer largestMinLength = null; | 
  | 304 |  |                   | 
  | 305 |  |                   | 
  | 306 | 91 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 307 | 91 |                          if (cons.getMinLength() != null) { | 
  | 308 | 88 |                                  if (largestMinLength == null) { | 
  | 309 | 88 |                                          largestMinLength = cons.getMinLength(); | 
  | 310 | 0 |                                  } else if (cons.getMinLength() > largestMinLength) { | 
  | 311 | 0 |                                          largestMinLength = cons.getMinLength(); | 
  | 312 |  |                                  } | 
  | 313 |  |                          } | 
  | 314 |  |                  } | 
  | 315 | 91 |                  return largestMinLength; | 
  | 316 |  |          } | 
  | 317 |  |   | 
  | 318 |  |          public static Integer getSmallestMaxLength(Metadata meta) { | 
  | 319 | 69 |                  if (meta == null) { | 
  | 320 | 0 |                          return null; | 
  | 321 |  |                  } | 
  | 322 | 69 |                  Integer smallestMaxLength = null; | 
  | 323 |  |                   | 
  | 324 |  |                   | 
  | 325 | 69 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 326 | 69 |                          if (cons.getMaxLength() != null) { | 
  | 327 | 66 |                                  if (smallestMaxLength == null) { | 
  | 328 | 66 |                                          smallestMaxLength = cons.getMaxLength(); | 
  | 329 | 0 |                                  } else if (cons.getMaxLength() < smallestMaxLength) { | 
  | 330 | 0 |                                          smallestMaxLength = cons.getMaxLength(); | 
  | 331 |  |                                  } | 
  | 332 |  |                          } | 
  | 333 |  |                  } | 
  | 334 | 69 |                  return smallestMaxLength; | 
  | 335 |  |          } | 
  | 336 |  |   | 
  | 337 |  |          public static boolean isMultilined(Metadata meta) { | 
  | 338 | 0 |                  if (meta == null) { | 
  | 339 | 0 |                          return false; | 
  | 340 |  |                  } | 
  | 341 |  |                   | 
  | 342 | 0 |                  boolean result = (hasConstraint(meta, "rich.text") || hasConstraint(meta, "multi.line.text"))  | 
  | 343 |  |                          && !(hasConstraint(meta, "single.line.text") || hasConstraint(meta, "code") || hasConstraint(meta, "no.linefeeds")); | 
  | 344 |  |                   | 
  | 345 |  |                   | 
  | 346 | 0 |                  if (!result) { | 
  | 347 | 0 |                          Integer maxLength = getSmallestMaxLength(meta); | 
  | 348 | 0 |                          if (maxLength != null) { | 
  | 349 | 0 |                                  if (maxLength > 150) { | 
  | 350 | 0 |                                          result = true; | 
  | 351 |  |                                  } | 
  | 352 |  |                          } | 
  | 353 |  |                  } | 
  | 354 | 0 |                  return result; | 
  | 355 |  |          } | 
  | 356 |  |   | 
  | 357 |  |           | 
  | 358 |  |          public static Long getLargestMinValue(Metadata meta) { | 
  | 359 | 0 |                  if (meta == null) { | 
  | 360 | 0 |                          return null; | 
  | 361 |  |                  } | 
  | 362 | 0 |                  Long result = null; | 
  | 363 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 364 | 0 |                          Long min = tryParseLong(cons.getMinValue()); | 
  | 365 | 0 |                          if (min != null) { | 
  | 366 | 0 |                                  if (result == null || min > result) { | 
  | 367 | 0 |                                          result = min; | 
  | 368 |  |                                  } | 
  | 369 |  |                          } | 
  | 370 | 0 |                  } | 
  | 371 | 0 |                  return result; | 
  | 372 |  |          } | 
  | 373 |  |   | 
  | 374 |  |          public static Long getSmallestMaxValue(Metadata meta) { | 
  | 375 | 0 |                  if (meta == null) { | 
  | 376 | 0 |                          return null; | 
  | 377 |  |                  } | 
  | 378 | 0 |                  Long result = null; | 
  | 379 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 380 | 0 |                          Long max = tryParseLong(cons.getMaxValue()); | 
  | 381 | 0 |                          if (max != null) { | 
  | 382 | 0 |                                  if (result == null || max < result) { | 
  | 383 | 0 |                                          result = max; | 
  | 384 |  |                                  } | 
  | 385 |  |                          } | 
  | 386 | 0 |                  } | 
  | 387 | 0 |                  return result; | 
  | 388 |  |          } | 
  | 389 |  |   | 
  | 390 |  |           | 
  | 391 |  |          public static Double getLargestMinValueDouble(Metadata meta) { | 
  | 392 | 0 |                  if (meta == null) { | 
  | 393 | 0 |                          return null; | 
  | 394 |  |                  } | 
  | 395 | 0 |                  Double result = null; | 
  | 396 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 397 | 0 |                          Double min = tryParseDouble(cons.getMinValue()); | 
  | 398 | 0 |                          if (min != null) { | 
  | 399 | 0 |                                  if (result == null || min > result) { | 
  | 400 | 0 |                                          result = min; | 
  | 401 |  |                                  } | 
  | 402 |  |                          } | 
  | 403 | 0 |                  } | 
  | 404 | 0 |                  return result; | 
  | 405 |  |          } | 
  | 406 |  |   | 
  | 407 |  |          public static Double getSmallestMaxValueDouble(Metadata meta) { | 
  | 408 | 0 |                  if (meta == null) { | 
  | 409 | 0 |                          return null; | 
  | 410 |  |                  } | 
  | 411 | 0 |                  Double result = null; | 
  | 412 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 413 | 0 |                          Double max = tryParseDouble(cons.getMaxValue()); | 
  | 414 | 0 |                          if (max != null) { | 
  | 415 | 0 |                                  if (result == null || max < result) { | 
  | 416 | 0 |                                          result = max; | 
  | 417 |  |                                  } | 
  | 418 |  |                          } | 
  | 419 | 0 |                  } | 
  | 420 | 0 |                  return result; | 
  | 421 |  |          } | 
  | 422 |  |   | 
  | 423 |  |          public static Date getLargestMinValueDate(Metadata meta, DateParser parser, Object dataValue) { | 
  | 424 | 0 |                  if (meta == null) { | 
  | 425 | 0 |                          return null; | 
  | 426 |  |                  } | 
  | 427 | 0 |                  Date result = null; | 
  | 428 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 429 | 0 |                          Date min = tryParseDate(cons.getMinValue(), parser); | 
  | 430 | 0 |                          if (min != null) { | 
  | 431 | 0 |                                  if (result == null || min.getTime() > result.getTime()) { | 
  | 432 | 0 |                                          result = min; | 
  | 433 |  |                                  } | 
  | 434 |  |                          } | 
  | 435 | 0 |                  } | 
  | 436 | 0 |                  if (dataValue != null && dataValue instanceof Date){ | 
  | 437 | 0 |                          if (result == null || (((Date)dataValue).getTime() > result.getTime())){ | 
  | 438 | 0 |                                  result = (Date)dataValue; | 
  | 439 |  |                          } | 
  | 440 |  |                  } | 
  | 441 | 0 |                  return result; | 
  | 442 |  |          } | 
  | 443 |  |   | 
  | 444 |  |          public static Date getSmallestMaxValueDate(Metadata meta, DateParser parser, Object dataValue) { | 
  | 445 | 0 |                  if (meta == null) { | 
  | 446 | 0 |                          return null; | 
  | 447 |  |                  } | 
  | 448 | 0 |                  Date result = null; | 
  | 449 | 0 |                  for (ConstraintMetadata cons : meta.getConstraints()) { | 
  | 450 | 0 |                          Date max = tryParseDate(cons.getMaxValue(), parser); | 
  | 451 | 0 |                          if (max != null) { | 
  | 452 | 0 |                                  if (result == null || max.getTime() < result.getTime()) { | 
  | 453 | 0 |                                          result = max; | 
  | 454 |  |                                  } | 
  | 455 |  |                          } | 
  | 456 | 0 |                  } | 
  | 457 | 0 |                  if (dataValue != null && dataValue instanceof Date){ | 
  | 458 | 0 |                          if (result==null || (((Date)dataValue).getTime() < result.getTime())){ | 
  | 459 | 0 |                                  result = (Date)dataValue; | 
  | 460 |  |                          } | 
  | 461 |  |                  } | 
  | 462 | 0 |                  return result; | 
  | 463 |  |          } | 
  | 464 |  |           | 
  | 465 |  |          private static Long tryParseLong(String s) { | 
  | 466 | 0 |                  Long result = null; | 
  | 467 |  |                   | 
  | 468 | 0 |                  if (s != null) { | 
  | 469 |  |                          try { | 
  | 470 | 0 |                                  result = Long.valueOf(s); | 
  | 471 | 0 |                          } catch (Exception e) { | 
  | 472 |  |                                   | 
  | 473 | 0 |                          } | 
  | 474 |  |                  } | 
  | 475 |  |                   | 
  | 476 | 0 |                  return result; | 
  | 477 |  |          } | 
  | 478 |  |           | 
  | 479 |  |          private static Double tryParseDouble(String s) { | 
  | 480 | 0 |                  Double result = null; | 
  | 481 |  |                   | 
  | 482 | 0 |                  if (s != null) { | 
  | 483 |  |                          try { | 
  | 484 | 0 |                                  result = Double.valueOf(s); | 
  | 485 | 0 |                          } catch (Exception e) { | 
  | 486 |  |                                   | 
  | 487 | 0 |                          } | 
  | 488 |  |                  } | 
  | 489 |  |                   | 
  | 490 | 0 |                  return result; | 
  | 491 |  |          } | 
  | 492 |  |           | 
  | 493 |  |          private static Date tryParseDate(String s, DateParser parser) { | 
  | 494 | 0 |                  Date result = null; | 
  | 495 |  |                   | 
  | 496 | 0 |                  if (s != null) { | 
  | 497 |  |                          try { | 
  | 498 | 0 |                                  result = parser.parseDate(s); | 
  | 499 | 0 |                          } catch (Exception e) { | 
  | 500 |  |                                   | 
  | 501 | 0 |                          } | 
  | 502 |  |                  } | 
  | 503 |  |                   | 
  | 504 | 0 |                  return result; | 
  | 505 |  |          } | 
  | 506 |  |  } |