/** * This method is for update Presenter * * @param presenter Presenter object * @param requestParameters Servlet Request Parameter * @return Presenter Object */ public Presenter updatePresenter(Presenter presenter, HttpServletRequest request) { log.log(Level.INFO, "PresenterMaintenance --> update presenter"); User user = (User) request.getSession().getAttribute(SessionAttributes.CURR_USER_OBJ); AuditTrailMaintenance auditTrailMaintenance = new AuditTrailMaintenance(); int i = 0; String msg = "The Presenter has not been successfully updated."; String tempDir = System.getProperty("java.io.tmpdir"); FileOutputStream stream = null; String thumbnail_file_name = ""; String material_file_name = ""; // ResourceBundle msgProps = ResourceBundle.getBundle("configurations"); // String PresenterPath = msgProps.getString("PresenterPath"); Map<String, String> configurationMap = (Map<String, String>) request.getSession().getAttribute(ApplicationAttribute.CONFIGURATION_PROPERTIES_MAP); String PresenterPath = configurationMap.get("PresenterPath"); String serverFilename = ""; String filePath = ""; Session session = HibernateFactory.openSession(); Transaction tx = session.beginTransaction(); presenter.setModified_by(user.getStaffLoginId()); presenter.setModified_date(new Date()); try { if (request.getSession().getAttribute("thumbnail_file_name") != null) { thumbnail_file_name = (String) request.getSession().getAttribute("thumbnail_file_name"); } if (request.getSession().getAttribute("material_file_name") != null) { material_file_name = (String) request.getSession().getAttribute("material_file_name"); } String paramater = ""; paramater = " presenterCode!=:presenterCode and presenterName=:presenterName and status=:status"; Query queryDup = session.createQuery("from Presenter where " + paramater + ""); queryDup.setParameter("presenterCode", presenter.getPresenterCode()); queryDup.setParameter("presenterName", presenter.getPresenterName()); queryDup.setParameter("status", true); ArrayList<Presenter> arrActivity = (ArrayList<Presenter>) queryDup.list(); // if(arrActivity.size()>0) // { // msg="The Presenter already exist."; // } // else // { ArrayList<Presenter> list = new ArrayList<Presenter>(); Query query = session.createQuery("from Presenter where presenterCode=:presenterCode"); query.setParameter("presenterCode", presenter.getPresenterCode()); list = (ArrayList<Presenter>) query.list(); Presenter presenterObj = new Presenter(); presenterObj = list.get(0); String oldPresenter = presenterObj.getPresenterName(); String new_presenter = presenter.getPresenterName(); // upload thumbnail file if (thumbnail_file_name != null && !thumbnail_file_name.equals("")) { // chk for new name or old name if (!oldPresenter.equals( new_presenter)) { // if new one then create new presenter and delete old one serverFilename = PresenterPath + "/" + new_presenter; File uploadedFolder = new File(serverFilename); if (!uploadedFolder.exists()) { uploadedFolder.mkdirs(); } FileUtils.deleteFileNFolder(new File(PresenterPath + "/" + oldPresenter)); } else { // if its same then only delete file and store new file serverFilename = PresenterPath + "/" + oldPresenter + "/" + presenterObj.getImage_fileName(); File uploadedFolder = new File(serverFilename); if (uploadedFolder.exists()) { FileUtils.deleteFileNFolder(uploadedFolder); } } // end serverFilename = PresenterPath + "/" + new_presenter; File uploadedFolder = new File(serverFilename); if (!uploadedFolder.exists()) { uploadedFolder.mkdirs(); } byte[] bytearray = (byte[]) request.getSession().getAttribute("thumbnail_byte_session"); BufferedImage imag; try { imag = ImageIO.read(new ByteArrayInputStream(bytearray)); if (thumbnail_file_name.contains(".png") || thumbnail_file_name.contains(".PNG")) ImageIO.write(imag, "png", new File(serverFilename, thumbnail_file_name)); else if (thumbnail_file_name.contains(".jpg") || thumbnail_file_name.contains(".JPG")) ImageIO.write(imag, "jpg", new File(serverFilename, thumbnail_file_name)); else if (thumbnail_file_name.contains(".gif") || thumbnail_file_name.contains(".GIF")) ImageIO.write(imag, "gif", new File(serverFilename, thumbnail_file_name)); } catch (IOException e) { e.printStackTrace(); } // delete temp directory File temp_file = new File(tempDir + "/" + thumbnail_file_name); FileUtils.deleteFileNFolder(temp_file); presenter.setImage_fileName(thumbnail_file_name); presenter.setImage_fileLocation((serverFilename + "/" + thumbnail_file_name).substring(8)); serverFilename = ""; } else { FileUtils.renameFolder(PresenterPath + "/" + oldPresenter, new_presenter); presenter.setImage_fileName(presenterObj.getImage_fileName()); presenter.setImage_fileLocation(presenter.getImage_fileLocation()); } // upload material if (material_file_name != null && !material_file_name.equals("")) { // chk for new name or old name if (!oldPresenter.equals( new_presenter)) { // if new one then create new presenter and delete old one serverFilename = PresenterPath + "/" + new_presenter; File uploadedFolder = new File(serverFilename); if (!uploadedFolder.exists()) { uploadedFolder.mkdirs(); } FileUtils.deleteFileNFolder(new File(PresenterPath + "/" + oldPresenter)); } else { // if its same then only delete file and store new file serverFilename = PresenterPath + "/" + oldPresenter + "/" + presenterObj.getFileName(); File uploadedFolder = new File(serverFilename); if (uploadedFolder.exists()) { FileUtils.deleteFileNFolder(uploadedFolder); } } // end serverFilename = PresenterPath + "/" + new_presenter; File uploadedFolder = new File(serverFilename); if (!uploadedFolder.exists()) { uploadedFolder.mkdirs(); } byte[] bytearray = (byte[]) request.getSession().getAttribute("material_byte_session"); if (material_file_name.contains(".pdf") || material_file_name.contains(".PDF")) { try { stream = new FileOutputStream(serverFilename + "/" + material_file_name); stream.write(bytearray); stream.close(); } catch (Exception e) { e.printStackTrace(); } } // delete temp directory File temp_file = new File(tempDir + "/" + material_file_name); FileUtils.deleteFileNFolder(temp_file); presenter.setFileName(material_file_name); presenter.setFileLocation((serverFilename + "/" + material_file_name).substring(8)); } else { FileUtils.renameFolder(PresenterPath + "/" + oldPresenter, presenter.getPresenterName()); presenter.setFileName(presenterObj.getFileName()); presenter.setFileLocation(presenter.getFileLocation()); } presenter.setModified_by(user.getStaffLoginId()); presenter.setModified_date(new Date()); // presenterObj.setFileName(file_name); // presenterObj.setFileLocation(file_path.replace(oldPresenter, presenter_name)); // category.setHash_code((int)(Math.random() * 1000000000)); HibernateFactory.close(session); session = HibernateFactory.openSession(); tx = session.beginTransaction(); session.update(presenter); if (request.getSession().getAttribute("thumbnail_file_name") != null) { if (thumbnail_file_name.contains(".jpg") || thumbnail_file_name.contains(".JPG") || thumbnail_file_name.contains(".png") || thumbnail_file_name.contains(".PNG") || thumbnail_file_name.contains(".gif") || thumbnail_file_name.contains("GIF")) { String field_name = (String) request.getSession().getAttribute("thumbnail_field_name"); storeMaterialToDB( session, presenter.getPresenterCode(), "UPDATE", request, "thumbnail_byte_session", thumbnail_file_name, field_name); } request .getSession() .removeAttribute( "thumbnail_file_name"); // removing uploaded file details from session after file is // uploaded request.getSession().removeAttribute("thumbnail_byte_session"); // request.getSession().removeAttribute("thumbnail_field_name"); // } if (request.getSession().getAttribute("material_file_name") != null) { if (material_file_name.contains(".pdf") || material_file_name.contains(".PDF")) { String field_name = (String) request.getSession().getAttribute("material_field_name"); storeMaterialToDB( session, presenter.getPresenterCode(), "UPDATE", request, "material_byte_session", material_file_name, field_name); } request .getSession() .removeAttribute( "material_file_name"); // removing uploaded file details from session after file is // uploaded request.getSession().removeAttribute("material_byte_session"); // request.getSession().removeAttribute("material_field_name"); // } tx.commit(); auditTrailMaintenance.insertAuditTrail( new AuditTrail( user.getStaffLoginId() + "", AuditTrail.MODULE_PRESENTER, AuditTrail.FUNCTION_UPDATE, "SUCCESS,Presenter_code:" + presenter.getPresenterCode())); msg = "The Presnter has been successfully updated."; // } } catch (Exception e) { log.log(Level.SEVERE, e.getMessage()); auditTrailMaintenance.insertAuditTrail( new AuditTrail( user.getStaffLoginId() + "", AuditTrail.MODULE_PRESENTER, AuditTrail.FUNCTION_UPDATE, "FAILED,Presenter_code:" + presenter.getPresenterCode())); e.printStackTrace(); } try { HibernateFactory.close(session); } catch (Exception e) { log.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } MsgObject msgObj = new MsgObject(msg); request.setAttribute(RequestAttributes.MESSAGE_OBJ, msgObj); request.setAttribute("CacheName", "Presenter"); Pager pager = retrieveActivityListing(getAllPresenter(request)); request.getSession().setAttribute("pager", pager); request.setAttribute("pager", pager); request .getSession() .setAttribute("pageObj", new PathDetail().getPageObj("PresenterMaintenance")); return presenter; }
/** * This method is for creating new Presenter * * @param presenter Presenter object * @param requestParameters Servlet Request Parameter * @return Presenter Object */ public Presenter insertPresenter(Presenter presenter, HttpServletRequest request) { log.log(Level.INFO, "PresenterMaintenance --> crrating presenter"); User user = (User) request.getSession().getAttribute(SessionAttributes.CURR_USER_OBJ); AuditTrailMaintenance auditTrailMaintenance = new AuditTrailMaintenance(); String msg = "The new Presenter has not been successfully created."; String tempDir = System.getProperty("java.io.tmpdir"); FileOutputStream stream = null; String thumbnail_file_name = ""; String material_file_name = ""; // ResourceBundle msgProps = ResourceBundle.getBundle("configurations"); // String PresenterPath = msgProps.getString("PresenterPath"); Map<String, String> configurationMap = (Map<String, String>) request.getSession().getAttribute(ApplicationAttribute.CONFIGURATION_PROPERTIES_MAP); String PresenterPath = configurationMap.get("PresenterPath"); Session session = HibernateFactory.openSession(); Transaction tx = session.beginTransaction(); presenter.setCreated_by(user.getStaffLoginId()); presenter.setCreated_date(new Date()); presenter.setModified_by(user.getStaffLoginId()); presenter.setModified_date(new Date()); try { if (request.getSession().getAttribute("thumbnail_file_name") != null) { thumbnail_file_name = (String) request.getSession().getAttribute("thumbnail_file_name"); } if (request.getSession().getAttribute("material_file_name") != null) { material_file_name = (String) request.getSession().getAttribute("material_file_name"); } Query query = session.createQuery( "from Presenter where presenterName=:presenterName and status=:status"); query.setParameter("presenterName", presenter.getPresenterName().trim()); query.setParameter("status", true); ArrayList<Presenter> arrActivity = (ArrayList<Presenter>) query.list(); if (arrActivity.size() > 0) { msg = "The Presenter already exist."; } else { String serverFilename = ""; serverFilename = PresenterPath + "/" + presenter.getPresenterName(); File uploadedFolder = new File(serverFilename); if (!uploadedFolder.exists()) { uploadedFolder.mkdirs(); } // upload thumbnail file if (thumbnail_file_name != null && !thumbnail_file_name.equals("")) { byte[] bytearray = (byte[]) request.getSession().getAttribute("thumbnail_byte_session"); BufferedImage imag; try { imag = ImageIO.read(new ByteArrayInputStream(bytearray)); if (thumbnail_file_name.contains(".png") || thumbnail_file_name.contains(".PNG")) ImageIO.write(imag, "png", new File(serverFilename, thumbnail_file_name)); else if (thumbnail_file_name.contains(".jpg") || thumbnail_file_name.contains(".JPG")) ImageIO.write(imag, "jpg", new File(serverFilename, thumbnail_file_name)); else if (thumbnail_file_name.contains(".gif") || thumbnail_file_name.contains(".GIF")) ImageIO.write(imag, "gif", new File(serverFilename, thumbnail_file_name)); } catch (IOException e) { e.printStackTrace(); } // delete temp directory File temp_file = new File(tempDir + "/" + thumbnail_file_name); FileUtils.deleteFileNFolder(temp_file); presenter.setImage_fileName(thumbnail_file_name); presenter.setImage_fileLocation( (serverFilename + "/" + thumbnail_file_name).substring(8)); serverFilename = ""; } // upload material if (material_file_name != null && !material_file_name.equals("")) { serverFilename = PresenterPath + "/" + presenter.getPresenterName(); byte[] bytearray = (byte[]) request.getSession().getAttribute("material_byte_session"); if (material_file_name.contains(".pdf") || material_file_name.contains(".PDF")) { try { stream = new FileOutputStream(serverFilename + "/" + material_file_name); stream.write(bytearray); stream.close(); } catch (Exception e) { e.printStackTrace(); } } // delete temp directory File temp_file = new File(tempDir + "/" + material_file_name); FileUtils.deleteFileNFolder(temp_file); presenter.setFileName(material_file_name); presenter.setFileLocation((serverFilename + "/" + material_file_name).substring(8)); } presenter.setCreated_by(user.getStaffLoginId()); presenter.setCreated_date(new Date()); session.save(presenter); if (request.getSession().getAttribute("thumbnail_file_name") != null) { if (thumbnail_file_name.contains(".jpg") || thumbnail_file_name.contains(".JPG") || thumbnail_file_name.contains(".png") || thumbnail_file_name.contains(".PNG") || thumbnail_file_name.contains(".gif") || thumbnail_file_name.contains("GIF")) { String field_name = (String) request.getSession().getAttribute("thumbnail_field_name"); storeMaterialToDB( session, presenter.getPresenterCode(), "INSERT", request, "thumbnail_byte_session", thumbnail_file_name, field_name); } request .getSession() .removeAttribute( "thumbnail_file_name"); // removing uploaded file details from session after file // is uploaded request.getSession().removeAttribute("thumbnail_byte_session"); // request.getSession().removeAttribute("thumbnail_field_name"); // } if (request.getSession().getAttribute("material_file_name") != null) { if (material_file_name.contains(".pdf") || material_file_name.contains(".PDF")) { String field_name = (String) request.getSession().getAttribute("material_field_name"); storeMaterialToDB( session, presenter.getPresenterCode(), "INSERT", request, "material_byte_session", material_file_name, field_name); } request .getSession() .removeAttribute( "material_file_name"); // removing uploaded file details from session after file // is uploaded request.getSession().removeAttribute("material_byte_session"); // request.getSession().removeAttribute("material_field_name"); // } tx.commit(); msg = "The new Presenter has been successfully created."; // auditTrailMaintenance.insertAuditTrail(new AuditTrail(String.valueOf(user.getUserCode()), // AuditTrail.MODULE_CATEGORY, AuditTrail.FUNCTION_CREATE, "Action // on:T_CATEGORY,CATEGORY_ID:"+activity_id)); auditTrailMaintenance.insertAuditTrail( new AuditTrail( user.getStaffLoginId() + "", AuditTrail.MODULE_PRESENTER, AuditTrail.FUNCTION_CREATE, "SUCCESS,Presenter_code:" + presenter.getPresenterCode())); Pager pager = retrieveActivityListing(getAllPresenter(request)); request.getSession().setAttribute("pager", pager); request.setAttribute("pager", pager); request .getSession() .setAttribute("pageObj", new PathDetail().getPageObj("PresenterMaintenance")); } } catch (Exception e) { auditTrailMaintenance.insertAuditTrail( new AuditTrail( user.getStaffLoginId() + "", AuditTrail.MODULE_PRESENTER, AuditTrail.FUNCTION_CREATE, "FAILED,presenter_code:" + presenter.getPresenterCode())); log.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } try { HibernateFactory.close(session); } catch (Exception e) { log.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } MsgObject msgObj = new MsgObject(msg); request.setAttribute(RequestAttributes.MESSAGE_OBJ, msgObj); request.setAttribute("CacheName", "Presenter"); // ArrayList arr=getAllPresenter(request); // Pager pager=retrieveActivityListing(arr); // request.setAttribute("pager", pager); return presenter; }