Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java =================================================================== diff -u -r4e143f1522cf63a2c32a7cb22a3c1ea57853284b -r7298890ace7050aeafa1e04ee0d224266ac2d848 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java (.../ExportPortfolioService.java) (revision 4e143f1522cf63a2c32a7cb22a3c1ea57853284b) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java (.../ExportPortfolioService.java) (revision 7298890ace7050aeafa1e04ee0d224266ac2d848) @@ -27,22 +27,28 @@ */ package org.lamsfoundation.lams.learning.export.service; -import java.util.ArrayList; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.Vector; +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.learning.export.ExportPortfolioConstants; import org.lamsfoundation.lams.learning.export.ExportPortfolioException; import org.lamsfoundation.lams.learning.export.Portfolio; import org.lamsfoundation.lams.learning.service.ILearnerService; import org.lamsfoundation.lams.learningdesign.Activity; import org.lamsfoundation.lams.learningdesign.ActivityOrderComparator; import org.lamsfoundation.lams.learningdesign.LearningDesign; import org.lamsfoundation.lams.learningdesign.ToolActivity; +import org.lamsfoundation.lams.learningdesign.dao.IActivityDAO; import org.lamsfoundation.lams.learningdesign.dao.ITransitionDAO; import org.lamsfoundation.lams.lesson.LearnerProgress; import org.lamsfoundation.lams.lesson.Lesson; @@ -58,20 +64,27 @@ import org.lamsfoundation.lams.util.zipfile.ZipFileUtil; import org.lamsfoundation.lams.util.zipfile.ZipFileUtilException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.MalformedURLException; + + /** * @author mtruong * */ public class ExportPortfolioService implements IExportPortfolioService { - + private static Logger log = Logger.getLogger(ExportPortfolioService.class); + /** Two different starting points depending on whether the export was * initiated by a teacher or learner * */ private ILamsCoreToolService lamsCoreToolService; private ITransitionDAO transitionDAO; - private ILearnerService learnerService; + private IActivityDAO activityDAO; + private ILearnerService learnerService; /** @@ -89,13 +102,60 @@ } /** + * @param activityDAO The activityDAO to set. + */ + public void setActivityDAO(IActivityDAO activityDAO) { + this.activityDAO = activityDAO; + } + + /** * @param lamsCoreToolService The lamsCoreToolService to set. */ public void setLamsCoreToolService(ILamsCoreToolService lamsCoreToolService) { this.lamsCoreToolService = lamsCoreToolService; } - /** returns a Portfolio array , change from void to Portfolio[]*/ + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#getOrderedActivityList(org.lamsfoundation.lams.learningdesign.LearningDesign) */ + public Vector getOrderedActivityList(LearningDesign learningDesign) + { + if (learningDesign == null) + { + String error="the learningdesign is null. Cannot continue"; + throw new ExportPortfolioException(error); + } + HashMap activityTree = learningDesign.getActivityTree(); + Vector v = new Vector(); + Activity nextActivity = learningDesign.getFirstActivity(); + + while(nextActivity!=null){ + addActivityToVector(activityTree, v, nextActivity); + nextActivity = transitionDAO.getNextActivity(nextActivity.getActivityId()); + } + return v; + } + + /** + * Used by getOrderedActivityList(LearningDesign) + * + * @param activityTree + * @param v + * @param nextActivity + */ + private void addActivityToVector(HashMap activityTree, Vector v, Activity activity) { + Set childActivities = (Set) activityTree.get(activity.getActivityId()); + if(childActivities.size()!=0){ + // must have been a complex activity + v.addAll(childActivities); + }else{ + // must be a simple activity + v.add(activity); + } + } + + + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#exportPortfolioForTeacher(org.lamsfoundation.lams.lesson.Lesson) */ public Portfolio[] exportPortfolioForTeacher(Lesson lesson) { @@ -107,101 +167,56 @@ throw new ExportPortfolioException(error); } Vector activities = getOrderedActivityList(lesson.getLearningDesign()); - Portfolio[] portfolios = null; - + Vector portfolios = null; + Portfolio[] exports = null; + Vector portfolioArray = null; try { portfolios = setupPortfolios(activities, ToolAccessMode.TEACHER, null); - } - catch (LamsToolServiceException e) - { - //throw new ExportPortfolioException(e); - } - return portfolios; - } - - /* returns a Portfolio array , change from void to Portfolio[] - * Not used currently - was having problems with lazy initialisation. Delete once export - * portfolio is working. - public Portfolio[] exportPortfolioForStudent(LearnerProgress learnerProgress, User user, boolean anonymity) - { - if (learnerProgress == null || user == null) - { - String error="learnerProgress or user is null"; - throw new ExportPortfolioException(error); - } - //LearnerProgress progress = learnerService.getProgress(learner, lesson); - Portfolio[] portfolios = null; - Vector activities = getOrderedActivityList(learnerProgress); - try - { - portfolios = setupPortfolios(activities, ToolAccessMode.LEARNER, user); + exports = doExport(portfolios, ExportPortfolioConstants.EXPORT_TMP_DIR); + } catch (LamsToolServiceException e) { //throw new ExportPortfolioException(e); } - return portfolios; - - + return exports; } - */ - /* Not used currently - was having problems with lazy initialisation. Delete once export - * portfolio is working. - public Portfolio[] exportPortfolioForStudent(Lesson lesson, User user, boolean anonymity) - { - if (lesson == null || user == null) - { - String error="learnerProgress or user is null"; - throw new ExportPortfolioException(error); - } - LearnerProgress learnerProgress = learnerService.getProgress(user, lesson); - - Portfolio[] portfolios = null; - Vector activities = getOrderedActivityList(learnerProgress); - - try - { - portfolios = setupPortfolios(activities, ToolAccessMode.LEARNER, user); - } - catch (LamsToolServiceException e) - { - //throw new ExportPortfolioException(e); - } - return portfolios; - - - } - */ + + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#exportPortfolioForStudent(java.lang.Long, org.lamsfoundation.lams.usermanagement.User,boolean) */ public Portfolio[] exportPortfolioForStudent(Long learnerProgressId, User user, boolean anonymity) { if (learnerProgressId == null || user == null) { String error="learnerProgress or user is null"; throw new ExportPortfolioException(error); } - //LearnerProgress progress = learnerService.getProgress(learner, lesson); - Portfolio[] portfolios = null; + Vector activities = getOrderedActivityList(learnerProgressId); - - try + + Vector portfolios = null; + Portfolio[] exports = null; + Vector portfolioArray = null; + try { portfolios = setupPortfolios(activities, ToolAccessMode.LEARNER, user); + + exports = doExport(portfolios, ExportPortfolioConstants.EXPORT_TMP_DIR); } catch (LamsToolServiceException e) { //throw new ExportPortfolioException(e); } - return portfolios; + + return exports; } + - - - /** return an array of portfolio objects that can be used int he web layer and display appropriate information **/ - public Portfolio[] setupPortfolios(Vector sortedActivityList, ToolAccessMode accessMode, User user) throws LamsToolServiceException//the iterator will traverse the set in ascending element order + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#setupPortfolios(java.util.Vector, org.lamsfoundation.lams.tool.ToolAccessMode, org.lamsfoundation.lams.usermanagement.User) */ + public Vector setupPortfolios(Vector sortedActivityList, ToolAccessMode accessMode, User user) throws LamsToolServiceException { - if (sortedActivityList == null) { String error="the activityList is null"; @@ -215,17 +230,16 @@ Iterator i = sortedActivityList.iterator(); - List portfolioList = new ArrayList(); + Vector portfolioList = new Vector(); Map mapOfValuesToAppend = new HashMap(); while(i.hasNext()) { Activity activity = (Activity)i.next(); - // if (!(activity instanceof ComplexActivity)) if (activity.isToolActivity()) { - ToolActivity toolActivity = (ToolActivity)activity; + ToolActivity toolActivity = (ToolActivity)activityDAO.getActivityByActivityId(activity.getActivityId()); Portfolio portfolio = createPortfolio(toolActivity); //setup the export url @@ -239,26 +253,20 @@ else if (accessMode == ToolAccessMode.TEACHER) { mapOfValuesToAppend.put(WebUtil.PARAM_MODE, ToolAccessMode.TEACHER.toString()); - mapOfValuesToAppend.put(WebUtil.PARAM_TOOL_CONTENT_ID, toolActivity.getToolContentId().toString()); + mapOfValuesToAppend.put("toolContentId", toolActivity.getToolContentId().toString()); } String url = setupExportUrl(mapOfValuesToAppend, portfolio.getExportUrl()); - portfolio.setExportUrl(url); + portfolio.setExportUrl(url); portfolioList.add(portfolio); } } - return (Portfolio[])portfolioList.toArray(new Portfolio[portfolioList.size()]); - + return portfolioList; } - /** - * all information would be the same, except when setting up the url, - * if export was done by a teacher, then the mode and toolContentId is appended - * if the export was done by a student , then the mode and toolSessionId and userId is appended - * @param activity - * @return - */ + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#createPortfolio(org.lamsfoundation.lams.learningdesign.ToolActivity) */ public Portfolio createPortfolio(ToolActivity activity) { if (activity == null) @@ -276,6 +284,7 @@ return p; } + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#setupExportUrl(java.util.Map, java.lang.String) */ public String setupExportUrl(Map parametersToAppend, String url) { int mapSize = parametersToAppend.size(); @@ -291,81 +300,9 @@ return urlToReturn; } - public Vector getOrderedActivityList(LearningDesign learningDesign) - { - if (learningDesign == null) - { - String error="the learningdesign is null. Cannot continue"; - throw new ExportPortfolioException(error); - } - HashMap activityTree = learningDesign.getActivityTree(); - Vector v = new Vector(); - Activity nextActivity = learningDesign.getFirstActivity(); - - while(nextActivity!=null){ - addActivityToVector(activityTree, v, nextActivity); - nextActivity = transitionDAO.getNextActivity(nextActivity.getActivityId()); - } - return v; - } - /** - * Used by getOrderedActivityList(LearningDesign) - * - * @param activityTree - * @param v - * @param nextActivity - */ - private void addActivityToVector(HashMap activityTree, Vector v, Activity activity) { - Set childActivities = (Set) activityTree.get(activity.getActivityId()); - if(childActivities.size()!=0){ - // must have been a complex activity - v.addAll(childActivities); - }else{ - // must be a simple activity - v.add(activity); - } - } - - /* Not used currently - was having problems with lazy initialisation. Delete once export - * portfolio is working. - public Vector getOrderedActivityList(LearnerProgress learnerProgress) - { - if (learnerProgress == null) - { - String error="the learnerProgress is null"; - throw new ExportPortfolioException(error); - } - Set activitySet = learnerProgress.getCompletedActivities(); - //if (activitySet.size() == 0) - //{ - // String error="Cannot export your portfolio. You have not completed any activities yet."; - // throw new ExportPortfolioException(error); - //} - TreeSet sortedActivities = new TreeSet(new ActivityOrderComparator()); - sortedActivities.addAll(activitySet); - - Vector v = new Vector(); - //sort the activities according to order_id or if not activityId - - - Iterator i = sortedActivities.iterator(); - - while (i.hasNext()) - { - Activity activity = (Activity)i.next(); - if (!activity.isComplexActivity()) - { - v.add(activity); - } - } - - return v; - } - */ - - + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#getOrderedActivityList(java.lang.Long) */ public Vector getOrderedActivityList(Long learnerProgressId) { if (learnerProgressId == null) @@ -404,7 +341,8 @@ } - public boolean createTempDirectory(String directoryName) + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#createTempDirectory(String) */ +/* public boolean createTempDirectory(String directoryName) { boolean created; try @@ -418,8 +356,9 @@ } return created; - } + } */ + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#zipPortfolio(String, String) */ public String zipPortfolio(String filename, String directoryToZip) { String zipfileName = null; @@ -434,4 +373,200 @@ return zipfileName; } + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#doExport(java.util.Vector, String) */ + public Portfolio[] doExport(Vector portfolios, String tempDirectoryName) + { + Iterator i = portfolios.iterator(); + String baseSubDirectoryName = "Activity"; + String activitySubDirectory; + String exportURL; + String toolLink; + + String mainFileName = null; + //iterate through the list of portfolios, create subdirectory, + while(i.hasNext()) + { + Portfolio portfolio = (Portfolio)i.next(); + //get the activityId so the subdirectory can be created + String subDirectoryName = baseSubDirectoryName + portfolio.getActivityId().toString(); + if(!createSubDirectory(tempDirectoryName, subDirectoryName)) + { + throw new ExportPortfolioException("The subdirectory could not be created"); + } + // The directory in which the activity must place its files + activitySubDirectory = tempDirectoryName + File.separator + subDirectoryName; + portfolio.setDirectoryName(activitySubDirectory); + //append directory name to export url call. + exportURL = WebUtil.appendParameterToURL(portfolio.getExportUrl(), WebUtil.PARAM_DIRECTORY_NAME, activitySubDirectory); + + //append the host name to the export url for eg: http://localhost:8080/export_url + String absoluteExportURL = ExportPortfolioConstants.HOST + exportURL; + + //set the new exportURL + portfolio.setExportUrl(absoluteExportURL); + + //get tool to export its files + mainFileName = exportToolPortfolio(portfolio); + + toolLink = subDirectoryName + File.separator + mainFileName; + portfolio.setMainFileName(mainFileName); + portfolio.setToolLink(toolLink); + + // break; //testing purposes only, only go through one iteration to make sure tool exports content + + } + + return (Portfolio[])portfolios.toArray(new Portfolio[portfolios.size()]); + + + } + + /** + * Helper method which calls the FileUtil to create a subdirectory. This method might not be needed, + * can be used why calling the FileUtil directly. + * @param parentDir The name of the parent directory + * @param subDir The name of the child directory to create. + * @return true is the subdirectory was created, false otherwise + */ + private boolean createSubDirectory(String parentDir, String subDir) + { + boolean created; + try + { + created = FileUtil.createDirectory(parentDir, subDir); + + } + catch (FileUtilException e) + { + throw new ExportPortfolioException("An error has occurred while creating the sub directory " + subDir); + } + + return created; + } + + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#exportToolPortfolio(org.lamsfoundation.lams.learning.export.Portfolio) */ + public String exportToolPortfolio(Portfolio tool) + { + String mainFileName = null; + try + { + //URL url = new URL("http://localhost:8080/lams/tool/lanb11/portfolioExport?directoryName=D:\\tempExportDir\\Activity20&mode=teacher&toolContentId=355"); + URL url = new URL(tool.getExportUrl()); + HttpURLConnection con = (HttpURLConnection)url.openConnection(); + + BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream())); + + //the main file name is returned, or null + + mainFileName = input.readLine(); + + input.close(); + + return mainFileName; + + } + catch(MalformedURLException e) + { + throw new ExportPortfolioException("The URL given is invalid. ",e); + } + catch(FileNotFoundException e) + { + throw new ExportPortfolioException("The directory or file may not exist. ",e); + } + catch(IOException e) + { + throw new ExportPortfolioException("A problem has occurred while writing file. ", e); + } + + } + + //Methods that are not used + + /* public Vector getOrderedActivityList(LearnerProgress learnerProgress) + { + if (learnerProgress == null) + { + String error="the learnerProgress is null"; + throw new ExportPortfolioException(error); + } + Set activitySet = learnerProgress.getCompletedActivities(); + // if (activitySet.size() == 0) + //{ + //String error="Cannot export your portfolio. You have not completed any activities yet."; + //throw new ExportPortfolioException(error); + // } + TreeSet sortedActivities = new TreeSet(new ActivityOrderComparator()); + sortedActivities.addAll(activitySet); + + Vector v = new Vector(); + //sort the activities according to order_id or if not activityId + + + Iterator i = sortedActivities.iterator(); + + while (i.hasNext()) + { + Activity activity = (Activity)i.next(); + if (!activity.isComplexActivity()) + { + v.add(activity); + } + } + + return v; + } + */ + + /* + public Portfolio[] exportPortfolioForStudent(LearnerProgress learnerProgress, User user, boolean anonymity) + { + if (learnerProgress == null || user == null) + { + String error="learnerProgress or user is null"; + throw new ExportPortfolioException(error); + } + //LearnerProgress progress = learnerService.getProgress(learner, lesson); + Portfolio[] portfolios = null; + Vector activities = getOrderedActivityList(learnerProgress); + + try + { + portfolios = setupPortfolios(activities, ToolAccessMode.LEARNER, user); + + } + catch (LamsToolServiceException e) + { + //throw new ExportPortfolioException(e); + } + return portfolios; + + } + + public Portfolio[] exportPortfolioForStudent(Lesson lesson, User user, boolean anonymity) + { + if (lesson == null || user == null) + { + String error="learnerProgress or user is null"; + throw new ExportPortfolioException(error); + } + LearnerProgress learnerProgress = learnerService.getProgress(user, lesson); + + Portfolio[] portfolios = null; + Vector activities = getOrderedActivityList(learnerProgress); + + try + { + portfolios = setupPortfolios(activities, ToolAccessMode.LEARNER, user); + } + catch (LamsToolServiceException e) + { + //throw new ExportPortfolioException(e); + } + return portfolios; + + + } */ + }