1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lu.ui.course.server.gwt; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.student.common.dto.DtoConstants; |
25 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
26 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
27 | |
import org.kuali.student.common.search.dto.SearchRequest; |
28 | |
import org.kuali.student.common.search.dto.SearchResult; |
29 | |
import org.kuali.student.common.ui.server.gwt.AbstractDataService; |
30 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
31 | |
import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; |
32 | |
import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter; |
33 | |
import org.kuali.student.lum.course.dto.CourseCrossListingInfo; |
34 | |
import org.kuali.student.lum.course.dto.CourseInfo; |
35 | |
import org.kuali.student.lum.course.service.CourseService; |
36 | |
import org.kuali.student.lum.course.service.CourseServiceConstants; |
37 | |
import org.kuali.student.lum.lu.LUConstants; |
38 | |
import org.kuali.student.lum.lu.service.LuService; |
39 | |
import org.kuali.student.lum.lu.service.LuServiceConstants; |
40 | |
import org.springframework.util.StringUtils; |
41 | |
|
42 | 0 | public class CourseDataService extends AbstractDataService { |
43 | |
|
44 | 0 | final static Logger LOG = Logger.getLogger(CourseDataService.class); |
45 | |
|
46 | |
private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT; |
47 | |
|
48 | |
protected CourseService courseService; |
49 | |
protected LuService luService; |
50 | |
|
51 | |
@Override |
52 | |
protected Object get(String id) throws Exception { |
53 | 0 | CourseInfo courseInfo = null; |
54 | |
|
55 | |
try { |
56 | 0 | courseInfo = courseService.getCourse(id); |
57 | 0 | } catch (DoesNotExistException dne) { |
58 | 0 | LOG.info("Course not found for key " + id + ". Course loaded from proposal instead."); |
59 | 0 | } |
60 | |
|
61 | 0 | return courseInfo; |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
protected Object save(Object dto, Map<String, Object> properties) throws Exception { |
66 | |
|
67 | 0 | CourseInfo courseInfo = (CourseInfo)dto; |
68 | |
|
69 | |
|
70 | 0 | if(LUConstants.PROPOSAL_TYPE_COURSE_RETIRE.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))){ |
71 | 0 | if(courseInfo.getVersionInfo()==null){ |
72 | 0 | courseInfo = (CourseInfo) get(courseInfo.getId()); |
73 | |
} |
74 | 0 | String startTerm = courseInfo.getStartTerm(); |
75 | 0 | Map<String,String> proposalAttributes = new HashMap<String,String>(); |
76 | 0 | if(startTerm!=null) { |
77 | 0 | proposalAttributes.put("prevStartTerm", startTerm); |
78 | |
} |
79 | 0 | properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes); |
80 | 0 | return courseInfo; |
81 | |
} |
82 | |
|
83 | |
|
84 | 0 | courseInfo = calculateCourseDerivedFields(courseInfo); |
85 | |
|
86 | 0 | if(properties!=null&&(LUConstants.PROPOSAL_TYPE_COURSE_MODIFY.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))|| |
87 | |
LUConstants.PROPOSAL_TYPE_COURSE_MODIFY_ADMIN.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE)))){ |
88 | |
|
89 | 0 | if(courseInfo.getId() == null){ |
90 | |
|
91 | 0 | if (isLatestVersion(courseInfo.getVersionInfo().getVersionIndId())){ |
92 | 0 | String courseIndId = courseInfo.getVersionInfo().getVersionIndId(); |
93 | |
|
94 | |
|
95 | 0 | VersionDisplayInfo versionInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, courseIndId); |
96 | 0 | CourseInfo originalCourseInfo = courseService.getCourse(versionInfo.getId()); |
97 | |
|
98 | |
|
99 | 0 | String startTerm = originalCourseInfo.getStartTerm(); |
100 | 0 | String endTerm = originalCourseInfo.getEndTerm(); |
101 | 0 | Map<String,String> proposalAttributes = new HashMap<String,String>(); |
102 | 0 | if(startTerm!=null) |
103 | 0 | proposalAttributes.put("prevStartTerm",startTerm); |
104 | 0 | if(endTerm!=null) |
105 | 0 | proposalAttributes.put("prevEndTerm",endTerm); |
106 | |
|
107 | 0 | properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes); |
108 | |
|
109 | 0 | courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment()); |
110 | 0 | } else { |
111 | 0 | throw new OperationFailedException("Error creating new version for course, this course is currently under modification."); |
112 | |
} |
113 | |
}else{ |
114 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
115 | |
} |
116 | |
} else { |
117 | 0 | if (courseInfo.getId() == null){ |
118 | 0 | courseInfo = courseService.createCourse(courseInfo); |
119 | |
} else { |
120 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
121 | |
} |
122 | |
} |
123 | 0 | return courseInfo; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
@Override |
128 | |
protected List<ValidationResultInfo> validate(Object dto) throws Exception { |
129 | 0 | return courseService.validateCourse("OBJECT", (CourseInfo)dto); |
130 | |
} |
131 | |
|
132 | |
@Override |
133 | |
protected String getDefaultMetaDataState() { |
134 | 0 | return DEFAULT_METADATA_STATE; |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
protected String getDefaultWorkflowDocumentType() { |
139 | 0 | return LUConstants.PROPOSAL_TYPE_COURSE_CREATE; |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
protected boolean checkDocumentLevelPermissions() { |
144 | 0 | return true; |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
protected Class<?> getDtoClass() { |
149 | 0 | return CourseInfo.class; |
150 | |
} |
151 | |
|
152 | |
public void setCourseService(CourseService courseService) { |
153 | 0 | this.courseService = courseService; |
154 | 0 | } |
155 | |
|
156 | |
public void setLuService(LuService luService) { |
157 | 0 | this.luService = luService; |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){ |
164 | |
|
165 | 0 | if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){ |
166 | 0 | courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix())); |
167 | |
} |
168 | |
|
169 | |
|
170 | 0 | for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){ |
171 | 0 | if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){ |
172 | 0 | crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix())); |
173 | |
} |
174 | |
} |
175 | |
|
176 | 0 | return courseInfo; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private String calculateCourseCode(String subjectArea, String suffixNumber) { |
188 | 0 | return subjectArea + suffixNumber; |
189 | |
} |
190 | |
|
191 | |
public Boolean isLatestVersion(String versionIndId) throws Exception { |
192 | 0 | VersionDisplayInfo currentVersion = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, versionIndId); |
193 | |
|
194 | |
|
195 | 0 | SearchRequest request = new SearchRequest("lu.search.isVersionable"); |
196 | 0 | request.addParam("lu.queryParam.versionIndId", versionIndId); |
197 | 0 | request.addParam("lu.queryParam.sequenceNumber", currentVersion.getSequenceNumber().toString()); |
198 | 0 | List<String> states = new ArrayList<String>(); |
199 | 0 | states.add("Approved"); |
200 | 0 | states.add("Active"); |
201 | 0 | states.add("Draft"); |
202 | 0 | states.add("Superseded"); |
203 | 0 | request.addParam("lu.queryParam.luOptionalState", states); |
204 | 0 | SearchResult result = luService.search(request); |
205 | |
|
206 | 0 | String resultString = result.getRows().get(0).getCells().get(0).getValue(); |
207 | 0 | return "0".equals(resultString); |
208 | |
} |
209 | |
} |