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.lu.LUConstants; |
37 | |
import org.kuali.student.lum.lu.service.LuService; |
38 | |
import org.kuali.student.lum.lu.service.LuServiceConstants; |
39 | |
import org.springframework.util.StringUtils; |
40 | |
|
41 | 0 | public class CourseDataService extends AbstractDataService { |
42 | |
|
43 | |
private static final long serialVersionUID = 1L; |
44 | 0 | final static Logger LOG = Logger.getLogger(CourseDataService.class); |
45 | |
|
46 | |
private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT; |
47 | |
|
48 | |
private CourseService courseService; |
49 | |
private 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 | 0 | CourseInfo courseInfo = (CourseInfo)dto; |
67 | |
|
68 | |
|
69 | 0 | courseInfo = calculateCourseDerivedFields(courseInfo); |
70 | |
|
71 | 0 | if(properties!=null&&(LUConstants.PROPOSAL_TYPE_COURSE_MODIFY.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))|| |
72 | |
LUConstants.PROPOSAL_TYPE_COURSE_MODIFY_ADMIN.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE)))){ |
73 | |
|
74 | 0 | if(courseInfo.getId() == null){ |
75 | |
|
76 | 0 | if (isLatestVersion(courseInfo.getVersionInfo().getVersionIndId())){ |
77 | |
|
78 | 0 | String startTerm = courseInfo.getStartTerm(); |
79 | 0 | String endTerm = courseInfo.getEndTerm(); |
80 | 0 | Map<String,String> proposalAttributes = new HashMap<String,String>(); |
81 | 0 | if(startTerm!=null) |
82 | 0 | proposalAttributes.put("prevStartTerm",startTerm); |
83 | 0 | if(endTerm!=null) |
84 | 0 | proposalAttributes.put("prevEndTerm",endTerm); |
85 | |
|
86 | 0 | properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes); |
87 | |
|
88 | 0 | courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment()); |
89 | 0 | } else { |
90 | 0 | throw new OperationFailedException("Error creating new version for course, this course is currently under modification."); |
91 | |
} |
92 | |
}else{ |
93 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
94 | |
} |
95 | |
}else{ |
96 | 0 | if (courseInfo.getId() == null){ |
97 | 0 | courseInfo = courseService.createCourse(courseInfo); |
98 | |
} else { |
99 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
100 | |
} |
101 | |
} |
102 | 0 | return courseInfo; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
protected List<ValidationResultInfo> validate(Object dto) throws Exception { |
108 | 0 | return courseService.validateCourse("OBJECT", (CourseInfo)dto); |
109 | |
} |
110 | |
|
111 | |
@Override |
112 | |
protected String getDefaultMetaDataState() { |
113 | 0 | return DEFAULT_METADATA_STATE; |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
protected String getDefaultWorkflowDocumentType() { |
118 | 0 | return LUConstants.PROPOSAL_TYPE_COURSE_CREATE; |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
protected boolean checkDocumentLevelPermissions() { |
123 | 0 | return true; |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
protected Class<?> getDtoClass() { |
128 | 0 | return CourseInfo.class; |
129 | |
} |
130 | |
|
131 | |
public void setCourseService(CourseService courseService) { |
132 | 0 | this.courseService = courseService; |
133 | 0 | } |
134 | |
|
135 | |
public void setLuService(LuService luService) { |
136 | 0 | this.luService = luService; |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){ |
143 | |
|
144 | 0 | if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){ |
145 | 0 | courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix())); |
146 | |
} |
147 | |
|
148 | |
|
149 | 0 | for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){ |
150 | 0 | if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){ |
151 | 0 | crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix())); |
152 | |
} |
153 | |
} |
154 | |
|
155 | 0 | return courseInfo; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private String calculateCourseCode(String subjectArea, String suffixNumber) { |
167 | 0 | return subjectArea + suffixNumber; |
168 | |
} |
169 | |
|
170 | |
public Boolean isLatestVersion(String versionIndId) throws Exception { |
171 | 0 | VersionDisplayInfo currentVersion = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, versionIndId); |
172 | |
|
173 | |
|
174 | 0 | SearchRequest request = new SearchRequest("lu.search.isVersionable"); |
175 | 0 | request.addParam("lu.queryParam.versionIndId", versionIndId); |
176 | 0 | request.addParam("lu.queryParam.sequenceNumber", currentVersion.getSequenceNumber().toString()); |
177 | 0 | List<String> states = new ArrayList<String>(); |
178 | 0 | states.add("Approved"); |
179 | 0 | states.add("Active"); |
180 | 0 | states.add("Draft"); |
181 | 0 | states.add("Superseded"); |
182 | 0 | request.addParam("lu.queryParam.luOptionalState", states); |
183 | 0 | SearchResult result = luService.search(request); |
184 | |
|
185 | 0 | String resultString = result.getRows().get(0).getCells().get(0).getValue(); |
186 | 0 | return "0".equals(resultString); |
187 | |
} |
188 | |
} |