View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.lum.lu.ui.tools.server.gwt;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.student.common.ui.client.service.DataSaveResult;
22  import org.kuali.student.common.ui.client.service.exceptions.OperationFailedException;
23  import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
24  import org.kuali.student.core.assembly.data.AssemblyException;
25  import org.kuali.student.core.assembly.data.Data;
26  import org.kuali.student.core.exceptions.DataValidationErrorException;
27  import org.kuali.student.core.search.dto.SearchRequest;
28  import org.kuali.student.core.search.dto.SearchResult;
29  import org.kuali.student.core.search.dto.SearchResultCell;
30  import org.kuali.student.core.search.dto.SearchResultRow;
31  import org.kuali.student.core.versionmanagement.dto.VersionDisplayInfo;
32  import org.kuali.student.lum.common.client.widgets.CluInformation;
33  import org.kuali.student.lum.common.client.widgets.CluSetInformation;
34  import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcService;
35  import org.kuali.student.lum.lrc.dto.ResultComponentInfo;
36  import org.kuali.student.lum.lrc.service.LrcService;
37  import org.kuali.student.lum.lu.dto.*;
38  import org.kuali.student.lum.lu.service.LuService;
39  import org.kuali.student.lum.lu.service.LuServiceConstants;
40  
41  import org.apache.log4j.Logger;
42  
43  public class CluSetManagementRpcGwtServlet extends DataGwtServlet implements
44  		CluSetManagementRpcService {
45  
46  	private static final long serialVersionUID = 1L;
47  	final static Logger LOG = Logger.getLogger(CluSetManagementRpcGwtServlet.class);
48  	private LuService luService;
49  	private LrcService lrcService;
50      
51  	public LuService getLuService() {
52          return luService;
53      }
54  
55      public void setLuService(LuService luService) {
56          this.luService = luService;
57      }
58  
59      public LrcService getLrcService() {
60          return lrcService;
61      }
62  
63      public void setLrcService(LrcService lrcService) {
64          this.lrcService = lrcService;
65      }
66      
67      @Override
68      public Data getData(String id) throws OperationFailedException {
69          try{
70              return getDataService().getData(id);
71          } catch (Exception e) {
72              LOG.error("Could not get Data ", e);
73              throw new OperationFailedException("Failed to get data");
74          }
75      }
76  
77      @Override
78      public DataSaveResult saveData(Data data) throws OperationFailedException {
79          try{
80              return getDataService().saveData(data);
81          }catch (DataValidationErrorException dvee){
82              return new DataSaveResult(dvee.getValidationResults(), null);
83          } catch (Exception e) {
84              LOG.error("Could not save data ", e);
85              throw new OperationFailedException("Failed to save data");
86          } 
87      }
88  
89      private CluSetInfo getCluSetInfo(String cluSetId) throws OperationFailedException {
90          List<String> cluIds = null;
91          CluSetInfo cluSetInfo = null;
92          try {
93              // note: the cluIds returned by luService.getCluSetInfo also contains the clus
94              //       that are the result of query parameter search.  Set to null here and
95              //       retrieve the clus that are direct members.
96              cluSetInfo = luService.getCluSetInfo(cluSetId);
97              cluSetInfo.setCluIds(null);
98              cluIds = luService.getCluIdsFromCluSet(cluSetId);
99              cluSetInfo.setCluIds(cluIds);
100             upWrap(cluSetInfo);
101         } catch (Exception e) {
102             throw new OperationFailedException("Failed to retrieve cluset info for " + cluSetId, e);
103         }
104         return cluSetInfo;
105     }
106     
107     private List<CluSetInfo> getCluSetInfos(List<String> cluSetIds) throws OperationFailedException {
108         List<CluSetInfo> clusetInfos = null;
109         if (cluSetIds != null) {
110             for (String cluSetId : cluSetIds) {
111                 clusetInfos = (clusetInfos == null)? new ArrayList<CluSetInfo>() : clusetInfos;
112                 clusetInfos.add(getCluSetInfo(cluSetId));
113             }
114         }
115         return clusetInfos;
116     }
117 
118     private void upWrap(CluSetInfo cluSetInfo) throws AssemblyException {
119         List<String> cluSetIds = (cluSetInfo == null)? null : cluSetInfo.getCluSetIds();
120         List<String> unWrappedCluSetIds = null;
121         List<CluSetInfo> wrappedCluSets = null;
122         List<CluSetInfo> subCluSets = null;
123 
124         try {
125             if (cluSetIds != null && !cluSetIds.isEmpty()) {
126                 subCluSets = luService.getCluSetInfoByIdList(cluSetIds);
127             }
128         } catch (Exception e) {
129             LOG.error(e.getMessage(), e);
130             throw new AssemblyException("Failed to retrieve the sub clusets of cluset " +
131                     cluSetInfo.getId());
132         }
133         // goes through the list of sub clusets and ignore the ones that are not reusable
134         if (subCluSets != null) {
135             for (CluSetInfo subCluSet : subCluSets) {
136                 if (subCluSet.getIsReusable()) {
137                     unWrappedCluSetIds = (unWrappedCluSetIds == null)?
138                             new ArrayList<String>() : unWrappedCluSetIds;
139                             unWrappedCluSetIds.add(subCluSet.getId());
140                 } else {
141                     wrappedCluSets = (wrappedCluSets == null)?
142                             new ArrayList<CluSetInfo>() : wrappedCluSets;
143                             wrappedCluSets.add(subCluSet);
144                 }
145             }
146         }
147         cluSetInfo.setCluSetIds(unWrappedCluSetIds);
148         if (wrappedCluSets != null) {
149             for (CluSetInfo wrappedCluSet : wrappedCluSets) {
150                 MembershipQueryInfo mqInfo = wrappedCluSet.getMembershipQuery();
151                 if (wrappedCluSet.getCluIds() != null && !wrappedCluSet.getCluIds().isEmpty()) {
152                     cluSetInfo.setCluIds(wrappedCluSet.getCluIds());
153                 }
154                 if (mqInfo != null && mqInfo.getSearchTypeKey() != null && !mqInfo.getSearchTypeKey().isEmpty()) {
155                     cluSetInfo.setMembershipQuery(mqInfo);
156                 }
157             }
158         }
159     }
160     
161     private List<CluInformation> getCluInformations(List<String> cluIds) throws OperationFailedException {
162         List<CluInformation> result = new ArrayList<CluInformation>();
163         if (cluIds != null) {
164             for (String cluId : cluIds) {
165                 try {
166                 	VersionDisplayInfo versionInfo = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, cluId);
167                     CluInfo cluInfo = luService.getClu(versionInfo.getId());
168                     if (cluInfo != null) {
169 
170                         //retrieve credits
171                         String credits = "";
172                         List<CluResultInfo> cluResultInfos = luService.getCluResultByClu(versionInfo.getId());
173                         if (cluResultInfos != null) {
174                             for (CluResultInfo cluResultInfo : cluResultInfos) {
175                                 String cluType = cluResultInfo.getType();
176 
177                                 //ignore non-credit results
178                                 if ((cluType == null) || (!cluType.equals("kuali.resultType.creditCourseResult"))) {
179                                     continue;
180                                 }
181 
182                                 //retrieve credit type and credit values
183                                 ResultComponentInfo resultComponentInfo = null;
184                                 List<String> resultValues = null;
185                                 String creditType = "";
186                                 if (cluResultInfo.getResultOptions() != null) {
187                                     for (ResultOptionInfo resultOption : cluResultInfo.getResultOptions()) {
188                                         if (resultOption.getResultComponentId() != null) {
189                                             resultComponentInfo = lrcService.getResultComponent(resultOption.getResultComponentId());
190                                             resultValues = resultComponentInfo.getResultValues();
191                                             creditType = resultComponentInfo.getType();
192                                             break;
193                                         }
194                                     }
195                                 }
196                                 if (resultValues == null) {
197                                     continue;
198                                 }
199 
200                                 if (!credits.isEmpty()) {
201                                     credits = credits + "; ";
202                                 }
203 
204                                 if (creditType.equals("kuali.resultComponentType.credit.degree.fixed")) {
205                                     credits = credits + resultValues.get(0);
206                                 } else if (creditType.equals("kuali.resultComponentType.credit.degree.multiple")) {
207                                     boolean firstValue = true;
208                                     for (String resultValue : resultValues) {
209                                         credits = credits + (firstValue ? "" :", ")  + resultValue;
210                                         firstValue = false;
211                                     }
212                                 } else if (creditType.equals("kuali.resultComponentType.credit.degree.range")) {
213                                     credits = credits + resultComponentInfo.getAttributes().get("minCreditValue") + " - " + resultComponentInfo.getAttributes().get("maxCreditValue");
214                                 }
215                             }
216                         }
217                         
218 
219                         CluInformation cluInformation = new CluInformation();
220                         if (cluInfo.getOfficialIdentifier() != null) {
221                             cluInformation.setCode(cluInfo.getOfficialIdentifier().getCode());
222                             cluInformation.setTitle(cluInfo.getOfficialIdentifier().getShortName());
223                             cluInformation.setCredits(credits);
224                         }
225                         cluInformation.setVerIndependentId(cluInfo.getId());
226                         result.add(cluInformation);
227                     }
228                 } catch (Exception e) {
229                     throw new OperationFailedException("Failed to get info for cluId " + cluId, e);
230                 }
231             }
232         }
233         return result;
234     }
235 
236     @Override
237     public CluSetInformation getCluSetInformation(String cluSetId) throws OperationFailedException {
238         CluSetInformation result = new CluSetInformation();
239         CluSetInfo cluSetInfo = getCluSetInfo(cluSetId);
240         List<String> allCluIds = cluSetInfo.getCluIds();
241         List<String> cluSetIds =  cluSetInfo.getCluSetIds();
242         final MembershipQueryInfo membershipQueryInfo = cluSetInfo.getMembershipQuery();
243         result.setId(cluSetId);
244         if (allCluIds != null) {
245             List<CluInformation> clus = getCluInformations(allCluIds);
246             result.setClus(clus);
247         }
248         if (cluSetIds != null) {
249             List<CluSetInfo> cluSetInfos = getCluSetInfos(cluSetIds);
250             result.setCluSets(cluSetInfos);
251         }
252         if (membershipQueryInfo != null) {
253             SearchRequest searchRequest = new SearchRequest();
254             searchRequest.setSearchKey(membershipQueryInfo.getSearchTypeKey());
255             searchRequest.setParams(membershipQueryInfo.getQueryParamValueList());
256             SearchResult searchResult = null;
257             try {
258                 searchResult = luService.search(searchRequest);
259             } catch (Exception e) {
260                 throw new OperationFailedException("Failed to search for clus in clu range", e);
261             }
262             List<CluInformation> clusInRange = new ArrayList<CluInformation>();
263             List<SearchResultRow> rows = searchResult.getRows();
264             for(SearchResultRow row : rows) {
265                 List<SearchResultCell> cells = row.getCells();
266                 CluInformation cluInformation = new CluInformation();
267                 for(SearchResultCell cell : cells) {
268                     if(cell.getKey().equals("lu.resultColumn.cluId")) {
269                         cluInformation.setVerIndependentId(cell.getValue());
270                     }
271                     if (cell.getKey().equals("lu.resultColumn.luOptionalCode")) {
272                         cluInformation.setCode(cell.getValue());
273                     }
274                     if (cell.getKey().equals("lu.resultColumn.luOptionalShortName")) {
275                         cluInformation.setTitle(cell.getValue());
276                     }
277                 }
278                 clusInRange.add(cluInformation);
279             }
280             result.setMembershipQueryInfo(membershipQueryInfo);
281             result.setClusInRange(clusInRange);
282         }
283         return result;
284     }
285 	
286 }