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.assembly.data.AssemblyException;
22  import org.kuali.student.common.assembly.data.Data;
23  import org.kuali.student.common.exceptions.DataValidationErrorException;
24  import org.kuali.student.common.search.dto.SearchRequest;
25  import org.kuali.student.common.search.dto.SearchResult;
26  import org.kuali.student.common.search.dto.SearchResultCell;
27  import org.kuali.student.common.search.dto.SearchResultRow;
28  import org.kuali.student.common.ui.client.service.DataSaveResult;
29  import org.kuali.student.common.ui.client.service.exceptions.OperationFailedException;
30  import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
31  import org.kuali.student.common.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  import edu.emory.mathcs.backport.java.util.Collections;
44  
45  public class CluSetManagementRpcGwtServlet extends DataGwtServlet implements
46  		CluSetManagementRpcService {
47  
48  	private static final long serialVersionUID = 1L;
49  	final static Logger LOG = Logger.getLogger(CluSetManagementRpcGwtServlet.class);
50  	private LuService luService;
51  	private LrcService lrcService;
52      
53  	public LuService getLuService() {
54          return luService;
55      }
56  
57      public void setLuService(LuService luService) {
58          this.luService = luService;
59      }
60  
61      public LrcService getLrcService() {
62          return lrcService;
63      }
64  
65      public void setLrcService(LrcService lrcService) {
66          this.lrcService = lrcService;
67      }
68      
69      @Override
70      public Data getData(String id) throws OperationFailedException {
71          try{
72              return getDataService().getData(id);
73          } catch (Exception e) {
74              LOG.error("Could not get Data ", e);
75              throw new OperationFailedException("Failed to get data");
76          }
77      }
78  
79      @Override
80      public DataSaveResult saveData(Data data) throws OperationFailedException {
81          try{
82              return getDataService().saveData(data);
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                         CluInformation cluInformation = new CluInformation();
219                         if (cluInfo.getOfficialIdentifier() != null) {
220                             cluInformation.setCode(cluInfo.getOfficialIdentifier().getCode());
221                             cluInformation.setTitle(cluInfo.getOfficialIdentifier().getShortName());
222                             cluInformation.setCredits(credits);
223                         }
224                         
225                         cluInformation.setType(cluInfo.getType());
226                         //If the clu type is variation, get the parent clu id. 
227                         if ("kuali.lu.type.Variation".equals(cluInfo.getType())){
228                             List<String> clus = luService.getCluIdsByRelation(cluInfo.getId(), "kuali.lu.lu.relation.type.hasVariationProgram");
229                             if (clus == null || clus.size() == 0){ 
230                                 throw new RuntimeException("Statement Dependency clu found, but no parent Program exists"); 
231                             } else if(clus.size()>1){ 
232                                 throw new RuntimeException("Statement Dependency clu can only have one parent Program relation"); 
233                             }
234                             cluInformation.setParentCluId(clus.get(0));
235                         }
236                         
237                         cluInformation.setVerIndependentId(cluInfo.getId());
238                         result.add(cluInformation);
239                     }
240                 } catch (Exception e) {
241                     throw new OperationFailedException("Failed to get info for cluId " + cluId, e);
242                 }
243             }
244         }
245         return result;
246     }
247 
248     @Override
249     public CluSetInformation getCluSetInformation(String cluSetId) throws OperationFailedException {
250         CluSetInformation result = new CluSetInformation();
251         CluSetInfo cluSetInfo = getCluSetInfo(cluSetId);
252         List<String> allCluIds = cluSetInfo.getCluIds();
253         List<String> cluSetIds =  cluSetInfo.getCluSetIds();
254         final MembershipQueryInfo membershipQueryInfo = cluSetInfo.getMembershipQuery();
255         result.setId(cluSetId);
256         if (allCluIds != null) {
257             List<CluInformation> clus = getCluInformations(allCluIds);
258             result.setClus(clus);
259         }
260         if (cluSetIds != null) {
261             List<CluSetInfo> cluSetInfos = getCluSetInfos(cluSetIds);
262             result.setCluSets(cluSetInfos);
263         }
264         if (membershipQueryInfo != null) {
265             SearchRequest searchRequest = new SearchRequest();
266             searchRequest.setSearchKey(membershipQueryInfo.getSearchTypeKey());
267             searchRequest.setParams(membershipQueryInfo.getQueryParamValueList());
268             SearchResult searchResult = null;
269             try {
270                 searchResult = luService.search(searchRequest);
271             } catch (Exception e) {
272                 throw new OperationFailedException("Failed to search for clus in clu range", e);
273             }
274             List<CluInformation> clusInRange = new ArrayList<CluInformation>();
275             List<SearchResultRow> rows = searchResult.getRows();
276             for(SearchResultRow row : rows) {
277                 List<SearchResultCell> cells = row.getCells();
278                 CluInformation cluInformation = new CluInformation();
279                 for(SearchResultCell cell : cells) {
280                     if(cell.getKey().equals("lu.resultColumn.cluId")) {
281                         cluInformation.setVerIndependentId(cell.getValue());
282                     }
283                     if (cell.getKey().equals("lu.resultColumn.luOptionalCode")) {
284                         cluInformation.setCode(cell.getValue());
285                     }
286                     if (cell.getKey().equals("lu.resultColumn.luOptionalShortName")) {
287                         cluInformation.setTitle(cell.getValue());
288                     }
289                 }
290                 clusInRange.add(cluInformation);
291             }
292             result.setMembershipQueryInfo(membershipQueryInfo);
293             result.setClusInRange(clusInRange);
294         }
295         if(result.getClus()!=null)
296         	Collections.sort(result.getClus());
297         return result;
298     }
299 	
300 }