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 public class CourseServiceUtils {
39 public static void resetIds(CourseInfo course) {
40
41
42 for(CourseJointInfo joint:course.getJoints()){
43 joint.setRelationId(null);
44 }
45
46 for(LoDisplayInfo lo:course.getCourseSpecificLOs()){
47 resetLoRecursively(lo);
48 }
49
50 for(FormatInfo format:course.getFormats()){
51 format.setId(null);
52 for(ActivityInfo activity:format.getActivities()){
53 activity.setId(null);
54 }
55 }
56
57 for(ResultComponentInfo result:course.getCreditOptions()){
58 result.setId(null);
59 }
60
61 for(CourseCrossListingInfo crossListing:course.getCrossListings()){
62 crossListing.setId(null);
63 }
64
65 for(AffiliatedOrgInfo orgInfo:course.getExpenditure().getAffiliatedOrgs()){
66 orgInfo.setId(null);
67 }
68
69 for(CourseFeeInfo fee:course.getFees()){
70 fee.setId(null);
71 for(CurrencyAmountInfo feeAmount:fee.getFeeAmounts()){
72 feeAmount.setId(null);
73 }
74 }
75
76 for(CourseRevenueInfo revenue:course.getRevenues()){
77 revenue.setId(null);
78 for(AffiliatedOrgInfo orgInfo:revenue.getAffiliatedOrgs()){
79 orgInfo.setId(null);
80 }
81 }
82
83 for(CourseVariationInfo variation:course.getVariations()){
84 variation.setId(null);
85 }
86 }
87
88 private static void resetLoRecursively(LoDisplayInfo lo){
89
90 lo.getLoInfo().setId(null);
91 for(LoDisplayInfo nestedLo:lo.getLoDisplayInfoList()){
92 resetLoRecursively(nestedLo);
93 }
94 }
95
96 private static void clearStatementTreeViewIds(
97 List<StatementTreeViewInfo> statementTreeViews, String newState, LuService luService) throws OperationFailedException {
98
99 for(StatementTreeViewInfo statementTreeView:statementTreeViews){
100 clearStatementTreeViewIdsRecursively(statementTreeView, newState, luService);
101 }
102 }
103
104
105
106
107
108
109
110 private static void clearStatementTreeViewIdsRecursively(StatementTreeViewInfo statementTreeView, String newState,LuService luService) throws OperationFailedException{
111 statementTreeView.setId(null);
112 statementTreeView.setState(newState);
113
114
115 for(ReqComponentInfo reqComp:statementTreeView.getReqComponents()){
116 reqComp.setId(null);
117 reqComp.setState(newState);
118 for(ReqCompFieldInfo field:reqComp.getReqCompFields()){
119 field.setId(null);
120
121 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 CluSetInfo cluSet = luService.getCluSetInfo(field.getValue());
126 cluSet.setId(null);
127 cluSet.setState(newState);
128
129 if (cluSet.getMembershipQuery() != null){
130 cluSet.getCluIds().clear();
131 cluSet.getCluSetIds().clear();
132 }
133 cluSet = luService.createCluSet(cluSet.getType(), cluSet);
134 field.setValue(cluSet.getId());
135 } catch (Exception e) {
136 throw new OperationFailedException("Error copying clusets.", e);
137 }
138 }
139
140 }
141 }
142
143 for(StatementTreeViewInfo child: statementTreeView.getStatements()){
144 clearStatementTreeViewIdsRecursively(child,newState,luService);
145 }
146 }
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 List<StatementTreeViewInfo> statementTreeViews = courseService.getCourseStatements(originalCluId,null,null);
151
152 clearStatementTreeViewIds(statementTreeViews,newState,luService);
153
154 for(StatementTreeViewInfo statementTreeView:statementTreeViews){
155 courseService.createCourseStatement(newCluId, statementTreeView);
156 }
157 }
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 CourseInfo originalCourse = courseService.getCourse(originalCluId);
161 resetIds(originalCourse);
162
163
164
165 if(newState==null){
166 newState = originalCourse.getState();
167 }
168
169 originalCourse.setId(newCluId);
170 originalCourse.setState(newState);
171
172 if(ignoreProperties!=null){
173 for(String property:ignoreProperties){
174 try {
175 PropertyUtils.setProperty(originalCourse, property, null);
176 } catch (Exception e) {
177 throw new InvalidParameterException("Ignore property is invalid and is causing an exception.",e);
178 }
179 }
180 }
181
182 CourseInfo newCourse = courseService.createCourse(originalCourse);
183 copyStatements(originalCluId, newCourse.getId(), newState, statementService, luService, courseService);
184 return newCourse;
185 }
186
187 }