Java tutorial
/* * Kuali Coeus, a comprehensive research administration system for higher education. * * Copyright 2005-2015 Kuali, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.kuali.coeus.propdev.impl.print; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.kuali.coeus.common.framework.print.stream.xml.XmlStream; import org.kuali.coeus.sys.api.model.ScaleTwoDecimal; import org.kuali.coeus.propdev.impl.core.ProposalDevelopmentDocument; import org.kuali.coeus.common.budget.framework.core.Budget; import org.kuali.rice.core.api.datetime.DateTimeService; import org.kuali.rice.krad.service.BusinessObjectService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Date; import java.util.Calendar; /** * This class will contain all common methods that can be used across Proposal * Development XML generator streams . All those report XML stream * implementations need to extend and use the functions defined in this class. * */ public abstract class ProposalBaseStream implements XmlStream { @Autowired @Qualifier("dateTimeService") private DateTimeService dateTimeService; @Autowired @Qualifier("businessObjectService") private BusinessObjectService businessObjectService; /** * This method fetches the final/latest Budget associated with the given * {@link ProposalDevelopmentDocument} * * @param proposalDevelopmentDocument * @return {@link Budget} final/latest version */ protected Budget getBudget(ProposalDevelopmentDocument proposalDevelopmentDocument) { return proposalDevelopmentDocument.getDevelopmentProposal().getFinalBudget(); } /** * * This method computes the number of months between any 2 given {@link java.sql.Date} objects * * @param dateStart starting date. * @param dateEnd end date. * * @return number of months between the start date and end date. */ public ScaleTwoDecimal getNumberOfMonths(Date dateStart, Date dateEnd) { ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO; int fullMonthCount = 0; Calendar startDate = Calendar.getInstance(); Calendar endDate = Calendar.getInstance(); startDate.setTime(dateStart); endDate.setTime(dateEnd); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.SECOND); startDate.clear(Calendar.MILLISECOND); endDate.clear(Calendar.HOUR); endDate.clear(Calendar.MINUTE); endDate.clear(Calendar.SECOND); endDate.clear(Calendar.MILLISECOND); if (startDate.after(endDate)) { return ScaleTwoDecimal.ZERO; } int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE); startMonthDays++; int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE); BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide( BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP); int endMonthDays = endDate.get(Calendar.DATE); int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE); BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide( BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP); startDate.set(Calendar.DATE, 1); endDate.set(Calendar.DATE, 1); while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) { startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1); fullMonthCount++; } fullMonthCount = fullMonthCount - 1; monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount)) .add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction)); return monthCount; } /** * @return the dateTimeService */ public DateTimeService getDateTimeService() { return dateTimeService; } /** * @param dateTimeService * the dateTimeService to set */ public void setDateTimeService(DateTimeService dateTimeService) { this.dateTimeService = dateTimeService; } /** * @return the businessObjectService */ public BusinessObjectService getBusinessObjectService() { return businessObjectService; } /** * @param businessObjectService * the businessObjectService to set */ public void setBusinessObjectService(BusinessObjectService businessObjectService) { this.businessObjectService = businessObjectService; } }