1 package org.kuali.student.lum.program.server;
2
3 import org.kuali.student.common.ui.server.gwt.AbstractDataService;
4 import org.kuali.student.core.exceptions.InvalidParameterException;
5 import org.kuali.student.core.exceptions.OperationFailedException;
6 import org.kuali.student.lum.lu.service.LuService;
7 import org.kuali.student.lum.program.client.ProgramClientConstants;
8 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
9 import org.kuali.student.lum.program.service.ProgramService;
10
11 import java.util.List;
12 import java.util.Map;
13
14
15
16
17 public class MajorDisciplineDataService extends AbstractDataService {
18
19 private static final long serialVersionUID = 1L;
20
21 private ProgramService programService;
22 private LuService luService;
23
24 @Override
25 protected String getDefaultWorkflowDocumentType() {
26 return null;
27 }
28
29 @Override
30 protected String getDefaultMetaDataState() {
31 return null;
32 }
33
34 @Override
35 protected Object get(String id) throws Exception {
36
37 MajorDisciplineInfo returnDTO;
38 if (null == id || id.length() == 0) {
39 returnDTO = new MajorDisciplineInfo();
40 returnDTO.setType(ProgramClientConstants.MAJOR_PROGRAM);
41 returnDTO.setState(ProgramClientConstants.STATE_DRAFT);
42 returnDTO.setCredentialProgramId(getCredentialId());
43 } else {
44 returnDTO = programService.getMajorDiscipline(id);
45 }
46 return returnDTO;
47 }
48
49 @Override
50 protected Object save(Object dto, Map<String, Object> properties) throws Exception {
51 if (dto instanceof MajorDisciplineInfo) {
52 MajorDisciplineInfo mdInfo = (MajorDisciplineInfo) dto;
53 if (mdInfo.getId() == null && mdInfo.getVersionInfo() != null) {
54 String majorVersionIndId = mdInfo.getVersionInfo().getVersionIndId();
55 mdInfo = programService.createNewMajorDisciplineVersion(majorVersionIndId, "New major discipline version");
56 } else if (mdInfo.getId() == null){
57 mdInfo = programService.createMajorDiscipline(mdInfo);
58 } else {
59 mdInfo = programService.updateMajorDiscipline(mdInfo);
60 }
61 return mdInfo;
62 } else {
63 throw new InvalidParameterException("Only persistence of MajorDiscipline is supported by this DataService implementation.");
64 }
65 }
66
67 @Override
68 protected Class<?> getDtoClass() {
69 return MajorDisciplineInfo.class;
70 }
71
72 private String getCredentialId() throws Exception {
73
74 List<String> credIds = luService.getCluIdsByLuType(ProgramClientConstants.CREDENTIAL_BACCALAUREATE_PROGRAM, ProgramClientConstants.STATE_ACTIVE);
75 if (null == credIds || credIds.size() != 1) {
76 throw new OperationFailedException("A single credential program of type " + ProgramClientConstants.CREDENTIAL_BACCALAUREATE_PROGRAM + " is required; database contains " +
77 (null == credIds ? "0" : credIds.size() +
78 "."));
79 }
80 return credIds.get(0);
81 }
82
83 public void setProgramService(ProgramService programService) {
84 this.programService = programService;
85 }
86
87 public void setLuService(LuService luService) {
88 this.luService = luService;
89 }
90
91 }