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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.ManagedBeans.ScheduleBean.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.Daily;
import com.mycompany.CRMFly.entities.Employees;
import com.mycompany.CRMFly.entities.Events;
import com.mycompany.CRMFly.entities.Projects;
import com.mycompany.CRMFly.serviceLayer.DailyService;
import com.mycompany.CRMFly.serviceLayer.EventsService;
import com.mycompany.CRMFly.serviceLayer.ProjectsService;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author ??
 */
@Component
//@ManagedBean
@Scope("view")
public class ScheduleBean implements Serializable {

    @Autowired
    private EventsService eventsService;

    @Autowired
    private DailyService dailyService;

    @Autowired
    private ProjectsService projectsService;

    @Autowired
    private EmployeeBean employeeBean;
    /*   @Autowired
       private ProjectBean projectBean;
           
       @Autowired
       private EventBean eventBean;
           
       @Autowired
       private TaskBean taskBean;*/

    //  @Autowired
    //   private EmployeeBean employeeBean;

    private ScheduleModel eventModel;

    private ScheduleEvent event = new DefaultScheduleEvent();

    private boolean forEmployee;
    private boolean forDate;
    private boolean editable;

    private Employees employee = new Employees();
    private Date selectedDate;

    public void getSelectedTasks() {
        eventModel = new DefaultScheduleModel();
        Long id = employee.getId();
        List<Daily> tasks;
        if (isForEmployee()) {
            tasks = dailyService.getTasksForEmployee(id);
        } else {
            tasks = dailyService.getAllTasks();
        }
        for (Daily task : tasks) {
            if (task.getTask_enddate() != null)
                eventModel.addEvent(
                        new DefaultScheduleEvent(task.getTask(), task.getTask_date(), task.getTask_enddate()));
            else
                eventModel.addEvent(
                        new DefaultScheduleEvent(task.getTask(), task.getTask_date(), task.getTask_date()));
        }
    }

    public void getSelectedEvents() {
        eventModel = new DefaultScheduleModel();
        Long id = employee.getId();
        List<Events> events;
        if (isForEmployee()) {
            events = eventsService.getEventsForEmployee(id);
        } else
            events = eventsService.getAllEvents();
        for (Events event : events) {
            if (event.getEvent_enddate() != null)
                eventModel.addEvent(
                        new DefaultScheduleEvent(event.getName(), event.getEvent_date(), event.getEvent_enddate()));
            else
                eventModel.addEvent(
                        new DefaultScheduleEvent(event.getName(), event.getEvent_date(), event.getEvent_date()));
        }
    }

    public List<Projects> getSelectedProjects() {
        Long id = employee.getId();
        List<Projects> projects;
        //  if ((getSelectedDate() == null || isForDate() ==false) && 
        //          (employee == null || isForEmployee() == false))
        //    {projects = new ArrayList<Projects>();} 
        //     else 
        if (getSelectedDate() == null || isForDate() == false) {
            if (employee == null || isForEmployee() == false)
                projects = projectsService.getAllProjects();
            else
                projects = projectsService.getProjectsForEmployee(id);
        } else {
            if (isForEmployee())
                projects = projectsService.getProjectsForEmployeeOnDate(id, getSelectedDate());
            else
                projects = projectsService.getProjectsOnDate(getSelectedDate());
        }
        return projects;

    }

    /*
     public void SelectProjectsForDate(Date date) 
    {
      List<Projects> projects =   projectsService.getProjectsOnDate(date);
     for( Projects project: projects)
    {if  (project.getEnd_date()!=null)
    eventModel.addEvent(new DefaultScheduleEvent(project.getName(),
    project.getBegin_date(), project.getEnd_date()
    ));
    else eventModel.addEvent(new DefaultScheduleEvent(project.getName(),
    project.getBegin_date(), project.getBegin_date()
    ));
    }
    }
         
     public void SelectProjectsForEmployeeOnDateloyee(Employees empl, Date date) 
    {Long id = empl.getId();
     List<Projects> projects =   projectsService.getProjectsForEmployeeOnDate(id, date);
     for( Projects project: projects)
    {if  (project.getEnd_date()!=null)
    eventModel.addEvent(new DefaultScheduleEvent(project.getName(),
    project.getBegin_date(), project.getEnd_date()
    ));
    else eventModel.addEvent(new DefaultScheduleEvent(project.getName(),
    project.getBegin_date(), project.getBegin_date()
    ));
    }
         
    }*/

    public ScheduleBean() {
        eventModel = new DefaultScheduleModel();
    }

    public Date getRandomDate(Date base) {
        Calendar date = Calendar.getInstance();
        date.setTime(base);
        date.add(Calendar.DATE, ((int) (Math.random() * 30)) + 1); //set random day of month  

        return date.getTime();
    }

    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar.getTime();
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    private Calendar today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0,
                0);

        return calendar;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }

    public void addEvent() {
        if (event.getId() == null) {
            eventModel.addEvent(event);
            Events newEvent = new Events();
            newEvent.setEvent_date(event.getStartDate());
            newEvent.setEvent_enddate(event.getEndDate());
            newEvent.setName(event.getTitle());
            eventsService.addEvent(newEvent);
        } else {
            Events updatedEvent = eventsService.getEventForName(event.getTitle());
            updatedEvent.setEvent_date(event.getStartDate());
            updatedEvent.setEvent_enddate(event.getEndDate());
            updatedEvent.setName(event.getTitle());
            eventModel.updateEvent(event);
            eventsService.changeEvent(updatedEvent);
        }
        event = new DefaultScheduleEvent();
    }

    public void addTask() {
        if (event.getId() == null) {
            eventModel.addEvent(event);
            Daily newTask = new Daily();
            newTask.setTask_date(event.getStartDate());
            newTask.setTask_enddate(event.getEndDate());
            newTask.setTask(event.getTitle());
            if (employeeBean.getSelectedEmployee() != null)
                newTask.setImplementer(employeeBean.getSelectedEmployee());
            dailyService.addTask(newTask);
        } else {
            Daily updatedTask = dailyService.getTaskForName(event.getTitle());
            updatedTask.setTask_date(event.getStartDate());
            updatedTask.setTask_enddate(event.getEndDate());
            updatedTask.setTask(event.getTitle());
            eventModel.updateEvent(event);
            dailyService.changeTask(updatedTask);
        }
        event = new DefaultScheduleEvent();
    }

    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
    }

    public void onDateSelect(SelectEvent selectEvent) {
        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
    }

    /* public List<Daily> SelectTasksForDate(Date date)
    return dailyService.
    }
        
        
        
        
    public List<Events> SelectEventsForDate(Date date) 
    {
    return eventsService.
    }
        
        
    public List<Daily> SelectTasksForEmployee(Employees empl) 
    {
    return dailyService.
    }
        
    public List<Projects> SelectProjectsForEmployee(Employees empl) 
    {
    return projectsService.
    }
        
        
    public List<Events> SelectEventsForEmployee(Employees empl) 
    {
    return eventsService.
    }*/

    /**
     * @return the forEmployee
     */
    public boolean isForEmployee() {
        return forEmployee;
    }

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

    /**
     * @return the forDate
     */
    public boolean isForDate() {
        return forDate;
    }

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

    /**
     * @return the employee
     */
    public Employees getEmployee() {
        return employee;
    }

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

    /**
     * @return the selectedDate
     */
    public Date getSelectedDate() {
        return selectedDate;
    }

    /**
     * @param selectedDate the selectedDate to set
     */
    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    /**
     * @return the editable
     */
    public boolean isEditable() {
        return editable;
    }

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

}