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.course.server.gwt;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.student.common.dto.StatusInfo;
24  import org.kuali.student.common.search.dto.SearchRequest;
25  import org.kuali.student.common.search.dto.SearchResult;
26  import org.kuali.student.common.ui.client.service.DataSaveResult;
27  import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
28  import org.kuali.student.core.statement.dto.ReqComponentInfo;
29  import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
30  import org.kuali.student.core.statement.service.StatementService;
31  import org.kuali.student.core.statement.ui.client.widgets.rules.ReqComponentInfoUi;
32  import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil;
33  import org.kuali.student.lum.common.server.StatementUtil;
34  import org.kuali.student.lum.course.service.CourseService;
35  import org.kuali.student.lum.lu.service.LuService;
36  import org.kuali.student.lum.lu.ui.course.client.requirements.CourseRequirementsDataModel;
37  import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcService;
38  
39  public class CourseRpcGwtServlet extends DataGwtServlet implements CourseRpcService {
40  
41  	private static final long serialVersionUID = 1L;
42  
43      private CourseService courseService;
44      private LuService luService;
45  	private StatementService statementService;
46  	private CourseStateChangeServiceImpl stateChangeService;
47  
48      @Override
49      public List<StatementTreeViewInfo> getCourseStatements(String courseId, String nlUsageTypeKey, String language) throws Exception {
50          List<StatementTreeViewInfo> rules = courseService.getCourseStatements(courseId, nlUsageTypeKey, language);
51          if (rules != null) {
52          	for (StatementTreeViewInfo rule : rules) {
53          		setReqCompNL(rule);
54          	}
55          }
56          return rules;
57      }
58  
59      public Map<Integer, StatementTreeViewInfo> storeCourseStatements(String courseId, String courseState, Map<Integer, CourseRequirementsDataModel.requirementState> states,
60                                                                          Map<Integer, StatementTreeViewInfo> rules) throws Exception {
61  
62          Map<Integer, StatementTreeViewInfo> storedRules = new HashMap<Integer, StatementTreeViewInfo>();
63  
64          for (Integer key : rules.keySet()) {
65              StatementTreeViewInfo rule = rules.get(key);
66              switch (states.get(key)) {
67                  case STORED:
68                      //rule was not changed so continue
69                      storedRules.put(key, null);
70                      break;
71                  case ADDED:
72                      storedRules.put(key, createCourseStatement(courseId, courseState, rule));
73                      break;
74                  case EDITED:
75                      storedRules.put(key, updateCourseStatement(courseId, courseState, rule));
76                      break;
77                  case DELETED:
78                      storedRules.put(key, null);
79                      deleteCourseStatement(courseId, rule);
80                      break;
81                  default:
82                      break;
83              }
84          }
85          return storedRules;
86      }
87  
88      @Override
89      public StatementTreeViewInfo createCourseStatement(String courseId, String courseState, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
90      	StatementUtil.updateStatementTreeViewInfoState(courseState, statementTreeViewInfo);
91      	CourseRequirementsDataModel.stripStatementIds(statementTreeViewInfo);
92          StatementTreeViewInfo rule = courseService.createCourseStatement(courseId, statementTreeViewInfo);
93          setReqCompNL(rule);
94          return rule;
95      }
96  
97      @Override
98      public StatusInfo deleteCourseStatement(String courseId, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
99          return courseService.deleteCourseStatement(courseId, statementTreeViewInfo);
100     }
101 
102     @Override
103     public StatementTreeViewInfo updateCourseStatement(String courseId, String courseState, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
104     	StatementUtil.updateStatementTreeViewInfoState(courseState, statementTreeViewInfo);
105     	CourseRequirementsDataModel.stripStatementIds(statementTreeViewInfo);
106         StatementTreeViewInfo rule = courseService.updateCourseStatement(courseId, statementTreeViewInfo);
107         setReqCompNL(rule);
108         return rule;
109     }
110 
111 	@Override
112 	public DataSaveResult createCopyCourse(String originalCluId)
113 			throws Exception {
114 		throw new UnsupportedOperationException("Copy is not implemented without a proposal.");
115 	}
116 
117 	@Override
118 	public DataSaveResult createCopyCourseProposal(String originalProposalId)
119 			throws Exception {
120 		throw new UnsupportedOperationException("Copy is not implemented without a proposal.");
121 	}
122 
123 	@Override  
124     public StatusInfo changeState(String courseId, String newState) throws Exception {
125     	return changeState(courseId, newState, null);
126     }
127 	
128 	@Override
129     public StatusInfo changeState(String courseId, String newState, String prevEndTerm) throws Exception {
130     	return stateChangeService.changeState(courseId, newState, prevEndTerm);
131     }
132 
133     private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
134         List<StatementTreeViewInfo> statements = tree.getStatements();
135         List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
136 
137          if ((statements != null) && (statements.size() > 0)) {
138             // retrieve all statements
139             for (StatementTreeViewInfo statement : statements) {
140                 setReqCompNL(statement); // inside set the children of this statementTreeViewInfo
141             }
142         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
143             // retrieve all req. component LEAFS
144         	for (int i = 0; i < reqComponentInfos.size(); i++) {
145         		ReqComponentInfoUi reqUi = RulesUtil.clone(reqComponentInfos.get(i));
146         		reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE", "en"));
147         		reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE.PREVIEW", "en"));
148         		reqComponentInfos.set(i, reqUi);
149         	}
150         }
151     }
152 
153     @Override
154 	public Boolean isLatestVersion(String versionIndId, Long versionSequenceNumber) throws Exception {
155     	//Perform a search to see if there are any new versions of the course that are approved, draft, etc.
156     	//We don't want to version if there are
157     	SearchRequest request = new SearchRequest("lu.search.isVersionable");
158     	request.addParam("lu.queryParam.versionIndId", versionIndId);
159     	request.addParam("lu.queryParam.sequenceNumber", versionSequenceNumber.toString());
160     	List<String> states = new ArrayList<String>();
161     	states.add("Approved");
162     	states.add("Active");
163     	states.add("Draft");
164     	states.add("Superseded");
165     	request.addParam("lu.queryParam.luOptionalState", states);
166     	SearchResult result = luService.search(request);
167     	
168     	String resultString = result.getRows().get(0).getCells().get(0).getValue();
169     	return "0".equals(resultString);
170 	}
171     
172     public void setCourseService(CourseService courseService) {
173         this.courseService = courseService;
174     }
175 
176     public void setStatementService(StatementService statementService) {
177         this.statementService = statementService;
178     }
179 
180 	public void setStateChangeService(
181 			CourseStateChangeServiceImpl stateChangeService) {
182 		this.stateChangeService = stateChangeService;
183 	}
184 
185 	public void setLuService(LuService luService) {
186 		this.luService = luService;
187 	}
188 }