com.mycompany.CRMFly.ManagedBeans.EmployeeBean.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.ManagedBeans.EmployeeBean.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.mycompany.CRMFly.ManagedBeans;

import com.mycompany.CRMFly.entities.Contracts;
import com.mycompany.CRMFly.entities.Daily;
import com.mycompany.CRMFly.entities.Employees;
import com.mycompany.CRMFly.entities.Projects;
import com.mycompany.CRMFly.entities.UserAccount;
import com.mycompany.CRMFly.hibernateAccess.CustomUserDetailService;
import com.mycompany.CRMFly.serviceLayer.EmployeesService;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 *
 * @author ??
 */
@Component
//@ManagedBean
@Scope("view")
public class EmployeeBean implements Serializable {
    private Employees employee = new Employees();
    private Employees selectedEmployee;
    private Employees searchedEmployee = new Employees();
    private Projects addProject = new Projects();
    private Daily addTask = new Daily();
    private Employees addEmployee = new Employees();
    private Contracts addContract = new Contracts();

    private UserAccount sessionEmployee;
    private boolean employeeAccount;
    private boolean thisEmployeeAccount;

    @Autowired
    private CustomUserDetailService userService;

    @PostConstruct
    public void thisuser() {
        UserDetails user;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            user = (UserDetails) principal;
            String name = user.getUsername();
            sessionEmployee = userService.loadUserByUsername(name);
        }
    }

    public void compareEmployee() {
        if (sessionEmployee.getEmployee() != null && sessionEmployee.getEmployee().equals(selectedEmployee))
            thisEmployeeAccount = true;
        else
            thisEmployeeAccount = false;

    }

    private boolean Editable;

    public boolean isEditable() {
        return Editable;
    }

    public void setEditable(boolean Editable) {
        this.Editable = Editable;
    }

    public void changeEditable() {
        if (Editable == true)
            Editable = false;
        else
            Editable = true;
    }

    public Employees getSelectedEmployee() {
        return selectedEmployee;
    }

    public void setSelectedEmployee(Employees selectedEmployee) {
        this.selectedEmployee = selectedEmployee;
    }

    @Autowired
    EmployeesService employeesService;

    public void addEmployee() {
        employeesService.addEmployee(employee);
        employee = new Employees();
    }

    public List<Employees> listEmployees() {

        return employeesService.listEmployees();
    }

    public void removeEmployee() {
        employeesService.removeEmployee(selectedEmployee);
    }

    public void changeEmployee() {
        if (employee.getId() != null)
            employeesService.changeEmployee(employee);
    }

    public void editEmployee() {
        if (selectedEmployee != null)
            employeesService.changeEmployee(selectedEmployee);
    }

    public Employees getEmployeeForId(Long id) {
        return employeesService.getEmployeeForId(id);
    }

    public List<Employees> getAllEmployees() {
        return employeesService.getAllEmployees();
    }

    public List<Contracts> getContractsOnWorker(Long id) {

        return employeesService.getContractsOnWorker(id);
    }

    public List<Projects> getProjectsEmployeeResponsibleFor(Long id) {

        return employeesService.getProjectsEmployeeResponsibleFor(id);
    }

    public List<Projects> getProjectsPaticipation(Long id) {

        return employeesService.getProjectsPaticipation(id);
    }

    public List<Daily> getTasksForEmployee(Long id) {
        return employeesService.getTasksForEmployee(id);
    }

    public List<Employees> getSubordinatesEmployee(Long id) {

        return employeesService.getSubordinatesEmployee(id);
    }

    public List<Contracts> getContractsForSelectedId() {
        Long id;
        if (selectedEmployee != null) {
            id = selectedEmployee.getId();
            return employeesService.getContractsOnWorker(id);
        } else
            return null;
    }

    public List<Projects> getManagedProjectsForSelectedId() {
        Long id;
        if (selectedEmployee != null) {
            id = selectedEmployee.getId();
            return employeesService.getProjectsEmployeeResponsibleFor(id);
        } else
            return null;
    }

    public List<Projects> getProjectsForSelectedId() {
        Long id;
        if (selectedEmployee != null) {
            id = selectedEmployee.getId();
            return employeesService.getProjectsPaticipation(id);
        } else
            return null;
    }

    public Employees getEmployee() {
        return employee;
    }

    public void setEmployee(Employees employee) {
        this.employee = employee;
    }

    public List<Employees> getSearchEmployee() {
        if (searchedEmployee != null)
            return employeesService.searchEmployee(searchedEmployee);
        else
            return null;
    }

    public Employees getSearchedEmployee() {
        return searchedEmployee;
    }

    public void setSearchedEmployee(Employees searchedEmployee) {
        this.searchedEmployee = searchedEmployee;
    }

    public void setThisEmployee(Employees employee) {
        this.employee = employee;
    }

    public void flushEmployee() {
        this.employee = new Employees();
    }

    public void addSubordinadeConnection() {
        if (selectedEmployee != null)
            employeesService.addSubordinadeConnection(selectedEmployee, addEmployee);
    }

    public void addTaskConnection() {
        if (selectedEmployee != null)
            employeesService.addTaskConnection(selectedEmployee, addTask);
    }

    public void addProjectConnection() {
        if (selectedEmployee != null)
            employeesService.addProjectConnection(selectedEmployee, addProject);
    }

    public void addManagedProjectConnection() {
        if (selectedEmployee != null)
            employeesService.addManagedProjectConnection(selectedEmployee, addProject);
    }

    public void addManagedContractConnection() {
        if (selectedEmployee != null)
            employeesService.addManagedContractConnection(selectedEmployee, addContract);
    }

    public void deleteSubordinadeConnection() {
        if (selectedEmployee != null)
            employeesService.deleteSubordinadeConnection(selectedEmployee, addEmployee);
    }

    public void deleteTaskConnection() {
        if (selectedEmployee != null)
            employeesService.deleteTaskConnection(selectedEmployee, addTask);
    }

    public void deleteProjectConnection() {
        if (selectedEmployee != null)
            employeesService.deleteProjectConnection(selectedEmployee, addProject);
    }

    public void deleteManagedProjectConnection() {
        if (selectedEmployee != null)
            employeesService.deleteManagedProjectConnection(selectedEmployee, addProject);
    }

    public void deleteManagedContractConnection() {
        if (selectedEmployee != null)
            employeesService.addManagedContractConnection(selectedEmployee, addContract);
    }

    /**
     * @return the addProject
     */
    public Projects getAddProject() {
        return addProject;
    }

    /**
     * @param addProject the addProject to set
     */
    public void setAddProject(Projects addProject) {
        this.addProject = addProject;
    }

    /**
     * @return the addTask
     */
    public Daily getAddTask() {
        return addTask;
    }

    /**
     * @param addTask the addTask to set
     */
    public void setAddTask(Daily addTask) {
        this.addTask = addTask;
    }

    /**
     * @return the addEmployee
     */
    public Employees getAddEmployee() {
        return addEmployee;
    }

    /**
     * @param addEmployee the addEmployee to set
     */
    public void setAddEmployee(Employees addEmployee) {
        this.addEmployee = addEmployee;
    }

    /**
     * @return the addContract
     */
    public Contracts getAddContract() {
        return addContract;
    }

    /**
     * @param addContract the addContract to set
     */
    public void setAddContract(Contracts addContract) {
        this.addContract = addContract;
    }

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            return new DefaultStreamedContent();
        } else {
            String id = context.getExternalContext().getRequestParameterMap().get("id");
            byte[] image = employeesService.getImage(selectedEmployee.getId());
            return new DefaultStreamedContent(new ByteArrayInputStream(image));
        }
    }

    private UploadedFile uploaded_image;
    private String upload_location;

    public void handleFileUpload(FileUploadEvent event) {
        // UploadedFile file = event.getFile();
        setUploaded_image(event.getFile());
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
                .getContext();
        String v_file_ext = getUploaded_image().getFileName()
                .split("\\.")[(getUploaded_image().getFileName().split("\\.").length) - 1];
        setUpload_location(servletContext.getRealPath("") + File.separator + "temp-images" + File.separator + "3"
                + "." + v_file_ext);
        FileImageOutputStream imageOutput;
        try {
            imageOutput = new FileImageOutputStream(new File(getUpload_location()));
            imageOutput.write(getUploaded_image().getContents(), 0, getUploaded_image().getContents().length);
            imageOutput.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] file = new byte[uploaded_image.getContents().length];
        System.arraycopy(uploaded_image.getContents(), 0, file, 0, uploaded_image.getContents().length);
        employee.setPhoto(file);

        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void btn_save_clicked() {

        //ent.setImg(file);
        //yourfacade.create(ent);
    }

    /**
     * @return the uploaded_image
     */
    public UploadedFile getUploaded_image() {
        return uploaded_image;
    }

    /**
     * @param uploaded_image the uploaded_image to set
     */
    public void setUploaded_image(UploadedFile uploaded_image) {
        this.uploaded_image = uploaded_image;
    }

    /**
     * @return the upload_location
     */
    public String getUpload_location() {
        return upload_location;
    }

    /**
     * @param upload_location the upload_location to set
     */
    public void setUpload_location(String upload_location) {
        this.upload_location = upload_location;
    }

    /**
     * @return the employeeAccount
     */
    public boolean isEmployeeAccount() {
        return employeeAccount;
    }

    /**
     * @param employeeAccount the employeeAccount to set
     */
    public void setEmployeeAccount(boolean employeeAccount) {
        this.employeeAccount = employeeAccount;
    }

    /**
     * @return the sessionEmployee
     */
    public UserAccount getSessionEmployee() {
        return sessionEmployee;
    }

    /**
     * @param sessionEmployee the sessionEmployee to set
     */
    public void setSessionEmployee(UserAccount sessionEmployee) {
        this.sessionEmployee = sessionEmployee;
    }

    /**
     * @return the thisEmployeeAccount
     */
    public boolean isThisEmployeeAccount() {
        return thisEmployeeAccount;
    }

    /**
     * @param thisEmployeeAccount the thisEmployeeAccount to set
     */
    public void setThisEmployeeAccount(boolean thisEmployeeAccount) {
        this.thisEmployeeAccount = thisEmployeeAccount;
    }
}