| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |  package org.kuali.student.lum.common.client.widgets; | 
  | 16 |  |   | 
  | 17 |  |  import java.util.ArrayList; | 
  | 18 |  |  import java.util.List; | 
  | 19 |  |   | 
  | 20 |  |  import org.kuali.student.common.assembly.data.Data; | 
  | 21 |  |  import org.kuali.student.common.assembly.data.Metadata; | 
  | 22 |  |  import org.kuali.student.common.ui.client.configurable.mvc.layouts.BasicLayout; | 
  | 23 |  |  import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; | 
  | 24 |  |  import org.kuali.student.common.ui.client.mvc.*; | 
  | 25 |  |  import org.kuali.student.common.ui.client.service.DataSaveResult; | 
  | 26 |  |  import org.kuali.student.common.ui.client.widgets.progress.BlockingTask; | 
  | 27 |  |  import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator; | 
  | 28 |  |  import org.kuali.student.common.validation.dto.ValidationResultInfo; | 
  | 29 |  |  import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; | 
  | 30 |  |  import org.kuali.student.core.statement.ui.client.widgets.rules.AccessWidgetValue; | 
  | 31 |  |   | 
  | 32 |  |  import com.google.gwt.core.client.GWT; | 
  | 33 |  |  import com.google.gwt.user.client.Window; | 
  | 34 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 35 |  |   | 
  | 36 | 0 |  public class BuildCluSetWidget extends FlowPanel implements AccessWidgetValue { | 
  | 37 |  |   | 
  | 38 |  |      private CluSetRetriever cluSetRetriever; | 
  | 39 |  |      private CluSetEditorWidget cluSetEditorWidgetView; | 
  | 40 |  |      private static final String CLUSET_MODEL_ID = "clussetModelId"; | 
  | 41 |  |      private DataModel ruleFieldsData; | 
  | 42 |  |      private BasicLayout reqCompController; | 
  | 43 | 0 |      public enum BuildCourseView {VIEW} | 
  | 44 | 0 |      private BlockingTask retrievingTask = new BlockingTask("Retrieving ..."); | 
  | 45 |  |      private String cluSetType; | 
  | 46 |  |      private String metadataId; | 
  | 47 |  |      private String itemLabel; | 
  | 48 |  |      private boolean singularCluOnly; | 
  | 49 |  |   | 
  | 50 |  |      public BuildCluSetWidget(final CluSetRetriever cluSetRetriever, String cluSetType, | 
  | 51 |  |              boolean singularCluOnly) { | 
  | 52 | 0 |          super(); | 
  | 53 |  |   | 
  | 54 | 0 |          this.singularCluOnly = singularCluOnly; | 
  | 55 | 0 |          cluSetEditorWidgetView = new CluSetEditorWidget( | 
  | 56 |  |                  new CluSetRetrieverImpl(), | 
  | 57 |  |                  BuildCourseView.VIEW, | 
  | 58 |  |                  "", CLUSET_MODEL_ID, false, | 
  | 59 |  |                  null, cluSetType, singularCluOnly); | 
  | 60 |  |   | 
  | 61 | 0 |          ruleFieldsData = new DataModel(); | 
  | 62 | 0 |          ruleFieldsData.setRoot(new Data()); | 
  | 63 | 0 |          this.cluSetType = cluSetType; | 
  | 64 | 0 |          if (cluSetType != null && cluSetType.equals("kuali.cluSet.type.Program")) { | 
  | 65 | 0 |              this.metadataId = "programSet"; | 
  | 66 | 0 |              this.itemLabel = "program"; | 
  | 67 |  |          } else { | 
  | 68 | 0 |              this.metadataId = "courseSet"; | 
  | 69 | 0 |              this.itemLabel = "course"; | 
  | 70 |  |          } | 
  | 71 |  |   | 
  | 72 |  |           | 
  | 73 | 0 |          final CluSetRetriever theRetriever = cluSetRetriever; | 
  | 74 | 0 |          this.cluSetRetriever = cluSetRetriever; | 
  | 75 | 0 |          reqCompController = new BasicLayout(null); | 
  | 76 | 0 |          reqCompController.addView(cluSetEditorWidgetView); | 
  | 77 | 0 |          reqCompController.setDefaultModelId(CLUSET_MODEL_ID); | 
  | 78 | 0 |          reqCompController.registerModel(CLUSET_MODEL_ID, new ModelProvider<DataModel>() { | 
  | 79 |  |              @Override | 
  | 80 |  |              public void requestModel(final ModelRequestCallback<DataModel> callback) { | 
  | 81 | 0 |                  if (ruleFieldsData.getDefinition() == null) { | 
  | 82 | 0 |                      theRetriever.getMetadata(metadataId, new Callback<Metadata>(){ | 
  | 83 |  |                          @Override | 
  | 84 |  |                          public void exec(Metadata result) { | 
  | 85 | 0 |                              DataModelDefinition def = new DataModelDefinition(result); | 
  | 86 | 0 |                              ruleFieldsData.setDefinition(def); | 
  | 87 | 0 |                              callback.onModelReady(ruleFieldsData); | 
  | 88 | 0 |                          } | 
  | 89 |  |                      }); | 
  | 90 |  |                  } else { | 
  | 91 | 0 |                      callback.onModelReady(ruleFieldsData); | 
  | 92 |  |                  } | 
  | 93 | 0 |              } | 
  | 94 |  |          }); | 
  | 95 |  |   | 
  | 96 | 0 |          reqCompController.showView(BuildCourseView.VIEW); | 
  | 97 | 0 |          add(reqCompController); | 
  | 98 | 0 |      } | 
  | 99 |  |   | 
  | 100 |  |      @Override | 
  | 101 |  |      public void setValue(final String cluSetId) { | 
  | 102 | 0 |          if (cluSetId != null) { | 
  | 103 | 0 |              KSBlockingProgressIndicator.addTask(retrievingTask); | 
  | 104 | 0 |              cluSetRetriever.getData(cluSetId,  new Callback<Data>() { | 
  | 105 |  |                  @Override | 
  | 106 |  |                  public void exec(Data result) { | 
  | 107 |  |                      try { | 
  | 108 | 0 |                          ruleFieldsData.setRoot(result); | 
  | 109 | 0 |                          cluSetEditorWidgetView.updateView(ruleFieldsData); | 
  | 110 | 0 |                          reqCompController.showView(BuildCourseView.VIEW); | 
  | 111 |  |                      } finally { | 
  | 112 | 0 |                          KSBlockingProgressIndicator.removeTask(retrievingTask); | 
  | 113 | 0 |                      } | 
  | 114 | 0 |                  } | 
  | 115 |  |              }); | 
  | 116 |  |          } | 
  | 117 | 0 |      } | 
  | 118 |  |   | 
  | 119 |  |      @Override | 
  | 120 |  |      public void initWidget(List<Metadata> fieldsMetadata) { | 
  | 121 | 0 |      } | 
  | 122 |  |   | 
  | 123 |  |      @Override | 
  | 124 |  |      public void getValue(final Callback<String> doneSaveCallback) { | 
  | 125 | 0 |          GWT.log("CluSetManagementController received save action request.", null); | 
  | 126 | 0 |          reqCompController.getCurrentView().updateModel(); | 
  | 127 | 0 |          if(ruleFieldsData!=null){ | 
  | 128 | 0 |              ruleFieldsData.validate(new Callback<List<ValidationResultInfo>>() { | 
  | 129 |  |                  @Override | 
  | 130 |  |                  public void exec(List<ValidationResultInfo> result) { | 
  | 131 |  |       | 
  | 132 | 0 |                      boolean save = true; | 
  | 133 | 0 |                      View v = reqCompController.getCurrentView(); | 
  | 134 | 0 |                      if(v instanceof Section){ | 
  | 135 | 0 |                          ((Section) v).setFieldHasHadFocusFlags(true); | 
  | 136 | 0 |                          ErrorLevel status = ((Section) v).processValidationResults(result); | 
  | 137 | 0 |                          if(status == ErrorLevel.ERROR){ | 
  | 138 | 0 |                              save = false; | 
  | 139 |  |                          } | 
  | 140 |  |                      } | 
  | 141 |  |       | 
  | 142 | 0 |                      if(save){ | 
  | 143 | 0 |                          reqCompController.getCurrentView().updateModel(); | 
  | 144 | 0 |                          reqCompController.updateModel(); | 
  | 145 |  |                           | 
  | 146 | 0 |                          Calendar startCal = new GregorianCalendar(); | 
  | 147 | 0 |                          startCal.set(Calendar.YEAR, 1990); | 
  | 148 | 0 |                          startCal.set(Calendar.MONTH, Calendar.JANUARY); | 
  | 149 | 0 |                          startCal.set(Calendar.DATE, 1); | 
  | 150 | 0 |                          startCal.set(Calendar.HOUR_OF_DAY, 1); | 
  | 151 | 0 |                          startCal.set(Calendar.MINUTE, 0); | 
  | 152 | 0 |                          startCal.set(Calendar.SECOND, 0); | 
  | 153 | 0 |                          Calendar endCal = new GregorianCalendar(); | 
  | 154 | 0 |                          endCal.set(Calendar.YEAR, 2200); | 
  | 155 | 0 |                          endCal.set(Calendar.MONTH, Calendar.JANUARY); | 
  | 156 | 0 |                          endCal.set(Calendar.DATE, 1); | 
  | 157 | 0 |                          endCal.set(Calendar.HOUR_OF_DAY, 1); | 
  | 158 | 0 |                          endCal.set(Calendar.MINUTE, 0); | 
  | 159 | 0 |                          endCal.set(Calendar.SECOND, 0); | 
  | 160 | 0 |                          CluSetHelper cluSetHelper = CluSetHelper.wrap(ruleFieldsData.getRoot()); | 
  | 161 | 0 |                          cluSetHelper.setType(cluSetType); | 
  | 162 | 0 |                          cluSetHelper.setName("AdHock"); | 
  | 163 | 0 |                          cluSetHelper.setReusable(new Boolean(false)); | 
  | 164 | 0 |                          cluSetHelper.setReferenceable(new Boolean(false)); | 
  | 165 | 0 |                          cluSetHelper.setEffectiveDate(startCal.getTime()); | 
  | 166 | 0 |                          cluSetHelper.setEffectiveDate(endCal.getTime()); | 
  | 167 |  |                           | 
  | 168 | 0 |                          int numClus = 0; | 
  | 169 | 0 |                          if (cluSetHelper.getApprovedClus() != null) { | 
  | 170 | 0 |                              List<String> approvedCluIds = new ArrayList<String>(); | 
  | 171 | 0 |                              for (Data.Property p : cluSetHelper.getApprovedClus()) { | 
  | 172 | 0 |                                  if(!"_runtimeData".equals(p.getKey())){ | 
  | 173 | 0 |                                      String approvedCluId = p.getValue(); | 
  | 174 | 0 |                                      approvedCluIds.add(approvedCluId); | 
  | 175 | 0 |                                  } | 
  | 176 |  |                              } | 
  | 177 | 0 |                              numClus = numClus + approvedCluIds.size(); | 
  | 178 |  |                          } | 
  | 179 | 0 |                          if (cluSetHelper.getProposedClus() != null) { | 
  | 180 | 0 |                              List<String> proposedCluIds = new ArrayList<String>(); | 
  | 181 | 0 |                              for (Data.Property p : cluSetHelper.getProposedClus()) { | 
  | 182 | 0 |                                  if(!"_runtimeData".equals(p.getKey())){ | 
  | 183 | 0 |                                      String proposedCluId = p.getValue(); | 
  | 184 | 0 |                                      proposedCluIds.add(proposedCluId); | 
  | 185 | 0 |                                  } | 
  | 186 |  |                              } | 
  | 187 | 0 |                              numClus = numClus + proposedCluIds.size(); | 
  | 188 |  |                          } | 
  | 189 | 0 |                          if (singularCluOnly && numClus > 1) { | 
  | 190 | 0 |                              Window.alert("Only one " + itemLabel + " is allowed.  " + | 
  | 191 |  |                                      "Please delete all " + itemLabel + " until there is only one left."); | 
  | 192 |  |                          } else { | 
  | 193 | 0 |                              cluSetRetriever.saveData(ruleFieldsData.getRoot(), new Callback<DataSaveResult>() { | 
  | 194 |  |                                  @Override | 
  | 195 |  |                                  public void exec(DataSaveResult result) { | 
  | 196 | 0 |                                      if (result.getValidationResults() != null && | 
  | 197 |  |                                              !result.getValidationResults().isEmpty()) { | 
  | 198 | 0 |                                          StringBuilder errorMessage = new StringBuilder(); | 
  | 199 | 0 |                                          errorMessage.append("Validation error: "); | 
  | 200 | 0 |                                          for (ValidationResultInfo validationError : result.getValidationResults()) { | 
  | 201 | 0 |                                              errorMessage.append(validationError.getMessage()).append(" "); | 
  | 202 |  |                                          } | 
  | 203 | 0 |                                          doneSaveCallback.exec(null); | 
  | 204 | 0 |                                          Window.alert(errorMessage.toString()); | 
  | 205 | 0 |                                      } else { | 
  | 206 | 0 |                                          ruleFieldsData.setRoot(result.getValue()); | 
  | 207 | 0 |                                          CluSetHelper helper = CluSetHelper.wrap((Data)ruleFieldsData.getRoot()); | 
  | 208 | 0 |                                          String cluSetId = helper.getId(); | 
  | 209 | 0 |                                          Data approvedClusData = helper.getApprovedClus(); | 
  | 210 | 0 |                                          Data proposedClusData = helper.getProposedClus(); | 
  | 211 | 0 |                                          String cluId = null; | 
  | 212 | 0 |                                          if (singularCluOnly) { | 
  | 213 | 0 |                                              if (cluId == null && approvedClusData != null) { | 
  | 214 | 0 |                                                  for (Data.Property p : approvedClusData) { | 
  | 215 | 0 |                                                      if(!"_runtimeData".equals(p.getKey())){ | 
  | 216 | 0 |                                                          cluId = p.getValue(); | 
  | 217 | 0 |                                                          break; | 
  | 218 |  |                                                      } | 
  | 219 |  |                                                  } | 
  | 220 |  |                                              } | 
  | 221 | 0 |                                              if (cluId == null && proposedClusData != null) { | 
  | 222 | 0 |                                                  for (Data.Property p : proposedClusData) { | 
  | 223 | 0 |                                                      if(!"_runtimeData".equals(p.getKey())){ | 
  | 224 | 0 |                                                          cluId = p.getValue(); | 
  | 225 | 0 |                                                          break; | 
  | 226 |  |                                                      } | 
  | 227 |  |                                                  } | 
  | 228 |  |                                              } | 
  | 229 | 0 |                                              doneSaveCallback.exec(cluId); | 
  | 230 |  |                                          } else { | 
  | 231 | 0 |                                              doneSaveCallback.exec(cluSetId); | 
  | 232 |  |                                          } | 
  | 233 |  |                                      } | 
  | 234 | 0 |                                  } | 
  | 235 |  |                              }); | 
  | 236 |  |                          } | 
  | 237 | 0 |                      } | 
  | 238 |  |                      else { | 
  | 239 | 0 |                          Window.alert("Save failed.  Please check fields for errors."); | 
  | 240 |  |                      } | 
  | 241 |  |   | 
  | 242 | 0 |                  } | 
  | 243 |  |              }); | 
  | 244 |  |          } | 
  | 245 | 0 |      } | 
  | 246 |  |       | 
  | 247 |  |  } |