Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.elearing.admin; import com.elearing.entity.DescQues; import com.elearing.entity.SubChapter; import com.elearing.imageSlide.ChapterImageSlide; import com.elearing.impl.DescQuesDaoImpl; import com.elearing.impl.SubChapterDaoImpl; import java.io.File; import java.io.InputStream; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils; import org.primefaces.model.UploadedFile; /** * * @author Emrul */ @ManagedBean @SessionScoped public class DescriptiveCRUD { public SubChapter subChapter = new SubChapter(); public SubChapterDaoImpl subChapterDaoImpl = new SubChapterDaoImpl(); public DescQues descQues = new DescQues(); public DescQuesDaoImpl descQuesDaoImpl = new DescQuesDaoImpl(); private String imageName = null, imagePath, saveImagePath; private Boolean save = false; public UploadedFile file; public ChapterImageSlide chapterImageSlide = new ChapterImageSlide(); public ChapterImageSlide getChapterImageSlide() { return chapterImageSlide; } public void setChapterImageSlide(ChapterImageSlide chapterImageSlide) { this.chapterImageSlide = chapterImageSlide; } public String getImageName() { return imageName; } public void setImageName(String imageName) { this.imageName = imageName; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public String getSaveImagePath() { return saveImagePath; } public void setSaveImagePath(String saveImagePath) { this.saveImagePath = saveImagePath; } public Boolean getSave() { return save; } public void setSave(Boolean save) { this.save = save; } public UploadedFile getFile() { return file; } public void setFile(UploadedFile file) { this.file = file; } public SubChapter getSubChapter() { return subChapter; } public void setSubChapter(SubChapter subChapter) { this.subChapter = subChapter; } public DescQuesDaoImpl getDescQuesDaoImpl() { return descQuesDaoImpl; } public void setDescQuesDaoImpl(DescQuesDaoImpl descQuesDaoImpl) { this.descQuesDaoImpl = descQuesDaoImpl; } public SubChapterDaoImpl getSubChapterDaoImpl() { return subChapterDaoImpl; } public void setSubChapterDaoImpl(SubChapterDaoImpl subChapterDaoImpl) { this.subChapterDaoImpl = subChapterDaoImpl; } public DescQues getDescQues() { return descQues; } public void setDescQues(DescQues descQues) { this.descQues = descQues; } public void upload() { if (file != null) { try { FacesContext context = FacesContext.getCurrentInstance(); ServletContext servletContext = (ServletContext) context.getExternalContext().getContext(); String bdpath = servletContext.getRealPath("/"); String webcut = bdpath.substring(0, bdpath.lastIndexOf("\\")); String buildcut = webcut.substring(0, webcut.lastIndexOf("\\")); String mainUrlPath = buildcut.substring(0, buildcut.lastIndexOf("\\")); //this code for web main logic cut //HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest(); //String requestURL = request.getRequestURL().toString(); //String url = requestURL.substring(0, requestURL.lastIndexOf("/")); //get content InputStream inputStr = file.getInputstream(); imagePath = mainUrlPath + "\\web\\resources\\image\\descriptiveImage\\" + file.getFileName(); //upload file copy to project folder File destFile = new File(imagePath); if (!destFile.exists()) { FileUtils.copyInputStreamToFile(inputStr, destFile); } imageName = file.getFileName(); System.out.println(imageName); } catch (Exception ex) { Logger.getLogger(DescriptiveCRUD.class.getName()).log(Level.SEVERE, null, ex); } } } public String addDescrip() { descQues.setSubChapter(subChapterDaoImpl.getObjectById(subChapter.getSchId())); descQuesDaoImpl.insert(descQues); return null; } public void chap() { if (subChapter.getSchId() != null) { subChapter = subChapterDaoImpl.getObjectById(subChapter.getSchId()); } } public List<DescQues> getDescriptive() { subChapter = subChapterDaoImpl.getObjectById(subChapter.getSchId()); descQues.setSubChapter(subChapter); return descQuesDaoImpl.getAllDesc(descQues.getSubChapter()); } public String escapeHTML(String s){ StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; case '': sb.append("à");break; case '': sb.append("À");break; case '': sb.append("â");break; case '': sb.append("Â");break; case '': sb.append("ä");break; case '': sb.append("Ä");break; case '': sb.append("å");break; case '': sb.append("Å");break; case '': sb.append("æ");break; case '': sb.append("Æ");break; case '': sb.append("ç");break; case '': sb.append("Ç");break; case '': sb.append("é");break; case '': sb.append("É");break; case '': sb.append("è");break; case '': sb.append("È");break; case '': sb.append("ê");break; case '': sb.append("Ê");break; case '': sb.append("ë");break; case '': sb.append("Ë");break; case '': sb.append("ï");break; case '?': sb.append("Ï");break; case '': sb.append("ô");break; case '': sb.append("Ô");break; case '': sb.append("ö");break; case '': sb.append("Ö");break; case '': sb.append("ø");break; case '': sb.append("Ø");break; case '': sb.append("ß");break; case '': sb.append("ù");break; case '': sb.append("Ù");break; case '': sb.append("û");break; case '': sb.append("Û");break; case '': sb.append("ü");break; case '': sb.append("Ü");break; case '': sb.append("®");break; case '': sb.append("©");break; case '': sb.append("€"); break; // be carefull with this one (non-breaking whitee space) case ' ': sb.append(" ");break; default: sb.append(c); break; } } return sb.toString(); } }