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.List; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.student.common.assembly.data.Data; |
23 | |
import org.kuali.student.common.dto.DtoConstants; |
24 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
25 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
26 | |
import org.kuali.student.common.ui.server.gwt.AbstractDataService; |
27 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
28 | |
import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter; |
29 | |
import org.kuali.student.lum.course.dto.CourseCrossListingInfo; |
30 | |
import org.kuali.student.lum.course.dto.CourseInfo; |
31 | |
import org.kuali.student.lum.course.service.CourseService; |
32 | |
import org.springframework.util.StringUtils; |
33 | |
|
34 | 0 | public class CourseDataService extends AbstractDataService { |
35 | |
|
36 | |
private static final long serialVersionUID = 1L; |
37 | 0 | final static Logger LOG = Logger.getLogger(CourseDataService.class); |
38 | |
|
39 | |
private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT; |
40 | |
|
41 | |
private CourseService courseService; |
42 | |
|
43 | |
|
44 | |
@Override |
45 | |
protected Object get(String id) throws Exception { |
46 | 0 | CourseInfo courseInfo = null; |
47 | |
|
48 | |
try { |
49 | 0 | courseInfo = courseService.getCourse(id); |
50 | 0 | } catch (DoesNotExistException dne) { |
51 | 0 | LOG.info("Course not found for key " + id + ". Course loaded from proposal instead."); |
52 | 0 | } |
53 | |
|
54 | 0 | return courseInfo; |
55 | |
} |
56 | |
|
57 | |
@Override |
58 | |
protected Object save(Object dto, Map<String, Object> properties) throws Exception { |
59 | 0 | CourseInfo courseInfo = (CourseInfo)dto; |
60 | |
|
61 | |
|
62 | 0 | courseInfo = calculateCourseDerivedFields(courseInfo); |
63 | |
|
64 | 0 | if(properties!=null&&"kuali.proposal.type.course.modify".equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))){ |
65 | |
|
66 | 0 | if(courseInfo.getId() == null){ |
67 | 0 | courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment()); |
68 | |
}else{ |
69 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
70 | |
} |
71 | |
}else{ |
72 | 0 | if (courseInfo.getId() == null){ |
73 | 0 | courseInfo = courseService.createCourse(courseInfo); |
74 | |
} else { |
75 | 0 | courseInfo = courseService.updateCourse(courseInfo); |
76 | |
} |
77 | |
} |
78 | 0 | return courseInfo; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
protected List<ValidationResultInfo> validate(Object dto) throws Exception { |
84 | 0 | return courseService.validateCourse("OBJECT", (CourseInfo)dto); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
protected String getDefaultMetaDataState() { |
89 | 0 | return DEFAULT_METADATA_STATE; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
protected String getDefaultWorkflowDocumentType() { |
94 | 0 | return "kuali.proposal.type.course.create"; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
protected boolean checkDocumentLevelPermissions() { |
99 | 0 | return true; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
protected Class<?> getDtoClass() { |
104 | 0 | return CourseInfo.class; |
105 | |
} |
106 | |
|
107 | |
public void setCourseService(CourseService courseService) { |
108 | 0 | this.courseService = courseService; |
109 | 0 | } |
110 | |
|
111 | |
public Data createNewCourseVersion(String courseId, String versionComment) throws OperationFailedException { |
112 | |
try { |
113 | |
|
114 | 0 | CourseInfo course = this.courseService.createNewCourseVersion(courseId, versionComment); |
115 | 0 | return getData(course.getId()); |
116 | 0 | } catch (Exception e) { |
117 | 0 | throw new OperationFailedException("Error getting data",e); |
118 | |
} |
119 | |
|
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){ |
127 | |
|
128 | 0 | if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){ |
129 | 0 | courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix())); |
130 | |
} |
131 | |
|
132 | |
|
133 | 0 | for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){ |
134 | 0 | if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){ |
135 | 0 | crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix())); |
136 | |
} |
137 | |
} |
138 | |
|
139 | 0 | return courseInfo; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private String calculateCourseCode(String subjectArea, String suffixNumber) { |
151 | 0 | return subjectArea + suffixNumber; |
152 | |
} |
153 | |
|
154 | |
} |