View Javadoc
1   /**
2    * Copyright 2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by venkat on 8/6/14
16   */
17  package org.kuali.student.cm.course.service.impl;
18  
19  import org.kuali.rice.krms.dto.AgendaEditor;
20  import org.kuali.rice.krms.dto.PropositionEditor;
21  import org.kuali.rice.krms.dto.PropositionParameterEditor;
22  import org.kuali.rice.krms.dto.RuleEditor;
23  import org.kuali.rice.krms.dto.RuleManagementWrapper;
24  import org.kuali.rice.krms.dto.TermEditor;
25  import org.kuali.rice.krms.dto.TermParameterEditor;
26  import org.kuali.student.cm.common.util.CurriculumManagementConstants;
27  import org.kuali.student.cm.course.form.wrapper.CourseInfoWrapper;
28  import org.kuali.student.cm.course.service.CourseCopyHelper;
29  import org.kuali.student.r2.common.dto.AttributeInfo;
30  import org.kuali.student.r2.common.dto.DtoConstants;
31  import org.kuali.student.r2.lum.clu.dto.AffiliatedOrgInfo;
32  import org.kuali.student.r2.lum.course.dto.ActivityInfo;
33  import org.kuali.student.r2.lum.course.dto.CourseCrossListingInfo;
34  import org.kuali.student.r2.lum.course.dto.CourseFeeInfo;
35  import org.kuali.student.r2.lum.course.dto.CourseInfo;
36  import org.kuali.student.r2.lum.course.dto.CourseJointInfo;
37  import org.kuali.student.r2.lum.course.dto.CourseRevenueInfo;
38  import org.kuali.student.r2.lum.course.dto.CourseVariationInfo;
39  import org.kuali.student.r2.lum.course.dto.FormatInfo;
40  import org.kuali.student.r2.lum.course.dto.LoDisplayInfo;
41  import org.springframework.beans.BeanUtils;
42  
43  import java.util.Arrays;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   *
49   * A helper class to copy course. Configured at ks-lu-no-tx-context.xml and export
50   * to service bus. This is to support institutionally configurable the ignore properties
51   * on copy and allows institutions to customize if they want to change the out of the box
52   * functionality
53   *
54   * @author Kuali Student Team
55   */
56  public class CourseCopyHelperImpl implements CourseCopyHelper {
57  
58      private List<String> ignoreProperties;
59  
60      public CourseCopyHelperImpl() {}
61  
62      /**
63       * This method populates all the data from source to target excluding the properties
64       * configured at 'ignoreProperties'. This method uses #BeanUtils.copyProperties() to copy
65       * properties.
66       *
67       * @param source
68       * @param target
69       */
70      @Override
71      public void copyCourse(CourseInfo source, CourseInfo target){
72  
73          Object[] propertiesToSkip = (Object[]) getIgnoreProperties().toArray();
74          String[] stringArray = Arrays.copyOf(propertiesToSkip, propertiesToSkip.length, String[].class);
75  
76          BeanUtils.copyProperties(source, target, stringArray);
77  
78          target.setCourseTitle("Copy of " + source.getCourseTitle());
79  
80          resetCourse(target);
81      }
82  
83      @Override
84      public void cleanUpCourseWrapperOnCopy(CourseInfoWrapper target){
85  
86  
87          target.setRefObjectId(null);
88          
89  //        CourseCreateUnitsContentOwner newCourseCreateUnitsContentOwner = new CourseCreateUnitsContentOwner();
90  //        newCourseCreateUnitsContentOwner.getRenderHelper().setNewRow(true);
91  //        target.getUnitsContentOwner().add(newCourseCreateUnitsContentOwner);
92  
93          resetCourse(target.getCourseInfo());
94          resetRequisites(target);
95  
96      }
97  
98      /**
99       * Resets a CourseInfo so that it can be persisted as a new entity.
100      *
101      * @param course The CourseInfo to reset.
102      */
103     protected void resetCourse(CourseInfo course) {
104         //  Clobber the IDs
105         course.setId(null);
106         course.setVersion(null);
107         course.setMeta(null);
108         course.setEffectiveDate(null);
109         course.setExpirationDate(null);
110 
111         //  Fix the state. Courses start in state draft.
112         course.setStateKey(DtoConstants.STATE_DRAFT);
113 
114         //  Clobber the IDs in these collections and remove the retirement related attributes.
115         List<AttributeInfo> attributes = course.getAttributes();
116         for (Iterator<AttributeInfo> it = attributes.iterator(); it.hasNext();) {
117             AttributeInfo attributeInfo = it.next();
118             if (attributeInfo.getKey().equals(CurriculumManagementConstants.COURSE_ATTRIBUTE_LAST_TERM_OFFERED)
119                     || attributeInfo.getKey().equals(CurriculumManagementConstants.COURSE_ATTRIBUTE_LAST_PUBLICATION_YEAR)
120                     || attributeInfo.getKey().equals(CurriculumManagementConstants.COURSE_ATTRIBUTE_RETIREMENT_COMMENT)
121                     || attributeInfo.getKey().equals(CurriculumManagementConstants.COURSE_ATTRIBUTE_RETIREMENT_RATIONALE)
122                     ) {
123                 it.remove();
124             } else {
125                 attributeInfo.setId(null);
126             }
127         }
128 
129         for (CourseJointInfo joint : course.getJoints()) {
130             joint.setRelationId(null);
131         }
132 
133         for (LoDisplayInfo lo : course.getCourseSpecificLOs()) {
134             recursivelyClobberLoIds(lo);
135         }
136 
137         for (CourseCrossListingInfo crossListing : course.getCrossListings()) {
138             crossListing.setId(null);
139         }
140 
141         for (FormatInfo format : course.getFormats()) {
142             format.setId(null);
143             format.setMeta(null);
144             for (ActivityInfo activity : format.getActivities()) {
145                 activity.setId(null);
146                 activity.setMeta(null);
147             }
148         }
149 
150         for (AffiliatedOrgInfo orgInfo : course.getExpenditure().getAffiliatedOrgs()) {
151             orgInfo.setId(null);
152         }
153 
154         for (CourseFeeInfo fee : course.getFees()) {
155             fee.setId(null);
156         }
157 
158         for (CourseRevenueInfo revenue : course.getRevenues()) {
159             revenue.setId(null);
160             for (AffiliatedOrgInfo orgInfo : revenue.getAffiliatedOrgs()) {
161                 orgInfo.setId(null);
162             }
163         }
164 
165         for (CourseVariationInfo variation : course.getVariations()) {
166             variation.setId(null);
167         }
168     }
169 
170     protected void recursivelyClobberLoIds(LoDisplayInfo lo) {
171         lo.getLoInfo().setId(null);
172         for (LoDisplayInfo nestedLo : lo.getLoDisplayInfoList()) {
173             recursivelyClobberLoIds(nestedLo);
174         }
175     }
176 
177     /**
178      * Removes IDs and other properties from the data object so that new entities are created on persist.
179      */
180     public void resetRequisites(RuleManagementWrapper dataObject) {
181         for (AgendaEditor agenda : dataObject.getAgendas()) {
182             agenda.setContextId(null);
183             agenda.setFirstItemId(null);
184             agenda.setId(null);
185             agenda.setVersionNumber(null);
186             agenda.setName(null);
187 
188             for (RuleEditor re : agenda.getRuleEditors().values()) {
189                 re.setId(null);
190                 re.setVersionNumber(null);
191                 re.setName(null);
192                 re.setPropId(null);
193 
194                 PropositionEditor pe = re.getPropositionEditor();
195                 if (pe != null) {
196                     pe.setId(null);
197                     pe.setRuleId(null);
198                     pe.setVersionNumber(null);
199 
200                     for (PropositionParameterEditor parameter : pe.getParameters()) {
201                         parameter.setId(null);
202                         parameter.setVersionNumber(null);
203                         parameter.setPropId(null);
204                     }
205 
206                     TermEditor te = pe.getTerm();
207                     if (te != null) {
208                         te.setId(null);
209                         te.setVersionNumber(null);
210 
211                         for (TermParameterEditor parameter : te.getEditorParameters()) {
212                             parameter.setId(null);
213                             parameter.setVersionNumber(null);
214                             parameter.setTermId(null);
215                         }
216                     }
217                 }
218             }
219         }
220     }
221 
222     public List<String> getIgnoreProperties() {
223         return ignoreProperties;
224     }
225 
226     public void setIgnoreProperties(List<String> ignoreProperties) {
227         this.ignoreProperties = ignoreProperties;
228     }
229 }