Java tutorial
/** * TNTConcept Easy Enterprise Management by Autentia Real Bussiness Solution S.L. * Copyright (C) 2007 Autentia Real Bussiness Solution S.L. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.autentia.tnt.manager.holiday; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.time.DateUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.autentia.tnt.businessobject.Holiday; import com.autentia.tnt.businessobject.HolidayState; import com.autentia.tnt.businessobject.RequestHoliday; import com.autentia.tnt.businessobject.User; import com.autentia.tnt.businessobject.UserHolidaysState; import com.autentia.tnt.businessobject.WorkingAgreement; import com.autentia.tnt.dao.hibernate.UserDAO; import com.autentia.tnt.dao.hibernate.WorkingAgreementDAO; import com.autentia.tnt.dao.search.HolidaySearch; import com.autentia.tnt.dao.search.RequestHolidaySearch; import com.autentia.tnt.manager.holiday.HolidayManager; import com.autentia.tnt.util.SpringUtils; /* Activity - generated by stajanov (do not edit/delete) */ public class UserHolidaysStateManager { /** Logger */ private static final Log log = LogFactory.getLog(UserHolidaysStateManager.class); private UserDAO userDAO; public static UserHolidaysStateManager getDefault() { return (UserHolidaysStateManager) SpringUtils.getSpringBean("managerUserHolidaysState"); } /** * Empty constructor needed by CGLIB (Spring AOP) */ protected UserHolidaysStateManager() { } /** * Default constructor * @deprecated do not construct managers alone: use Spring's declared beans */ public UserHolidaysStateManager(UserDAO userDAO) { this.userDAO = userDAO; } /** * @return Indica si un da es festivo */ private boolean isHoliday(List<Holiday> fiestas, Date date) { // Es fin de semana ? Calendar cActual = Calendar.getInstance(); cActual.setTime(date); if ((cActual.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (cActual.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) { return true; } Iterator<Holiday> ite = fiestas.iterator(); Holiday current; while (ite.hasNext()) { current = ite.next(); if (DateUtils.isSameDay(current.getDate(), date)) { return true; } } return false; } /** * @return Devuelve el nmero de das laborables que hay entre dos fechas */ public int getWorkingDays(Date fromDate, Date toDate) { int total = 0; // Evitamos un bucle infinito en el bucle que viene a continuacin if (fromDate.before(toDate) || DateUtils.isSameDay(fromDate, toDate)) { HolidaySearch fiestaSearch = new HolidaySearch(); Date current = (Date) fromDate.clone(); fiestaSearch.setStartDate(fromDate); fiestaSearch.setEndDate(toDate); List<Holiday> fiestas = HolidayManager.getDefault().getAllEntities(fiestaSearch, null); while (current.before(toDate) || DateUtils.isSameDay(current, toDate)) { if (!this.isHoliday(fiestas, current)) { total++; } current = DateUtils.addDays(current, 1); } } return total; } public UserHolidaysState calcUserHolidaysState(User usuario, Date chargeYear) { UserHolidaysState uhs = new UserHolidaysState(); uhs.setUser(usuario); // WorkingAgreement attribute is an HB proxy not initialized, we need to get it from DB WorkingAgreementDAO workingAgreementDao = WorkingAgreementDAO.getDefault(); WorkingAgreement agreement = workingAgreementDao.getById(usuario.getAgreement().getId()); // and refresh the user instance agreement attribute usuario.setAgreement(agreement); uhs.setTotalYear(agreement.getHolidays()); int acceptedHolidays = 0; if (chargeYear != null) { HolidayManager fiestasManager = HolidayManager.getDefault(); // We must take in account previous year holidays and next year holidays Calendar calMin = Calendar.getInstance(); calMin.setTime(chargeYear); calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH)); calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH)); calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY)); calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE)); calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND)); calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND)); Calendar calMax = Calendar.getInstance(); calMax.setTime(chargeYear); calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH)); calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH)); calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY)); calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE)); calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND)); calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND)); calMin.add(Calendar.YEAR, -1); calMax.add(Calendar.YEAR, 1); HolidaySearch fiestaSearch = new HolidaySearch(); fiestaSearch.setStartDate(calMin.getTime()); fiestaSearch.setEndDate(calMax.getTime()); List<Holiday> listFiestas = fiestasManager.getAllEntities(fiestaSearch, null); calMin.setTime(chargeYear); calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH)); calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH)); calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY)); calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE)); calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND)); calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND)); calMax.setTime(chargeYear); calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH)); calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH)); calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY)); calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE)); calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND)); calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND)); RequestHolidayManager holyManager = RequestHolidayManager.getDefault(); RequestHolidaySearch holSearch = new RequestHolidaySearch(); holSearch.setUserRequest(uhs.getUser()); holSearch.setState(HolidayState.ACCEPT); holSearch.setStartChargeYear(calMin.getTime()); holSearch.setEndChargeYear(calMax.getTime()); List<RequestHoliday> listH = holyManager.getAllEntities(holSearch, null); for (RequestHoliday rH : listH) { Calendar cActual = Calendar.getInstance(); cActual.setTime(rH.getBeginDate()); while (!cActual.getTime().after(rH.getFinalDate())) { if (cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { boolean isFiesta = false; for (Holiday fiest : listFiestas) { Calendar cFiesta = Calendar.getInstance(); cFiesta.setTime(fiest.getDate()); if (cFiesta.get(Calendar.YEAR) == cActual.get(Calendar.YEAR) && cFiesta.get(Calendar.MONTH) == cActual.get(Calendar.MONTH) && cFiesta.get(Calendar.DAY_OF_MONTH) == cActual.get(Calendar.DAY_OF_MONTH)) { isFiesta = true; } } if (!isFiesta) { acceptedHolidays++; } } cActual.add(Calendar.DAY_OF_MONTH, 1); } } uhs.setTotalAccepted(acceptedHolidays); Calendar calAuxCont = Calendar.getInstance(); calAuxCont.setTime(uhs.getUser().getStartDate()); Calendar calAux = Calendar.getInstance(); calAux.setTime(chargeYear); int yearCharge = calAux.get(Calendar.YEAR); int yearContract = calAuxCont.get(Calendar.YEAR); if (yearCharge == yearContract) { // Dividimos los das de cada usuario entre los meses del ao. double ratio = uhs.getUser().getAgreement().getHolidays() / 12.0; int monthContract = calAuxCont.get(Calendar.MONTH); int meses = (Calendar.DECEMBER - monthContract); double diasVacaciones = meses * ratio; double aux = Math.ceil(diasVacaciones); uhs.setTotalYear((int) aux); } } return uhs; } /** * @return Devuelve el nmero de dias de vacaciones que le quedan a un usuario en un ao */ public int getFreeDays(User user, Date year) { UserHolidaysState state = this.calcUserHolidaysState(user, year); return state.getTotalYear() - state.getTotalAccepted(); } }