| 1 |  |  package org.kuali.student.lum.course.service.impl; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.List; | 
  | 4 |  |   | 
  | 5 |  |  import org.apache.commons.beanutils.PropertyUtils; | 
  | 6 |  |  import org.kuali.student.common.dto.CurrencyAmountInfo; | 
  | 7 |  |  import org.kuali.student.common.exceptions.AlreadyExistsException; | 
  | 8 |  |  import org.kuali.student.common.exceptions.CircularRelationshipException; | 
  | 9 |  |  import org.kuali.student.common.exceptions.DataValidationErrorException; | 
  | 10 |  |  import org.kuali.student.common.exceptions.DependentObjectsExistException; | 
  | 11 |  |  import org.kuali.student.common.exceptions.DoesNotExistException; | 
  | 12 |  |  import org.kuali.student.common.exceptions.InvalidParameterException; | 
  | 13 |  |  import org.kuali.student.common.exceptions.MissingParameterException; | 
  | 14 |  |  import org.kuali.student.common.exceptions.OperationFailedException; | 
  | 15 |  |  import org.kuali.student.common.exceptions.PermissionDeniedException; | 
  | 16 |  |  import org.kuali.student.common.exceptions.UnsupportedActionException; | 
  | 17 |  |  import org.kuali.student.common.exceptions.VersionMismatchException; | 
  | 18 |  |  import org.kuali.student.core.statement.dto.ReqCompFieldInfo; | 
  | 19 |  |  import org.kuali.student.core.statement.dto.ReqComponentInfo; | 
  | 20 |  |  import org.kuali.student.core.statement.dto.StatementTreeViewInfo; | 
  | 21 |  |  import org.kuali.student.core.statement.service.StatementService; | 
  | 22 |  |  import org.kuali.student.lum.course.dto.ActivityInfo; | 
  | 23 |  |  import org.kuali.student.lum.course.dto.CourseCrossListingInfo; | 
  | 24 |  |  import org.kuali.student.lum.course.dto.CourseFeeInfo; | 
  | 25 |  |  import org.kuali.student.lum.course.dto.CourseInfo; | 
  | 26 |  |  import org.kuali.student.lum.course.dto.CourseJointInfo; | 
  | 27 |  |  import org.kuali.student.lum.course.dto.CourseRevenueInfo; | 
  | 28 |  |  import org.kuali.student.lum.course.dto.CourseVariationInfo; | 
  | 29 |  |  import org.kuali.student.lum.course.dto.FormatInfo; | 
  | 30 |  |  import org.kuali.student.lum.course.dto.LoDisplayInfo; | 
  | 31 |  |  import org.kuali.student.lum.course.service.CourseService; | 
  | 32 |  |  import org.kuali.student.lum.lrc.dto.ResultComponentInfo; | 
  | 33 |  |  import org.kuali.student.lum.lu.dto.AffiliatedOrgInfo; | 
  | 34 |  |  import org.kuali.student.lum.lu.dto.CluSetInfo; | 
  | 35 |  |  import org.kuali.student.lum.lu.service.LuService; | 
  | 36 |  |  import org.kuali.student.lum.statement.typekey.ReqComponentFieldTypes; | 
  | 37 |  |   | 
  | 38 | 0 |  public class CourseServiceUtils { | 
  | 39 |  |          public static void resetIds(CourseInfo course) { | 
  | 40 |  |                   | 
  | 41 |  |                   | 
  | 42 | 8 |                  for(CourseJointInfo joint:course.getJoints()){ | 
  | 43 | 0 |                          joint.setRelationId(null); | 
  | 44 |  |                  } | 
  | 45 |  |                   | 
  | 46 | 8 |                  for(LoDisplayInfo lo:course.getCourseSpecificLOs()){ | 
  | 47 | 16 |                          resetLoRecursively(lo); | 
  | 48 |  |                  } | 
  | 49 |  |                   | 
  | 50 | 8 |                  for(FormatInfo format:course.getFormats()){ | 
  | 51 | 16 |                          format.setId(null); | 
  | 52 | 16 |                          for(ActivityInfo activity:format.getActivities()){ | 
  | 53 | 32 |                                  activity.setId(null); | 
  | 54 |  |                          } | 
  | 55 |  |                  } | 
  | 56 |  |                   | 
  | 57 | 8 |                  for(ResultComponentInfo result:course.getCreditOptions()){ | 
  | 58 | 16 |                          result.setId(null); | 
  | 59 |  |                  } | 
  | 60 |  |                   | 
  | 61 | 8 |                  for(CourseCrossListingInfo crossListing:course.getCrossListings()){ | 
  | 62 | 0 |                          crossListing.setId(null); | 
  | 63 |  |                  } | 
  | 64 |  |                   | 
  | 65 | 8 |                  for(AffiliatedOrgInfo orgInfo:course.getExpenditure().getAffiliatedOrgs()){ | 
  | 66 | 16 |                          orgInfo.setId(null); | 
  | 67 |  |                  } | 
  | 68 |  |                   | 
  | 69 | 8 |                  for(CourseFeeInfo fee:course.getFees()){ | 
  | 70 | 16 |                          fee.setId(null); | 
  | 71 | 16 |                          for(CurrencyAmountInfo feeAmount:fee.getFeeAmounts()){ | 
  | 72 | 32 |                                  feeAmount.setId(null); | 
  | 73 |  |                          } | 
  | 74 |  |                  } | 
  | 75 |  |                   | 
  | 76 | 8 |                  for(CourseRevenueInfo revenue:course.getRevenues()){ | 
  | 77 | 16 |                          revenue.setId(null); | 
  | 78 | 16 |                          for(AffiliatedOrgInfo orgInfo:revenue.getAffiliatedOrgs()){ | 
  | 79 | 32 |                                  orgInfo.setId(null); | 
  | 80 |  |                          } | 
  | 81 |  |                  } | 
  | 82 |  |                   | 
  | 83 | 8 |                  for(CourseVariationInfo variation:course.getVariations()){ | 
  | 84 | 16 |                          variation.setId(null); | 
  | 85 |  |                  } | 
  | 86 | 8 |          } | 
  | 87 |  |   | 
  | 88 |  |          private static void resetLoRecursively(LoDisplayInfo lo){ | 
  | 89 |  |                   | 
  | 90 | 112 |                  lo.getLoInfo().setId(null); | 
  | 91 | 112 |                  for(LoDisplayInfo nestedLo:lo.getLoDisplayInfoList()){ | 
  | 92 | 96 |                          resetLoRecursively(nestedLo); | 
  | 93 |  |                  } | 
  | 94 | 112 |          } | 
  | 95 |  |   | 
  | 96 |  |          private static void clearStatementTreeViewIds( | 
  | 97 |  |                          List<StatementTreeViewInfo> statementTreeViews, String newState, LuService luService) throws OperationFailedException { | 
  | 98 |  |                   | 
  | 99 | 8 |                  for(StatementTreeViewInfo statementTreeView:statementTreeViews){ | 
  | 100 | 1 |                          clearStatementTreeViewIdsRecursively(statementTreeView, newState, luService); | 
  | 101 |  |                  } | 
  | 102 | 8 |          } | 
  | 103 |  |   | 
  | 104 |  |           | 
  | 105 |  |   | 
  | 106 |  |   | 
  | 107 |  |   | 
  | 108 |  |   | 
  | 109 |  |   | 
  | 110 |  |          private static void clearStatementTreeViewIdsRecursively(StatementTreeViewInfo statementTreeView, String newState,LuService luService) throws OperationFailedException{ | 
  | 111 | 3 |                  statementTreeView.setId(null); | 
  | 112 | 3 |                  statementTreeView.setState(newState); | 
  | 113 |  |                   | 
  | 114 |  |                   | 
  | 115 | 3 |                  for(ReqComponentInfo reqComp:statementTreeView.getReqComponents()){ | 
  | 116 | 4 |                          reqComp.setId(null); | 
  | 117 | 4 |                          reqComp.setState(newState); | 
  | 118 | 4 |                          for(ReqCompFieldInfo field:reqComp.getReqCompFields()){ | 
  | 119 | 0 |                                  field.setId(null); | 
  | 120 |  |                                   | 
  | 121 | 0 |                                  if(ReqComponentFieldTypes.COURSE_CLUSET_KEY.getId().equals(field.getType())|| | 
  | 122 |  |                                     ReqComponentFieldTypes.PROGRAM_CLUSET_KEY.getId().equals(field.getType())|| | 
  | 123 |  |                                     ReqComponentFieldTypes.CLUSET_KEY.getId().equals(field.getType())){ | 
  | 124 |  |                                          try { | 
  | 125 | 0 |                                                  CluSetInfo cluSet = luService.getCluSetInfo(field.getValue()); | 
  | 126 | 0 |                                                  cluSet.setId(null); | 
  | 127 | 0 |                                                  cluSet.setState(newState); | 
  | 128 |  |                                                   | 
  | 129 | 0 |                                                  if (cluSet.getMembershipQuery() != null){ | 
  | 130 | 0 |                                                          cluSet.getCluIds().clear(); | 
  | 131 | 0 |                                                          cluSet.getCluSetIds().clear(); | 
  | 132 |  |                                                  } | 
  | 133 | 0 |                                                  cluSet = luService.createCluSet(cluSet.getType(), cluSet); | 
  | 134 | 0 |                                                  field.setValue(cluSet.getId()); | 
  | 135 | 0 |                                          } catch (Exception e) { | 
  | 136 | 0 |                                                  throw new OperationFailedException("Error copying clusets.", e); | 
  | 137 | 0 |                                          } | 
  | 138 |  |                                  } | 
  | 139 |  |                                   | 
  | 140 |  |                          } | 
  | 141 |  |                  } | 
  | 142 |  |                   | 
  | 143 | 3 |                  for(StatementTreeViewInfo child: statementTreeView.getStatements()){ | 
  | 144 | 2 |                          clearStatementTreeViewIdsRecursively(child,newState,luService); | 
  | 145 |  |                  } | 
  | 146 | 3 |          } | 
  | 147 |  |   | 
  | 148 |  |          public static void copyStatements(String originalCluId, String newCluId, String newState, | 
  | 149 |  |                          StatementService statementService, LuService luService, CourseService courseService) throws OperationFailedException, DoesNotExistException, InvalidParameterException, MissingParameterException, PermissionDeniedException, DataValidationErrorException { | 
  | 150 | 8 |                  List<StatementTreeViewInfo> statementTreeViews = courseService.getCourseStatements(originalCluId,null,null); | 
  | 151 |  |                   | 
  | 152 | 8 |                  clearStatementTreeViewIds(statementTreeViews,newState,luService); | 
  | 153 |  |                   | 
  | 154 | 8 |                  for(StatementTreeViewInfo statementTreeView:statementTreeViews){ | 
  | 155 | 1 |                          courseService.createCourseStatement(newCluId, statementTreeView); | 
  | 156 |  |                  } | 
  | 157 | 8 |          } | 
  | 158 |  |           | 
  | 159 |  |          public static CourseInfo copyCourse(String originalCluId, String newCluId, String newState, String[] ignoreProperties, StatementService statementService, LuService luService, CourseService courseService) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, AlreadyExistsException, DataValidationErrorException, VersionMismatchException, CircularRelationshipException, DependentObjectsExistException, UnsupportedActionException{ | 
  | 160 | 0 |                  CourseInfo originalCourse = courseService.getCourse(originalCluId); | 
  | 161 | 0 |                  resetIds(originalCourse); | 
  | 162 |  |                   | 
  | 163 |  |                   | 
  | 164 |  |                   | 
  | 165 | 0 |                  if(newState==null){ | 
  | 166 | 0 |                          newState = originalCourse.getState(); | 
  | 167 |  |                  } | 
  | 168 |  |                   | 
  | 169 | 0 |                  originalCourse.setId(newCluId); | 
  | 170 | 0 |                  originalCourse.setState(newState); | 
  | 171 |  |                   | 
  | 172 | 0 |                  if(ignoreProperties!=null){ | 
  | 173 | 0 |                          for(String property:ignoreProperties){ | 
  | 174 |  |                                  try { | 
  | 175 | 0 |                                          PropertyUtils.setProperty(originalCourse, property, null); | 
  | 176 | 0 |                                  } catch (Exception e) { | 
  | 177 | 0 |                                          throw new InvalidParameterException("Ignore property is invalid and is causing an exception.",e); | 
  | 178 | 0 |                                  } | 
  | 179 |  |                          } | 
  | 180 |  |                  } | 
  | 181 |  |                   | 
  | 182 | 0 |                  CourseInfo newCourse = courseService.createCourse(originalCourse); | 
  | 183 | 0 |                  copyStatements(originalCluId, newCourse.getId(), newState, statementService, luService, courseService); | 
  | 184 | 0 |                  return newCourse; | 
  | 185 |  |          } | 
  | 186 |  |           | 
  | 187 |  |  } |