vn.webapp.controller.cp.AcademicYearController.java Source code

Java tutorial

Introduction

Here is the source code for vn.webapp.controller.cp.AcademicYearController.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 vn.webapp.controller.cp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * Customize
 */
import vn.webapp.controller.BaseWeb;
import vn.webapp.service.AcademicYearService;
import vn.webapp.service.WeeksService;
import vn.webapp.validation.AcademicYearValidation;
import vn.webapp.validation.RoomLoanValidation;
import vn.webapp.validation.RoomValidation;
import vn.webapp.model.AcademicYear;
import vn.webapp.model.RoomLoan;
import vn.webapp.model.Weeks;

@Controller("cpAcademicYears")
@RequestMapping(value = { "/cp" })
public class AcademicYearController extends BaseWeb {

    @Autowired
    private AcademicYearService academicYearService;

    @Autowired
    private WeeksService weeksService;

    /**
    *
    * @param model
    * @return
    */
    @RequestMapping(value = "/AcademicYears", method = RequestMethod.GET)
    public String AcademicYears(ModelMap model, HttpSession session) {

        /*
         * Get current user name and role
         */
        String currentUserName = session.getAttribute("currentUserName").toString();
        String userRole = session.getAttribute("currentUserRole").toString();

        // Get list reportingYear
        List<AcademicYear> academicYearList = academicYearService.list();

        /*
         * Put data back to view
         */
        model.put("academicYearList", academicYearList);
        return "cp.academicYears";
    }

    @RequestMapping(value = "/AddAcademicYear", method = RequestMethod.GET)
    public String AddAcademicYear(ModelMap model, HttpSession session) {

        /*
         * Get current user name and role
         */
        String currentUserName = session.getAttribute("currentUserName").toString();
        String userRole = session.getAttribute("currentUserRole").toString();

        model.put("academicYearAdd", new AcademicYearValidation());

        return "cp.addAcademicYear";
    }

    @RequestMapping(value = "/EditAcademicYear/{id}", method = RequestMethod.GET)
    public String EditRoomLoan(ModelMap model, @PathVariable("id") String aY_Code, HttpSession session) {

        /*
         * Get current user name and role
         */
        //  System.out.println(aY_Code);

        AcademicYear acadYear = academicYearService.loadByCode(aY_Code);
        model.put("acadYear", acadYear);
        model.put("academicYearEdit", new AcademicYearValidation());
        return "cp.editAcademicYear";
    }

    @RequestMapping(value = "edit-a-academic-year", method = RequestMethod.POST)
    public String editAAcademicYear(HttpServletRequest request,
            @Valid @ModelAttribute("academicYearAdd") AcademicYearValidation academicYearValidation,
            BindingResult result, Map model, HttpSession session) {

        if (result.hasErrors()) {
            return "cp.editAcademicYear";
        } else {
            /*
             * Prepare data for inserting DB
             */

            String aYCode = academicYearValidation.getACAYEAR_Code();
            String aYStartDate = academicYearValidation.getACAYEAR_FromDate();
            String aYEndDate = academicYearValidation.getACAYEAR_ToDate();
            academicYearService.edit(aYCode, aYStartDate, aYEndDate);

            /*AcademicYear aY = academicYearService.loadByCode(aYCode);
            SimpleDateFormat dateFormat = new SimpleDateFormat( "dd-MM-yyyy" );   
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
                
            try {
            start.setTime( dateFormat.parse(aYStartDate));
            end.setTime(dateFormat.parse(aYEndDate));
            } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();}
            int i = 1;
            while(end.after(start)){
               Weeks week = new Weeks();
               String code = aYCode+i+"";
               String weekStart = dateFormat.format(start.getTime());
               start.add(Calendar.DATE, 6);
               String weekEnd = dateFormat.format(start.getTime());
               weeksService.save(code, i, weekStart, weekEnd, aY);
               start.add(Calendar.DATE, 1);
            }      */

            List<AcademicYear> academicYearList = academicYearService.list();

            /*
             * Put data back to view
             */
            model.put("academicYearList", academicYearList);
            return "cp.academicYears";
        }
    }

    /**
     * Save a paper
     * @param paperValid
     * @param result
     * @param model
     * @param session
     * @return String
     */
    @RequestMapping(value = "saveAcademicYear", method = RequestMethod.POST)
    public String saveAcademicYear(HttpServletRequest request,
            @Valid @ModelAttribute("academicYearAdd") AcademicYearValidation academicYearValidation,
            BindingResult result, Map model, HttpSession session) {
        /*
         * Get list of paper category and journalList
         */

        /*
         * Put data back to view
         */
        if (result.hasErrors()) {
            return "cp.addAcademicYear";
        } else {
            /*
             * Prepare data for inserting DB
             */
            String aYCode = academicYearValidation.getACAYEAR_Code();
            String aYStartDate = academicYearValidation.getACAYEAR_FromDate();
            String aYEndDate = academicYearValidation.getACAYEAR_ToDate();
            academicYearService.save(aYCode, aYStartDate, aYEndDate);

            AcademicYear aY = academicYearService.loadByCode(aYCode);
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();

            try {
                start.setTime(dateFormat.parse(aYStartDate));
                end.setTime(dateFormat.parse(aYEndDate));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int i = 1;
            while (end.after(start)) {
                Weeks week = new Weeks();
                String code = aYCode + i + "";
                String weekStart = dateFormat.format(start.getTime());
                start.add(Calendar.DATE, 6);
                String weekEnd = dateFormat.format(start.getTime());
                weeksService.save(code, i, weekStart, weekEnd, aY);
                start.add(Calendar.DATE, 1);
            }

            List<AcademicYear> academicYearList = academicYearService.list();

            /*
             * Put data back to view
             */
            model.put("academicYearList", academicYearList);
            return "cp.academicYears";
        }
    }
}