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