001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/qti/XMLImportBean.java $
003: * $Id: XMLImportBean.java 22608 2007-03-14 19:27:17Z lydial@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.ui.bean.qti;
021:
022: import java.io.File;
023: import java.io.Serializable;
024: import java.util.ArrayList;
025: import org.sakaiproject.util.ResourceLoader;
026:
027: import javax.faces.application.FacesMessage;
028: import javax.faces.context.FacesContext;
029: import javax.faces.event.ValueChangeEvent;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.service.gradebook.shared.GradebookService;
034: import org.sakaiproject.spring.SpringBeanLocator;
035: import org.sakaiproject.tool.assessment.contentpackaging.ImportService;
036: import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
037: import org.sakaiproject.tool.assessment.facade.AssessmentFacade;
038: import org.sakaiproject.tool.assessment.facade.AssessmentFacadeQueries;
039: import org.sakaiproject.tool.assessment.facade.AssessmentTemplateFacade;
040: import org.sakaiproject.tool.assessment.facade.QuestionPoolFacade;
041: import org.sakaiproject.tool.assessment.integration.context.IntegrationContextFactory;
042: import org.sakaiproject.tool.assessment.integration.helper.ifc.GradebookServiceHelper;
043: import org.sakaiproject.tool.assessment.qti.constants.QTIVersion;
044: import org.sakaiproject.tool.assessment.qti.util.XmlUtil;
045: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
046: import org.sakaiproject.tool.assessment.services.qti.QTIService;
047: import org.sakaiproject.tool.assessment.ui.bean.author.AssessmentBean;
048: import org.sakaiproject.tool.assessment.ui.bean.author.AuthorBean;
049: import org.sakaiproject.tool.assessment.ui.bean.author.ItemAuthorBean;
050: import org.sakaiproject.tool.assessment.ui.bean.questionpool.QuestionPoolBean;
051: import org.w3c.dom.Document;
052:
053: /**
054: * <p>Bean for QTI Import Data</p>
055: */
056:
057: public class XMLImportBean implements Serializable {
058:
059: /** Use serialVersionUID for interoperability. */
060: private final static long serialVersionUID = 418920360211039758L;
061: private static Log log = LogFactory.getLog(XMLImportBean.class);
062:
063: private int qtiVersion;
064: private String uploadFileName;
065: private String importType;
066: private String pathToData;
067: private AuthorBean authorBean;
068: private AssessmentBean assessmentBean;
069: private ItemAuthorBean itemAuthorBean;
070: private QuestionPoolBean questionPoolBean;
071: private boolean isCP;
072:
073: private static final GradebookServiceHelper gbsHelper = IntegrationContextFactory
074: .getInstance().getGradebookServiceHelper();
075: private static final boolean integrated = IntegrationContextFactory
076: .getInstance().isIntegrated();
077:
078: public XMLImportBean() {
079: qtiVersion = QTIVersion.VERSION_1_2;//default
080: }
081:
082: // put tests here...
083: public static void main(String[] args) {
084: //XMLImportBean XMLImportBean1 = new XMLImportBean();
085: }
086:
087: public void importAssessment(ValueChangeEvent e) {
088: String uploadFile = (String) e.getNewValue();
089: if (uploadFile.endsWith(".zip")) {
090: isCP = true;
091: importFromCP(uploadFile);
092: } else {
093: isCP = false;
094: importFromQti(uploadFile);
095: }
096: }
097:
098: /**
099: * Value change on upload
100: * @param e the event
101: */
102: public void importFromQti(String uploadFile) {
103: try {
104: processFile(uploadFile);
105: } catch (Exception ex) {
106: ResourceLoader rb = new ResourceLoader(
107: "org.sakaiproject.tool.assessment.bundle.AuthorImportExport");
108: FacesMessage message = new FacesMessage(rb
109: .getString("import_err")
110: + ex);
111: FacesContext.getCurrentInstance().addMessage(null, message);
112: // remove unsuccessful file
113: log.debug("****remove unsuccessful uplaodFile="
114: + uploadFile);
115: File upload = new File(uploadFile);
116: upload.delete();
117: }
118: }
119:
120: /**
121: * Value change on upload
122: * @param e the event
123: */
124:
125: public void importFromCP(String uploadFile) {
126: ImportService importService = new ImportService();
127: String unzipLocation = importService
128: .unzipImportFile(uploadFile);
129: String filename = unzipLocation + "/"
130: + importService.getQTIFilename();
131: try {
132: processFile(filename);
133: } catch (Exception ex) {
134: ResourceLoader rb = new ResourceLoader(
135: "org.sakaiproject.tool.assessment.bundle.AuthorImportExport");
136: FacesMessage message = new FacesMessage(rb
137: .getString("import_err")
138: + ex);
139: FacesContext.getCurrentInstance().addMessage(null, message);
140: // remove unsuccessful file
141: log.debug("****remove unsuccessful filename=" + filename);
142: File upload = new File(filename);
143: upload.delete();
144: }
145: }
146:
147: /**
148: *
149: * @return QTI version of XML file
150: */
151: public int getQtiVersion() {
152: return qtiVersion;
153: }
154:
155: /**
156: *
157: * @param qtiVersion QTI version of XML file
158: */
159: public void setQtiVersion(int qtiVersion) {
160: if (!QTIVersion.isValid(qtiVersion)) {
161: throw new IllegalArgumentException("NOT Legal Qti Version.");
162: }
163: this .qtiVersion = qtiVersion;
164: }
165:
166: /**
167: *
168: * @return file name and path
169: */
170: public String getUploadFileName() {
171: return uploadFileName;
172: }
173:
174: /**
175: *
176: * @param uploadFileName file name and path
177: */
178: public void setUploadFileName(String uploadFileName) {
179: this .uploadFileName = uploadFileName;
180: }
181:
182: /**
183: * A, S, I
184: * @return type of upload
185: */
186: public String getImportType() {
187: return importType;
188: }
189:
190: /**
191: * A, S, I
192: * @param importType A, S, or I
193: */
194: public void setImportType(String importType) {
195: this .importType = importType;
196: }
197:
198: private void processFile(String uploadFile) {
199: itemAuthorBean.setTarget(ItemAuthorBean.FROM_ASSESSMENT); // save to assessment
200:
201: // Get the file name
202: String fileName = uploadFile;
203: AssessmentService assessmentService = new AssessmentService();
204: // Create an assessment based on the uploaded file
205: AssessmentFacade assessment = createImportedAssessment(
206: fileName, qtiVersion);
207:
208: // change grading book settings if there is no gradebook in the site
209: boolean hasGradebook = false;
210: GradebookService g = null;
211: if (integrated) {
212: g = (GradebookService) SpringBeanLocator
213: .getInstance()
214: .getBean(
215: "org.sakaiproject.service.gradebook.GradebookService");
216: }
217: try {
218: if (gbsHelper.isAssignmentDefined(assessment.getTitle(), g)) {
219: hasGradebook = true;
220: }
221: } catch (Exception e) {
222: log.debug("Error calling gradebookHelper");
223: }
224:
225: // gradebook options, don't know how this is supposed to work, leave alone for now
226: if (!hasGradebook && assessment != null) {
227: assessment.getEvaluationModel().setToGradeBook(
228: EvaluationModelIfc.NOT_TO_GRADEBOOK.toString());
229: }
230: assessmentService.saveAssessment(assessment);
231:
232: // Go to editAssessment.jsp, so prepare assessmentBean
233: assessmentBean.setAssessment(assessment);
234: // reset in case anything hanging around
235: authorBean.setAssessTitle("");
236: authorBean.setAssessmentDescription("");
237: authorBean.setAssessmentTypeId("");
238: authorBean
239: .setAssessmentTemplateId(AssessmentTemplateFacade.DEFAULTTEMPLATE
240: .toString());
241:
242: // update core AssessmentList: get the managed bean, author and set the list
243:
244: ArrayList list = assessmentService
245: .getBasicInfoOfAllActiveAssessments(
246: AssessmentFacadeQueries.TITLE, true);
247: //
248: authorBean.setAssessments(list);
249:
250: // remove uploaded file
251: try {
252: //log.debug("****filename="+fileName);
253: File upload = new File(fileName);
254: upload.delete();
255: } catch (Exception e) {
256: log.error(e.getMessage());
257: }
258: }
259:
260: /**
261: * Create assessment from uploaded QTI XML
262: * @param fullFileName file name and path
263: * @param qti QTI version
264: * @return
265: */
266: private AssessmentFacade createImportedAssessment(
267: String fullFileName, int qti) {
268: //trim = true so that xml processing instruction at top line, even if not.
269: Document document = XmlUtil.readDocument(fullFileName, true);
270: QTIService qtiService = new QTIService();
271: if (isCP) {
272: return qtiService.createImportedAssessment(document, qti,
273: fullFileName.substring(0, fullFileName
274: .lastIndexOf("/")));
275: } else {
276: return qtiService.createImportedAssessment(document, qti,
277: null);
278: }
279: }
280:
281: public AuthorBean getAuthorBean() {
282: return authorBean;
283: }
284:
285: public void setAuthorBean(AuthorBean authorBean) {
286: this .authorBean = authorBean;
287: }
288:
289: public AssessmentBean getAssessmentBean() {
290: return assessmentBean;
291: }
292:
293: public void setAssessmentBean(AssessmentBean assessmentBean) {
294: this .assessmentBean = assessmentBean;
295: }
296:
297: public ItemAuthorBean getItemAuthorBean() {
298: return itemAuthorBean;
299: }
300:
301: public void setItemAuthorBean(ItemAuthorBean itemAuthorBean) {
302: this .itemAuthorBean = itemAuthorBean;
303: }
304:
305: /**
306: * Value change on upload
307: * @param e the event
308: */
309: public void importPoolFromQti(ValueChangeEvent e) {
310: String uploadFile = (String) e.getNewValue();
311:
312: try {
313: processAsPoolFile(uploadFile);
314: } catch (Exception ex) {
315: ResourceLoader rb = new ResourceLoader(
316: "org.sakaiproject.tool.assessment.bundle.AuthorImportExport");
317: FacesMessage message = new FacesMessage(rb
318: .getString("import_err")
319: + ex);
320: FacesContext.getCurrentInstance().addMessage(null, message);
321: }
322: }
323:
324: /**
325: * Process uploaded QTI XML
326: * assessment as question pool
327: */
328: private void processAsPoolFile(String uploadFile) {
329: itemAuthorBean.setTarget(ItemAuthorBean.FROM_QUESTIONPOOL); // save to questionpool
330:
331: // Get the file name
332: String fileName = uploadFile;
333:
334: // Create a questionpool based on the uploaded assessment file
335: QuestionPoolFacade questionPool = createImportedQuestionPool(
336: fileName, qtiVersion);
337:
338: // remove uploaded file
339: try {
340: //System.out.println("****filename="+fileName);
341: File upload = new File(fileName);
342: upload.delete();
343: } catch (Exception e) {
344: System.out.println(e.getMessage());
345: }
346: }
347:
348: /**
349: * Create questionpool from uploaded QTI assessment XML
350: * @param fullFileName file name and path
351: * @param qti QTI version
352: * @return
353: */
354: private QuestionPoolFacade createImportedQuestionPool(
355: String fullFileName, int qti) {
356: //trim = true so that xml processing instruction at top line, even if not.
357: Document document = XmlUtil.readDocument(fullFileName, true);
358: QTIService qtiService = new QTIService();
359: return qtiService.createImportedQuestionPool(document, qti);
360: }
361:
362: public QuestionPoolBean getQuestionPoolBean() {
363: return questionPoolBean;
364: }
365:
366: public void setQuestionPoolBean(QuestionPoolBean questionPoolBean) {
367: this .questionPoolBean = questionPoolBean;
368: }
369:
370: public String getPathToData() {
371: return pathToData;
372: }
373:
374: public void setPathToData(String pathToData) {
375: this.pathToData = pathToData;
376: }
377: }
|