| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.lum.lu.ui.tools.server.gwt; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |   | 
  | 21 |  |  import org.kuali.student.common.ui.client.service.DataSaveResult; | 
  | 22 |  |  import org.kuali.student.common.ui.client.service.exceptions.OperationFailedException; | 
  | 23 |  |  import org.kuali.student.common.ui.server.gwt.DataGwtServlet; | 
  | 24 |  |  import org.kuali.student.core.assembly.data.AssemblyException; | 
  | 25 |  |  import org.kuali.student.core.assembly.data.Data; | 
  | 26 |  |  import org.kuali.student.core.exceptions.DataValidationErrorException; | 
  | 27 |  |  import org.kuali.student.core.search.dto.SearchRequest; | 
  | 28 |  |  import org.kuali.student.core.search.dto.SearchResult; | 
  | 29 |  |  import org.kuali.student.core.search.dto.SearchResultCell; | 
  | 30 |  |  import org.kuali.student.core.search.dto.SearchResultRow; | 
  | 31 |  |  import org.kuali.student.core.versionmanagement.dto.VersionDisplayInfo; | 
  | 32 |  |  import org.kuali.student.lum.common.client.widgets.CluInformation; | 
  | 33 |  |  import org.kuali.student.lum.common.client.widgets.CluSetInformation; | 
  | 34 |  |  import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcService; | 
  | 35 |  |  import org.kuali.student.lum.lrc.dto.ResultComponentInfo; | 
  | 36 |  |  import org.kuali.student.lum.lrc.service.LrcService; | 
  | 37 |  |  import org.kuali.student.lum.lu.dto.*; | 
  | 38 |  |  import org.kuali.student.lum.lu.service.LuService; | 
  | 39 |  |  import org.kuali.student.lum.lu.service.LuServiceConstants; | 
  | 40 |  |   | 
  | 41 |  |  import org.apache.log4j.Logger; | 
  | 42 |  |   | 
  | 43 | 0 |  public class CluSetManagementRpcGwtServlet extends DataGwtServlet implements | 
  | 44 |  |                  CluSetManagementRpcService { | 
  | 45 |  |   | 
  | 46 |  |          private static final long serialVersionUID = 1L; | 
  | 47 | 0 |          final static Logger LOG = Logger.getLogger(CluSetManagementRpcGwtServlet.class); | 
  | 48 |  |          private LuService luService; | 
  | 49 |  |          private LrcService lrcService; | 
  | 50 |  |       | 
  | 51 |  |          public LuService getLuService() { | 
  | 52 | 0 |          return luService; | 
  | 53 |  |      } | 
  | 54 |  |   | 
  | 55 |  |      public void setLuService(LuService luService) { | 
  | 56 | 0 |          this.luService = luService; | 
  | 57 | 0 |      } | 
  | 58 |  |   | 
  | 59 |  |      public LrcService getLrcService() { | 
  | 60 | 0 |          return lrcService; | 
  | 61 |  |      } | 
  | 62 |  |   | 
  | 63 |  |      public void setLrcService(LrcService lrcService) { | 
  | 64 | 0 |          this.lrcService = lrcService; | 
  | 65 | 0 |      } | 
  | 66 |  |       | 
  | 67 |  |      @Override | 
  | 68 |  |      public Data getData(String id) throws OperationFailedException { | 
  | 69 |  |          try{ | 
  | 70 | 0 |              return getDataService().getData(id); | 
  | 71 | 0 |          } catch (Exception e) { | 
  | 72 | 0 |              LOG.error("Could not get Data ", e); | 
  | 73 | 0 |              throw new OperationFailedException("Failed to get data"); | 
  | 74 |  |          } | 
  | 75 |  |      } | 
  | 76 |  |   | 
  | 77 |  |      @Override | 
  | 78 |  |      public DataSaveResult saveData(Data data) throws OperationFailedException { | 
  | 79 |  |          try{ | 
  | 80 | 0 |              return getDataService().saveData(data); | 
  | 81 | 0 |          }catch (DataValidationErrorException dvee){ | 
  | 82 | 0 |              return new DataSaveResult(dvee.getValidationResults(), null); | 
  | 83 | 0 |          } catch (Exception e) { | 
  | 84 | 0 |              LOG.error("Could not save data ", e); | 
  | 85 | 0 |              throw new OperationFailedException("Failed to save data"); | 
  | 86 |  |          }  | 
  | 87 |  |      } | 
  | 88 |  |   | 
  | 89 |  |      private CluSetInfo getCluSetInfo(String cluSetId) throws OperationFailedException { | 
  | 90 | 0 |          List<String> cluIds = null; | 
  | 91 | 0 |          CluSetInfo cluSetInfo = null; | 
  | 92 |  |          try { | 
  | 93 |  |               | 
  | 94 |  |               | 
  | 95 |  |               | 
  | 96 | 0 |              cluSetInfo = luService.getCluSetInfo(cluSetId); | 
  | 97 | 0 |              cluSetInfo.setCluIds(null); | 
  | 98 | 0 |              cluIds = luService.getCluIdsFromCluSet(cluSetId); | 
  | 99 | 0 |              cluSetInfo.setCluIds(cluIds); | 
  | 100 | 0 |              upWrap(cluSetInfo); | 
  | 101 | 0 |          } catch (Exception e) { | 
  | 102 | 0 |              throw new OperationFailedException("Failed to retrieve cluset info for " + cluSetId, e); | 
  | 103 | 0 |          } | 
  | 104 | 0 |          return cluSetInfo; | 
  | 105 |  |      } | 
  | 106 |  |       | 
  | 107 |  |      private List<CluSetInfo> getCluSetInfos(List<String> cluSetIds) throws OperationFailedException { | 
  | 108 | 0 |          List<CluSetInfo> clusetInfos = null; | 
  | 109 | 0 |          if (cluSetIds != null) { | 
  | 110 | 0 |              for (String cluSetId : cluSetIds) { | 
  | 111 | 0 |                  clusetInfos = (clusetInfos == null)? new ArrayList<CluSetInfo>() : clusetInfos; | 
  | 112 | 0 |                  clusetInfos.add(getCluSetInfo(cluSetId)); | 
  | 113 |  |              } | 
  | 114 |  |          } | 
  | 115 | 0 |          return clusetInfos; | 
  | 116 |  |      } | 
  | 117 |  |   | 
  | 118 |  |      private void upWrap(CluSetInfo cluSetInfo) throws AssemblyException { | 
  | 119 | 0 |          List<String> cluSetIds = (cluSetInfo == null)? null : cluSetInfo.getCluSetIds(); | 
  | 120 | 0 |          List<String> unWrappedCluSetIds = null; | 
  | 121 | 0 |          List<CluSetInfo> wrappedCluSets = null; | 
  | 122 | 0 |          List<CluSetInfo> subCluSets = null; | 
  | 123 |  |   | 
  | 124 |  |          try { | 
  | 125 | 0 |              if (cluSetIds != null && !cluSetIds.isEmpty()) { | 
  | 126 | 0 |                  subCluSets = luService.getCluSetInfoByIdList(cluSetIds); | 
  | 127 |  |              } | 
  | 128 | 0 |          } catch (Exception e) { | 
  | 129 | 0 |              LOG.error(e.getMessage(), e); | 
  | 130 | 0 |              throw new AssemblyException("Failed to retrieve the sub clusets of cluset " + | 
  | 131 |  |                      cluSetInfo.getId()); | 
  | 132 | 0 |          } | 
  | 133 |  |           | 
  | 134 | 0 |          if (subCluSets != null) { | 
  | 135 | 0 |              for (CluSetInfo subCluSet : subCluSets) { | 
  | 136 | 0 |                  if (subCluSet.getIsReusable()) { | 
  | 137 | 0 |                      unWrappedCluSetIds = (unWrappedCluSetIds == null)? | 
  | 138 |  |                              new ArrayList<String>() : unWrappedCluSetIds; | 
  | 139 | 0 |                              unWrappedCluSetIds.add(subCluSet.getId()); | 
  | 140 |  |                  } else { | 
  | 141 | 0 |                      wrappedCluSets = (wrappedCluSets == null)? | 
  | 142 |  |                              new ArrayList<CluSetInfo>() : wrappedCluSets; | 
  | 143 | 0 |                              wrappedCluSets.add(subCluSet); | 
  | 144 |  |                  } | 
  | 145 |  |              } | 
  | 146 |  |          } | 
  | 147 | 0 |          cluSetInfo.setCluSetIds(unWrappedCluSetIds); | 
  | 148 | 0 |          if (wrappedCluSets != null) { | 
  | 149 | 0 |              for (CluSetInfo wrappedCluSet : wrappedCluSets) { | 
  | 150 | 0 |                  MembershipQueryInfo mqInfo = wrappedCluSet.getMembershipQuery(); | 
  | 151 | 0 |                  if (wrappedCluSet.getCluIds() != null && !wrappedCluSet.getCluIds().isEmpty()) { | 
  | 152 | 0 |                      cluSetInfo.setCluIds(wrappedCluSet.getCluIds()); | 
  | 153 |  |                  } | 
  | 154 | 0 |                  if (mqInfo != null && mqInfo.getSearchTypeKey() != null && !mqInfo.getSearchTypeKey().isEmpty()) { | 
  | 155 | 0 |                      cluSetInfo.setMembershipQuery(mqInfo); | 
  | 156 |  |                  } | 
  | 157 | 0 |              } | 
  | 158 |  |          } | 
  | 159 | 0 |      } | 
  | 160 |  |       | 
  | 161 |  |      private List<CluInformation> getCluInformations(List<String> cluIds) throws OperationFailedException { | 
  | 162 | 0 |          List<CluInformation> result = new ArrayList<CluInformation>(); | 
  | 163 | 0 |          if (cluIds != null) { | 
  | 164 | 0 |              for (String cluId : cluIds) { | 
  | 165 |  |                  try { | 
  | 166 | 0 |                          VersionDisplayInfo versionInfo = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, cluId); | 
  | 167 | 0 |                      CluInfo cluInfo = luService.getClu(versionInfo.getId()); | 
  | 168 | 0 |                      if (cluInfo != null) { | 
  | 169 |  |   | 
  | 170 |  |                           | 
  | 171 | 0 |                          String credits = ""; | 
  | 172 | 0 |                          List<CluResultInfo> cluResultInfos = luService.getCluResultByClu(versionInfo.getId()); | 
  | 173 | 0 |                          if (cluResultInfos != null) { | 
  | 174 | 0 |                              for (CluResultInfo cluResultInfo : cluResultInfos) { | 
  | 175 | 0 |                                  String cluType = cluResultInfo.getType(); | 
  | 176 |  |   | 
  | 177 |  |                                   | 
  | 178 | 0 |                                  if ((cluType == null) || (!cluType.equals("kuali.resultType.creditCourseResult"))) { | 
  | 179 | 0 |                                      continue; | 
  | 180 |  |                                  } | 
  | 181 |  |   | 
  | 182 |  |                                   | 
  | 183 | 0 |                                  ResultComponentInfo resultComponentInfo = null; | 
  | 184 | 0 |                                  List<String> resultValues = null; | 
  | 185 | 0 |                                  String creditType = ""; | 
  | 186 | 0 |                                  if (cluResultInfo.getResultOptions() != null) { | 
  | 187 | 0 |                                      for (ResultOptionInfo resultOption : cluResultInfo.getResultOptions()) { | 
  | 188 | 0 |                                          if (resultOption.getResultComponentId() != null) { | 
  | 189 | 0 |                                              resultComponentInfo = lrcService.getResultComponent(resultOption.getResultComponentId()); | 
  | 190 | 0 |                                              resultValues = resultComponentInfo.getResultValues(); | 
  | 191 | 0 |                                              creditType = resultComponentInfo.getType(); | 
  | 192 | 0 |                                              break; | 
  | 193 |  |                                          } | 
  | 194 |  |                                      } | 
  | 195 |  |                                  } | 
  | 196 | 0 |                                  if (resultValues == null) { | 
  | 197 | 0 |                                      continue; | 
  | 198 |  |                                  } | 
  | 199 |  |   | 
  | 200 | 0 |                                  if (!credits.isEmpty()) { | 
  | 201 | 0 |                                      credits = credits + "; "; | 
  | 202 |  |                                  } | 
  | 203 |  |   | 
  | 204 | 0 |                                  if (creditType.equals("kuali.resultComponentType.credit.degree.fixed")) { | 
  | 205 | 0 |                                      credits = credits + resultValues.get(0); | 
  | 206 | 0 |                                  } else if (creditType.equals("kuali.resultComponentType.credit.degree.multiple")) { | 
  | 207 | 0 |                                      boolean firstValue = true; | 
  | 208 | 0 |                                      for (String resultValue : resultValues) { | 
  | 209 | 0 |                                          credits = credits + (firstValue ? "" :", ")  + resultValue; | 
  | 210 | 0 |                                          firstValue = false; | 
  | 211 |  |                                      } | 
  | 212 | 0 |                                  } else if (creditType.equals("kuali.resultComponentType.credit.degree.range")) { | 
  | 213 | 0 |                                      credits = credits + resultComponentInfo.getAttributes().get("minCreditValue") + " - " + resultComponentInfo.getAttributes().get("maxCreditValue"); | 
  | 214 |  |                                  } | 
  | 215 | 0 |                              } | 
  | 216 |  |                          } | 
  | 217 |  |                           | 
  | 218 |  |   | 
  | 219 | 0 |                          CluInformation cluInformation = new CluInformation(); | 
  | 220 | 0 |                          if (cluInfo.getOfficialIdentifier() != null) { | 
  | 221 | 0 |                              cluInformation.setCode(cluInfo.getOfficialIdentifier().getCode()); | 
  | 222 | 0 |                              cluInformation.setTitle(cluInfo.getOfficialIdentifier().getShortName()); | 
  | 223 | 0 |                              cluInformation.setCredits(credits); | 
  | 224 |  |                          } | 
  | 225 | 0 |                          cluInformation.setVerIndependentId(cluInfo.getId()); | 
  | 226 | 0 |                          result.add(cluInformation); | 
  | 227 |  |                      } | 
  | 228 | 0 |                  } catch (Exception e) { | 
  | 229 | 0 |                      throw new OperationFailedException("Failed to get info for cluId " + cluId, e); | 
  | 230 | 0 |                  } | 
  | 231 |  |              } | 
  | 232 |  |          } | 
  | 233 | 0 |          return result; | 
  | 234 |  |      } | 
  | 235 |  |   | 
  | 236 |  |      @Override | 
  | 237 |  |      public CluSetInformation getCluSetInformation(String cluSetId) throws OperationFailedException { | 
  | 238 | 0 |          CluSetInformation result = new CluSetInformation(); | 
  | 239 | 0 |          CluSetInfo cluSetInfo = getCluSetInfo(cluSetId); | 
  | 240 | 0 |          List<String> allCluIds = cluSetInfo.getCluIds(); | 
  | 241 | 0 |          List<String> cluSetIds =  cluSetInfo.getCluSetIds(); | 
  | 242 | 0 |          final MembershipQueryInfo membershipQueryInfo = cluSetInfo.getMembershipQuery(); | 
  | 243 | 0 |          result.setId(cluSetId); | 
  | 244 | 0 |          if (allCluIds != null) { | 
  | 245 | 0 |              List<CluInformation> clus = getCluInformations(allCluIds); | 
  | 246 | 0 |              result.setClus(clus); | 
  | 247 |  |          } | 
  | 248 | 0 |          if (cluSetIds != null) { | 
  | 249 | 0 |              List<CluSetInfo> cluSetInfos = getCluSetInfos(cluSetIds); | 
  | 250 | 0 |              result.setCluSets(cluSetInfos); | 
  | 251 |  |          } | 
  | 252 | 0 |          if (membershipQueryInfo != null) { | 
  | 253 | 0 |              SearchRequest searchRequest = new SearchRequest(); | 
  | 254 | 0 |              searchRequest.setSearchKey(membershipQueryInfo.getSearchTypeKey()); | 
  | 255 | 0 |              searchRequest.setParams(membershipQueryInfo.getQueryParamValueList()); | 
  | 256 | 0 |              SearchResult searchResult = null; | 
  | 257 |  |              try { | 
  | 258 | 0 |                  searchResult = luService.search(searchRequest); | 
  | 259 | 0 |              } catch (Exception e) { | 
  | 260 | 0 |                  throw new OperationFailedException("Failed to search for clus in clu range", e); | 
  | 261 | 0 |              } | 
  | 262 | 0 |              List<CluInformation> clusInRange = new ArrayList<CluInformation>(); | 
  | 263 | 0 |              List<SearchResultRow> rows = searchResult.getRows(); | 
  | 264 | 0 |              for(SearchResultRow row : rows) { | 
  | 265 | 0 |                  List<SearchResultCell> cells = row.getCells(); | 
  | 266 | 0 |                  CluInformation cluInformation = new CluInformation(); | 
  | 267 | 0 |                  for(SearchResultCell cell : cells) { | 
  | 268 | 0 |                      if(cell.getKey().equals("lu.resultColumn.cluId")) { | 
  | 269 | 0 |                          cluInformation.setVerIndependentId(cell.getValue()); | 
  | 270 |  |                      } | 
  | 271 | 0 |                      if (cell.getKey().equals("lu.resultColumn.luOptionalCode")) { | 
  | 272 | 0 |                          cluInformation.setCode(cell.getValue()); | 
  | 273 |  |                      } | 
  | 274 | 0 |                      if (cell.getKey().equals("lu.resultColumn.luOptionalShortName")) { | 
  | 275 | 0 |                          cluInformation.setTitle(cell.getValue()); | 
  | 276 |  |                      } | 
  | 277 |  |                  } | 
  | 278 | 0 |                  clusInRange.add(cluInformation); | 
  | 279 | 0 |              } | 
  | 280 | 0 |              result.setMembershipQueryInfo(membershipQueryInfo); | 
  | 281 | 0 |              result.setClusInRange(clusInRange); | 
  | 282 |  |          } | 
  | 283 | 0 |          return result; | 
  | 284 |  |      } | 
  | 285 |  |           | 
  | 286 |  |  } |