| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.lum.course.service.assembler; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.HashMap; | 
  | 20 |  |  import java.util.List; | 
  | 21 |  |  import java.util.Map; | 
  | 22 |  |  import java.util.Map.Entry; | 
  | 23 |  |   | 
  | 24 |  |  import org.apache.log4j.Logger; | 
  | 25 |  |  import org.kuali.student.common.util.UUIDHelper; | 
  | 26 |  |  import org.kuali.student.core.assembly.BOAssembler; | 
  | 27 |  |  import org.kuali.student.core.assembly.BaseDTOAssemblyNode; | 
  | 28 |  |  import org.kuali.student.core.assembly.BaseDTOAssemblyNode.NodeOperation; | 
  | 29 |  |  import org.kuali.student.core.assembly.data.AssemblyException; | 
  | 30 |  |  import org.kuali.student.core.exceptions.DoesNotExistException; | 
  | 31 |  |  import org.kuali.student.core.exceptions.InvalidParameterException; | 
  | 32 |  |  import org.kuali.student.core.exceptions.MissingParameterException; | 
  | 33 |  |  import org.kuali.student.core.exceptions.OperationFailedException; | 
  | 34 |  |  import org.kuali.student.lum.course.dto.ActivityInfo; | 
  | 35 |  |  import org.kuali.student.lum.course.dto.CourseInfo; | 
  | 36 |  |  import org.kuali.student.lum.course.dto.FormatInfo; | 
  | 37 |  |  import org.kuali.student.lum.lu.dto.CluCluRelationInfo; | 
  | 38 |  |  import org.kuali.student.lum.lu.dto.CluInfo; | 
  | 39 |  |  import org.kuali.student.lum.lu.service.LuService; | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 | 47 |  public class FormatAssembler implements BOAssembler<FormatInfo, CluInfo> { | 
  | 49 | 1 |          final static Logger LOG = Logger.getLogger(FormatAssembler.class); | 
  | 50 |  |   | 
  | 51 |  |          private BOAssembler<ActivityInfo, CluInfo> activityAssembler; | 
  | 52 |  |          private LuService luService; | 
  | 53 |  |   | 
  | 54 |  |          @Override | 
  | 55 |  |          public FormatInfo assemble(CluInfo clu, FormatInfo formatInfo, | 
  | 56 |  |                          boolean shallowBuild) throws AssemblyException { | 
  | 57 |  |   | 
  | 58 | 76 |                  if (clu == null) { | 
  | 59 | 0 |                          return null; | 
  | 60 |  |                  } | 
  | 61 |  |   | 
  | 62 | 76 |                  FormatInfo format = (null != formatInfo) ? formatInfo | 
  | 63 |  |                                  : new FormatInfo(); | 
  | 64 |  |   | 
  | 65 |  |                   | 
  | 66 | 76 |                  format.setId(clu.getId()); | 
  | 67 | 76 |                  format.setType(clu.getType()); | 
  | 68 | 76 |                  format.setState(clu.getState()); | 
  | 69 | 76 |                  format.setMetaInfo(clu.getMetaInfo()); | 
  | 70 | 76 |                  format.setAttributes(clu.getAttributes()); | 
  | 71 | 76 |              format.setDuration(clu.getStdDuration()); | 
  | 72 | 76 |              format.setTermsOffered(clu.getOfferedAtpTypes()); | 
  | 73 |  |                   | 
  | 74 |  |                   | 
  | 75 | 76 |                  if (!shallowBuild) { | 
  | 76 |  |                           | 
  | 77 |  |                           | 
  | 78 |  |                          try { | 
  | 79 | 30 |                                  List<CluInfo> activities = luService.getRelatedClusByCluId( | 
  | 80 |  |                                                  format.getId(), | 
  | 81 |  |                                                  CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE); | 
  | 82 | 30 |                                  for (CluInfo activity : activities) { | 
  | 83 | 57 |                                          ActivityInfo activityInfo = activityAssembler.assemble( | 
  | 84 |  |                                                          activity, null, false); | 
  | 85 | 57 |                                          format.getActivities().add(activityInfo); | 
  | 86 | 57 |                                  } | 
  | 87 | 0 |                          } catch (DoesNotExistException e) { | 
  | 88 | 0 |                          } catch (Exception e) { | 
  | 89 | 0 |                                  throw new AssemblyException("Error getting related activities", e); | 
  | 90 | 30 |                          }  | 
  | 91 |  |                  } | 
  | 92 | 76 |                  return format; | 
  | 93 |  |          } | 
  | 94 |  |   | 
  | 95 |  |          @Override | 
  | 96 |  |          public BaseDTOAssemblyNode<FormatInfo, CluInfo> disassemble( | 
  | 97 |  |                          FormatInfo format, NodeOperation operation) | 
  | 98 |  |                          throws AssemblyException { | 
  | 99 | 52 |                  BaseDTOAssemblyNode<FormatInfo, CluInfo> result = new BaseDTOAssemblyNode<FormatInfo, CluInfo>( | 
  | 100 |  |                                  this); | 
  | 101 | 52 |                  if (format == null) { | 
  | 102 |  |                           | 
  | 103 |  |                           | 
  | 104 | 0 |                          throw new AssemblyException("Format can not be null"); | 
  | 105 |  |                  } | 
  | 106 | 52 |                  if (NodeOperation.CREATE != operation && null == format.getId()) { | 
  | 107 | 0 |                          throw new AssemblyException("Course Format Shell's id can not be null"); | 
  | 108 |  |                  } | 
  | 109 |  |   | 
  | 110 |  |                  CluInfo clu; | 
  | 111 |  |          try { | 
  | 112 | 52 |              clu = (NodeOperation.UPDATE == operation) ? clu = luService.getClu(format.getId()) : new CluInfo(); | 
  | 113 | 0 |          } catch (Exception e) { | 
  | 114 | 0 |              throw new AssemblyException("Error retrieving course format shell during update", e); | 
  | 115 | 52 |          }  | 
  | 116 |  |   | 
  | 117 |  |                   | 
  | 118 | 52 |                  clu.setId(UUIDHelper.genStringUUID(format.getId())); | 
  | 119 |  |                                                                                                                           | 
  | 120 |  |                                                                                                                           | 
  | 121 |  |                                                                                                                           | 
  | 122 |  |                                                                                                                           | 
  | 123 | 52 |                  clu.setType(CourseAssemblerConstants.COURSE_FORMAT_TYPE); | 
  | 124 | 52 |                  clu.setState(format.getState()); | 
  | 125 | 52 |                  clu.setMetaInfo(format.getMetaInfo()); | 
  | 126 | 52 |                  clu.setAttributes(format.getAttributes()); | 
  | 127 | 52 |                  clu.setStdDuration(format.getDuration()); | 
  | 128 | 52 |                  clu.setOfferedAtpTypes(format.getTermsOffered()); | 
  | 129 |  |                   | 
  | 130 |  |                   | 
  | 131 | 52 |                  result.setNodeData(clu); | 
  | 132 | 52 |                  result.setOperation(operation); | 
  | 133 | 52 |                  result.setBusinessDTORef(format); | 
  | 134 |  |   | 
  | 135 |  |                   | 
  | 136 |  |                   | 
  | 137 |  |                  List<BaseDTOAssemblyNode<?, ?>> activityResults; | 
  | 138 |  |          try { | 
  | 139 | 52 |              activityResults = disassembleActivities(clu.getId(), | 
  | 140 |  |                              format, operation); | 
  | 141 | 52 |              result.getChildNodes().addAll(activityResults); | 
  | 142 |  |               | 
  | 143 | 0 |          } catch (Exception e) { | 
  | 144 | 0 |              throw new AssemblyException("Error while disassembling format", e); | 
  | 145 | 52 |          } | 
  | 146 |  |           | 
  | 147 | 52 |                  return result; | 
  | 148 |  |          } | 
  | 149 |  |   | 
  | 150 |  |           | 
  | 151 |  |   | 
  | 152 |  |   | 
  | 153 |  |   | 
  | 154 |  |   | 
  | 155 |  |   | 
  | 156 |  |   | 
  | 157 |  |   | 
  | 158 |  |   | 
  | 159 |  |   | 
  | 160 |  |   | 
  | 161 |  |   | 
  | 162 |  |   | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |   | 
  | 167 |  |   | 
  | 168 |  |   | 
  | 169 |  |   | 
  | 170 |  |   | 
  | 171 |  |   | 
  | 172 |  |          private List<BaseDTOAssemblyNode<?, ?>> disassembleActivities(String nodeId, | 
  | 173 |  |                          FormatInfo format, NodeOperation operation) | 
  | 174 |  |                          throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException { | 
  | 175 | 52 |                  List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>(); | 
  | 176 |  |   | 
  | 177 |  |                   | 
  | 178 |  |                   | 
  | 179 | 52 |                  Map<String, String> currentActivityIds = new HashMap<String, String>(); | 
  | 180 |  |   | 
  | 181 | 52 |                  if (!NodeOperation.CREATE.equals(operation)) { | 
  | 182 |  |                          try { | 
  | 183 | 7 |                                  List<CluCluRelationInfo> activityRelationships = luService | 
  | 184 |  |                                                  .getCluCluRelationsByClu(format.getId()); | 
  | 185 |  |                                   | 
  | 186 | 7 |                                  for (CluCluRelationInfo activityRelation : activityRelationships) { | 
  | 187 | 18 |                                          if (CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE | 
  | 188 |  |                                                          .equals(activityRelation.getType())) { | 
  | 189 | 11 |                                                  currentActivityIds.put(activityRelation | 
  | 190 |  |                                                                  .getRelatedCluId(), activityRelation.getId()); | 
  | 191 |  |                                          } | 
  | 192 |  |                                  } | 
  | 193 | 0 |                          } catch (DoesNotExistException e) { | 
  | 194 | 0 |                          } catch (Exception e) { | 
  | 195 | 0 |                                  throw new AssemblyException("Error getting related activities",        e); | 
  | 196 | 7 |                          } | 
  | 197 |  |                  } | 
  | 198 |  |   | 
  | 199 |  |                   | 
  | 200 | 52 |                  for (ActivityInfo activity : format.getActivities()) { | 
  | 201 |  |   | 
  | 202 |  |                           | 
  | 203 | 97 |                      if (NodeOperation.CREATE == operation | 
  | 204 |  |                              || (NodeOperation.UPDATE == operation &&  !currentActivityIds.containsKey(activity.getId()))) { | 
  | 205 |  |                           | 
  | 206 |  |                   | 
  | 207 |  |                   | 
  | 208 | 87 |                  BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler | 
  | 209 |  |                          .disassemble(activity, NodeOperation.CREATE); | 
  | 210 | 87 |                  activityNode.getNodeData().setState(format.getState()); | 
  | 211 | 87 |                  results.add(activityNode); | 
  | 212 |  |   | 
  | 213 |  |                   | 
  | 214 | 87 |                  CluCluRelationInfo relation = new CluCluRelationInfo(); | 
  | 215 | 87 |                  relation.setCluId(nodeId); | 
  | 216 | 87 |                  relation.setRelatedCluId(activityNode.getNodeData().getId()); | 
  | 217 |  |                                                               | 
  | 218 |  |                                                               | 
  | 219 |  |                                                               | 
  | 220 | 87 |                  relation | 
  | 221 |  |                          .setType(CourseAssemblerConstants.COURSE_ACTIVITY_RELATION_TYPE); | 
  | 222 | 87 |                  relation.setState(format.getState()); | 
  | 223 |  |   | 
  | 224 | 87 |                  BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo> relationNode = new BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo>( | 
  | 225 |  |                          null); | 
  | 226 | 87 |                  relationNode.setNodeData(relation); | 
  | 227 | 87 |                  relationNode.setOperation(NodeOperation.CREATE); | 
  | 228 |  |   | 
  | 229 | 87 |                  results.add(relationNode); | 
  | 230 | 87 |              } else if (NodeOperation.UPDATE == operation | 
  | 231 |  |                                          && currentActivityIds.containsKey(activity.getId())) { | 
  | 232 |  |                                   | 
  | 233 |  |                                   | 
  | 234 | 4 |                                  BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler | 
  | 235 |  |                                                  .disassemble(activity, NodeOperation.UPDATE); | 
  | 236 | 4 |                                  activityNode.getNodeData().setState(format.getState()); | 
  | 237 | 4 |                                  results.add(activityNode); | 
  | 238 |  |   | 
  | 239 |  |                                   | 
  | 240 |  |                                   | 
  | 241 | 4 |                                  currentActivityIds.remove(activity.getId()); | 
  | 242 | 4 |                          } else if (NodeOperation.DELETE == operation | 
  | 243 |  |                      && currentActivityIds.containsKey(activity.getId())) { | 
  | 244 |  |                               | 
  | 245 |  |                   | 
  | 246 | 6 |                  CluCluRelationInfo relationToDelete = new CluCluRelationInfo(); | 
  | 247 | 6 |                  relationToDelete.setId( currentActivityIds.get(activity.getId()) ); | 
  | 248 | 6 |                  BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo>( | 
  | 249 |  |                          null); | 
  | 250 | 6 |                  relationToDeleteNode.setNodeData(relationToDelete); | 
  | 251 | 6 |                  relationToDeleteNode.setOperation(NodeOperation.DELETE); | 
  | 252 | 6 |                  results.add(relationToDeleteNode); | 
  | 253 |  |               | 
  | 254 | 6 |                  BaseDTOAssemblyNode<ActivityInfo, CluInfo> formatNode = activityAssembler | 
  | 255 |  |                  .disassemble(activity, NodeOperation.DELETE); | 
  | 256 | 6 |                  results.add(formatNode);                                 | 
  | 257 |  |   | 
  | 258 |  |                   | 
  | 259 |  |                   | 
  | 260 | 6 |                  currentActivityIds.remove(activity.getId());                             | 
  | 261 | 97 |                          } | 
  | 262 |  |                  }          | 
  | 263 |  |   | 
  | 264 |  |           | 
  | 265 |  |           | 
  | 266 | 52 |          for (Entry<String, String> entry : currentActivityIds.entrySet()) { | 
  | 267 |  |               | 
  | 268 |  |               | 
  | 269 | 1 |              CluCluRelationInfo relationToDelete = new CluCluRelationInfo(); | 
  | 270 | 1 |              relationToDelete.setId(entry.getValue()); | 
  | 271 | 1 |              BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<FormatInfo, CluCluRelationInfo>( | 
  | 272 |  |                      null); | 
  | 273 | 1 |              relationToDeleteNode.setNodeData(relationToDelete); | 
  | 274 | 1 |              relationToDeleteNode.setOperation(NodeOperation.DELETE); | 
  | 275 | 1 |              results.add(relationToDeleteNode); | 
  | 276 |  |   | 
  | 277 | 1 |              CluInfo activityCluToDelete = luService.getClu(entry.getKey()); | 
  | 278 | 1 |              ActivityInfo activityToDelete = activityAssembler.assemble(activityCluToDelete, null, false); | 
  | 279 | 1 |              BaseDTOAssemblyNode<ActivityInfo, CluInfo> activityNode = activityAssembler | 
  | 280 |  |              .disassemble(activityToDelete, NodeOperation.DELETE); | 
  | 281 | 1 |              results.add(activityNode);                                             | 
  | 282 | 1 |          } | 
  | 283 |  |          | 
  | 284 |  |                   | 
  | 285 | 52 |                  return results; | 
  | 286 |  |          } | 
  | 287 |  |   | 
  | 288 |  |          public BOAssembler<ActivityInfo, CluInfo> getActivityAssembler() { | 
  | 289 | 0 |                  return activityAssembler; | 
  | 290 |  |          } | 
  | 291 |  |   | 
  | 292 |  |          public void setActivityAssembler( | 
  | 293 |  |                          BOAssembler<ActivityInfo, CluInfo> activityAssembler) { | 
  | 294 | 1 |                  this.activityAssembler = activityAssembler; | 
  | 295 | 1 |          } | 
  | 296 |  |   | 
  | 297 |  |          public LuService getLuService() { | 
  | 298 | 0 |                  return luService; | 
  | 299 |  |          } | 
  | 300 |  |   | 
  | 301 |  |          public void setLuService(LuService luService) { | 
  | 302 | 1 |                  this.luService = luService; | 
  | 303 | 1 |          } | 
  | 304 |  |  } |