All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sakaiproject.tool.assessment.contentpackaging.ManifestGenerator Maven / Gradle / Ivy

/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2007, 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.tool.assessment.contentpackaging;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;


import lombok.extern.slf4j.Slf4j;
import org.sakaiproject.component.cover.ServerConfigurationService;
import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.exception.ServerOverloadException;
import org.sakaiproject.exception.TypeException;
import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
import org.sakaiproject.tool.assessment.data.dao.assessment.SectionAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
import org.sakaiproject.tool.assessment.data.ifc.shared.TypeIfc;
import org.sakaiproject.tool.assessment.facade.AssessmentFacade;
import org.sakaiproject.tool.assessment.facade.SectionFacade;
import org.sakaiproject.tool.assessment.qti.util.XmlUtil;
import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.cover.UserDirectoryService;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
import org.w3c.dom.Element;

/**
 * 

* Copyright: Copyright (c) 2007 Sakai *

* * @version $Id$ */ @Slf4j public class ManifestGenerator { /**default namespace and metadata namespace*/ private static final String DEFAULT_NAMESPACE_URI = "http://www.imsglobal.org/xsd/imscp_v1p1"; private static final String DEFAULT_NAMESPACE_SCHEMA_LOCATION = "http://www.imsglobal.org/xsd/imscp_v1p1.xsd"; private static final String IMSMD_NAMESPACE_URI = "http://www.imsglobal.org/xsd/imsmd_v1p2"; private static final String IMSMD_NAMESPACE_SCHEMA_LOCATION = "http://www.imsglobal.org/xsd/imsmd_v1p2.xsd"; private static final String EXPORT_ASSESSMENT = "exportAssessment"; private static final String EXPORT_ASSESSMENT_XML = EXPORT_ASSESSMENT + ".xml"; private Document document; private String assessmentId; private HashMap contentMap = new HashMap(); private ContentHostingService contentHostingService; public ManifestGenerator(String assessmentId) { this.assessmentId = assessmentId; } public String getManifest() { document = readXMLDocument(); if (document == null) { log.info("document == null"); return ""; } String xmlString = XmlUtil.getDOMString(document); String newXmlString = xmlString; if (xmlString.startsWith(" -1 || description.indexOf(" -1 || description.indexOf(" -1)) { byte[] content = null; int srcStartIndex = 0; int srcEndIndex = 0; String src = null; String resourceId = null; String eidResourceId = null; String[] splittedString = description.split(" -1) { int offset = 5; srcStartIndex = splittedRefString[j].indexOf("src=\""); if(srcStartIndex == -1){ srcStartIndex = splittedRefString[j].indexOf("href=\""); offset++; } srcEndIndex = splittedRefString[j].indexOf("\"", srcStartIndex + offset); src = splittedRefString[j].substring(srcStartIndex + offset, srcEndIndex); try { src = URLDecoder.decode(src, "UTF-8"); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); } if (src.indexOf(siteCollection) > -1) { resourceId = src.replace(prependString, ""); content = contentHostingService.getResource(resourceId).getContent(); if (content != null) { contentMap.put(resourceId.replace(" ", ""), content); } } else if (src.indexOf(userCollection) > -1) { eidResourceId = src.replace(prependString, ""); resourceId = eidResourceId.replace(eid, userId); content = contentHostingService.getResource(resourceId).getContent(); if (content != null) { contentMap.put(eidResourceId.replace(" ", ""), content); } } else if (src.indexOf(attachment) > -1) { resourceId = src.replace(prependString, ""); content = contentHostingService.getResource(resourceId).getContent(); if (content != null) { contentMap.put(resourceId.replace(" ", ""), content); } } else { log.error("Neither group nor user"); } } } } } } catch (PermissionException e) { log.error(e.getMessage()); } catch (IdUnusedException e) { log.error(e.getMessage()); } catch (TypeException e) { log.error(e.getMessage()); } catch (ServerOverloadException e) { log.error(e.getMessage()); } } public void setContentHostingService(){ if (contentHostingService == null) { this.contentHostingService = AssessmentService.getContentHostingService(); } } private Element createManifestElement() { Element manifest = createDefaultNSElement("manifest"); manifest.setAttribute("identifier", "MANIFEST1"); manifest.setAttribute("xmlns", DEFAULT_NAMESPACE_URI); manifest.setAttribute("xmlns:imsmd", IMSMD_NAMESPACE_URI); manifest.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); manifest.setAttribute("xsi:schemaLocation", DEFAULT_NAMESPACE_URI + " " + DEFAULT_NAMESPACE_SCHEMA_LOCATION + " " + IMSMD_NAMESPACE_URI + " " + IMSMD_NAMESPACE_SCHEMA_LOCATION); return manifest; } public void appendLOMMetadataToElement(String title, Element parent) { // imsmd:lom Element imsmdLom = createLOMElement("lom"); // imsmd:general Element imsmdGeneral = createLOMElement("general"); // imsmd:identifier String identifier = Long.toHexString((new Date()).getTime()); Element imsmdIdentifier = createLOMElementWithLangstring("identifier", identifier); imsmdGeneral.appendChild(imsmdIdentifier); // imsmd:title Element imsmdTitle = createLOMElementWithLangstring("title", title); imsmdGeneral.appendChild(imsmdTitle); imsmdLom.appendChild(imsmdGeneral); parent.appendChild(imsmdLom); } private Element createOrganizationsElement() { Element organizations = createDefaultNSElement("organizations"); return organizations; } public Element createResourceElement() { Element resourceElement = createDefaultNSElement("resource"); resourceElement.setAttributeNS(DEFAULT_NAMESPACE_URI, "identifier", "RESOURCE1"); resourceElement.setAttributeNS(DEFAULT_NAMESPACE_URI, "type","imsqti_xmlv1p1"); resourceElement.setAttributeNS(DEFAULT_NAMESPACE_URI, "href", EXPORT_ASSESSMENT_XML); return resourceElement; } public Element createFileElement(String href) { Element fileElement = createDefaultNSElement("file"); fileElement.setAttributeNS(DEFAULT_NAMESPACE_URI, "href", href); return fileElement; } private Element createDefaultNSElement(String elename) { return getDocument().createElementNS(DEFAULT_NAMESPACE_URI, elename); } private Element createLOMElement(String elename) { Element imsmdlom = getDocument().createElementNS(IMSMD_NAMESPACE_URI, elename); imsmdlom.setPrefix("imsmd"); return imsmdlom; } private Element createLOMElementWithLangstring(String elementName, String text) { Element element = createLOMElement(elementName); //imsmd:langstring Element imsmdlangstring = createLOMElement("langstring"); imsmdlangstring.setAttribute("xml:lang", "en-US"); setNodeValue(imsmdlangstring, text); element.appendChild(imsmdlangstring); return element; } private void setNodeValue( Element parent, String data ) { Text textNode = getDocument().createTextNode(data); parent.appendChild( textNode ); } private Document getDocument() { if (document != null) return document; else document = XmlUtil.createDocument(); return document; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy