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