1 package org.kuali.student.lum.program.server;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.kuali.student.common.ui.server.gwt.AbstractDataService;
8 import org.kuali.student.core.exceptions.DoesNotExistException;
9 import org.kuali.student.core.exceptions.InvalidParameterException;
10 import org.kuali.student.core.exceptions.MissingParameterException;
11 import org.kuali.student.core.exceptions.OperationFailedException;
12 import org.kuali.student.core.exceptions.PermissionDeniedException;
13 import org.kuali.student.core.search.dto.SearchParam;
14 import org.kuali.student.core.search.dto.SearchRequest;
15 import org.kuali.student.core.search.dto.SearchResult;
16 import org.kuali.student.core.search.dto.SearchResultCell;
17 import org.kuali.student.core.search.dto.SearchResultRow;
18 import org.kuali.student.lum.lu.service.LuService;
19 import org.kuali.student.lum.program.client.ProgramClientConstants;
20 import org.kuali.student.lum.program.dto.CoreProgramInfo;
21 import org.kuali.student.lum.program.service.ProgramService;
22
23
24
25
26 public class CoreProgramDataService extends AbstractDataService {
27
28 private static final long serialVersionUID = 1L;
29
30 private ProgramService programService;
31 private LuService luService;
32
33 @Override
34 protected String getDefaultWorkflowDocumentType() {
35 return null;
36 }
37
38 @Override
39 protected String getDefaultMetaDataState() {
40 return null;
41 }
42
43 @Override
44 protected Object get(String id) throws Exception {
45 if (id==null || id.isEmpty()){
46 return findCurrentCoreProgram();
47 } else {
48 return programService.getCoreProgram(id);
49 }
50
51 }
52
53 @Override
54 protected Object save(Object dto, Map<String, Object> properties) throws Exception {
55 if (dto instanceof CoreProgramInfo) {
56 CoreProgramInfo cpInfo = (CoreProgramInfo) dto;
57 if (cpInfo.getId() == null && cpInfo.getVersionInfo() != null) {
58 String coreVersionIndId = cpInfo.getVersionInfo().getVersionIndId();
59 cpInfo = programService.createNewCoreProgramVersion(coreVersionIndId, "New core program version");
60 } else if (cpInfo.getId() == null) {
61 cpInfo = programService.createCoreProgram(cpInfo);
62 } else {
63 cpInfo = programService.updateCoreProgram(cpInfo);
64 }
65 return cpInfo;
66 } else {
67 throw new InvalidParameterException("Only persistence of CoreProgram is supported by this DataService implementation.");
68 }
69 }
70
71 @Override
72 protected Class<?> getDtoClass() {
73 return CoreProgramInfo.class;
74 }
75
76 private CoreProgramInfo findCurrentCoreProgram() throws MissingParameterException, InvalidParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
77 SearchRequest request = new SearchRequest();
78
79
80
81 CoreProgramInfo core = new CoreProgramInfo();
82 String coreProgramId = null;
83 request.setSearchKey("lu.search.mostCurrent.union");
84
85 List<SearchParam> searchParams = new ArrayList<SearchParam>();
86 SearchParam qpv1 = new SearchParam();
87 qpv1.setKey("lu.queryParam.luOptionalType");
88 qpv1.setValue(ProgramClientConstants.CORE_PROGRAM);
89 searchParams.add(qpv1);
90
91 request.setParams(searchParams);
92
93 SearchResult searchResult = luService.search(request);
94 if (searchResult.getRows().size() > 0) {
95 for(SearchResultRow srrow : searchResult.getRows()){
96 List<SearchResultCell> srCells = srrow.getCells();
97 if(srCells != null && srCells.size() > 0){
98 for(SearchResultCell srcell : srCells){
99 if (srcell.getKey().equals("lu.resultColumn.cluId")) {
100 coreProgramId = srcell.getValue();
101 break;
102 }
103 }
104 }
105 }
106 }
107 if (coreProgramId != null) {
108 core = programService.getCoreProgram(coreProgramId);
109 }
110 return core;
111 }
112
113 public void setProgramService(ProgramService programService) {
114 this.programService = programService;
115 }
116
117 public void setLuService(LuService luService) {
118 this.luService = luService;
119 }
120 }