Coverage Report - org.kuali.student.lum.course.service.assembler.CourseAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseAssembler
0%
0/659
0%
0/266
11.167
 
 1  
 /*
 2  
  * Copyright 2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.student.lum.course.service.assembler;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Date;
 21  
 import java.util.HashMap;
 22  
 import java.util.HashSet;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import java.util.Map.Entry;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.apache.log4j.Logger;
 30  
 import org.kuali.student.common.assembly.BOAssembler;
 31  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode;
 32  
 import org.kuali.student.common.assembly.BaseDTOAssemblyNode.NodeOperation;
 33  
 import org.kuali.student.common.assembly.data.AssemblyException;
 34  
 import org.kuali.student.common.dto.RichTextInfo;
 35  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 36  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 37  
 import org.kuali.student.common.exceptions.MissingParameterException;
 38  
 import org.kuali.student.common.exceptions.OperationFailedException;
 39  
 import org.kuali.student.common.util.UUIDHelper;
 40  
 import org.kuali.student.lum.course.dto.CourseCrossListingInfo;
 41  
 import org.kuali.student.lum.course.dto.CourseExpenditureInfo;
 42  
 import org.kuali.student.lum.course.dto.CourseFeeInfo;
 43  
 import org.kuali.student.lum.course.dto.CourseInfo;
 44  
 import org.kuali.student.lum.course.dto.CourseJointInfo;
 45  
 import org.kuali.student.lum.course.dto.CourseRevenueInfo;
 46  
 import org.kuali.student.lum.course.dto.CourseVariationInfo;
 47  
 import org.kuali.student.lum.course.dto.FormatInfo;
 48  
 import org.kuali.student.lum.course.dto.LoDisplayInfo;
 49  
 import org.kuali.student.lum.lo.dto.LoInfo;
 50  
 import org.kuali.student.lum.lo.service.LearningObjectiveService;
 51  
 import org.kuali.student.lum.lrc.dto.ResultComponentInfo;
 52  
 import org.kuali.student.lum.lrc.service.LrcService;
 53  
 import org.kuali.student.lum.lu.dto.AdminOrgInfo;
 54  
 import org.kuali.student.lum.lu.dto.CluAccountingInfo;
 55  
 import org.kuali.student.lum.lu.dto.CluCluRelationInfo;
 56  
 import org.kuali.student.lum.lu.dto.CluFeeInfo;
 57  
 import org.kuali.student.lum.lu.dto.CluFeeRecordInfo;
 58  
 import org.kuali.student.lum.lu.dto.CluIdentifierInfo;
 59  
 import org.kuali.student.lum.lu.dto.CluInfo;
 60  
 import org.kuali.student.lum.lu.dto.CluLoRelationInfo;
 61  
 import org.kuali.student.lum.lu.dto.CluResultInfo;
 62  
 import org.kuali.student.lum.lu.dto.LuCodeInfo;
 63  
 import org.kuali.student.lum.lu.dto.ResultOptionInfo;
 64  
 import org.kuali.student.lum.lu.service.LuService;
 65  
 import org.kuali.student.lum.service.assembler.CluAssemblerUtils;
 66  
 import org.springframework.util.StringUtils;
 67  
 /**
 68  
  * Assembler for CourseInfo. Provides assemble and disassemble operation on
 69  
  * CourseInfo from/to CluInfo and other base DTOs
 70  
  * 
 71  
  * @author Kuali Student Team
 72  
  * 
 73  
  */
 74  0
 public class CourseAssembler implements BOAssembler<CourseInfo, CluInfo> {
 75  
 
 76  0
     final static Logger LOG = Logger.getLogger(CourseAssembler.class);
 77  
         private LuService luService;
 78  
         private FormatAssembler formatAssembler;
 79  
         private CourseJointAssembler courseJointAssembler;
 80  
         private LoAssembler loAssembler;
 81  
         private LearningObjectiveService loService;
 82  
     private CluAssemblerUtils cluAssemblerUtils;
 83  
     private LrcService lrcService;
 84  
         
 85  
         @Override
 86  
         public CourseInfo assemble(CluInfo clu, CourseInfo courseInfo,
 87  
                         boolean shallowBuild) throws AssemblyException {
 88  
 
 89  0
                 CourseInfo course = (null != courseInfo) ? courseInfo
 90  
                                 : new CourseInfo();
 91  
 
 92  
                 // Copy all the data from the clu to the course
 93  
                 
 94  0
                 course.setAttributes(clu.getAttributes());
 95  0
                 course.setCampusLocations(clu.getCampusLocations());
 96  0
                 course.setCode(clu.getOfficialIdentifier().getCode());
 97  0
                 course.setCourseNumberSuffix(clu.getOfficialIdentifier()
 98  
                                 .getSuffixCode());
 99  0
                 course.setLevel(clu.getOfficialIdentifier().getLevel());
 100  0
                 course.setOutOfClassHours(clu.getIntensity());
 101  0
                 course.setInstructors(clu.getInstructors());
 102  0
                 course.setStartTerm(clu.getExpectedFirstAtp());
 103  0
                 course.setEndTerm(clu.getLastAtp());
 104  0
                 course.setCourseTitle(clu.getOfficialIdentifier().getLongName());
 105  
 
 106  
                 // CrossListings
 107  0
                 List<CourseCrossListingInfo> crossListings = assembleCrossListings(clu.getAlternateIdentifiers()); 
 108  0
                 course.setCrossListings(crossListings);
 109  
                 
 110  
                 //Variation
 111  0
                 List<CourseVariationInfo> variations = assembleVariations(clu.getAlternateIdentifiers()); 
 112  0
                 course.setVariations(variations);
 113  
                 
 114  
 //                course.setDepartment(clu.getPrimaryAdminOrg().getOrgId());
 115  0
                 if(course.getUnitsDeployment()==null){
 116  0
                         course.setUnitsDeployment(new ArrayList<String>());
 117  
                 }
 118  0
                 if(course.getUnitsContentOwner()==null){
 119  0
                         course.setUnitsContentOwner(new ArrayList<String>());
 120  
                 }
 121  0
                 List<String> courseAdminOrgs = new ArrayList<String>();
 122  0
                 List<String> courseSubjectOrgs = new ArrayList<String>();
 123  0
                 for(AdminOrgInfo adminOrg: clu.getAdminOrgs()){
 124  0
                         if(adminOrg.getType().equals(CourseAssemblerConstants.ADMIN_ORG)){
 125  0
                                 courseAdminOrgs.add(adminOrg.getOrgId());
 126  
                         }
 127  0
                         if(adminOrg.getType().equals(CourseAssemblerConstants.SUBJECT_ORG)){
 128  0
                                 courseSubjectOrgs.add(adminOrg.getOrgId());
 129  
                         }
 130  
                 }
 131  0
                 course.setUnitsDeployment(courseAdminOrgs);
 132  0
                 course.setUnitsContentOwner(courseSubjectOrgs);
 133  0
                 course.setDescr(clu.getDescr());
 134  0
                 course.setDuration(clu.getStdDuration());
 135  0
                 course.setEffectiveDate(clu.getEffectiveDate());
 136  0
                 course.setExpirationDate(clu.getExpirationDate());
 137  
 
 138  
                 //Fees
 139  
                 //Fee justification
 140  0
                 List<CourseFeeInfo> fees = new ArrayList<CourseFeeInfo>();
 141  0
                 List<CourseRevenueInfo> revenues = new ArrayList<CourseRevenueInfo>();
 142  0
                 if(clu.getFeeInfo() != null){
 143  0
                         course.setFeeJustification(clu.getFeeInfo().getDescr());
 144  
 
 145  
                         //Fees and revenues come from the same place but revenues have a special feeType
 146  0
                         for(CluFeeRecordInfo cluFeeRecord: clu.getFeeInfo().getCluFeeRecords()){
 147  0
                                 String feeType = cluFeeRecord.getFeeType();
 148  0
                                 if(CourseAssemblerConstants.COURSE_FINANCIALS_REVENUE_TYPE.equals(feeType)){
 149  0
                                         CourseRevenueInfo courseRevenue = new CourseRevenueInfo();
 150  0
                                         courseRevenue.setFeeType(feeType);
 151  0
                                         courseRevenue.setAffiliatedOrgs(cluFeeRecord.getAffiliatedOrgs());
 152  0
                                         courseRevenue.setAttributes(cluFeeRecord.getAttributes());
 153  0
                                         courseRevenue.setId(cluFeeRecord.getId());
 154  0
                                         courseRevenue.setMetaInfo(cluFeeRecord.getMetaInfo());
 155  0
                                         revenues.add(courseRevenue);
 156  0
                                 }else{
 157  0
                                         CourseFeeInfo courseFee = new CourseFeeInfo();
 158  0
                                         courseFee.setFeeType(feeType);
 159  0
                                         courseFee.setRateType(cluFeeRecord.getRateType());
 160  0
                                         courseFee.setDescr(cluFeeRecord.getDescr());
 161  0
                                         courseFee.setMetaInfo(cluFeeRecord.getMetaInfo());
 162  0
                                         courseFee.setId(cluFeeRecord.getId());
 163  0
                                         courseFee.setFeeAmounts(cluFeeRecord.getFeeAmounts());
 164  0
                                         courseFee.setAttributes(cluFeeRecord.getAttributes());
 165  0
                                         fees.add(courseFee);
 166  
                                 }
 167  0
                         }
 168  
                 }
 169  0
                 course.setFees(fees);
 170  0
                 course.setRevenues(revenues);
 171  
                 //Expenditures are mapped from accounting info
 172  0
                 if(course.getExpenditure() == null || clu.getAccountingInfo() == null){
 173  0
                         course.setExpenditure(new CourseExpenditureInfo());
 174  
                 }
 175  0
                 if(clu.getAccountingInfo() != null){
 176  0
                         course.getExpenditure().setAffiliatedOrgs(clu.getAccountingInfo().getAffiliatedOrgs());
 177  
                 }
 178  
                 
 179  0
                 course.setId(clu.getId());
 180  0
                 course.setType(clu.getType());
 181  0
                 course.setTermsOffered(clu.getOfferedAtpTypes());
 182  0
                 course.setPrimaryInstructor(clu.getPrimaryInstructor());
 183  0
                 course.setInstructors(clu.getInstructors());
 184  0
                 course.setState(clu.getState());
 185  0
                 course.setSubjectArea(clu.getOfficialIdentifier().getDivision());
 186  0
                 course.setTranscriptTitle(clu.getOfficialIdentifier().getShortName());
 187  0
                 course.setMetaInfo(clu.getMetaInfo());
 188  0
                 course.setVersionInfo(clu.getVersionInfo());
 189  
 
 190  
                 
 191  
                 //Special topics code
 192  0
                 course.setSpecialTopicsCourse(false);
 193  0
                 for(LuCodeInfo luCode : clu.getLuCodes()){
 194  0
                         if(CourseAssemblerConstants.COURSE_CODE_SPECIAL_TOPICS.equals(luCode.getType())){
 195  0
                                 course.setSpecialTopicsCourse(Boolean.parseBoolean(luCode.getValue()));
 196  0
                                 break;
 197  
                         }
 198  
                 }
 199  
                 //Pilot Course code
 200  0
                 course.setPilotCourse(false);
 201  0
                 for(LuCodeInfo luCode : clu.getLuCodes()){
 202  0
                         if(CourseAssemblerConstants.COURSE_CODE_PILOT_COURSE.equals(luCode.getType())){
 203  0
                                 course.setPilotCourse(Boolean.parseBoolean(luCode.getValue()));
 204  0
                                 break;
 205  
                         }
 206  
                 }
 207  
                 
 208  
                 // Don't make any changes to nested datastructures if this is
 209  0
                 if (!shallowBuild) {
 210  
                         try {
 211  
                                 // Use the luService to find Joints, then convert and add to the
 212  
                                 // course
 213  0
                                 List<CluCluRelationInfo> cluClus = luService
 214  
                                                 .getCluCluRelationsByClu(clu.getId());
 215  
                                 
 216  0
                                 for (CluCluRelationInfo cluRel : cluClus) {
 217  0
                                         if (cluRel.getType().equals(
 218  
                                                         CourseAssemblerConstants.JOINT_RELATION_TYPE)) {
 219  0
                                                 CourseJointInfo jointInfo = courseJointAssembler
 220  
                                                                 .assemble(cluRel, null, false);
 221  0
                                                 course.getJoints().add(jointInfo);
 222  0
                                         }
 223  
                                 }
 224  0
                         } catch (DoesNotExistException e) {
 225  0
                         } catch (Exception e) {
 226  0
                                 throw new AssemblyException("Error getting course joints", e);
 227  0
                         }
 228  
 
 229  
                         try {
 230  
                                 // Use the luService to find formats, then convert and add to
 231  
                                 // the course
 232  0
                                 List<CluInfo> formats = luService.getRelatedClusByCluId(course
 233  
                                                 .getId(),
 234  
                                                 CourseAssemblerConstants.COURSE_FORMAT_RELATION_TYPE);
 235  
                                 
 236  0
                                 for (CluInfo format : formats) {
 237  0
                                         FormatInfo formatInfo = formatAssembler.assemble(format,
 238  
                                                         null, false);
 239  0
                                         course.getFormats().add(formatInfo);
 240  0
                                 }
 241  
 
 242  0
                         } catch (DoesNotExistException e) {
 243  0
                         } catch (Exception e) {
 244  0
                                 throw new AssemblyException("Error getting related formats", e);
 245  0
                         }
 246  
 
 247  
                         try{
 248  
                                 //Set Credit and Grading options
 249  0
                                 List<CluResultInfo> cluResults = luService.getCluResultByClu(course.getId());
 250  
 
 251  0
                                 List<ResultComponentInfo> creditOptions = assembleCreditOptions(cluResults);
 252  0
                                 course.setCreditOptions(creditOptions);
 253  
                                 
 254  0
                                 List<String> gradingOptions = assembleGradingOptions(cluResults);
 255  
                                 
 256  0
                                 course.setGradingOptions(gradingOptions);
 257  0
                         } catch (DoesNotExistException e){
 258  0
                         } catch (Exception e) {
 259  0
                                 throw new AssemblyException("Error getting course results", e);
 260  0
                         }
 261  
                         
 262  
                         //Learning Objectives
 263  0
             course.getCourseSpecificLOs().addAll(cluAssemblerUtils.assembleLos(course.getId(), shallowBuild));
 264  
                         
 265  
                 }
 266  
 
 267  
                 //Remove special cases for grading options
 268  0
                 course.getGradingOptions().remove(CourseAssemblerConstants.COURSE_RESULT_COMP_GRADE_AUDIT);
 269  
                 
 270  0
                 return course;
 271  
         }
 272  
 
 273  
         @Override
 274  
         public BaseDTOAssemblyNode<CourseInfo, CluInfo> disassemble(
 275  
                         CourseInfo course, NodeOperation operation)
 276  
                         throws AssemblyException {
 277  
 
 278  0
                 if (course == null) {
 279  
                         // FIXME Unsure now if this is an exception or just return null or
 280  
                         // empty assemblyNode
 281  0
                     LOG.error("Course to disassemble is null!");
 282  0
                         throw new AssemblyException("Course can not be null");
 283  
                 }
 284  
 
 285  0
                 BaseDTOAssemblyNode<CourseInfo, CluInfo> result = new BaseDTOAssemblyNode<CourseInfo, CluInfo>(
 286  
                                 this);
 287  
 
 288  
                 CluInfo clu;
 289  
                 try {
 290  0
                         clu = (NodeOperation.UPDATE == operation) ? luService.getClu(course.getId()) : new CluInfo();
 291  0
         } catch (Exception e) {
 292  0
                         throw new AssemblyException("Error getting existing learning unit during course update", e);
 293  0
         } 
 294  
 
 295  
                 // Create the id if it's not there already(important for creating
 296  
                 // relations)
 297  0
                 clu.setId(UUIDHelper.genStringUUID(course.getId()));
 298  0
                 if (null == course.getId()) {
 299  0
                         course.setId(clu.getId());
 300  
                 }
 301  0
                 clu.setType(CourseAssemblerConstants.COURSE_TYPE);
 302  0
                 clu.setState(course.getState());
 303  
 
 304  0
                 CluIdentifierInfo identifier = new CluIdentifierInfo();
 305  0
                 identifier.setType(CourseAssemblerConstants.COURSE_OFFICIAL_IDENT_TYPE);
 306  0
                 identifier.setState(course.getState());
 307  0
                 identifier.setLongName(course.getCourseTitle());
 308  0
                 identifier.setShortName(course.getTranscriptTitle());
 309  0
                 identifier.setSuffixCode(course.getCourseNumberSuffix());
 310  0
                 identifier.setDivision(course.getSubjectArea());
 311  0
                 identifier.setCode(course.getCode());
 312  
                 
 313  
                 //Custom logic to set the level, if level not provided
 314  0
                 if(StringUtils.hasText(course.getLevel())) {
 315  0
                     identifier.setLevel(course.getLevel());
 316  0
                 } else if(course.getCourseNumberSuffix()!=null&&course.getCourseNumberSuffix().length()>=3){
 317  0
                         identifier.setLevel(course.getCourseNumberSuffix().substring(0, 1)+"00");
 318  
                 }
 319  
                 
 320  0
                 clu.setOfficialIdentifier(identifier);
 321  
 
 322  0
                 clu.setAdminOrgs(new ArrayList<AdminOrgInfo>());
 323  
 
 324  
                 // Use the Course Variation assembler to disassemble the variations
 325  
                 
 326  
                 // copy all fields
 327  
                 //Remove any existing variations or crosslistings
 328  0
                 for(Iterator<CluIdentifierInfo> iter = clu.getAlternateIdentifiers().iterator();iter.hasNext();){
 329  0
                         CluIdentifierInfo cluIdentifier = iter.next();
 330  0
                         if(CourseAssemblerConstants.COURSE_VARIATION_IDENT_TYPE.equals(cluIdentifier.getType()) ||
 331  
                                 CourseAssemblerConstants.COURSE_CROSSLISTING_IDENT_TYPE.equals(cluIdentifier.getType()) ){
 332  0
                                 iter.remove();
 333  
                         }
 334  0
                 }
 335  
                 //Add in variations
 336  0
                 for(CourseVariationInfo variation:course.getVariations()){
 337  0
                         CluIdentifierInfo cluIdentifier = new CluIdentifierInfo();
 338  0
                         cluIdentifier.setId(variation.getId());
 339  0
                         cluIdentifier.setType(CourseAssemblerConstants.COURSE_VARIATION_IDENT_TYPE);
 340  0
                         cluIdentifier.setCode(identifier.getCode());
 341  0
                         cluIdentifier.setSuffixCode(course.getCourseNumberSuffix());
 342  0
                         cluIdentifier.setDivision(course.getSubjectArea());
 343  0
                         cluIdentifier.setVariation(variation.getVariationCode());
 344  0
                         cluIdentifier.setLongName(variation.getVariationTitle());
 345  0
                         cluIdentifier.setState(course.getState());
 346  0
                         clu.getAlternateIdentifiers().add(cluIdentifier);
 347  0
                 }
 348  
                 //Add in crosslistings
 349  0
                 for(CourseCrossListingInfo crossListing:course.getCrossListings()){
 350  0
                         CluIdentifierInfo cluIdentifier = new CluIdentifierInfo();
 351  0
                         cluIdentifier.setId(crossListing.getId());
 352  0
                         cluIdentifier.setType(CourseAssemblerConstants.COURSE_CROSSLISTING_IDENT_TYPE);
 353  0
                         cluIdentifier.setSuffixCode(crossListing.getCourseNumberSuffix());
 354  0
                         cluIdentifier.setDivision(crossListing.getSubjectArea());
 355  0
                         cluIdentifier.setState(course.getState());
 356  0
                         cluIdentifier.setOrgId(crossListing.getDepartment());
 357  0
                         cluIdentifier.setAttributes(crossListing.getAttributes());
 358  0
             cluIdentifier.setCode(crossListing.getCode());                                        
 359  0
                         clu.getAlternateIdentifiers().add(cluIdentifier);
 360  0
                 }
 361  
 
 362  0
                 List<AdminOrgInfo> adminOrgInfos = new ArrayList<AdminOrgInfo>();
 363  0
                 for(String org:course.getUnitsDeployment()){
 364  0
                         AdminOrgInfo adminOrg = new AdminOrgInfo();
 365  0
                         adminOrg.setType(CourseAssemblerConstants.ADMIN_ORG);
 366  0
                         adminOrg.setOrgId(org);
 367  0
                         adminOrgInfos.add(adminOrg);
 368  0
                 }
 369  0
                 clu.getAdminOrgs().addAll(adminOrgInfos);
 370  
                 
 371  0
                 List<AdminOrgInfo> subjectOrgs = new ArrayList<AdminOrgInfo>();
 372  0
                 for (String subOrg : course.getUnitsContentOwner()) {
 373  0
                         AdminOrgInfo subjectOrg = new AdminOrgInfo();
 374  0
                         subjectOrg.setType(CourseAssemblerConstants.SUBJECT_ORG);
 375  0
                         subjectOrg.setOrgId(subOrg);
 376  0
                         subjectOrgs.add(subjectOrg);
 377  0
                 }
 378  0
                 clu.getAdminOrgs().addAll(subjectOrgs);
 379  
 
 380  
                 
 381  0
                 clu.setAttributes(course.getAttributes());
 382  0
                 clu.setCampusLocations(course.getCampusLocations());
 383  0
                 clu.setDescr(course.getDescr());
 384  0
                 clu.setStdDuration(course.getDuration());
 385  0
                 clu.setEffectiveDate(course.getEffectiveDate());
 386  0
                 clu.setExpirationDate(course.getExpirationDate());
 387  
 
 388  0
                 clu.setOfferedAtpTypes(course.getTermsOffered());
 389  0
                 clu.setPrimaryInstructor(course.getPrimaryInstructor());
 390  
                 
 391  0
                 clu.setIntensity(course.getOutOfClassHours());
 392  0
                 clu.setInstructors(course.getInstructors());
 393  
                 
 394  0
                 clu.setExpectedFirstAtp(course.getStartTerm());
 395  0
                 clu.setLastAtp(course.getEndTerm());
 396  
                 
 397  0
                 clu.setMetaInfo(course.getMetaInfo());
 398  0
                 clu.setVersionInfo(course.getVersionInfo());
 399  
 
 400  
                 // Add the Clu to the result
 401  0
                 result.setNodeData(clu);
 402  0
                 result.setOperation(operation);
 403  0
                 result.setBusinessDTORef(course);
 404  
 
 405  
                 // Use the Format assembler to disassemble the formats and relations
 406  
                 List<BaseDTOAssemblyNode<?, ?>> formatResults;
 407  
         try {
 408  0
             formatResults = disassembleFormats(clu
 409  
                             .getId(), course, operation);
 410  0
             result.getChildNodes().addAll(formatResults);
 411  
             
 412  0
         } catch (DoesNotExistException e) {
 413  0
         } catch (Exception e) {
 414  0
             throw new AssemblyException("Error while disassembling format", e);
 415  0
         }
 416  
 
 417  
                 // Use the CourseJoint assembler to disassemble the CourseJoints and
 418  
                 // relations
 419  0
                 List<BaseDTOAssemblyNode<?, ?>> courseJointResults = disassembleJoints(
 420  
                                 clu.getId(), course, operation);
 421  0
                 result.getChildNodes().addAll(courseJointResults);
 422  
 
 423  
                 //Disassemble the CluResults (grading and credit options)
 424  
                 //Special code to take audit from attributes and put into options
 425  0
                 if(course.getAttributes().containsKey(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_AUDIT)&&"true".equals(course.getAttributes().get(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_AUDIT))){
 426  0
                         if(!course.getGradingOptions().contains(CourseAssemblerConstants.COURSE_RESULT_COMP_GRADE_AUDIT)){
 427  0
                                 course.getGradingOptions().add(CourseAssemblerConstants.COURSE_RESULT_COMP_GRADE_AUDIT);
 428  
                         }
 429  
                 }
 430  
                 
 431  
                 List<CluResultInfo> cluResultList;
 432  
                 try {
 433  0
                         cluResultList = luService.getCluResultByClu(clu.getId());
 434  0
                 } catch (DoesNotExistException e) {
 435  0
                         cluResultList = Collections.emptyList();
 436  0
                 } catch (Exception e) {
 437  0
                         throw new AssemblyException("Error getting cluResults", e);
 438  0
                 }
 439  
                 
 440  0
                 List<BaseDTOAssemblyNode<?, ?>> creditOutcomes = disassembleCreditOutcomes(course, clu, cluResultList, operation);
 441  0
                 result.getChildNodes().addAll(creditOutcomes);
 442  
                 
 443  0
                 BaseDTOAssemblyNode<?, ?> gradingOptions = disassembleGradingOptions(
 444  
                                 clu.getId(), course.getState(), course.getGradingOptions(), cluResultList, operation);
 445  0
                 result.getChildNodes().add(gradingOptions);
 446  
                 
 447  
                 //Use the LoAssembler to disassemble Los
 448  
         try {
 449  
                     List<BaseDTOAssemblyNode<?, ?>> loResults;
 450  0
                     loResults = disassembleLos(clu.getId(), course, operation);
 451  0
             result.getChildNodes().addAll(loResults);
 452  0
         } catch (Exception e) {
 453  0
             throw new AssemblyException("Error while disassembling los", e);
 454  0
         }
 455  
                 
 456  
                 //add the special topics code if it did not exist, or remove if it was not wanted
 457  0
                 boolean alreadyHadSpecialTopicsCode = false;
 458  0
                 for(Iterator<LuCodeInfo> luCodeIterator = clu.getLuCodes().iterator();luCodeIterator.hasNext();){
 459  0
                         LuCodeInfo luCode = luCodeIterator.next();
 460  0
                         if(CourseAssemblerConstants.COURSE_CODE_SPECIAL_TOPICS.equals(luCode.getType())){
 461  0
                                 alreadyHadSpecialTopicsCode = true;
 462  0
                                 if(!course.isSpecialTopicsCourse()){
 463  0
                                         luCodeIterator.remove();
 464  
                                 }
 465  
                                 break;
 466  
                         }
 467  0
                 }
 468  0
                 if(!alreadyHadSpecialTopicsCode && course.isSpecialTopicsCourse()){
 469  0
                         LuCodeInfo luCode = new LuCodeInfo();
 470  0
                         luCode.setType(CourseAssemblerConstants.COURSE_CODE_SPECIAL_TOPICS);
 471  0
                         luCode.setValue("true");
 472  0
                         clu.getLuCodes().add(luCode);
 473  
                 }
 474  
                 
 475  
                 //add the special topics code if it did not exist, or remove if it was not wanted
 476  0
                 boolean alreadyHadPilotCourseCode = false;
 477  0
                 for(Iterator<LuCodeInfo> luCodeIterator = clu.getLuCodes().iterator();luCodeIterator.hasNext();){
 478  0
                         LuCodeInfo luCode = luCodeIterator.next();
 479  0
                         if(CourseAssemblerConstants.COURSE_CODE_PILOT_COURSE.equals(luCode.getType())){
 480  0
                                 alreadyHadPilotCourseCode = true;
 481  0
                                 if(!course.isPilotCourse()){
 482  0
                                         luCodeIterator.remove();
 483  
                                 }
 484  
                                 break;
 485  
                         }
 486  0
                 }
 487  0
                 if(!alreadyHadPilotCourseCode && course.isPilotCourse()){
 488  0
                         LuCodeInfo luCode = new LuCodeInfo();
 489  0
                         luCode.setType(CourseAssemblerConstants.COURSE_CODE_PILOT_COURSE);
 490  0
                         luCode.setValue("true");
 491  0
                         clu.getLuCodes().add(luCode);
 492  
                 }
 493  
                 
 494  
                 //FEES
 495  0
                 if(clu.getFeeInfo() == null){
 496  0
                         clu.setFeeInfo(new CluFeeInfo());
 497  
                 }
 498  0
                 clu.getFeeInfo().setDescr(course.getFeeJustification());
 499  0
                 clu.getFeeInfo().getCluFeeRecords().clear();
 500  0
                 for(CourseRevenueInfo courseRevenue:course.getRevenues()){
 501  0
                         CluFeeRecordInfo cluFeeRecord  = new CluFeeRecordInfo();
 502  0
                         cluFeeRecord.setFeeType(CourseAssemblerConstants.COURSE_FINANCIALS_REVENUE_TYPE);
 503  0
                         cluFeeRecord.setRateType(CourseAssemblerConstants.COURSE_FINANCIALS_REVENUE_TYPE);
 504  0
                         cluFeeRecord.setAttributes(courseRevenue.getAttributes());
 505  0
                         cluFeeRecord.setAffiliatedOrgs(courseRevenue.getAffiliatedOrgs());
 506  0
                         cluFeeRecord.setId(courseRevenue.getId());
 507  0
                         cluFeeRecord.setMetaInfo(courseRevenue.getMetaInfo());
 508  0
                         clu.getFeeInfo().getCluFeeRecords().add(cluFeeRecord);
 509  0
                 }
 510  0
                 for(CourseFeeInfo courseFee : course.getFees()){
 511  0
                         CluFeeRecordInfo cluFeeRecord  = new CluFeeRecordInfo();
 512  0
                         cluFeeRecord.setFeeType(courseFee.getFeeType());
 513  0
                         cluFeeRecord.setRateType(courseFee.getRateType());
 514  0
                         cluFeeRecord.setDescr(courseFee.getDescr());
 515  0
                         cluFeeRecord.setMetaInfo(courseFee.getMetaInfo());
 516  0
                         cluFeeRecord.setId(courseFee.getId());
 517  0
                         cluFeeRecord.setFeeAmounts(courseFee.getFeeAmounts());
 518  0
                         cluFeeRecord.setAttributes(courseFee.getAttributes());
 519  0
                         clu.getFeeInfo().getCluFeeRecords().add(cluFeeRecord);
 520  0
                 }
 521  0
                 if(clu.getAccountingInfo() == null || course.getExpenditure()== null){
 522  0
                         clu.setAccountingInfo( new CluAccountingInfo());
 523  
                 }
 524  0
                 if(course.getExpenditure() != null){
 525  0
                         clu.getAccountingInfo().setAffiliatedOrgs(course.getExpenditure().getAffiliatedOrgs());
 526  0
                         clu.getAccountingInfo().setAttributes(course.getExpenditure().getAttributes());
 527  
                 }
 528  
                 
 529  0
                 return result;
 530  
         }
 531  
 
 532  
         private List<BaseDTOAssemblyNode<?, ?>> disassembleCreditOutcomes(CourseInfo course, CluInfo clu, List<CluResultInfo> currentCluResults, NodeOperation operation) throws AssemblyException, NumberFormatException {
 533  
                 
 534  0
                 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 535  
                 
 536  0
                 String courseResultType = CourseAssemblerConstants.COURSE_RESULT_TYPE_CREDITS;
 537  
                 
 538  
                 //See if we need to create any new lrcs
 539  0
                 if(NodeOperation.DELETE!=operation){
 540  
                         //Find all the existing LRCs for the following three types
 541  0
                         Set<String> rsltComps = new HashSet<String>();
 542  
                         
 543  
                         try{
 544  
                                 try {
 545  0
                                         rsltComps.addAll(lrcService.getResultComponentIdsByResultComponentType(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_FIXED));
 546  0
                                 } catch (DoesNotExistException e) {}
 547  
                                 try {
 548  0
                                         rsltComps.addAll(lrcService.getResultComponentIdsByResultComponentType(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_MULTIPLE));
 549  0
                                 } catch (DoesNotExistException e) {}
 550  
                                 try {
 551  0
                                         rsltComps.addAll(lrcService.getResultComponentIdsByResultComponentType(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_VARIABLE));
 552  0
                                 } catch (DoesNotExistException e) {}
 553  
 
 554  
                                 //Create any LRCs that do not yet exist
 555  0
                                 for(ResultComponentInfo creditOption:course.getCreditOptions()){
 556  0
                                         String id = null;
 557  0
                                         String type = null;
 558  0
                                         List<String> resultValues = null;
 559  0
                                         Map<String,String> attributes = null;
 560  
                                         //Depending on the type, set the id, type and result values differently
 561  0
                                         if(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_FIXED.equals(creditOption.getType())){
 562  0
                                                 float fixedCreditValue = Float.parseFloat(creditOption.getAttributes().get(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_FIXED_CREDIT_VALUE));
 563  0
                                                 id = CourseAssemblerConstants.COURSE_RESULT_COMP_CREDIT_PREFIX + fixedCreditValue;
 564  0
                                                 type = CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_FIXED;
 565  0
                                                 resultValues = new ArrayList<String>();
 566  0
                                                 resultValues.add(String.valueOf(fixedCreditValue));
 567  0
                                                 attributes = new HashMap<String,String>();
 568  0
                                                 attributes.put(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_FIXED_CREDIT_VALUE, String.valueOf(fixedCreditValue));
 569  0
                                         }else if(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_MULTIPLE.equals(creditOption.getType())){
 570  0
                                                 Collections.sort(creditOption.getResultValues());
 571  0
                                                 StringBuilder sb = new StringBuilder(CourseAssemblerConstants.COURSE_RESULT_COMP_CREDIT_PREFIX);
 572  0
                                                 for(Iterator<String> iter = creditOption.getResultValues().iterator();iter.hasNext();){
 573  0
                                                         sb.append(iter.next());
 574  0
                                                         if(iter.hasNext()){
 575  0
                                                                 sb.append(",");
 576  
                                                         }
 577  
                                                 }
 578  0
                                                 id = sb.toString();
 579  0
                                                 type = CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_MULTIPLE;
 580  0
                                                 resultValues = creditOption.getResultValues();
 581  0
                                         }else if(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_VARIABLE.equals(creditOption.getType())){
 582  
                                             /*
 583  
                                              * For variable credits create a Result values that goes from min to max with the specified increment. 
 584  
                                              * If no increment is specified, use 1.0 as the increment. The increment can be specified as a float.
 585  
                                              */
 586  
                                                                                      
 587  0
                                             String minCreditValue = creditOption.getAttributes().get(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_MIN_CREDIT_VALUE);
 588  0
                                                 String maxCreditValue = creditOption.getAttributes().get(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_MAX_CREDIT_VALUE);
 589  0
                                                 String creditValueIncr = creditOption.getAttributes().get(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_CREDIT_VALUE_INCR);
 590  0
                                                 float minCredits = Float.parseFloat(minCreditValue);
 591  0
                                                 float maxCredits = Float.parseFloat(maxCreditValue);
 592  
                                                                                                 
 593  0
                                                 float increment = (null != creditValueIncr && creditValueIncr.length() > 0 ) ? Float.parseFloat(creditValueIncr) : 1.0f ;
 594  
                                                                                                 
 595  0
                                                 id = CourseAssemblerConstants.COURSE_RESULT_COMP_CREDIT_PREFIX + minCreditValue + "-" + maxCreditValue;
 596  0
                                                 type = CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_VARIABLE;
 597  0
                                                 resultValues = new ArrayList<String>();
 598  0
                                                 for(float i = minCredits; i <= maxCredits; i+=increment){
 599  0
                                                         resultValues.add(String.valueOf(i));
 600  
                                                 }
 601  0
                                                 attributes = new HashMap<String,String>();
 602  0
                                                 attributes.put(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_MIN_CREDIT_VALUE, minCreditValue);
 603  0
                                                 attributes.put(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_MAX_CREDIT_VALUE, maxCreditValue);
 604  0
                         attributes.put(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_CREDIT_VALUE_INCR, creditValueIncr);
 605  
                                         }
 606  
         
 607  
                                         //Set the id
 608  0
                                         creditOption.setId(id);
 609  
                                         
 610  
                                         //Create a new result component
 611  0
                                         if(id != null && !rsltComps.contains(id)){
 612  
                                                                                                 
 613  
                                                 //need to make a fixed degree result type component
 614  0
                                                 ResultComponentInfo resultComponent = new ResultComponentInfo();
 615  0
                                                 resultComponent.setId(id);
 616  0
                                                 resultComponent.setType(type);
 617  0
                                                 resultComponent.setState (course.getState());
 618  0
                                                 resultComponent.setResultValues(resultValues);
 619  0
                                                 resultComponent.setAttributes(attributes);
 620  0
                                                 BaseDTOAssemblyNode<ResultComponentInfo, ResultComponentInfo> node = new BaseDTOAssemblyNode<ResultComponentInfo, ResultComponentInfo>(null);
 621  0
                                                 node.setOperation(NodeOperation.CREATE);
 622  0
                                                 node.setNodeData(resultComponent);
 623  0
                                                 node.setBusinessDTORef(creditOption);
 624  0
                                                 results.add(node);
 625  
                                                 
 626  0
                                                 rsltComps.add(id);
 627  
                                         }
 628  0
                                 }
 629  0
                         }catch (NumberFormatException e){
 630  0
                                 throw new AssemblyException("Invalid Arguments for credit outcome values",e);
 631  0
                         }catch (Exception e){
 632  0
                                 throw new AssemblyException("Error Assembling", e);
 633  0
                         }
 634  
                 }
 635  
                 
 636  
                 //Now do dissassembly for the actual clu-lrc relations and result options
 637  
                 
 638  
                 // Get the current options and put them in a map of option type id/cluResult
 639  0
                 Map<String, List<CluResultInfo>> currentResults = new HashMap<String, List<CluResultInfo>>();
 640  
                 
 641  
                 //If this is not a create, lookup the results for this clu
 642  0
                 if (!NodeOperation.CREATE.equals(operation)) {
 643  0
                         for (CluResultInfo currentResult : currentCluResults) {
 644  0
                                 if (courseResultType.equals(currentResult.getType())) {
 645  
                                         //There should only be one grading option per CluResult for credit outcomes
 646  0
                                         if(currentResult.getResultOptions().size()==1){
 647  
                                                 //Create a mapping to a list of cluresults with the same result componentId
 648  0
                                                 String resultComponentId = currentResult.getResultOptions().get(0).getResultComponentId();
 649  0
                                                 if(!currentResults.containsKey(resultComponentId)){
 650  0
                                                         currentResults.put(resultComponentId, new ArrayList<CluResultInfo>());
 651  
                                                 }
 652  0
                                                 currentResults.get(resultComponentId).add(currentResult);
 653  0
                                         }else{
 654  0
                                                 LOG.warn("Credit Results should have exactly one result option each");
 655  
                                         }
 656  
                                 }
 657  
                         }
 658  
                 }
 659  
                 
 660  
                 //Loop through options on the course, if they are new, create a new cluResult
 661  0
                 for(ResultComponentInfo creditOption : course.getCreditOptions()){
 662  0
                     if (NodeOperation.CREATE == operation
 663  
                             || (NodeOperation.UPDATE == operation && !currentResults.containsKey(creditOption.getId()) )) {
 664  
                             
 665  0
                             ResultOptionInfo resultOption = new ResultOptionInfo();
 666  0
                             resultOption.setState(course.getState());
 667  0
                             resultOption.setResultComponentId(creditOption.getId());
 668  
                             
 669  0
                             CluResultInfo cluResult = new CluResultInfo();
 670  0
                                 cluResult.setCluId(clu.getId());
 671  0
                                 cluResult.setState(course.getState());
 672  0
                                 cluResult.setType(courseResultType);
 673  
                                 
 674  0
                                 cluResult.getResultOptions().add(resultOption);
 675  
                                 
 676  0
                                 BaseDTOAssemblyNode<ResultComponentInfo, CluResultInfo> cluResultNode = new BaseDTOAssemblyNode<ResultComponentInfo, CluResultInfo>(null);
 677  0
                                 cluResultNode.setNodeData(cluResult);
 678  0
                                 cluResultNode.setOperation(NodeOperation.CREATE);
 679  
                                 
 680  0
                 results.add(cluResultNode);
 681  0
             } else if (NodeOperation.UPDATE == operation
 682  
                                         && currentResults.containsKey(creditOption.getId())) {
 683  
                     //Get the list from the map and remove an entry, if the list is empty then remove it from the map
 684  0
                     List<CluResultInfo> cluResults = currentResults.get(creditOption.getId());
 685  0
                     cluResults.remove(cluResults.size()-1);
 686  0
                     if(cluResults.isEmpty()){
 687  0
                             currentResults.remove(creditOption.getId());
 688  
                     }
 689  0
                         }
 690  
                 }
 691  
                 
 692  
                 //Delete the leftovers
 693  0
                 for(Entry<String,List<CluResultInfo>> entry:currentResults.entrySet()){
 694  0
                         for(CluResultInfo cluResult:entry.getValue()){
 695  0
                                 BaseDTOAssemblyNode<ResultComponentInfo, CluResultInfo> cluResultNode = new BaseDTOAssemblyNode<ResultComponentInfo, CluResultInfo>(null);
 696  0
                                 cluResultNode.setNodeData(cluResult);
 697  0
                                 cluResultNode.setOperation(NodeOperation.DELETE);
 698  0
                                 results.add(cluResultNode);
 699  0
                         }
 700  
                 }
 701  
                 
 702  0
                 return results;
 703  
         }
 704  
 
 705  
         private List<String> assembleGradingOptions(List<CluResultInfo> cluResults){
 706  
                 
 707  0
                 String courseResultType = CourseAssemblerConstants.COURSE_RESULT_TYPE_GRADE;
 708  
                 
 709  0
                 List<String> results = new ArrayList<String>();
 710  
                 //Loop through all the CluResults to find the one with the matching type
 711  0
                 for(CluResultInfo cluResult:cluResults){
 712  0
                         if(courseResultType.equals(cluResult.getType())){
 713  
                                 //Loop through all options and add to the list of Strings
 714  0
                                 for(ResultOptionInfo resultOption: cluResult.getResultOptions()){
 715  0
                                         results.add(resultOption.getResultComponentId());
 716  
                                 }
 717  0
                                 break;
 718  
                         }
 719  
                 }
 720  0
                 return results;
 721  
         }
 722  
         
 723  
         private List<ResultComponentInfo> assembleCreditOptions(
 724  
                         List<CluResultInfo> cluResults) throws AssemblyException {
 725  0
                 String courseResultType = CourseAssemblerConstants.COURSE_RESULT_TYPE_CREDITS;
 726  0
                 List<ResultComponentInfo> results = new ArrayList<ResultComponentInfo>();
 727  
                 //Loop through all the CluResults to find the one with the matching type
 728  0
                 for(CluResultInfo cluResult:cluResults){
 729  0
                         if(courseResultType.equals(cluResult.getType())){
 730  
                                 //Loop through all options and add to the list of Strings
 731  0
                                 for(ResultOptionInfo resultOption: cluResult.getResultOptions()){
 732  
                                         try {
 733  0
                                                 ResultComponentInfo resultComponent = lrcService.getResultComponent(resultOption.getResultComponentId());
 734  0
                                                 results.add(resultComponent);
 735  0
                                         } catch (DoesNotExistException e) {
 736  0
                                                 LOG.warn("Course Credit option:"+resultOption.getId()+" refers to non-existant ResultComponentInfo "+resultOption.getResultComponentId());
 737  0
                                         } catch (Exception e) {
 738  0
                                                 throw new AssemblyException("Error getting result components",e);
 739  0
                                         }
 740  
                                 }
 741  
                         }
 742  
                 }
 743  0
                 return results;
 744  
         }
 745  
         
 746  
         // TODO Use CluAssemblerUtils
 747  
         private List<BaseDTOAssemblyNode<?, ?>> disassembleLos(String cluId,
 748  
                         CourseInfo course, NodeOperation operation) throws AssemblyException {
 749  
                 // TODO Auto-generated method stub
 750  0
                 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 751  
 
 752  
                 // Get the current formats and put them in a map of format id/relation
 753  
                 // id
 754  0
                 Map<String, CluLoRelationInfo> currentCluLoRelations = new HashMap<String, CluLoRelationInfo>();
 755  
                 try {
 756  0
                         List<CluLoRelationInfo> cluLoRelations = luService.getCluLoRelationsByClu(cluId);
 757  0
                         for(CluLoRelationInfo cluLoRelation:cluLoRelations){
 758  0
                                 if(CourseAssemblerConstants.COURSE_LO_COURSE_SPECIFIC_RELATION.equals(cluLoRelation.getType())){
 759  0
                                         currentCluLoRelations.put(cluLoRelation.getLoId(), cluLoRelation);
 760  
                                 }
 761  
                         }
 762  0
                 } catch (DoesNotExistException e) {
 763  0
                 } catch (Exception e) {
 764  0
                         throw new AssemblyException("Error finding related Los");
 765  0
                 }
 766  
                 
 767  
                 // Loop through all the los in this clu
 768  0
                 for(LoDisplayInfo loDisplay : course.getCourseSpecificLOs()){
 769  
                         
 770  
                         // If this is a clu create/new lo update then all los will be created
 771  0
                     if (NodeOperation.CREATE == operation
 772  
                             || (NodeOperation.UPDATE == operation &&  !currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId()))) {
 773  
                         
 774  
                 // the lo does not exist, so create
 775  
                 // Assemble and add the lo
 776  0
                             loDisplay.getLoInfo().setId(null);
 777  0
                             loDisplay.getLoInfo().setState(course.getState());
 778  0
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 779  
                         .disassemble(loDisplay, NodeOperation.CREATE);
 780  0
                 results.add(loNode);
 781  
 
 782  
                 // Create the relationship and add it as well
 783  0
                 CluLoRelationInfo relation = new CluLoRelationInfo();
 784  0
                 relation.setCluId(cluId);
 785  0
                 relation.setLoId(loNode.getNodeData().getId());
 786  0
                 relation
 787  
                         .setType(CourseAssemblerConstants.COURSE_LO_COURSE_SPECIFIC_RELATION);
 788  0
                 relation.setState(course.getState());
 789  
 
 790  0
                 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 791  
                         null);
 792  0
                 relationNode.setNodeData(relation);
 793  0
                 relationNode.setOperation(NodeOperation.CREATE);
 794  
 
 795  0
                 results.add(relationNode);
 796  0
             } else if (NodeOperation.UPDATE == operation
 797  
                                         && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
 798  0
                     loDisplay.getLoInfo().setState(course.getState());
 799  
                     // If the clu already has this lo, then just update the lo
 800  0
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 801  
                                 .disassemble(loDisplay, NodeOperation.UPDATE);
 802  0
                                 results.add(loNode);
 803  
 
 804  
                                 // remove this entry from the map so we can tell what needs to
 805  
                                 // be deleted at the end
 806  0
                                 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());
 807  0
                         } else if (NodeOperation.DELETE == operation
 808  
                     && currentCluLoRelations.containsKey(loDisplay.getLoInfo().getId())) {
 809  
                             
 810  
                 // Delete the Format and its relation
 811  0
                                 CluLoRelationInfo relationToDelete = currentCluLoRelations.get(loDisplay.getLoInfo().getId());
 812  0
                 BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 813  
                         null);
 814  0
                 relationToDeleteNode.setNodeData(relationToDelete);
 815  0
                 relationToDeleteNode.setOperation(NodeOperation.DELETE);
 816  0
                 results.add(relationToDeleteNode);
 817  
             
 818  0
                 BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 819  
                                         .disassemble(loDisplay, NodeOperation.DELETE);
 820  0
                 results.add(loNode);                                
 821  
 
 822  
                 // remove this entry from the map so we can tell what needs to
 823  
                 // be deleted at the end
 824  0
                 currentCluLoRelations.remove(loDisplay.getLoInfo().getId());                            
 825  0
                         }
 826  
                 }         
 827  
 
 828  
         // Now any leftover lo ids are no longer needed, so delete
 829  
         // los and relations
 830  0
         for (Entry<String, CluLoRelationInfo> entry : currentCluLoRelations.entrySet()) {
 831  
             // Create a new relation with the id of the relation we want to
 832  
             // delete
 833  0
                 CluLoRelationInfo relationToDelete = entry.getValue();
 834  0
             BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, CluLoRelationInfo>(
 835  
                     null);
 836  0
             relationToDeleteNode.setNodeData(relationToDelete);
 837  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 838  0
             results.add(relationToDeleteNode);
 839  
 
 840  
             try{
 841  0
                     LoInfo loToDelete = loService.getLo(entry.getKey());
 842  
             
 843  0
                     LoDisplayInfo loDisplayToDelete = loAssembler.assemble(loToDelete, null, false);
 844  0
                     BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = loAssembler
 845  
                                     .disassemble(loDisplayToDelete, NodeOperation.DELETE);
 846  0
                     results.add(loNode);
 847  0
                 } catch (DoesNotExistException e){
 848  0
                         LOG.warn("Trying to delete non exsistant LO:"+entry.getKey());
 849  0
             } catch (Exception e) {
 850  0
                                 throw new AssemblyException("Error disassembling LOs",e);
 851  0
                         }
 852  0
         }
 853  
                 
 854  0
                 return results;
 855  
         }
 856  
         
 857  
         private BaseDTOAssemblyNode<?, ?> disassembleGradingOptions(String cluId,
 858  
                         String courseState, List<String> options, List<CluResultInfo> currentCluResults, NodeOperation operation) throws AssemblyException {
 859  0
                 BaseDTOAssemblyNode<List<String>, CluResultInfo> cluResultNode = new BaseDTOAssemblyNode<List<String>, CluResultInfo>(null);
 860  
                                 
 861  0
                 String courseResultType=CourseAssemblerConstants.COURSE_RESULT_TYPE_GRADE; 
 862  0
                 String resultsDescription="Grading options";
 863  0
                 String resultDescription="Grading option";
 864  
 
 865  
                 // Get the current options and put them in a map of option type id/cluResult
 866  0
                 Map<String, ResultOptionInfo> currentResults = new HashMap<String, ResultOptionInfo>();
 867  
 
 868  0
                 CluResultInfo cluResult = null;
 869  
                 
 870  
                 //If this is not a create, lookup the results for this clu
 871  0
                 if (!NodeOperation.CREATE.equals(operation)) {
 872  0
                         for (CluResultInfo currentResult : currentCluResults) {
 873  0
                                 if (courseResultType.equals(currentResult.getType())) {
 874  0
                                         cluResult = currentResult;
 875  0
                                         if(NodeOperation.DELETE.equals(operation)){
 876  
                                                 //if this is a delete, then we only need the CluResultInfo
 877  0
                                                 cluResultNode.setOperation(NodeOperation.DELETE);
 878  
                                         }else{
 879  
                                                 //Find all the Result options and store in a map for easy access later
 880  0
                                                 cluResultNode.setOperation(NodeOperation.UPDATE);
 881  0
                                                 for(ResultOptionInfo resultOption:currentResult.getResultOptions()){
 882  0
                                                         currentResults.put(resultOption.getResultComponentId(), resultOption);
 883  
                                                 }
 884  
                                         }
 885  0
                                         break;
 886  
                                 }
 887  
                         }
 888  
                 }
 889  
 
 890  
                 //If this is a delete we don't need the result options, just the CluResultInfo
 891  0
                 if(!NodeOperation.DELETE.equals(operation)){
 892  0
                         if(cluResult == null){
 893  
                                 //Create a new resultInfo of the given type if one does not exist and set operation to Create
 894  0
                                 cluResult = new CluResultInfo();
 895  0
                                 cluResult.setCluId(cluId);
 896  0
                                 cluResult.setState(courseState);
 897  0
                                 cluResult.setType(courseResultType);
 898  0
                                 RichTextInfo desc = new RichTextInfo();
 899  0
                                 desc.setPlain(resultsDescription);
 900  0
                                 cluResult.setDesc(desc);
 901  0
                                 cluResult.setEffectiveDate(new Date());
 902  0
                                 cluResultNode.setOperation(NodeOperation.CREATE);
 903  
                         }
 904  
         
 905  0
                         cluResult.setResultOptions(new ArrayList<ResultOptionInfo>());
 906  
         
 907  
                         // Loop through all the credit options in this course
 908  0
                         for (String optionType : options) {
 909  0
                                 if(currentResults.containsKey(optionType)){
 910  
                                         //If the option exists already copy it to the new list of result options
 911  0
                                         ResultOptionInfo resultOptionInfo = currentResults.get(optionType);
 912  0
                                         cluResult.getResultOptions().add(resultOptionInfo);
 913  0
                                 }else{
 914  
                                         //Otherwise create a new result option
 915  0
                                         ResultOptionInfo resultOptionInfo = new ResultOptionInfo();
 916  0
                                         RichTextInfo desc = new RichTextInfo();
 917  0
                                         desc.setPlain(resultDescription);
 918  0
                                         resultOptionInfo.setDesc(desc);
 919  0
                                         resultOptionInfo.setResultComponentId(optionType);
 920  0
                                         resultOptionInfo.setState(courseState);
 921  
                                         
 922  0
                                         cluResult.getResultOptions().add(resultOptionInfo);
 923  0
                                 }
 924  
                         }
 925  
                 }
 926  
                 
 927  0
                 cluResultNode.setNodeData(cluResult);
 928  0
                 return cluResultNode;
 929  
         }
 930  
 
 931  
         // TODO This is pretty much a copy of the FormatAssembler's
 932  
         // disassembleActivities code... maybe can be made generic
 933  
         private List<BaseDTOAssemblyNode<?, ?>> disassembleFormats(String nodeId,
 934  
                         CourseInfo course, NodeOperation operation)
 935  
                         throws AssemblyException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 936  
 
 937  0
                 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 938  
 
 939  
                 // Get the current formats and put them in a map of format id/relation
 940  
                 // id
 941  0
                 Map<String, String> currentformatIds = new HashMap<String, String>();
 942  
 
 943  0
                 if (!NodeOperation.CREATE.equals(operation)) {
 944  
                         try {
 945  0
                                 List<CluCluRelationInfo> formatRelationships = luService
 946  
                                                 .getCluCluRelationsByClu(course.getId());
 947  
                                 
 948  
                                 //formatRelationships = (null == formatRelationships) ? new ArrayList<CluCluRelationInfo>() : formatRelationships;
 949  
                                 
 950  0
                                 for (CluCluRelationInfo formatRelation : formatRelationships) {
 951  0
                                         if (CourseAssemblerConstants.COURSE_FORMAT_RELATION_TYPE
 952  
                                                         .equals(formatRelation.getType())) {
 953  0
                                                 currentformatIds.put(formatRelation.getRelatedCluId(),
 954  
                                                                 formatRelation.getId());
 955  
                                         }
 956  
                                 }
 957  0
                         } catch (DoesNotExistException e) {
 958  0
                         } catch (InvalidParameterException e) {
 959  0
                                 throw new AssemblyException("Error getting related formats", e);
 960  0
                         } catch (MissingParameterException e) {
 961  0
                                 throw new AssemblyException("Error getting related formats", e);
 962  0
                         } catch (OperationFailedException e) {
 963  0
                                 throw new AssemblyException("Error getting related formats", e);
 964  0
                         }
 965  
                 }
 966  
 
 967  
                 // Loop through all the formats in this course
 968  0
                 for (FormatInfo format : course.getFormats()) {
 969  
 
 970  
                     //  If this is a course create/new format update then all formats will be created
 971  0
                     if (NodeOperation.CREATE == operation
 972  
                             || (NodeOperation.UPDATE == operation && !currentformatIds.containsKey(format.getId()) )) {
 973  
                 // the format does not exist, so create
 974  
                 // Assemble and add the format
 975  0
                             format.setState(course.getState());
 976  0
                 BaseDTOAssemblyNode<FormatInfo, CluInfo> formatNode = formatAssembler
 977  
                         .disassemble(format, NodeOperation.CREATE);
 978  0
                 results.add(formatNode);
 979  
 
 980  
                 
 981  
                 // Create the relationship and add it as well
 982  0
                 CluCluRelationInfo relation = new CluCluRelationInfo();
 983  0
                 relation.setCluId(nodeId);
 984  0
                 relation.setRelatedCluId(formatNode.getNodeData().getId());// this
 985  
                 // should
 986  
                 // already
 987  
                 // be set even if
 988  
                 // it's a create
 989  0
                 relation
 990  
                         .setType(CourseAssemblerConstants.COURSE_FORMAT_RELATION_TYPE);
 991  0
                 relation.setState(course.getState());
 992  
 
 993  0
                 BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo> relationNode = new BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo>(
 994  
                         null);
 995  0
                 relationNode.setNodeData(relation);
 996  0
                 relationNode.setOperation(NodeOperation.CREATE);
 997  
 
 998  0
                 results.add(relationNode);
 999  0
             } else if (NodeOperation.UPDATE == operation
 1000  
                                         && currentformatIds.containsKey(format.getId())) {
 1001  
                                 // If the course already has this format, then just update the
 1002  
                                 // format
 1003  0
                     format.setState(course.getState());
 1004  0
                                 BaseDTOAssemblyNode<FormatInfo, CluInfo> formatNode = formatAssembler
 1005  
                                                 .disassemble(format, NodeOperation.UPDATE);
 1006  0
                                 results.add(formatNode);
 1007  
 
 1008  
                                 // remove this entry from the map so we can tell what needs to
 1009  
                                 // be deleted at the end
 1010  0
                                 currentformatIds.remove(format.getId());
 1011  0
                         } else if (NodeOperation.DELETE == operation
 1012  
                     && currentformatIds.containsKey(format.getId()))  {
 1013  
                             // Delete the Format and its relation
 1014  0
                     CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 1015  0
                     relationToDelete.setId( currentformatIds.get(format.getId()) );
 1016  0
                     BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo>(
 1017  
                             null);
 1018  0
                     relationToDeleteNode.setNodeData(relationToDelete);
 1019  0
                     relationToDeleteNode.setOperation(NodeOperation.DELETE);
 1020  0
                     results.add(relationToDeleteNode);
 1021  
                         
 1022  0
                 BaseDTOAssemblyNode<FormatInfo, CluInfo> formatNode = formatAssembler
 1023  
                 .disassemble(format, NodeOperation.DELETE);
 1024  0
                 results.add(formatNode);                                        
 1025  
 
 1026  
                 // remove this entry from the map so we can tell what needs to
 1027  
                 // be deleted at the end
 1028  0
                 currentformatIds.remove(format.getId());
 1029  0
                         }                            
 1030  
                 }
 1031  
                 
 1032  
         // Now any leftover format ids are no longer needed, so delete
 1033  
         // formats and relations. These formats have to be assembled first before they can be marked for deletion
 1034  0
         for (Entry<String, String> entry : currentformatIds.entrySet()) {
 1035  
             // Create a new relation with the id of the relation we want to
 1036  
             // delete
 1037  0
             CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 1038  0
             relationToDelete.setId( entry.getValue() );
 1039  0
             BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<CourseInfo, CluCluRelationInfo>(
 1040  
                     null);
 1041  0
             relationToDeleteNode.setNodeData(relationToDelete);
 1042  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 1043  0
             results.add(relationToDeleteNode);
 1044  
             
 1045  0
             CluInfo formatCluToDelete = luService.getClu(entry.getKey());
 1046  0
             FormatInfo formatToDelete = formatAssembler.assemble(formatCluToDelete, null, false);
 1047  0
             BaseDTOAssemblyNode<FormatInfo, CluInfo> formatNode = formatAssembler
 1048  
             .disassemble(formatToDelete, NodeOperation.DELETE);
 1049  0
             results.add(formatNode);                                            
 1050  0
         }
 1051  
 
 1052  0
                 return results;
 1053  
         }
 1054  
         
 1055  
         private List<CourseVariationInfo> assembleVariations(List<CluIdentifierInfo> cluIdents) {
 1056  0
                 List<CourseVariationInfo> variations = new ArrayList<CourseVariationInfo>();
 1057  0
                 if (cluIdents != null) {
 1058  0
                         for (CluIdentifierInfo cluIdent : cluIdents) {
 1059  0
                                 if (cluIdent.getType() != null && 
 1060  
                                                 cluIdent.getType().equals(CourseAssemblerConstants.COURSE_VARIATION_IDENT_TYPE)) {
 1061  0
                                         CourseVariationInfo variation = new CourseVariationInfo();
 1062  0
                                         variation.setId(cluIdent.getId());
 1063  0
                                         variation.setType(cluIdent.getType());
 1064  0
                                         variation.setCourseNumberSuffix(cluIdent.getSuffixCode());
 1065  0
                                         variation.setSubjectArea(cluIdent.getDivision());
 1066  0
                                         variation.setVariationCode(cluIdent.getVariation());
 1067  0
                                         variation.setVariationTitle(cluIdent.getLongName());
 1068  0
                                         variations.add(variation);
 1069  0
                                 }
 1070  
                         }
 1071  
                 }
 1072  0
                 return variations;
 1073  
         }
 1074  
         
 1075  
         private List<CourseCrossListingInfo> assembleCrossListings(List<CluIdentifierInfo> cluIdents) {
 1076  0
                 List<CourseCrossListingInfo> crossListings = new ArrayList<CourseCrossListingInfo>();
 1077  0
                 if (cluIdents != null) {
 1078  0
                         for (CluIdentifierInfo cluIdent : cluIdents) {
 1079  0
                                 if (cluIdent.getType() != null && 
 1080  
                                                 cluIdent.getType().equals(CourseAssemblerConstants.COURSE_CROSSLISTING_IDENT_TYPE)) {
 1081  0
                                         CourseCrossListingInfo crosslisting = new CourseCrossListingInfo();
 1082  0
                                         crosslisting.setId(cluIdent.getId());
 1083  0
                                         crosslisting.setCode(cluIdent.getCode());
 1084  0
                                         crosslisting.setAttributes(cluIdent.getAttributes());
 1085  0
                                         crosslisting.setType(cluIdent.getType());
 1086  0
                                         crosslisting.setCourseNumberSuffix(cluIdent.getSuffixCode());
 1087  0
                                         crosslisting.setSubjectArea(cluIdent.getDivision());
 1088  0
                                         crosslisting.setDepartment(cluIdent.getOrgId());
 1089  0
                                         crossListings.add(crosslisting);
 1090  0
                                 }
 1091  
                         }
 1092  
                 }
 1093  0
                 return crossListings;
 1094  
         }
 1095  
         
 1096  
         // TODO This is pretty much a copy of the disassembleJoints
 1097  
         // code... maybe can be made generic
 1098  
         private List<BaseDTOAssemblyNode<?, ?>> disassembleJoints(String nodeId,
 1099  
                         CourseInfo course, NodeOperation operation)
 1100  
                         throws AssemblyException {
 1101  
 
 1102  0
                 List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 1103  
 
 1104  
                 // Get the current joints and put them in a map of joint id/relation
 1105  
                 // id
 1106  0
                 Map<String, CluCluRelationInfo> currentJointIds = new HashMap<String, CluCluRelationInfo>();
 1107  
 
 1108  0
                 if (!NodeOperation.CREATE.equals(operation)) {
 1109  
                         try {
 1110  0
                                 List<CluCluRelationInfo> jointRelationships = luService
 1111  
                                                 .getCluCluRelationsByClu(course.getId());
 1112  0
                                 for (CluCluRelationInfo jointRelation : jointRelationships) {
 1113  0
                                         if (CourseAssemblerConstants.JOINT_RELATION_TYPE
 1114  
                                                         .equals(jointRelation.getType())) {
 1115  0
                                                 currentJointIds.put(jointRelation.getId(),jointRelation);
 1116  
                                         }
 1117  
                                 }
 1118  0
                         } catch (DoesNotExistException e) {
 1119  0
                         } catch (InvalidParameterException e) {
 1120  0
                                 throw new AssemblyException("Error getting related formats", e);
 1121  0
                         } catch (MissingParameterException e) {
 1122  0
                                 throw new AssemblyException("Error getting related formats", e);
 1123  0
                         } catch (OperationFailedException e) {
 1124  0
                                 throw new AssemblyException("Error getting related formats", e);
 1125  0
                         }
 1126  
                 }
 1127  
 
 1128  
                 // Loop through all the joints in this course
 1129  0
                 for (CourseJointInfo joint : course.getJoints()) {
 1130  
                         // If this is a course create then all joints will be created
 1131  0
                         if (NodeOperation.UPDATE.equals(operation) && joint.getRelationId() != null
 1132  
                                         && currentJointIds.containsKey(joint.getRelationId())) {
 1133  
                                 // remove this entry from the map so we can tell what needs to
 1134  
                                 // be deleted at the end
 1135  0
                                 CluCluRelationInfo relation = currentJointIds.remove(joint.getRelationId());
 1136  0
                                 relation.setRelatedCluId(joint.getCourseId());
 1137  0
                                 relation.setState(course.getState());
 1138  0
                                 BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo> jointNode = new BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo>(courseJointAssembler);
 1139  0
                                 jointNode.setBusinessDTORef(joint);
 1140  0
                                 jointNode.setNodeData(relation);
 1141  0
                                 jointNode.setOperation(NodeOperation.UPDATE);
 1142  0
                                 results.add(jointNode);
 1143  0
                         } else if (!NodeOperation.DELETE.equals(operation)) {
 1144  
                                 // the joint does not exist, so create cluclurelation
 1145  0
                                 BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo> jointNode = courseJointAssembler
 1146  
                                                 .disassemble(joint, NodeOperation.CREATE);
 1147  0
                                 jointNode.getNodeData().setCluId(nodeId);
 1148  0
                                 jointNode.getNodeData().setState(course.getState());
 1149  0
                                 results.add(jointNode);
 1150  0
                         }
 1151  
                 }
 1152  
 
 1153  
         // Now any leftover joint ids are no longer needed, so delete
 1154  
         // joint relations
 1155  0
         for (String id : currentJointIds.keySet()) {
 1156  
             // Create a new relation with the id of the relation we want to
 1157  
             // delete
 1158  0
             CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 1159  0
             relationToDelete.setId(id);
 1160  0
             BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo>(
 1161  
                     courseJointAssembler);
 1162  0
             relationToDeleteNode.setNodeData(relationToDelete);
 1163  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 1164  0
             results.add(relationToDeleteNode);
 1165  0
         }
 1166  
                 
 1167  0
                 return results;
 1168  
         }
 1169  
         
 1170  
         public void setLuService(LuService luService) {
 1171  0
                 this.luService = luService;
 1172  0
         }
 1173  
 
 1174  
         public void setFormatAssembler(FormatAssembler formatAssembler) {
 1175  0
                 this.formatAssembler = formatAssembler;
 1176  0
         }
 1177  
 
 1178  
         public void setCourseJointAssembler(
 1179  
                         CourseJointAssembler courseJointAssembler) {
 1180  0
                 this.courseJointAssembler = courseJointAssembler;
 1181  0
         }
 1182  
 
 1183  
         public void setLoAssembler(LoAssembler loAssembler) {
 1184  0
                 this.loAssembler = loAssembler;
 1185  0
         }
 1186  
 
 1187  
         public void setLoService(LearningObjectiveService loService) {
 1188  0
                 this.loService = loService;
 1189  0
         }
 1190  
 
 1191  
     public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) {
 1192  0
         this.cluAssemblerUtils = cluAssemblerUtils;
 1193  0
     }
 1194  
 
 1195  
         public void setLrcService(LrcService lrcService) {
 1196  0
                 this.lrcService = lrcService;
 1197  0
         }
 1198  
 }