| 1 |  |  package org.kuali.student.common.ui.client.configurable.mvc.layouts; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.HashMap; | 
  | 4 |  |  import java.util.LinkedHashMap; | 
  | 5 |  |  import java.util.List; | 
  | 6 |  |  import java.util.Map; | 
  | 7 |  |  import java.util.Map.Entry; | 
  | 8 |  |   | 
  | 9 |  |  import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; | 
  | 10 |  |  import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; | 
  | 11 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 12 |  |  import org.kuali.student.common.ui.client.mvc.Controller; | 
  | 13 |  |  import org.kuali.student.common.ui.client.mvc.View; | 
  | 14 |  |  import org.kuali.student.core.validation.dto.ValidationResultInfo; | 
  | 15 |  |  import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel; | 
  | 16 |  |   | 
  | 17 |  |  public abstract class ViewLayout extends Controller{ | 
  | 18 |  |   | 
  | 19 | 0 |          public Map<Enum<?>, View> viewMap = new LinkedHashMap<Enum<?>, View>(); | 
  | 20 | 0 |          public Map<String, Enum<?>> viewEnumMap = new HashMap<String, Enum<?>>(); | 
  | 21 |  |          public Enum<?> defaultView; | 
  | 22 |  |           | 
  | 23 |  |          public ViewLayout(String controllerId) { | 
  | 24 | 0 |                  super(controllerId); | 
  | 25 | 0 |          } | 
  | 26 |  |           | 
  | 27 |  |          public void addView(View view){ | 
  | 28 | 0 |                  viewMap.put(view.getViewEnum(), view); | 
  | 29 | 0 |                  viewEnumMap.put(view.getViewEnum().toString(), view.getViewEnum()); | 
  | 30 | 0 |          } | 
  | 31 |  |           | 
  | 32 |  |          public <V extends Enum<?>> void setDefaultView(V viewType){ | 
  | 33 | 0 |                  this.defaultView = viewType; | 
  | 34 | 0 |          } | 
  | 35 |  |   | 
  | 36 |  |          @Override | 
  | 37 |  |          protected <V extends Enum<?>> void getView(V viewType, Callback<View> callback) { | 
  | 38 | 0 |                  callback.exec(viewMap.get(viewType)); | 
  | 39 | 0 |          } | 
  | 40 |  |   | 
  | 41 |  |          @Override | 
  | 42 |  |          public Enum<?> getViewEnumValue(String enumValue) { | 
  | 43 | 0 |                  return viewEnumMap.get(enumValue); | 
  | 44 |  |          } | 
  | 45 |  |   | 
  | 46 |  |          @Override | 
  | 47 |  |          public void showDefaultView(Callback<Boolean> onReadyCallback) { | 
  | 48 | 0 |                  if(!viewMap.isEmpty()){                 | 
  | 49 | 0 |                          if(defaultView == null){ | 
  | 50 | 0 |                                  showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); | 
  | 51 |  |                          } | 
  | 52 |  |                          else{ | 
  | 53 | 0 |                                  showView(defaultView, onReadyCallback); | 
  | 54 |  |                          } | 
  | 55 |  |                  } | 
  | 56 |  |                   | 
  | 57 | 0 |          } | 
  | 58 |  |           | 
  | 59 |  |           | 
  | 60 |  |   | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 |  |   | 
  | 66 |  |          public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly){ | 
  | 67 | 0 |                  boolean isValid = true; | 
  | 68 |  |   | 
  | 69 | 0 |                  if (checkCurrentSectionOnly){ | 
  | 70 |  |                           | 
  | 71 |  |                       | 
  | 72 |  |                               | 
  | 73 |  |                       | 
  | 74 | 0 |                              View v = getCurrentView(); | 
  | 75 | 0 |                          if(v instanceof Section){ | 
  | 76 | 0 |                                  isValid = isValid(validationResults, (Section)v); | 
  | 77 |  |                           | 
  | 78 |  |                      } | 
  | 79 | 0 |                  } else { | 
  | 80 |  |                           | 
  | 81 |  |                           | 
  | 82 | 0 |                          String errorSections = ""; | 
  | 83 | 0 |                          StringBuilder errorSectionsbuffer = new StringBuilder(); | 
  | 84 | 0 |                          errorSectionsbuffer.append(errorSections); | 
  | 85 | 0 |                          for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { | 
  | 86 | 0 |                                  View v = entry.getValue(); | 
  | 87 | 0 |                                  if (v instanceof Section){ | 
  | 88 | 0 |                                          if (!isValid(validationResults, (Section)v)){ | 
  | 89 | 0 |                                                  isValid = false; | 
  | 90 | 0 |                                                  errorSectionsbuffer.append(((SectionView)v).getName() + ", "); | 
  | 91 |  |                                          } | 
  | 92 |  |                                  } | 
  | 93 | 0 |                          } | 
  | 94 | 0 |                          errorSections = errorSectionsbuffer.toString(); | 
  | 95 | 0 |                          if (!errorSections.isEmpty()){ | 
  | 96 | 0 |                                  errorSections = errorSections.substring(0, errorSections.length()-2); | 
  | 97 |  |                                   | 
  | 98 |  |                          } | 
  | 99 |  |                  } | 
  | 100 |  |   | 
  | 101 | 0 |                  return isValid; | 
  | 102 |  |          } | 
  | 103 |  |   | 
  | 104 |  |          private boolean isValid(List<ValidationResultInfo> validationResults, Section section){ | 
  | 105 | 0 |                  section.setFieldHasHadFocusFlags(true); | 
  | 106 | 0 |                  ErrorLevel status = section.processValidationResults(validationResults); | 
  | 107 |  |   | 
  | 108 | 0 |                  return (status != ErrorLevel.ERROR); | 
  | 109 |  |          } | 
  | 110 |  |  } |