Index: lams_central/conf/xdoclet/servlet-mappings.xml =================================================================== diff -u -rab19db088359a46353cc92e806c40ff5cff818b9 -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_central/conf/xdoclet/servlet-mappings.xml (.../servlet-mappings.xml) (revision ab19db088359a46353cc92e806c40ff5cff818b9) +++ lams_central/conf/xdoclet/servlet-mappings.xml (.../servlet-mappings.xml) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -34,6 +34,11 @@ + LearningDesignSVG + /services/LearningDesignSVG/* + + + LessonManager /services/xml/LessonManager/* Index: lams_central/conf/xdoclet/servlets.xml =================================================================== diff -u -rab19db088359a46353cc92e806c40ff5cff818b9 -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_central/conf/xdoclet/servlets.xml (.../servlets.xml) (revision ab19db088359a46353cc92e806c40ff5cff818b9) +++ lams_central/conf/xdoclet/servlets.xml (.../servlets.xml) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -70,6 +70,14 @@ + LearningDesignSVG + + + org.lamsfoundation.lams.webservice.LearningDesignSVGServlet + + + + LessonManager Index: lams_central/src/java/org/lamsfoundation/lams/util/CentralConstants.java =================================================================== diff -u -r28827d671ccbd07e8f70eaaf3ca7d63047186bb3 -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_central/src/java/org/lamsfoundation/lams/util/CentralConstants.java (.../CentralConstants.java) (revision 28827d671ccbd07e8f70eaaf3ca7d63047186bb3) +++ lams_central/src/java/org/lamsfoundation/lams/util/CentralConstants.java (.../CentralConstants.java) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -56,6 +56,8 @@ public static final String PARAM_LANG = "lang"; public static final String PARAM_METHOD = "method"; + + public static final String PARAM_SVG_FORMAT = "svgFormat"; public static final String PARAM_PROGRESS_USER = "progressUser"; Index: lams_central/src/java/org/lamsfoundation/lams/webservice/LearningDesignSVGServlet.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/webservice/LearningDesignSVGServlet.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/webservice/LearningDesignSVGServlet.java (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -0,0 +1,148 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +/* $Id$ */ +package org.lamsfoundation.lams.webservice; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.integration.ExtServerOrgMap; +import org.lamsfoundation.lams.integration.security.Authenticator; +import org.lamsfoundation.lams.integration.service.IntegrationService; +import org.lamsfoundation.lams.learningdesign.service.ILearningDesignService; +import org.lamsfoundation.lams.util.CentralConstants; +import org.lamsfoundation.lams.util.WebUtil; +import org.lamsfoundation.lams.util.svg.SVGGenerator; +import org.springframework.web.context.support.WebApplicationContextUtils; + +public class LearningDesignSVGServlet extends HttpServlet { + + private static final long serialVersionUID = -1918180868204870617L; + + private static Logger log = Logger.getLogger(LearningDesignSVGServlet.class); + + private static IntegrationService integrationService = null; + + private ILearningDesignService learningDesignService; + + /** + * The doGet method of the servlet.
+ * + * This method is called when a form has its tag value method equals to get. + * + * @param request + * the request send by the client to the server + * @param response + * the response send by the server to the client + * @throws ServletException + * if an error occurred + * @throws IOException + * if an error occurred + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + try { + // get parameters + String serverId = request.getParameter(CentralConstants.PARAM_SERVER_ID); + String datetime = request.getParameter(CentralConstants.PARAM_DATE_TIME); + String hashValue = request.getParameter(CentralConstants.PARAM_HASH_VALUE); + String username = request.getParameter(CentralConstants.PARAM_USERNAME); + String courseId = request.getParameter(CentralConstants.PARAM_COURSE_ID); + String courseName = request.getParameter(CentralConstants.PARAM_COURSE_NAME); + String country = request.getParameter(CentralConstants.PARAM_COUNTRY); + String lang = request.getParameter(CentralConstants.PARAM_LANG); + String modeStr = request.getParameter(CentralConstants.PARAM_MODE); + Long learningDesignId = WebUtil.readLongParam(request, CentralConstants.PARAM_LEARNING_DESIGN_ID); + int imageFormat = WebUtil.readIntParam(request, CentralConstants.PARAM_SVG_FORMAT); + + if (serverId == null || datetime == null || hashValue == null || username == null || courseId == null + || country == null || lang == null || modeStr == null) { + String msg = "Parameters missing"; + log.error(msg); + response.sendError(response.SC_BAD_REQUEST, "Parameters missing"); + } + + //check imageFormat parameter is correct + if (!(imageFormat == SVGGenerator.OUTPUT_FORMAT_SVG) && !(imageFormat == SVGGenerator.OUTPUT_FORMAT_PNG)) { + String msg = "Image format parameter is incorrect"; + log.error(msg); + response.sendError(response.SC_BAD_REQUEST, msg); + } + + // LDEV-2196 preserve character encoding if necessary + if (request.getCharacterEncoding() == null) { + log.debug("request.getCharacterEncoding is empty, parsing username and courseName as 8859_1 to UTF-8..."); + username = new String(username.getBytes("8859_1"), "UTF-8"); + if (courseName != null) { + courseName = new String(courseName.getBytes("8859_1"), "UTF-8"); + } + } + + // get Server map + ExtServerOrgMap serverMap = integrationService.getExtServerOrgMap(serverId); + + // authenticate + Authenticator.authenticate(serverMap, datetime, username, hashValue); + + // generate response + String contentType; + if (imageFormat == SVGGenerator.OUTPUT_FORMAT_SVG) { + contentType = "image/svg+xml"; + } else { + contentType = "image/png"; + } + response.setContentType(contentType); + OutputStream out = response.getOutputStream(); + learningDesignService.getLearningDesignSVG(learningDesignId, imageFormat, out); + + } catch (Exception e) { + log.error("Problem with LearningDesignRepositoryServlet request", e); + response.sendError(response.SC_BAD_REQUEST, "Problem with LearningDesignRepositoryServlet request"); + } + + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } + + /** + * Initialization of the servlet.
+ * + * @throws ServletException + * if an error occure + */ + public void init() throws ServletException { + integrationService = (IntegrationService) WebApplicationContextUtils.getRequiredWebApplicationContext( + getServletContext()).getBean("integrationService"); + + learningDesignService = (ILearningDesignService) WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()) + .getBean("learningDesignService"); + } +} Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java =================================================================== diff -u -r6d0e19e0d48a4950c9afbb40823a2bc49c2bb5a5 -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (.../ExportToolContentService.java) (revision 6d0e19e0d48a4950c9afbb40823a2bc49c2bb5a5) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (.../ExportToolContentService.java) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -666,16 +666,14 @@ //generate SVG image if (format != ExportToolContentService.PACKAGE_FORMAT_IMS) { - SVGGenerator svgGenerator = new SVGGenerator(); - svgGenerator.generateSvgDom(ldDto); String svgFileName = FileUtil.getFullPath(contentDir, ExportToolContentService.SVG_IMAGE_FILE_NAME); FileOutputStream svgOutputStream = new FileOutputStream(svgFileName); - svgGenerator.streamOutDocument(svgOutputStream, SVGGenerator.OUTPUT_FORMAT_SVG); + service.getLearningDesignSVG(learningDesignId, SVGGenerator.OUTPUT_FORMAT_SVG, svgOutputStream); String pngFileName = FileUtil.getFullPath(contentDir, ExportToolContentService.PNG_IMAGE_FILE_NAME); FileOutputStream pngOutputStream = new FileOutputStream(pngFileName); - svgGenerator.streamOutDocument(pngOutputStream, SVGGenerator.OUTPUT_FORMAT_PNG); + service.getLearningDesignSVG(learningDesignId, SVGGenerator.OUTPUT_FORMAT_PNG, pngOutputStream); } log.debug("Learning design xml export success"); Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ILearningDesignService.java =================================================================== diff -u -r870373d635bc769aee2dc271e6ff12818fcfcea6 -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ILearningDesignService.java (.../ILearningDesignService.java) (revision 870373d635bc769aee2dc271e6ff12818fcfcea6) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ILearningDesignService.java (.../ILearningDesignService.java) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -24,9 +24,12 @@ package org.lamsfoundation.lams.learningdesign.service; import java.io.IOException; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Vector; +import org.apache.batik.transcoder.TranscoderException; +import org.jdom.JDOMException; import org.lamsfoundation.lams.learningdesign.LearningDesign; import org.lamsfoundation.lams.learningdesign.dto.LearningDesignDTO; import org.lamsfoundation.lams.learningdesign.dto.LearningLibraryDTO; @@ -76,4 +79,17 @@ */ public void setValid(Long learningLibraryId, boolean valid); + /** + * Streams out learning design SVG into specified OutputStream. Additionally, stores resulted SVG into file system for caching. + * + * @param learningDesignId + * @param imageFormat either SVGGenerator.OUTPUT_FORMAT_SVG or SVGGenerator.OUTPUT_FORMAT_PNG + * @param out + * @return whether operation was successful or not + * @throws JDOMException + * @throws IOException + * @throws TranscoderException + * @throws ExportToolContentException + */ + boolean getLearningDesignSVG(Long learningDesignId, int imageFormat, OutputStream out) throws JDOMException, IOException, TranscoderException, ExportToolContentException; } Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LearningDesignService.java =================================================================== diff -u -rfdbf7a3cfb10831b1ac581efa42807d3207a71ff -r87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LearningDesignService.java (.../LearningDesignService.java) (revision fdbf7a3cfb10831b1ac581efa42807d3207a71ff) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/LearningDesignService.java (.../LearningDesignService.java) (revision 87e2cccf93ac20d2b8e953cea2cf36c8a8fb5b14) @@ -23,24 +23,28 @@ /* $$Id$$ */ package org.lamsfoundation.lams.learningdesign.service; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; -import java.util.Set; import java.util.Vector; +import org.apache.batik.transcoder.TranscoderException; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; +import org.jdom.JDOMException; import org.lamsfoundation.lams.learningdesign.Activity; -import org.lamsfoundation.lams.learningdesign.Grouping; -import org.lamsfoundation.lams.learningdesign.GroupingActivity; import org.lamsfoundation.lams.learningdesign.LearningDesign; import org.lamsfoundation.lams.learningdesign.LearningLibrary; -import org.lamsfoundation.lams.learningdesign.OptionsActivity; -import org.lamsfoundation.lams.learningdesign.RandomGrouping; -import org.lamsfoundation.lams.learningdesign.Transition; import org.lamsfoundation.lams.learningdesign.dao.hibernate.ActivityDAO; import org.lamsfoundation.lams.learningdesign.dao.hibernate.GroupingDAO; import org.lamsfoundation.lams.learningdesign.dao.hibernate.LearningDesignDAO; @@ -50,8 +54,12 @@ import org.lamsfoundation.lams.learningdesign.dto.LibraryActivityDTO; import org.lamsfoundation.lams.learningdesign.dto.ValidationErrorDTO; import org.lamsfoundation.lams.tool.Tool; +import org.lamsfoundation.lams.util.Configuration; +import org.lamsfoundation.lams.util.ConfigurationKeys; +import org.lamsfoundation.lams.util.FileUtil; import org.lamsfoundation.lams.util.ILoadedMessageSourceService; import org.lamsfoundation.lams.util.MessageService; +import org.lamsfoundation.lams.util.svg.SVGGenerator; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; @@ -189,7 +197,68 @@ } return libraries; } + + public boolean getLearningDesignSVG(Long learningDesignId, int imageFormat, OutputStream out) throws JDOMException, IOException, TranscoderException, ExportToolContentException { + //construct absolute filePath to SVG + String earFolder = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR); + if (StringUtils.isBlank(earFolder)) { + log.error("Unable to get path to the LAMS Server URL from the configuration table. SVG image creation failed"); + return false; + } + String directoryToStoreFile = FileUtil.getFullPath(earFolder, "lams-www.war\\secure\\learning-design-images"); + String fileExtension; + if (imageFormat == SVGGenerator.OUTPUT_FORMAT_SVG) { + fileExtension = ".svg"; + } else { + fileExtension = ".png"; + } + String absoluteFilePath = FileUtil.getFullPath(directoryToStoreFile, learningDesignId.toString() + fileExtension); + File file = new File(absoluteFilePath); + + //check if SVG exists and up-to-date + LearningDesign learningDesign = learningDesignDAO.getLearningDesignById(learningDesignId); + boolean isSvgOutdated = new Date(file.lastModified()).before(learningDesign.getLastModifiedDateTime()); + if (!file.exists() || isSvgOutdated) { + //generate new SVG image and save it to the file system + SVGGenerator svgGenerator = new SVGGenerator(); + LearningDesignDTO ldDto = this.getLearningDesignDTO(learningDesignId, ""); + svgGenerator.generateSvgDom(ldDto); + FileOutputStream fileOutputStream = new FileOutputStream(file); + svgGenerator.streamOutDocument(fileOutputStream, imageFormat); + } + + //stream out SVG to the specified by user OutputStream + InputStream in = null; + try { + in = new BufferedInputStream(new FileInputStream(file)); + int count = 0; + int ch; + while ((ch = in.read()) != -1) { + out.write((char) ch); + count++; + } + out.flush(); + } catch (Exception e) { + log.error("Exception occured writing out file:" + e.getMessage()); + throw new ExportToolContentException(e); + } finally { + try { + if (in != null) { + in.close(); // very important + } + if (out != null) { + out.close(); + } + } catch (Exception e) { + log.error("Error Closing file. File already written out - no exception being thrown.", e); + return false; + } + } + + return true; + } + private void internationaliseActivities(Collection activities) { Iterator iter = activities.iterator(); Locale locale = LocaleContextHolder.getLocale();