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.accounting.office; import com.accounting.commonutils.HelperUtil; import static com.accounting.commonutils.HelperUtil.showFacesMsg; import com.accounting.entities.account.EmployeeMaster; import com.accounting.entities.office.Office; import com.accounting.entities.parameters.AppData; import com.accounting.entities.user.User; import com.accounting.sessions.account.EmployeeSessionBean; import com.accounting.util.JsfUtil; import com.accounting.utils.NepaliDateConverter; import com.accounting.utils.Util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.ejb.EJB; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.util.CellRangeAddress; import org.primefaces.context.RequestContext; /** * * @author samajdar */ @ManagedBean @SessionScoped public class EmployeeController { @EJB EmployeeSessionBean employeeSessionBean; private long employeeGroupId; private int employeePositionId; Date joinDate; Date inActivedate; private String editDate; private String inactivedDate, joinedDate; private String filterString, employeeStatus; private String employeeName, dateOfJoinNepali; private String inActiveDateNeplai; private boolean isInactiveDate; private String employeeAddress, contactNumber, panNo; private long positionId; private boolean editRender; private boolean inActiveDateStatus; private String salaryStatus; private boolean createForm; private boolean hideCreateForm; private List<EmployeeMaster> allEmployeeList; private List<AppData> employeeGroupList = new ArrayList<AppData>(); private List<AppData> employeePositionList = new ArrayList<AppData>(); private AppData newEmployeeGroup = new AppData(); private AppData newEmployeePosition = new AppData(); private EmployeeMaster newEmployee = new EmployeeMaster(); private NepaliDateConverter ndc = new NepaliDateConverter(); public EmployeeController() { } public void clearForm() { contactNumber = ""; employeeName = ""; employeeAddress = ""; panNo = ""; positionId = 0; employeeGroupId = 0; salaryStatus = "Active"; filterString = "All"; newEmployee = new EmployeeMaster(); employeeGroupList = new ArrayList<AppData>(); employeePositionList = new ArrayList<AppData>(); } public String gotoEmployeePage() { clearForm(); allEmployeeList = new ArrayList<EmployeeMaster>(); createForm = false; hideCreateForm = true; returnEmployeeGroupList(); returnEmployeePositionList(); returnAllEmployeeList(); isInactiveDate = false; inActiveDateStatus = true; employeeStatus = "All Employee List"; System.out.println("page head name is" + employeeStatus); return "createEmployee.xhtml?faces-redirect=true"; } public void returnAllEmployeeList() { allEmployeeList = employeeSessionBean.returnAllEmployeeListByOffice(HelperUtil.getLoggedInOffice().getId()); } public void showCreateEmployeeForm() { createForm = true; hideCreateForm = false; returnEmployeeGroupList(); returnEmployeePositionList(); editRender = false; isInactiveDate = false; newEmployee = new EmployeeMaster(); } public void returnEmployeeGroupList() { employeeGroupList = employeeSessionBean.returnEmployeeGroupByOffice(HelperUtil.getLoggedInOffice().getId()); } public void addNewEmployeeGroup() { if (employeeSessionBean.returnGroupByName(HelperUtil.getLoggedInOffice().getId(), newEmployeeGroup.getAppDataDesc()) != null) { JsfUtil.addErrorMessage("Group name already exists."); return; } newEmployeeGroup.setAppDataDesc(newEmployeeGroup.getAppDataDesc()); newEmployeeGroup.setDelFlag(false); newEmployeeGroup.setAppDataType("Employee_group"); newEmployeeGroup.setOfficeId(HelperUtil.getLoggedInOfficeID()); employeeSessionBean.persist(newEmployeeGroup); returnEmployeeGroupList(); employeeGroupId = newEmployeeGroup.getId(); RequestContext.getCurrentInstance().execute("PF('CategoryCreateDialog').hide();"); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee Group add successfully.", ""); } public void createNewEmployeeGroupClicked() { newEmployeeGroup = new AppData(); RequestContext.getCurrentInstance().execute("PF('CategoryCreateDialog').show();"); } public void returnEmployeePositionList() { employeePositionList = employeeSessionBean .returnEmployeePositionByOffice(HelperUtil.getLoggedInOffice().getId()); } public void addEmployeePosition() { if (employeeSessionBean.returnPositionByName(HelperUtil.getLoggedInOffice().getId(), newEmployeePosition.getAppDataDesc()) != null) { JsfUtil.addErrorMessage("Position name already exists."); return; } else { newEmployeePosition.setAppDataDesc(newEmployeePosition.getAppDataDesc()); newEmployeePosition.setDelFlag(false); newEmployeePosition.setAppDataType("Employee_position"); newEmployeePosition.setOfficeId(HelperUtil.getLoggedInOfficeID()); employeeSessionBean.persist(newEmployeePosition); returnEmployeePositionList(); positionId = newEmployeePosition.getId(); RequestContext.getCurrentInstance().execute("PF('SubCategoryCreateDialog').hide();"); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee position add successfully.", ""); } } public String cancelAction() { createForm = false; hideCreateForm = true; contactNumber = ""; employeeName = ""; employeeAddress = ""; positionId = 0; employeeGroupId = 0; // clearForm(); editRender = false; isInactiveDate = true; // returnAllEmployeeList(); return "createEmployee.xhtml?faces-redirect=true"; } public void addNewEmployee() { if (contactNumber.length() != 10) { HelperUtil.showFacesMsg(FacesMessage.SEVERITY_ERROR, "Mobile number invalid", ""); return; } if (employeeSessionBean.returnEmployeeByContactNumber(HelperUtil.getLoggedInOffice().getId(), contactNumber) != null) { JsfUtil.addErrorMessage("Employee with this number already exists."); return; } if (employeeGroupId == 0) { JsfUtil.addErrorMessage("Please select employee group."); return; } if (positionId == 0) { JsfUtil.addErrorMessage("Please select employee position."); return; } if (salaryStatus.equalsIgnoreCase("Active")) { dateOfJoinNepali = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); System.out.println("add enoloyee" + dateOfJoinNepali); joinDate = ndc.convertToEnglishDate(dateOfJoinNepali); newEmployee.setDateOfJoin(joinDate); newEmployee.setDateOfJoinNepali(dateOfJoinNepali); newEmployee.setContactNumber(contactNumber); newEmployee.setEmployeeAddress(employeeAddress); newEmployee.setEmployeeName(employeeName); newEmployee.setEmployeePositionId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), positionId)); newEmployee.setEmployeeGroupId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), employeeGroupId)); newEmployee.setSalaryStatus(salaryStatus); newEmployee.setPanNo(panNo); newEmployee.setDelFlag(false); newEmployee.setOfficeId(HelperUtil.getLoggedInOffice().getId()); employeeSessionBean.persist(newEmployee); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee add successfully.", ""); clearForm(); returnAllEmployeeList(); createForm = false; hideCreateForm = true; } else if (salaryStatus.equalsIgnoreCase("InActive")) { inActiveDateNeplai = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate5"); System.out.println("add enoloyee" + inActiveDateNeplai); inActivedate = ndc.convertToEnglishDate(inActiveDateNeplai); newEmployee.setInActiveDate(inActivedate); newEmployee.setDateOfinActiveNepali(inActiveDateNeplai); newEmployee.setContactNumber(contactNumber); newEmployee.setEmployeeAddress(employeeAddress); newEmployee.setEmployeeName(employeeName); newEmployee.setEmployeePositionId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), positionId)); newEmployee.setEmployeeGroupId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), employeeGroupId)); newEmployee.setSalaryStatus(salaryStatus); newEmployee.setPanNo(panNo); newEmployee.setDelFlag(false); newEmployee.setOfficeId(HelperUtil.getLoggedInOffice().getId()); employeeSessionBean.persist(newEmployee); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee add successfully.", ""); clearForm(); returnAllEmployeeList(); createForm = false; hideCreateForm = true; } } public void updateEmployee() { if (contactNumber.length() != 10) { HelperUtil.showFacesMsg(FacesMessage.SEVERITY_ERROR, "Mobile number invalid", ""); return; } dateOfJoinNepali = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate6"); inActiveDateNeplai = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate7"); if (employeeGroupId == 0) { JsfUtil.addErrorMessage("Please select employee group."); return; } if (positionId == 0) { JsfUtil.addErrorMessage("Please select employee position."); return; } if (salaryStatus.equalsIgnoreCase("Active")) { joinDate = ndc.convertToEnglishDate(dateOfJoinNepali); newEmployee.setDateOfJoin(joinDate); newEmployee.setContactNumber(contactNumber); newEmployee.setEmployeeAddress(employeeAddress); newEmployee.setEmployeeName(employeeName); newEmployee.setEmployeePositionId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), positionId)); newEmployee.setEmployeeGroupId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), employeeGroupId)); newEmployee.setSalaryStatus(salaryStatus); newEmployee.setPanNo(panNo); newEmployee.setDateOfJoinNepali(dateOfJoinNepali); employeeSessionBean.merge(newEmployee); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee updated successfully.", ""); contactNumber = ""; employeeName = ""; employeeAddress = ""; positionId = 0; panNo = ""; employeeGroupId = 0; createForm = false; hideCreateForm = true; } else if (salaryStatus.equalsIgnoreCase("InActive")) { if (inActiveDateNeplai == null || inActiveDateNeplai.isEmpty()) { JsfUtil.addErrorMessage("Please select inactive date."); return; } inActivedate = ndc.convertToEnglishDate(inActiveDateNeplai); newEmployee.setInActiveDate(inActivedate); newEmployee.setContactNumber(contactNumber); newEmployee.setEmployeeAddress(employeeAddress); newEmployee.setEmployeeName(employeeName); newEmployee.setEmployeePositionId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), positionId)); newEmployee.setEmployeeGroupId( employeeSessionBean.findAppDataById(HelperUtil.getLoggedInOffice().getId(), employeeGroupId)); newEmployee.setSalaryStatus(salaryStatus); newEmployee.setPanNo(panNo); newEmployee.setDateOfinActiveNepali(inActiveDateNeplai); employeeSessionBean.merge(newEmployee); showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee updated successfully.", ""); contactNumber = ""; employeeName = ""; employeeAddress = ""; positionId = 0; panNo = ""; employeeGroupId = 0; createForm = false; hideCreateForm = true; } // } } public void editEmploye(EmployeeMaster emp) { returnEmployeeGroupList(); returnEmployeePositionList(); newEmployee = emp; editRender = true; if (newEmployee.getSalaryStatus().equalsIgnoreCase("InActive")) { isInactiveDate = true; } else { isInactiveDate = false; } contactNumber = newEmployee.getContactNumber(); employeeAddress = newEmployee.getEmployeeAddress(); employeeName = newEmployee.getEmployeeName(); salaryStatus = newEmployee.getSalaryStatus(); employeeGroupId = newEmployee.getEmployeeGroupId().getId(); positionId = newEmployee.getEmployeePositionId().getId(); joinedDate = newEmployee.getDateOfJoinNepali(); inactivedDate = newEmployee.getDateOfinActiveNepali(); panNo = newEmployee.getPanNo(); createForm = true; hideCreateForm = false; } public String deleteEmployee(EmployeeMaster emp) { emp.setDelFlag(true); employeeSessionBean.merge(emp); // allEmployeeList = employeeSessionBean.returnAllEmployeeListByOffice(HelperUtil.getLoggedInOffice().getId()); HelperUtil.showFacesMsg(FacesMessage.SEVERITY_INFO, "Employee deleted successfully", ""); newEmployee = new EmployeeMaster(); editRender = false; // contact1 = ""; // contact2 = ""; returnAllEmployeeList(); return "createEmployee.xhtml"; } public void filterEmployeeList() { allEmployeeList = new ArrayList<EmployeeMaster>(); if (filterString.equalsIgnoreCase("All")) { allEmployeeList = employeeSessionBean .returnAllEmployeeListByOffice(HelperUtil.getLoggedInOffice().getId()); inActiveDateStatus = true; employeeStatus = "All Employee List"; } else if (filterString.equalsIgnoreCase("Active")) { filterString = "Active"; allEmployeeList = employeeSessionBean .returnAllEmployeeListFilterByOffice(HelperUtil.getLoggedInOfficeID(), filterString); inActiveDateStatus = false; employeeStatus = "Active Employee List"; } else if (filterString.equalsIgnoreCase("InActive")) { filterString = "InActive"; allEmployeeList = employeeSessionBean .returnAllEmployeeListFilterByOffice(HelperUtil.getLoggedInOfficeID(), filterString); inActiveDateStatus = true; employeeStatus = "InActive Employee List"; } } public void filterDate() { if (salaryStatus.equalsIgnoreCase("Active")) { isInactiveDate = false; } else if (salaryStatus.equalsIgnoreCase("InActive")) { isInactiveDate = true; } } public User getLoggedInUser() { User u = (User) Util.getSession().getAttribute("user"); return u; } public Office getLoggedInOffice() { Office o = (Office) Util.getSession().getAttribute("office"); return o; } public void simplePostProcessXLSEmployeeReport(Object document) { System.out.println("page head name is" + employeeStatus); String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate5"); HSSFWorkbook wb = (HSSFWorkbook) document; HSSFCellStyle headerCellStyle = wb.createCellStyle(); HSSFCellStyle headerCellStyle1 = wb.createCellStyle(); HSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); HSSFSheet sheet = wb.getSheetAt(0); int noOfColumns = sheet.getRow(0).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); } sheet.shiftRows(0, sheet.getLastRowNum(), 3); HSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue(employeeStatus); firstRow.getCell(0).setCellStyle(headerCellStyle); HSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); HSSFRow thirdRow = sheet.createRow(2); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 9)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 9)); } public void createNewEmployeePositionClicked() { newEmployeePosition = new AppData(); RequestContext.getCurrentInstance().execute("PF('SubCategoryCreateDialog').show();"); } public long getEmployeeGroupId() { return employeeGroupId; } public void setEmployeeGroupId(long employeeGroupId) { this.employeeGroupId = employeeGroupId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getDateOfJoinNepali() { return dateOfJoinNepali; } public void setDateOfJoinNepali(String dateOfJoinNepali) { this.dateOfJoinNepali = dateOfJoinNepali; } public String getEmployeeAddress() { return employeeAddress; } public void setEmployeeAddress(String employeeAddress) { this.employeeAddress = employeeAddress; } public String getContactNumber() { return contactNumber; } public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; } public int getEmployeePositionId() { return employeePositionId; } public void setEmployeePositionId(int employeePositionId) { this.employeePositionId = employeePositionId; } public long getPositionId() { return positionId; } public void setPositionId(long positionId) { this.positionId = positionId; } public EmployeeSessionBean getEmployeeSessionBean() { return employeeSessionBean; } public void setEmployeeSessionBean(EmployeeSessionBean employeeSessionBean) { this.employeeSessionBean = employeeSessionBean; } public List<AppData> getEmployeeGroupList() { return employeeGroupList; } public void setEmployeeGroupList(List<AppData> employeeGroupList) { this.employeeGroupList = employeeGroupList; } public AppData getNewEmployeeGroup() { return newEmployeeGroup; } public void setNewEmployeeGroup(AppData newEmployeeGroup) { this.newEmployeeGroup = newEmployeeGroup; } public String getEditDate() { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate6"); if (startDateString != null && !startDateString.isEmpty()) { editDate = startDateString; } return editDate; } public void setEditDate(String editDate) { this.editDate = editDate; } public AppData getNewEmployeePosition() { return newEmployeePosition; } public void setNewEmployeePosition(AppData newEmployeePosition) { this.newEmployeePosition = newEmployeePosition; } public List<EmployeeMaster> getAllEmployeeList() { return allEmployeeList; } public void setAllEmployeeList(List<EmployeeMaster> allEmployeeList) { this.allEmployeeList = allEmployeeList; } public EmployeeMaster getNewEmployee() { return newEmployee; } public void setNewEmployee(EmployeeMaster newEmployee) { this.newEmployee = newEmployee; } public List<AppData> getEmployeePositionList() { return employeePositionList; } public void setEmployeePositionList(List<AppData> employeePositionList) { this.employeePositionList = employeePositionList; } public boolean isEditRender() { return editRender; } public Date getJoinDate() { return joinDate; } public void setJoinDate(Date joinDate) { this.joinDate = joinDate; } public String getSalaryStatus() { return salaryStatus; } public void setSalaryStatus(String salaryStatus) { this.salaryStatus = salaryStatus; } public void setEditRender(boolean editRender) { this.editRender = editRender; } public boolean isCreateForm() { return createForm; } public void setCreateForm(boolean createForm) { this.createForm = createForm; } public boolean isHideCreateForm() { return hideCreateForm; } public void setHideCreateForm(boolean hideCreateForm) { this.hideCreateForm = hideCreateForm; } public String getPanNo() { return panNo; } public void setPanNo(String panNo) { this.panNo = panNo; } public String getJoinedDate() { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate6"); if (startDateString != null && !startDateString.isEmpty()) { joinedDate = startDateString; } return joinedDate; } public void setJoinedDate(String joineDate) { this.joinedDate = joineDate; } public String getInactivedDate() { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate7"); if (startDateString != null && !startDateString.isEmpty()) { inactivedDate = startDateString; } return inactivedDate; } public void setInactivedDate(String inactivedDate) { this.inactivedDate = inactivedDate; } public Date getInActivedate() { return inActivedate; } public void setInActivedate(Date inActivedate) { this.inActivedate = inActivedate; } public String getInActiveDateNeplai() { return inActiveDateNeplai; } public void setInActiveDateNeplai(String inActiveDateNeplai) { this.inActiveDateNeplai = inActiveDateNeplai; } public boolean isIsInactiveDate() { return isInactiveDate; } public void setIsInactiveDate(boolean isInactiveDate) { this.isInactiveDate = isInactiveDate; } public String getFilterString() { return filterString; } public void setFilterString(String filterString) { this.filterString = filterString; } public boolean isInActiveDateStatus() { return inActiveDateStatus; } public void setInActiveDateStatus(boolean inActiveDateStatus) { this.inActiveDateStatus = inActiveDateStatus; } public String getEmployeeStatus() { return employeeStatus; } public void setEmployeeStatus(String employeeStatus) { this.employeeStatus = employeeStatus; } }