Index: lams_tool_nb/src/java/org/lamsfoundation/lams/tool/noticeboard/web/NbAuthoringAction.java =================================================================== diff -u -r7d5102242e4e37d1e30ad0b0e9a6279117dd93ff -r747ce702953f6b156641a9293a372eb9f2318cc0 --- lams_tool_nb/src/java/org/lamsfoundation/lams/tool/noticeboard/web/NbAuthoringAction.java (.../NbAuthoringAction.java) (revision 7d5102242e4e37d1e30ad0b0e9a6279117dd93ff) +++ lams_tool_nb/src/java/org/lamsfoundation/lams/tool/noticeboard/web/NbAuthoringAction.java (.../NbAuthoringAction.java) (revision 747ce702953f6b156641a9293a372eb9f2318cc0) @@ -21,6 +21,8 @@ package org.lamsfoundation.lams.tool.noticeboard.web; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Map; import java.util.HashMap; @@ -33,15 +35,25 @@ import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; + import org.lamsfoundation.lams.tool.noticeboard.NoticeboardConstants; import org.lamsfoundation.lams.tool.noticeboard.NoticeboardContent; +import org.lamsfoundation.lams.tool.noticeboard.NoticeboardAttachment; import org.lamsfoundation.lams.tool.noticeboard.NbApplicationException; import org.lamsfoundation.lams.tool.noticeboard.util.NbWebUtil; +import org.lamsfoundation.lams.tool.noticeboard.util.NbToolContentHandler; import org.lamsfoundation.lams.tool.noticeboard.service.INoticeboardService; import org.lamsfoundation.lams.tool.noticeboard.service.NoticeboardServiceProxy; - +import org.apache.struts.upload.FormFile; import org.lamsfoundation.lams.util.WebUtil; +//import org.lamsfoundation.lams.contentrepository.CrNodeVersionProperty; +import org.lamsfoundation.lams.contentrepository.InvalidParameterException; +import org.lamsfoundation.lams.contentrepository.NodeKey; +import org.lamsfoundation.lams.contentrepository.RepositoryCheckedException; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + /** * Creation Date: 20-05-05 * Modified Date: 03-06-05 @@ -66,6 +78,17 @@ public class NbAuthoringAction extends LamsLookupDispatchAction { static Logger logger = Logger.getLogger(NbAuthoringAction.class.getName()); + private NbToolContentHandler toolContentHandler; + + private NbToolContentHandler getToolContentHandler() + { + if ( toolContentHandler == null ) { + WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); + toolContentHandler = (NbToolContentHandler) wac.getBean(NbToolContentHandler.SPRING_BEAN_NAME); + } + return toolContentHandler; + } + protected Map getKeyMethodMap() { @@ -75,10 +98,14 @@ map.put(NoticeboardConstants.BUTTON_INSTRUCTIONS, "instructions"); map.put(NoticeboardConstants.BUTTON_DONE, "done"); map.put(NoticeboardConstants.BUTTON_SAVE, "save"); + map.put(NoticeboardConstants.BUTTON_UPLOAD, "upload"); return map; } + /** + * + */ public ActionForward unspecified( ActionMapping mapping, ActionForm form, @@ -165,8 +192,117 @@ return mapping.findForward(NoticeboardConstants.BASIC_PAGE); /** TODO: once the content is saved, should close the window */ } + /** + * This method will either upload an online instructions file or an offline instructions file. + * It will upload an online file if the bean property onlineFile is not null and similarly, + * will upload an offline file if the bean property offlineFile is not null. + * By using the term "upload", we are saving the file information on the local database (?) + * + * @param mapping + * @param form + * @param request + * @param response + * @return + * @throws NbApplicationException + */ + public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) + throws InvalidParameterException, FileNotFoundException, RepositoryCheckedException, IOException, NbApplicationException { + + + //set up the values in the map + //call the uploadFile method from toolContentHandler + NbAuthoringForm nbForm = (NbAuthoringForm)form; + FormFile theFile; + INoticeboardService nbService = NoticeboardServiceProxy.getNbService(getServlet().getServletContext()); + + Long content_id = NbWebUtil.convertToLong(nbForm.getToolContentId()); + //throws exception if the content id does not exist + checkContentId(content_id); + NoticeboardContent nbContent = nbService.retrieveNoticeboard(content_id); + + //check if the file uploaded is an online instructions file or offline instructions file. + //if one of the types is null, then the other one must have been uploaded. + //here we check if the file is an online one + + if ((nbForm.getOnlineFile() != null && (nbForm.getOnlineFile().getFileName().trim().length() != 0)) || + (nbForm.getOfflineFile() != null && (nbForm.getOfflineFile().getFileName().trim().length() != 0))) + { + boolean isOnlineFile = ((nbForm.getOnlineFile() != null && (nbForm.getOnlineFile().getFileName().trim().length() != 0)) ? true: false ); + theFile = (isOnlineFile ? nbForm.getOnlineFile() : nbForm.getOfflineFile()); + String fileType = isOnlineFile ? NoticeboardAttachment.TYPE_ONLINE : NoticeboardAttachment.TYPE_OFFLINE; + + //check to see if FileExists + NoticeboardAttachment file = nbService.retrieveAttachmentByFilename(theFile.getFileName()); + + + try + { + + if (fileExists(content_id, file, isOnlineFile)) + { + /** + * The same file belonging to the same toolcontent id already exists. + * The old version of this file is deleted from the content repository + * and the new one is saved in the content repository. + * + * The entry in the database is then updated with the new uuid and version + */ + getToolContentHandler().deleteFile(file.getUuid()); + + NodeKey node = getToolContentHandler().uploadFile(theFile.getInputStream(), theFile.getFileName(), + theFile.getContentType(), fileType); + + file.setUuid(node.getUuid()); //only need to update the uuid, the rest of the info is the same + file.setVersionId(node.getVersion()); + nbService.saveAttachment(file); + + } + else + { + /** + * This is a new file and so is saved to the content repository. + * + * A new entry is added to the database. + */ + NodeKey node = getToolContentHandler().uploadFile(theFile.getInputStream(), theFile.getFileName(), + theFile.getContentType(), fileType); + file = new NoticeboardAttachment(); + file.setFilename(theFile.getFileName()); + file.setOnlineFile(isOnlineFile); + file.setNbContent(nbContent); + file.setUuid(node.getUuid()); + file.setVersionId(node.getVersion()); /** TODO: change versionId to version */ + nbService.saveAttachment(file); + } + + String keyName = file.getFilename() + "-" + fileType; + + //add the files to the map + Map attachmentMap = nbForm.getAttachments(); + if (!attachmentMap.containsKey(keyName)) + { + attachmentMap.put(keyName, file); + } + + NbWebUtil.addUploadsToSession(request, attachmentMap); + //reset the fields so that more files can be uploaded + nbForm.setOfflineFile(null); + nbForm.setOnlineFile(null); + } + catch (FileNotFoundException e) { + logger.error("Unable to uploadfile",e); + throw new NbApplicationException("Unable to upload file, exception was "+e.getMessage()); + } catch (IOException e) { + logger.error("Unable to uploadfile",e); + throw new NbApplicationException("Unable to upload file, exception was "+e.getMessage()); + } catch (RepositoryCheckedException e) { + logger.error("Unable to uploadfile",e); + throw new NbApplicationException("Unable to upload file, exception was "+e.getMessage()); + } + } + return mapping.findForward(NoticeboardConstants.INSTRUCTIONS_PAGE); + } - /** * It is assumed that the contentId is passed as a http parameter * if the contentId is null, an exception is thrown, otherwise proceed as normal @@ -210,6 +346,44 @@ form.setOfflineInstructions(offlineInstruction); } + /** + * This method checks whether a file + * already exists in the database. If this file already exists, then the + * type of file is checked, along with the associated toolContentId. + * If both files are also of the same type, ie. + * both are offline files or both are online files and the tool content Id matches + * toolContentId, then this method will + * return true, to indicate that the file already exists in the database. + * Otherwise false is returned. + * @param filename The filename of the attachment to check + * @param isOnlineFile A boolean to indicate whether it is an online File. 1 indicates an online file. The value 0 indicates an offline file. + * @return + */ + private boolean fileExists(Long toolContentId, NoticeboardAttachment fileFromDatabase, boolean isFileUploadedAnOnlineFile) + { + /** + * TODO: check whether they refer to the same contentIdk + */ + if (fileFromDatabase == null) + { + return false; + } + else + { + /** + * true && true = true <-- both files are both online files + * true && false = false <-- the files are different types + * false && true = false <-- the files are different types + * false && false = true <-- both files are offline files + */ + if (fileFromDatabase.isOnlineFile() && isFileUploadedAnOnlineFile && fileFromDatabase.getNbContent().getNbContentId().equals(toolContentId)) + { + return true; + } + else + return false; + } + }