| 1 |  |  package org.kuali.student.lum.course.service.assembler; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.Collections; | 
  | 5 |  |  import java.util.Comparator; | 
  | 6 |  |  import java.util.HashMap; | 
  | 7 |  |  import java.util.HashSet; | 
  | 8 |  |  import java.util.List; | 
  | 9 |  |  import java.util.Map; | 
  | 10 |  |  import java.util.Set; | 
  | 11 |  |  import java.util.Map.Entry; | 
  | 12 |  |   | 
  | 13 |  |  import org.kuali.student.common.assembly.BOAssembler; | 
  | 14 |  |  import org.kuali.student.common.assembly.BaseDTOAssemblyNode; | 
  | 15 |  |  import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation; | 
  | 16 |  |  import org.kuali.student.common.assembly.data.AssemblyException; | 
  | 17 |  |  import org.kuali.student.common.dto.DtoConstants; | 
  | 18 |  |  import org.kuali.student.common.exceptions.DoesNotExistException; | 
  | 19 |  |  import org.kuali.student.common.exceptions.InvalidParameterException; | 
  | 20 |  |  import org.kuali.student.common.exceptions.MissingParameterException; | 
  | 21 |  |  import org.kuali.student.common.exceptions.OperationFailedException; | 
  | 22 |  |  import org.kuali.student.common.util.UUIDHelper; | 
  | 23 |  |  import org.kuali.student.lum.course.dto.LoDisplayInfo; | 
  | 24 |  |  import org.kuali.student.lum.lo.dto.LoCategoryInfo; | 
  | 25 |  |  import org.kuali.student.lum.lo.dto.LoInfo; | 
  | 26 |  |  import org.kuali.student.lum.lo.dto.LoLoRelationInfo; | 
  | 27 |  |  import org.kuali.student.lum.lo.service.LearningObjectiveService; | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 | 674 |  public class LoAssembler implements BOAssembler<LoDisplayInfo, LoInfo> { | 
  | 31 |  |   | 
  | 32 |  |          private LearningObjectiveService loService; | 
  | 33 |  |           | 
  | 34 |  |          @Override | 
  | 35 |  |          public LoDisplayInfo assemble(LoInfo lo, LoDisplayInfo loDisplayInfo, | 
  | 36 |  |                          boolean shallowBuild) throws AssemblyException { | 
  | 37 |  |                   | 
  | 38 | 1059 |                  LoDisplayInfo loDisplay = (null != loDisplayInfo) ? loDisplayInfo : new LoDisplayInfo(); | 
  | 39 |  |                   | 
  | 40 | 1059 |                  loDisplay.setLoInfo(lo); | 
  | 41 |  |   | 
  | 42 | 1059 |                  if (!shallowBuild) { | 
  | 43 | 387 |                          String loId = lo.getId(); | 
  | 44 |  |                          try { | 
  | 45 | 387 |                                  List<LoCategoryInfo> loCategories = loService.getLoCategoriesForLo(loId); | 
  | 46 | 387 |                                  loDisplay.setLoCategoryInfoList(loCategories); | 
  | 47 | 0 |                          } catch (DoesNotExistException e) { | 
  | 48 | 0 |                          } catch (Exception e) { | 
  | 49 | 0 |                                  throw new AssemblyException("Error getting learning objective categories", e); | 
  | 50 | 387 |                          } | 
  | 51 |  |                          try { | 
  | 52 | 387 |                                  List<LoInfo> childLos = loService.getRelatedLosByLoId(loId,CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); | 
  | 53 | 387 |                                  for(LoInfo childLo:childLos){ | 
  | 54 | 304 |                                          LoDisplayInfo childLoDisplay = assemble(childLo, null, shallowBuild); | 
  | 55 | 304 |                                          childLoDisplay.setParentLoRelationid(lo.getId()); | 
  | 56 | 304 |                                          childLoDisplay.setParentRelType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); | 
  | 57 | 304 |                                          loDisplay.getLoDisplayInfoList().add(childLoDisplay); | 
  | 58 | 304 |                                  } | 
  | 59 | 387 |                                  if(loDisplay.getLoDisplayInfoList().size()>1){ | 
  | 60 | 151 |                                          Collections.sort(loDisplay.getLoDisplayInfoList(), LoDisplayComparator.getInstance()); | 
  | 61 |  |                                  } | 
  | 62 | 0 |                          } catch (DoesNotExistException e) { | 
  | 63 | 0 |                          } catch (Exception e) { | 
  | 64 | 0 |                                  throw new AssemblyException("Error getting learning objective", e); | 
  | 65 | 387 |                          } | 
  | 66 |  |   | 
  | 67 |  |                  } | 
  | 68 | 1059 |                  return loDisplay; | 
  | 69 |  |          } | 
  | 70 |  |   | 
  | 71 |  |          @Override | 
  | 72 |  |           | 
  | 73 |  |          public BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> disassemble( | 
  | 74 |  |                          LoDisplayInfo loDisplay, NodeOperation operation) | 
  | 75 |  |                          throws AssemblyException { | 
  | 76 |  |                   | 
  | 77 | 703 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> result = new BaseDTOAssemblyNode<LoDisplayInfo, LoInfo>(this); | 
  | 78 |  |                   | 
  | 79 |  |                   | 
  | 80 | 703 |                  if (loDisplay == null || loDisplay.getLoInfo() == null) { | 
  | 81 | 0 |                          throw new AssemblyException("Lo can not be null"); | 
  | 82 |  |                  } | 
  | 83 | 703 |                  if (NodeOperation.CREATE != operation && null == loDisplay.getLoInfo().getId()) { | 
  | 84 | 0 |                          throw new AssemblyException("Lo id can not be null"); | 
  | 85 |  |                  } | 
  | 86 |  |                   | 
  | 87 |  |                   | 
  | 88 | 703 |                  loDisplay.getLoInfo().setId(UUIDHelper.genStringUUID(loDisplay.getLoInfo().getId())); | 
  | 89 |  |                   | 
  | 90 |  |                   | 
  | 91 | 703 |                  loDisplay.getLoInfo().setType(CourseAssemblerConstants.COURSE_LO_TYPE); | 
  | 92 | 703 |                  loDisplay.getLoInfo().setLoRepositoryKey(CourseAssemblerConstants.COURSE_LO_REPOSITORY_KEY); | 
  | 93 |  |                   | 
  | 94 |  |                   | 
  | 95 |  |                   | 
  | 96 | 703 |                  result.setBusinessDTORef(loDisplay); | 
  | 97 | 703 |                  result.setNodeData(loDisplay.getLoInfo()); | 
  | 98 | 703 |                  result.setOperation(operation); | 
  | 99 |  |                   | 
  | 100 |  |                   | 
  | 101 |  |                  try { | 
  | 102 | 703 |                          List<BaseDTOAssemblyNode<?, ?>> childLoNodes = disassembleChildLos(loDisplay, operation); | 
  | 103 | 703 |                          result.getChildNodes().addAll(childLoNodes); | 
  | 104 | 0 |                  } catch (DoesNotExistException e) { | 
  | 105 | 0 |                  } catch (Exception e) { | 
  | 106 | 0 |                          throw new AssemblyException("Error disassembling child los", e); | 
  | 107 | 703 |                  }  | 
  | 108 |  |   | 
  | 109 |  |                   | 
  | 110 |  |                  try { | 
  | 111 | 703 |                          List<BaseDTOAssemblyNode<?, ?>> categoryNodes = disassembleCategories(loDisplay, operation); | 
  | 112 | 703 |                          result.getChildNodes().addAll(categoryNodes); | 
  | 113 | 0 |                  } catch (Exception e) { | 
  | 114 | 0 |                          throw new AssemblyException("Error disassembling categories", e); | 
  | 115 | 703 |                  }  | 
  | 116 |  |                   | 
  | 117 | 703 |                  return result; | 
  | 118 |  |          } | 
  | 119 |  |   | 
  | 120 |  |          private List<BaseDTOAssemblyNode<?, ?>> disassembleCategories( | 
  | 121 |  |                          LoDisplayInfo loDisplay, NodeOperation operation) throws AssemblyException { | 
  | 122 |  |                   | 
  | 123 | 703 |                  List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>(); | 
  | 124 |  |                   | 
  | 125 |  |                   | 
  | 126 | 703 |                  Set<String> currentCategoryIds = new HashSet<String>(); | 
  | 127 |  |                   | 
  | 128 | 703 |                  if (!NodeOperation.CREATE.equals(operation)) { | 
  | 129 |  |                          try { | 
  | 130 | 52 |                                  List<LoCategoryInfo> categories = loService.getLoCategoriesForLo(loDisplay.getLoInfo().getId()); | 
  | 131 | 52 |                                  for (LoCategoryInfo category : categories) { | 
  | 132 | 71 |                                          currentCategoryIds.add(category.getId()); | 
  | 133 |  |                                  } | 
  | 134 | 0 |                          } catch (DoesNotExistException e) { | 
  | 135 | 0 |                          } catch (Exception e) { | 
  | 136 | 0 |                                  throw new AssemblyException("Error getting categories",        e); | 
  | 137 | 52 |                          } | 
  | 138 |  |                  } | 
  | 139 |  |                   | 
  | 140 | 703 |                  for (LoCategoryInfo category : loDisplay.getLoCategoryInfoList()) { | 
  | 141 |  |   | 
  | 142 |  |                           | 
  | 143 | 1360 |                      if (NodeOperation.CREATE == operation | 
  | 144 |  |                              || (NodeOperation.UPDATE == operation &&  !currentCategoryIds.contains(category.getId()))) { | 
  | 145 |  |                               | 
  | 146 | 1289 |                              LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo(); | 
  | 147 | 1289 |                              loCategoryRelation.setCategoryId(category.getId()); | 
  | 148 | 1289 |                              loCategoryRelation.setLoId(loDisplay.getLoInfo().getId()); | 
  | 149 |  |                               | 
  | 150 | 1289 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToAddNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null); | 
  | 151 | 1289 |                  relationToAddNode.setNodeData(loCategoryRelation); | 
  | 152 | 1289 |                  relationToAddNode.setOperation(NodeOperation.CREATE); | 
  | 153 | 1289 |                  results.add(relationToAddNode); | 
  | 154 |  |   | 
  | 155 | 1289 |                  currentCategoryIds.remove(category.getId()); | 
  | 156 | 1289 |                      } else if (NodeOperation.UPDATE == operation | 
  | 157 |  |                                          && currentCategoryIds.contains(category.getId())) { | 
  | 158 | 29 |                  currentCategoryIds.remove(category.getId()); | 
  | 159 |  |                          }  | 
  | 160 |  |                  } | 
  | 161 |  |                   | 
  | 162 | 703 |                  for(String categoryId:currentCategoryIds){ | 
  | 163 | 42 |                      LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo(); | 
  | 164 | 42 |                      loCategoryRelation.setCategoryId(categoryId); | 
  | 165 | 42 |                      loCategoryRelation.setLoId(loDisplay.getLoInfo().getId()); | 
  | 166 |  |                       | 
  | 167 | 42 |              BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null); | 
  | 168 | 42 |              relationToDeleteNode.setNodeData(loCategoryRelation); | 
  | 169 | 42 |              relationToDeleteNode.setOperation(NodeOperation.DELETE); | 
  | 170 | 42 |              results.add(relationToDeleteNode); | 
  | 171 | 42 |                  } | 
  | 172 |  |                   | 
  | 173 | 703 |                  return results; | 
  | 174 |  |          } | 
  | 175 |  |   | 
  | 176 |  |          private List<BaseDTOAssemblyNode<?, ?>> disassembleChildLos(LoDisplayInfo loDisplay, NodeOperation operation) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, AssemblyException{ | 
  | 177 | 703 |                  List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>(); | 
  | 178 | 703 |                  Map<String,LoLoRelationInfo> currentLoRelations = new HashMap<String,LoLoRelationInfo>(); | 
  | 179 |  |                   | 
  | 180 | 703 |                  if (!NodeOperation.CREATE.equals(operation)) { | 
  | 181 |  |                          try { | 
  | 182 | 52 |                                  List<LoLoRelationInfo> loRelations = loService.getLoLoRelationsByLoId(loDisplay.getLoInfo().getId()); | 
  | 183 | 52 |                                  for (LoLoRelationInfo loRelation : loRelations) { | 
  | 184 |  |                                           | 
  | 185 | 66 |                                          if(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES.equals(loRelation.getType())&& | 
  | 186 |  |                                                          !loDisplay.getLoInfo().getId().equals(loRelation.getRelatedLoId())){ | 
  | 187 | 30 |                                                  currentLoRelations.put(loRelation.getRelatedLoId(), loRelation); | 
  | 188 |  |                                          } | 
  | 189 |  |                                  } | 
  | 190 | 0 |                          } catch (DoesNotExistException e) { | 
  | 191 | 0 |                          } catch (Exception e) { | 
  | 192 | 0 |                                  throw new AssemblyException("Error getting categories",        e); | 
  | 193 | 52 |                          } | 
  | 194 |  |                  } | 
  | 195 |  |                   | 
  | 196 |  |                   | 
  | 197 | 703 |                  for (LoDisplayInfo childDisplay : loDisplay.getLoDisplayInfoList()) { | 
  | 198 |  |                       | 
  | 199 |  |                       | 
  | 200 |  |                       | 
  | 201 |  |                       | 
  | 202 | 582 |                      childDisplay.getLoInfo().setState(loDisplay.getLoInfo().getState()); | 
  | 203 |  |                       | 
  | 204 |  |                           | 
  | 205 | 582 |                      if (NodeOperation.CREATE == operation | 
  | 206 |  |                              || (NodeOperation.UPDATE == operation &&  !currentLoRelations.containsKey(childDisplay.getLoInfo().getId()))) { | 
  | 207 |  |                                               | 
  | 208 |  |                   | 
  | 209 |  |                   | 
  | 210 | 552 |                              childDisplay.getLoInfo().setId(null); | 
  | 211 | 552 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this | 
  | 212 |  |                          .disassemble(childDisplay, NodeOperation.CREATE); | 
  | 213 | 552 |                  results.add(loNode); | 
  | 214 |  |   | 
  | 215 |  |                   | 
  | 216 | 552 |                  LoLoRelationInfo relation = new LoLoRelationInfo(); | 
  | 217 | 552 |                  relation.setLoId(loDisplay.getLoInfo().getId()); | 
  | 218 | 552 |                  relation.setRelatedLoId(loNode.getNodeData().getId()); | 
  | 219 | 552 |                  relation.setType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); | 
  | 220 |  |                   | 
  | 221 |  |                   | 
  | 222 |  |                   | 
  | 223 |  |                   | 
  | 224 | 552 |                  relation.setState(DtoConstants.STATE_ACTIVE); | 
  | 225 |  |                   | 
  | 226 |  |   | 
  | 227 | 552 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( | 
  | 228 |  |                          null); | 
  | 229 | 552 |                  relationNode.setNodeData(relation); | 
  | 230 | 552 |                  relationNode.setOperation(NodeOperation.CREATE); | 
  | 231 |  |   | 
  | 232 | 552 |                  results.add(relationNode); | 
  | 233 | 552 |              } else if (NodeOperation.UPDATE == operation | 
  | 234 |  |                                          && currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) { | 
  | 235 |  |                                   | 
  | 236 |  |                                   | 
  | 237 | 12 |                                  BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this | 
  | 238 |  |                                                  .disassemble(childDisplay, NodeOperation.UPDATE); | 
  | 239 | 12 |                                  results.add(loNode); | 
  | 240 |  |   | 
  | 241 |  |                                   | 
  | 242 |  |                                   | 
  | 243 | 12 |                                  currentLoRelations.remove(childDisplay.getLoInfo().getId()); | 
  | 244 | 12 |                          } else if (NodeOperation.DELETE == operation | 
  | 245 |  |                      && currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) { | 
  | 246 |  |                               | 
  | 247 |  |                   | 
  | 248 | 18 |                                  LoLoRelationInfo relationToDelete = currentLoRelations.get(childDisplay.getLoInfo().getId()); | 
  | 249 | 18 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( | 
  | 250 |  |                          null); | 
  | 251 | 18 |                  relationToDeleteNode.setNodeData(relationToDelete); | 
  | 252 | 18 |                  relationToDeleteNode.setOperation(NodeOperation.DELETE); | 
  | 253 | 18 |                  results.add(relationToDeleteNode); | 
  | 254 |  |               | 
  | 255 | 18 |                  BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this | 
  | 256 |  |                  .disassemble(childDisplay, NodeOperation.DELETE); | 
  | 257 | 18 |                  results.add(loNode);                                 | 
  | 258 |  |   | 
  | 259 |  |                   | 
  | 260 |  |                   | 
  | 261 | 18 |                  currentLoRelations.remove(childDisplay.getLoInfo().getId());                             | 
  | 262 | 582 |                          } | 
  | 263 |  |                  }          | 
  | 264 |  |   | 
  | 265 |  |           | 
  | 266 |  |           | 
  | 267 | 703 |          for (Entry<String, LoLoRelationInfo> entry : currentLoRelations.entrySet()) { | 
  | 268 |  |               | 
  | 269 |  |               | 
  | 270 | 0 |                  LoLoRelationInfo relationToDelete = entry.getValue(); | 
  | 271 | 0 |              BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( | 
  | 272 |  |                      null); | 
  | 273 | 0 |              relationToDeleteNode.setNodeData(relationToDelete); | 
  | 274 | 0 |              relationToDeleteNode.setOperation(NodeOperation.DELETE); | 
  | 275 | 0 |              results.add(relationToDeleteNode); | 
  | 276 |  |   | 
  | 277 | 0 |              LoInfo loToDelete = loService.getLo(entry.getKey()); | 
  | 278 | 0 |              LoDisplayInfo loDisplayToDelete = this.assemble(loToDelete, null, false); | 
  | 279 | 0 |              BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this.disassemble(loDisplayToDelete, NodeOperation.DELETE); | 
  | 280 | 0 |              results.add(loNode);                                             | 
  | 281 | 0 |          } | 
  | 282 | 703 |                  return results; | 
  | 283 |  |                   | 
  | 284 |  |          } | 
  | 285 |  |   | 
  | 286 |  |          public void setLoService(LearningObjectiveService loService) { | 
  | 287 | 2 |                  this.loService = loService; | 
  | 288 | 2 |          } | 
  | 289 |  |   | 
  | 290 | 2 |          public static class LoDisplayComparator implements Comparator<LoDisplayInfo>{ | 
  | 291 | 1 |                  private static LoDisplayComparator instance = new LoDisplayComparator(); | 
  | 292 |  |                  @Override | 
  | 293 |  |                  public int compare(LoDisplayInfo o1, LoDisplayInfo o2) { | 
  | 294 | 151 |                          String o1Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE); | 
  | 295 | 151 |                          String o2Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE); | 
  | 296 | 151 |                          if(o1Sequence!=null){ | 
  | 297 | 0 |                                  return o1Sequence.compareTo(o2Sequence); | 
  | 298 | 151 |                          }else if(o2Sequence!=null){ | 
  | 299 | 0 |                                  return -1; | 
  | 300 |  |                          } | 
  | 301 | 151 |                          return 0; | 
  | 302 |  |                  } | 
  | 303 |  |                  public static LoDisplayComparator getInstance() { | 
  | 304 | 151 |                          return instance; | 
  | 305 |  |                  } | 
  | 306 |  |          } | 
  | 307 |  |  } |