com.elearing.admin.DescriptiveCRUD.java Source code

Java tutorial

Introduction

Here is the source code for com.elearing.admin.DescriptiveCRUD.java

Source

    /*
     * 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("&lt;"); break;
         case '>': sb.append("&gt;"); break;
         case '&': sb.append("&amp;"); break;
         case '"': sb.append("&quot;"); break;
         case '': sb.append("&agrave;");break;
         case '': sb.append("&Agrave;");break;
         case '': sb.append("&acirc;");break;
         case '': sb.append("&Acirc;");break;
         case '': sb.append("&auml;");break;
         case '': sb.append("&Auml;");break;
         case '': sb.append("&aring;");break;
         case '': sb.append("&Aring;");break;
         case '': sb.append("&aelig;");break;
         case '': sb.append("&AElig;");break;
         case '': sb.append("&ccedil;");break;
         case '': sb.append("&Ccedil;");break;
         case '': sb.append("&eacute;");break;
         case '': sb.append("&Eacute;");break;
         case '': sb.append("&egrave;");break;
         case '': sb.append("&Egrave;");break;
         case '': sb.append("&ecirc;");break;
         case '': sb.append("&Ecirc;");break;
         case '': sb.append("&euml;");break;
         case '': sb.append("&Euml;");break;
         case '': sb.append("&iuml;");break;
         case '?': sb.append("&Iuml;");break;
         case '': sb.append("&ocirc;");break;
         case '': sb.append("&Ocirc;");break;
         case '': sb.append("&ouml;");break;
         case '': sb.append("&Ouml;");break;
         case '': sb.append("&oslash;");break;
         case '': sb.append("&Oslash;");break;
         case '': sb.append("&szlig;");break;
         case '': sb.append("&ugrave;");break;
         case '': sb.append("&Ugrave;");break;         
         case '': sb.append("&ucirc;");break;         
         case '': sb.append("&Ucirc;");break;
         case '': sb.append("&uuml;");break;
         case '': sb.append("&Uuml;");break;
         case '': sb.append("&reg;");break;         
         case '': sb.append("&copy;");break;   
         case '': sb.append("&euro;"); break;
         // be carefull with this one (non-breaking whitee space)
         case ' ': sb.append("&nbsp;");break;         
         
         default:  sb.append(c); break;
      }
   }
   return sb.toString();
}

    }