View Javadoc

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.lum.program.client.ProgramClientConstants;
9   import org.kuali.student.r1.common.assembly.data.Data;
10  import org.kuali.student.r1.common.search.dto.SearchParam;
11  import org.kuali.student.r1.common.search.dto.SearchRequest;
12  import org.kuali.student.r1.common.search.dto.SearchResult;
13  import org.kuali.student.r1.common.search.dto.SearchResultCell;
14  import org.kuali.student.r1.common.search.dto.SearchResultRow;
15  import org.kuali.student.r2.common.dto.ContextInfo;
16  import org.kuali.student.r2.common.dto.ValidationResultInfo;
17  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
18  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
19  import org.kuali.student.r2.common.exceptions.MissingParameterException;
20  import org.kuali.student.r2.common.exceptions.OperationFailedException;
21  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
22  import org.kuali.student.r2.common.util.ContextUtils;
23  import org.kuali.student.r2.lum.clu.service.CluService;
24  import org.kuali.student.r2.lum.program.dto.CoreProgramInfo;
25  import org.kuali.student.r2.lum.program.service.ProgramService;
26  
27  /**
28   * @author Jim
29   */
30  public class CoreProgramDataService extends AbstractDataService {
31  
32      private static final long serialVersionUID = 1L;
33      
34      private ProgramService programService;
35      private CluService cluService;
36  
37      @Override
38      protected String getDefaultWorkflowDocumentType() {
39          return null;
40      }
41  
42      @Override
43      protected String getDefaultMetaDataState() {
44          return null;
45      }
46  
47      @Override
48      protected Object get(String id,ContextInfo contextInfo) throws Exception {
49      	if (id==null || id.isEmpty()){
50              return findCurrentCoreProgram();
51      	} else {
52      		return programService.getCoreProgram(id,contextInfo);
53      	}
54  
55      }
56  
57      @Override
58      protected Object save(Object dto, Map<String, Object> properties,ContextInfo contextInfo) throws Exception {
59          if (dto instanceof CoreProgramInfo) {
60              CoreProgramInfo cpInfo = (CoreProgramInfo) dto;
61              if (cpInfo.getId() == null && cpInfo.getVersion() != null) {
62              	String coreVersionIndId = cpInfo.getVersion().getVersionIndId();
63              	cpInfo = programService.createNewCoreProgramVersion(coreVersionIndId, "New core program version", contextInfo);
64              } else if (cpInfo.getId() == null) {
65                  cpInfo = programService.createCoreProgram(cpInfo.getTypeKey(), cpInfo,contextInfo);
66              } else {
67              	cpInfo = programService.updateCoreProgram(cpInfo.getId(), cpInfo.getTypeKey(), cpInfo, contextInfo);
68              }
69              return cpInfo;
70          } else {
71              throw new InvalidParameterException("Only persistence of CoreProgram is supported by this DataService implementation.");
72          }
73      }
74  
75      @Override
76  	protected List<ValidationResultInfo> validate(Object dto,ContextInfo contextInfo) throws Exception {
77  		return programService.validateCoreProgram("OBJECT", (CoreProgramInfo)dto,ContextUtils.getContextInfo());
78  	}
79      
80      @Override
81      protected Class<?> getDtoClass() {
82          return CoreProgramInfo.class;
83      }
84  
85      private CoreProgramInfo findCurrentCoreProgram() throws MissingParameterException, InvalidParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
86          	    SearchRequest request = new SearchRequest();
87  
88          //TODO find a better way to get this search, param and resultcolumn names
89          
90          CoreProgramInfo core = new CoreProgramInfo();
91          String coreProgramId = null;
92  	    request.setSearchKey("lu.search.mostCurrent.union");
93  
94          List<SearchParam> searchParams = new ArrayList<SearchParam>();
95          SearchParam qpv1 = new SearchParam();
96          qpv1.setKey("lu.queryParam.luOptionalType");
97          qpv1.setValue(ProgramClientConstants.CORE_PROGRAM);
98          searchParams.add(qpv1);
99  
100         request.setParams(searchParams);
101 
102         SearchResult searchResult = null;
103         searchResult = cluService.search(request);
104         if (searchResult.getRows().size() > 0) {
105             for(SearchResultRow srrow : searchResult.getRows()){
106                 List<SearchResultCell> srCells = srrow.getCells();
107                 if(srCells != null && srCells.size() > 0){
108                     for(SearchResultCell srcell : srCells){
109                         if (srcell.getKey().equals("lu.resultColumn.cluId")) {
110                             coreProgramId = srcell.getValue();
111                             break;
112                         }
113                     }
114                 }
115             }
116         }
117         if (coreProgramId != null) {
118             core = programService.getCoreProgram(coreProgramId, ContextUtils.getContextInfo());
119         }
120         return core;
121     }
122 
123     public void setProgramService(ProgramService programService) {
124         this.programService = programService;
125     }
126 
127     public void setCluService(CluService cluService) {
128         this.cluService = cluService;
129     }
130 
131     
132   
133 }