Index: lams_central/conf/xdoclet/servlet-mappings.xml
===================================================================
diff -u -rb2fa0a2dbd866ab1861be242b5eb7a3095ce6eed -r8e8a1f2b58ed44e9480b576fbbbea82b98a028a2
--- lams_central/conf/xdoclet/servlet-mappings.xml (.../servlet-mappings.xml) (revision b2fa0a2dbd866ab1861be242b5eb7a3095ce6eed)
+++ lams_central/conf/xdoclet/servlet-mappings.xml (.../servlet-mappings.xml) (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -13,4 +13,9 @@
SimpleUploader
/fckeditor/editor/filemanager/upload/simpleuploader
+
+
+
+ ExportToolContent
+ /ExportToolContent
\ No newline at end of file
Index: lams_central/conf/xdoclet/servlets.xml
===================================================================
diff -u -rb2fa0a2dbd866ab1861be242b5eb7a3095ce6eed -r8e8a1f2b58ed44e9480b576fbbbea82b98a028a2
--- lams_central/conf/xdoclet/servlets.xml (.../servlets.xml) (revision b2fa0a2dbd866ab1861be242b5eb7a3095ce6eed)
+++ lams_central/conf/xdoclet/servlets.xml (.../servlets.xml) (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -85,3 +85,7 @@
1
+
+ ExportToolContent
+ org.lamsfoundation.lams.authoring.web.ExportToolContentServlet
+
Index: lams_central/src/java/org/lamsfoundation/lams/authoring/web/AuthoringConstants.java
===================================================================
diff -u -r50d68fddcd33b33903cd4c0119f01080b9314b80 -r8e8a1f2b58ed44e9480b576fbbbea82b98a028a2
--- lams_central/src/java/org/lamsfoundation/lams/authoring/web/AuthoringConstants.java (.../AuthoringConstants.java) (revision 50d68fddcd33b33903cd4c0119f01080b9314b80)
+++ lams_central/src/java/org/lamsfoundation/lams/authoring/web/AuthoringConstants.java (.../AuthoringConstants.java) (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -33,5 +33,10 @@
/** Spring context name for refering to the authoring service */
public static final String AUTHORING_SERVICE_BEAN_NAME = "authoringService";
public static final String TOOL_SERVICE_BEAN_NAME = "lamsToolService";
+ public static final String EXPORT_TOOLCONTENT_SERVICE_BEAN_NAME = "exportToolContentService";
+
+ public static final String PARAM_LEARING_DESIGN_ID = "learingDesignID";
+
+ //used by all tool authoring action class to mark the success flag.
public static final String LAMS_AUTHORING_SUCCESS_FLAG = "LAMS_AUTHORING_SUCCESS_FLAG";
}
Index: lams_central/src/java/org/lamsfoundation/lams/authoring/web/ExportToolContentServlet.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/authoring/web/ExportToolContentServlet.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/authoring/web/ExportToolContentServlet.java (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -0,0 +1,106 @@
+/****************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ * http://www.gnu.org/licenses/gpl.txt
+ * ****************************************************************
+ */
+
+/* $Id$ */
+package org.lamsfoundation.lams.authoring.web;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+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.learningdesign.service.ExportToolContentException;
+import org.lamsfoundation.lams.learningdesign.service.IExportToolContentService;
+import org.lamsfoundation.lams.util.WebUtil;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+/**
+ * Export tool content servlet. It needs learingDesignID as input parameter.
+ * @author Steve.Ni
+ *
+ * @version $Revision$
+ */
+public class ExportToolContentServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private Logger log = Logger.getLogger(ExportToolContentServlet.class);
+
+ /*
+ * @see javax.servlet.http.HttpServlet.service(HttpServletRequest, HttpServletResponse)
+ */
+ public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+ Long learningDesignId = WebUtil.readLongParam(request,AuthoringConstants.PARAM_LEARING_DESIGN_ID);
+ IExportToolContentService service = getExportService();
+ try {
+ String zipFilename = service.exportToolContent(learningDesignId);
+
+ //write zip file as response stream.
+ response.setContentType("application/zip");
+ response.setHeader("Content-Disposition","attachment;");
+ InputStream in = new BufferedInputStream(new FileInputStream(zipFilename));
+ OutputStream out = response.getOutputStream();
+ try {
+ int count = 0;
+
+ int ch;
+ while ((ch = in.read()) != -1)
+ {
+ out.write((char) ch);
+ count++;
+ }
+ log.debug("Wrote out " + count + " bytes");
+ response.setContentLength(count);
+ out.flush();
+ } catch (IOException e) {
+ log.error( "Exception occured writing out file:" + e.getMessage());
+ throw 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);
+ }
+ }
+ } catch (ExportToolContentException e1) {
+ log.error("Unable to export tool content: " + e1.toString());
+ }
+ }
+
+ //***************************************************************************************
+ // Private method
+ //***************************************************************************************
+ private IExportToolContentService getExportService(){
+ WebApplicationContext webContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
+ return (IExportToolContentService) webContext.getBean(AuthoringConstants.EXPORT_TOOLCONTENT_SERVICE_BEAN_NAME);
+ }
+}
Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentException.java
===================================================================
diff -u
--- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentException.java (revision 0)
+++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentException.java (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ * http://www.gnu.org/licenses/gpl.txt
+ * ****************************************************************
+ */
+
+/* $Id$ */
+package org.lamsfoundation.lams.learningdesign.service;
+/**
+ * Export Tool Content Exception class.
+ * @author Steve.Ni
+ *
+ * @version $Revision$
+ */
+public class ExportToolContentException extends Exception {
+
+ public ExportToolContentException() {
+ super();
+
+ }
+
+ public ExportToolContentException(String message, Throwable cause) {
+ super(message, cause);
+
+ }
+
+ public ExportToolContentException(String message) {
+ super(message);
+
+ }
+
+ public ExportToolContentException(Throwable cause) {
+ super(cause);
+
+ }
+
+}
Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java
===================================================================
diff -u
--- lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (revision 0)
+++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/service/ExportToolContentService.java (revision 8e8a1f2b58ed44e9480b576fbbbea82b98a028a2)
@@ -0,0 +1,128 @@
+/****************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ * http://www.gnu.org/licenses/gpl.txt
+ * ****************************************************************
+ */
+
+/* $Id$ */
+package org.lamsfoundation.lams.learningdesign.service;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.log4j.Logger;
+import org.lamsfoundation.lams.learningdesign.dao.hibernate.ActivityDAO;
+import org.lamsfoundation.lams.learningdesign.dto.LearningDesignDTO;
+import org.lamsfoundation.lams.util.FileUtil;
+import org.lamsfoundation.lams.util.FileUtilException;
+import org.lamsfoundation.lams.util.zipfile.ZipFileUtil;
+import org.lamsfoundation.lams.util.zipfile.ZipFileUtilException;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+import com.thoughtworks.xstream.XStream;
+/**
+ * Export tool content service bean.
+ * @author Steve.Ni
+ *
+ * @version $Revision$
+ */
+public class ExportToolContentService implements IExportToolContentService, ApplicationContextAware {
+ public static final String LEARNING_DESIGN_SERVICE_BEAN_NAME = "learningDesignService";
+ //export tool content zip file prefix
+ public static final String EXPORT_TOOLCONTNET_ZIP_PREFIX = "lams_toolcontent_";
+ public static final String EXPORT_TOOLCONTNET_CONTENT_FOLDER = "content";
+ public static final String EXPORT_TOOLCONTNET_FOLDER_SUFFIX = "export_toolcontent";
+ public static final String EXPORT_TOOLCONTNET_ZIP_SUFFIX = ".zip";
+ public static final String LEARNING_DESIGN_FILE_NAME = "learning_design.xml";
+
+ private Logger log = Logger.getLogger(ExportToolContentService.class);
+
+ private ApplicationContext applicationContext;
+
+ //spring injection properties
+ private ActivityDAO activityDAO;
+
+ /**
+ * @see org.lamsfoundation.lams.authoring.service.IExportToolContentService.exportToolContent(Long)
+ */
+ public String exportToolContent(Long learningDesignId) throws ExportToolContentException{
+ try {
+ //root temp directory, put target zip file
+ String rootDir = FileUtil.createTempDirectory(EXPORT_TOOLCONTNET_FOLDER_SUFFIX);
+ //content directory to put all toolContent, root file is learning design xml.
+ String contentDir = getFullPath(rootDir,EXPORT_TOOLCONTNET_CONTENT_FOLDER);
+ if(!(new File(contentDir).mkdir()))
+ throw new ExportToolContentException("Unable to create content directory.");
+
+ String targetZipFileName = getFullPath(rootDir, EXPORT_TOOLCONTNET_ZIP_PREFIX
+ + learningDesignId + EXPORT_TOOLCONTNET_ZIP_SUFFIX);
+
+ String ldFileName = getFullPath(contentDir,LEARNING_DESIGN_FILE_NAME);
+ Writer ldFile = new FileWriter(new File(ldFileName));
+
+ ILearningDesignService service = getLearningDesignService();
+ LearningDesignDTO ldDto = service.getLearningDesignDTO(learningDesignId);
+ XStream xstream = new XStream();
+ xstream.toXML(ldDto,ldFile);
+
+ return ZipFileUtil.createZipFile(targetZipFileName, rootDir);
+ } catch (FileUtilException e) {
+ log.error("FileUtilExcpetion:" + e.toString());
+ throw new ExportToolContentException(e);
+ } catch (ZipFileUtilException e) {
+ log.error("ZipFileUtilException:" + e.toString());
+ throw new ExportToolContentException(e);
+ } catch (IOException e) {
+ log.error("IOException:" + e.toString());
+ throw new ExportToolContentException(e);
+ }
+ }
+
+ //******************************************************************
+ // ApplicationContextAware method implementation
+ //******************************************************************
+ public void setApplicationContext(ApplicationContext context) throws BeansException {
+ this.applicationContext = context;
+ }
+ //******************************************************************
+ // Private methods
+ //******************************************************************
+ private ILearningDesignService getLearningDesignService(){
+ return (ILearningDesignService) applicationContext.getBean(AuthoringConstants.LEARNING_DESIGN_SERVICE_BEAN_NAME);
+ }
+
+ private String getFullPath(String path, String file){
+ return path + File.separator + file;
+ }
+
+ //******************************************************************
+ // Spring injection properties set/get
+ //******************************************************************
+ public ActivityDAO getActivityDAO() {
+ return activityDAO;
+ }
+ public void setActivityDAO(ActivityDAO activityDAO) {
+ this.activityDAO = activityDAO;
+ }
+}