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 org.apache.log4j.Logger;
19  import org.kuali.student.common.ui.client.service.DataSaveResult;
20  import org.kuali.student.common.ui.client.service.exceptions.OperationFailedException;
21  import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
22  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
23  import org.kuali.student.r2.core.search.dto.SearchResultCellInfo;
24  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
25  import org.kuali.student.r2.core.search.dto.SearchResultRowInfo;
26  import org.kuali.student.r2.lum.lrc.dto.ResultValueRangeInfo;
27  import org.kuali.student.r2.lum.lrc.service.LRCService;
28  import org.kuali.student.lum.common.client.widgets.CluInformation;
29  import org.kuali.student.lum.common.client.widgets.CluSetInformation;
30  import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcService;
31  import org.kuali.student.r1.common.assembly.data.AssemblyException;
32  import org.kuali.student.r1.common.assembly.data.Data;
33  import org.kuali.student.r2.common.dto.AttributeInfo;
34  import org.kuali.student.r2.common.dto.ContextInfo;
35  import org.kuali.student.r2.common.util.ContextUtils;
36  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
37  import org.kuali.student.r2.lum.clu.dto.*;
38  import org.kuali.student.r2.lum.clu.service.CluService;
39  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
40  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.List;
45  
46  public class CluSetManagementRpcGwtServlet extends DataGwtServlet implements
47  		CluSetManagementRpcService {
48  
49  	private static final long serialVersionUID = 1L;
50  	final static Logger LOG = Logger.getLogger(CluSetManagementRpcGwtServlet.class);
51  	private CluService cluService;
52  	private LRCService lrcService;
53      
54  	public CluService getCluService() {
55          return cluService;
56      }
57  
58      public void setCluService(CluService cluService) {
59          this.cluService = cluService;
60      }
61  
62      public LRCService getLrcService() {
63          return lrcService;
64      }
65  
66      public void setLrcService(LRCService lrcService) {
67          this.lrcService = lrcService;
68      }
69      
70      @Override
71      public Data getData(String id) throws OperationFailedException {
72          try{
73              return getDataService().getData(id, ContextUtils.getContextInfo());
74          } catch (Exception e) {
75              LOG.error("Could not get Data ", e);
76              throw new OperationFailedException("Failed to get data");
77          }
78      }
79  
80      @Override
81      public DataSaveResult saveData(Data data) throws OperationFailedException {
82          try{
83              return getDataService().saveData(data, ContextUtils.getContextInfo());
84          } catch (Exception e) {
85              LOG.error("Could not save data ", e);
86              throw new OperationFailedException("Failed to save data");
87          } 
88      }
89  
90      private CluSetInfo getCluSetInfo(String cluSetId, ContextInfo contextInfo) throws OperationFailedException {
91          List<String> cluIds = null;
92          CluSetInfo cluSetInfo = null;
93          try {
94              // note: the cluIds returned by cluService.getCluSetInfo also contains the clus
95              //       that are the result of query parameter search.  Set to null here and
96              //       retrieve the clus that are direct members.
97              cluSetInfo = cluService.getCluSet(cluSetId, contextInfo);
98              cluSetInfo.setCluIds(null);
99              cluIds = cluService.getCluIdsFromCluSet(cluSetId, contextInfo);
100             cluSetInfo.setCluIds(cluIds);
101             upWrap(cluSetInfo, contextInfo);
102         } catch (Exception e) {
103             throw new OperationFailedException("Failed to retrieve cluset info for " + cluSetId, e);
104         }
105         return cluSetInfo;
106     }
107     
108     private List<CluSetInfo> getCluSetInfos(List<String> cluSetIds, ContextInfo contextInfo) throws OperationFailedException {
109         List<CluSetInfo> clusetInfos = null;
110         if (cluSetIds != null) {
111             for (String cluSetId : cluSetIds) {
112                 clusetInfos = (clusetInfos == null)? new ArrayList<CluSetInfo>() : clusetInfos;
113                 clusetInfos.add(getCluSetInfo(cluSetId, contextInfo));
114             }
115         }
116         return clusetInfos;
117     }
118 
119     private void upWrap(CluSetInfo cluSetInfo, ContextInfo contextInfo) throws AssemblyException {
120         List<String> cluSetIds = (cluSetInfo == null)? null : cluSetInfo.getCluSetIds();
121         List<String> unWrappedCluSetIds = null;
122         List<CluSetInfo> wrappedCluSets = null;
123         List<CluSetInfo> subCluSets = null;
124 
125         try {
126             if (cluSetIds != null && !cluSetIds.isEmpty()) {
127                 subCluSets = cluService.getCluSetsByIds(cluSetIds, contextInfo);
128             }
129         } catch (Exception e) {
130             LOG.error(e.getMessage(), e);
131             throw new AssemblyException("Failed to retrieve the sub clusets of cluset " +
132                     cluSetInfo.getId());
133         }
134         // goes through the list of sub clusets and ignore the ones that are not reusable
135         if (subCluSets != null) {
136             for (CluSetInfo subCluSet : subCluSets) {
137                 if (subCluSet.getIsReusable()) {
138                     unWrappedCluSetIds = (unWrappedCluSetIds == null)?
139                             new ArrayList<String>() : unWrappedCluSetIds;
140                             unWrappedCluSetIds.add(subCluSet.getId());
141                 } else {
142                     wrappedCluSets = (wrappedCluSets == null)?
143                             new ArrayList<CluSetInfo>() : wrappedCluSets;
144                             wrappedCluSets.add(subCluSet);
145                 }
146             }
147         }
148         cluSetInfo.setCluSetIds(unWrappedCluSetIds);
149         if (wrappedCluSets != null) {
150             for (CluSetInfo wrappedCluSet : wrappedCluSets) {
151                 MembershipQueryInfo mqInfo = wrappedCluSet.getMembershipQuery();
152                 if (wrappedCluSet.getCluIds() != null && !wrappedCluSet.getCluIds().isEmpty()) {
153                     cluSetInfo.setCluIds(wrappedCluSet.getCluIds());
154                 }
155                 if (mqInfo != null && mqInfo.getSearchTypeKey() != null && !mqInfo.getSearchTypeKey().isEmpty()) {
156                     cluSetInfo.setMembershipQuery(mqInfo);
157                 }
158             }
159         }
160     }
161     
162     private List<CluInformation> getCluInformations(List<String> cluIds, ContextInfo contextInfo) throws OperationFailedException {
163         List<CluInformation> result = new ArrayList<CluInformation>();
164         if (cluIds != null) {
165             for (String cluId : cluIds) {
166                 try {
167                 	VersionDisplayInfo versionInfo = cluService.getCurrentVersion(CluServiceConstants.CLU_NAMESPACE_URI, cluId, contextInfo);
168                 	CluInfo cluInfo = cluService.getClu(versionInfo.getId(), contextInfo);
169                     if (cluInfo != null) {
170 
171                         //retrieve credits
172                         String credits = "";
173                         List<CluResultInfo> cluResultInfos = cluService.getCluResultByClu(versionInfo.getId(), contextInfo);
174                         if (cluResultInfos != null) {
175                             for (CluResultInfo cluResultInfo : cluResultInfos) {
176                                 String cluType = cluResultInfo.getTypeKey();
177 
178                                 //ignore non-credit results
179                                 if ((cluType == null) || (!cluType.equals("kuali.resultType.creditCourseResult"))) {
180                                     continue;
181                                 }
182 
183                                 //retrieve credit type and credit values
184                                 ResultValuesGroupInfo resultComponentInfo = null;
185                                 List<String> resultValues = null;
186                                 String creditType = "";
187                                 if (cluResultInfo.getResultOptions() != null) {
188                                     for (ResultOptionInfo resultOption : cluResultInfo.getResultOptions()) {
189                                         if (resultOption.getResultComponentId() != null) {
190                                             resultComponentInfo = lrcService.getResultValuesGroup(resultOption.getResultComponentId(), contextInfo);
191                                             resultValues = resultComponentInfo.getResultValueKeys();
192                                             creditType = resultComponentInfo.getTypeKey();
193                                             break;
194                                         }
195                                     }
196                                 }
197                                 if (resultValues == null) {
198                                     continue;
199                                 }
200 
201                                 if (!credits.isEmpty()) {
202                                     credits = credits + "; ";
203                                 }
204 
205                                 if (creditType.equals("kuali.result.values.group.type.fixed")) {
206                                     credits = credits + resultValues.get(0).substring(33);
207                                 } else if (creditType.equals("kuali.result.values.group.type.multiple")) {
208                                     boolean firstValue = true;
209                                     for (String resultValue : resultValues) {
210                                         credits = credits + (firstValue ? "" :", ")  + resultValue.substring(33);
211                                         firstValue = false;
212                                     }
213                                 } else if (creditType.equals("kuali.result.values.group.type.range")) {
214                                     String minCredits = resultComponentInfo.getResultValueRange().getMinValue();
215                                     String maxCredits = resultComponentInfo.getResultValueRange().getMaxValue();
216                                     credits += minCredits + " - " + maxCredits;
217                                 }
218                             }
219                         }
220                         
221                         CluInformation cluInformation = new CluInformation();
222                         if (cluInfo.getOfficialIdentifier() != null) {
223                             cluInformation.setCode(cluInfo.getOfficialIdentifier().getCode());
224                             cluInformation.setTitle(cluInfo.getOfficialIdentifier().getShortName());
225                             cluInformation.setCredits(credits);
226                         }
227                         
228                         cluInformation.setType(cluInfo.getTypeKey());
229                         //If the clu type is variation, get the parent clu id. 
230                         if ("kuali.lu.type.Variation".equals(cluInfo.getTypeKey())){
231                             List<String> clus = cluService.getCluIdsByRelatedCluAndRelationType(cluInfo.getId(), "kuali.lu.lu.relation.type.hasVariationProgram", contextInfo);
232                             if (clus == null || clus.size() == 0){ 
233                                 throw new RuntimeException("Statement Dependency clu found, but no parent Program exists"); 
234                             } else if(clus.size()>1){ 
235                                 throw new RuntimeException("Statement Dependency clu can only have one parent Program relation"); 
236                             }
237                             cluInformation.setParentCluId(clus.get(0));
238                         }
239                         
240                         cluInformation.setVerIndependentId(cluInfo.getId());
241                         result.add(cluInformation);
242                     }
243                 } catch (Exception e) {
244                     throw new OperationFailedException("Failed to get info for cluId " + cluId, e);
245                 }
246             }
247         }
248         return result;
249     }
250 
251     @Override
252     public CluSetInformation getCluSetInformation(String cluSetId) throws OperationFailedException {
253         CluSetInformation result = new CluSetInformation();
254         CluSetInfo cluSetInfo = getCluSetInfo(cluSetId, ContextUtils.getContextInfo());
255         List<String> allCluIds = cluSetInfo.getCluIds();
256         List<String> cluSetIds =  cluSetInfo.getCluSetIds();
257         final MembershipQueryInfo membershipQueryInfo = cluSetInfo.getMembershipQuery();
258         result.setId(cluSetId);
259         if (allCluIds != null) {
260             List<CluInformation> clus = getCluInformations(allCluIds, ContextUtils.getContextInfo());
261             result.setClus(clus);
262         }
263         if (cluSetIds != null) {
264             List<CluSetInfo> cluSetInfos = getCluSetInfos(cluSetIds, ContextUtils.getContextInfo());
265             result.setCluSets(cluSetInfos);
266         }
267         if (membershipQueryInfo != null) {
268             SearchRequestInfo searchRequest = new SearchRequestInfo();
269             searchRequest.setSearchKey(membershipQueryInfo.getSearchTypeKey());
270             searchRequest.setParams(membershipQueryInfo.getQueryParamValues());
271             SearchResultInfo searchResult = null;
272             try {
273                 searchResult = cluService.search(searchRequest, ContextUtils.getContextInfo());
274             } catch (Exception e) {
275                 throw new OperationFailedException("Failed to search for clus in clu range", e);
276             }
277             List<CluInformation> clusInRange = new ArrayList<CluInformation>();
278             List<SearchResultRowInfo> rows = searchResult.getRows();
279             for(SearchResultRowInfo row : rows) {
280                 List<SearchResultCellInfo> cells = row.getCells();
281                 CluInformation cluInformation = new CluInformation();
282                 for(SearchResultCellInfo cell : cells) {
283                     if(cell.getKey().equals("lu.resultColumn.cluId")) {
284                         cluInformation.setVerIndependentId(cell.getValue());
285                     }
286                     if (cell.getKey().equals("lu.resultColumn.luOptionalCode")) {
287                         cluInformation.setCode(cell.getValue());
288                     }
289                     if (cell.getKey().equals("lu.resultColumn.luOptionalShortName")) {
290                         cluInformation.setTitle(cell.getValue());
291                     }
292                 }
293                 clusInRange.add(cluInformation);
294             }
295             result.setMembershipQueryInfo(membershipQueryInfo);
296             result.setClusInRange(clusInRange);
297         }
298         if(result.getClus()!=null)
299         	Collections.sort(result.getClus());
300         return result;
301     }
302 
303 }