com.sfs.whichdoctor.beans.AgedDebtorsAnalysisBean.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.beans.AgedDebtorsAnalysisBean.java

Source

/*******************************************************************************
 * Copyright (c) 2010 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.whichdoctor.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.sfs.DataFilter;

/**
 * The Class AgedDebtorsAnalysisBean.
 */
public class AgedDebtorsAnalysisBean extends FinancialAnalysisCore {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 311131L;

    /** The data logger. */
    private static Logger logger = Logger.getLogger(AgedDebtorsAnalysisBean.class);

    /** The as at date. */
    private Date asAtDate;

    /** The show zero balances. */
    private boolean showZeroBalances;

    /** The period length. */
    private String periodLength;

    /** The number of periods. */
    private int numberOfPeriods;

    /** The group by. */
    private String groupBy;

    /** The total. */
    private double total;

    /** The periods for the aged debtors analysis. */
    private TreeMap<Integer, AgedDebtorsPeriod> periods;

    /** The groupings. */
    private TreeMap<String, AgedDebtorsGrouping> groupings;

    /** The Constant DEFAULT_PERIOD_LENGTH. */
    private static final String DEFAULT_PERIOD_LENGTH = "1m";

    /** The Constant DEFAULT_NUMBER_OF_PERIODS. */
    private static final int DEFAULT_NUMBER_OF_PERIODS = 4;

    /**
     * Sets the parameters for the aged debtors analysis.
     *
     * @param asAtDateVal the as at date
     * @param periodLengthVal the period length
     * @param numberOfPeriodsVal the number of periods
     * @param showZeroBalancesVal the boolean whether to show zero balances
     */
    public final void setParameters(final Date asAtDateVal, final String periodLengthVal,
            final int numberOfPeriodsVal, final boolean showZeroBalancesVal) {

        this.asAtDate = new Date(asAtDateVal.getTime());
        this.periodLength = StringUtils.replace(periodLengthVal, " ", "").toLowerCase();
        this.numberOfPeriods = numberOfPeriodsVal;
        this.showZeroBalances = showZeroBalancesVal;
        this.periods = constructPeriods();
    }

    /**
     * Gets the as at date.
     *
     * @return the as at date
     */
    public final Date getAsAtDate() {
        return this.asAtDate;
    }

    /**
     * Gets the period length. e.g. 30d, 3m, 1y
     *
     * @return the period length
     */
    public final String getPeriodLength() {
        if (StringUtils.isBlank(this.periodLength)) {
            this.periodLength = DEFAULT_PERIOD_LENGTH;
        }
        return this.periodLength;
    }

    /**
     * Gets the number of periods.
     *
     * @return the number of periods
     */
    public final int getNumberOfPeriods() {
        if (this.numberOfPeriods == 0) {
            this.numberOfPeriods = DEFAULT_NUMBER_OF_PERIODS;
        }
        return this.numberOfPeriods;
    }

    /**
     * Gets the zero balance flag.
     *
     * @return the zero balance flag
     */
    public final boolean getShowZeroBalances() {
        return this.showZeroBalances;
    }

    /**
     * Sets the group by value.
     *
     * @param groupByVal the new group by
     */
    public final void setGroupBy(final String groupByVal) {
        this.groupBy = groupByVal;
    }

    /**
     * Gets the group by.
     *
     * @return the group by
     */
    public final String getGroupBy() {
        if (this.groupBy == null) {
            this.groupBy = "";
        }
        return this.groupBy;
    }

    /**
     * Sets the total.
     *
     * @param totalVal the new total
     */
    public final void setTotal(final double totalVal) {
        this.total = totalVal;
    }

    /**
     * Gets the total.
     *
     * @return the total
     */
    public final double getTotal() {
        return this.total;
    }

    /**
     * Gets the periods for the aged debtors analysis.
     *
     * @return the periods
     */
    public final TreeMap<Integer, AgedDebtorsPeriod> getPeriods() {
        if (this.periods == null) {
            this.periods = new TreeMap<Integer, AgedDebtorsPeriod>();
        }
        return this.periods;
    }

    /**
     * Sets the groupings.
     *
     * @param groupingsMap the new grouping map
     */
    public final void setGroupings(final TreeMap<String, AgedDebtorsGrouping> groupingsMap) {
        this.groupings = groupingsMap;
    }

    /**
     * Gets the groups for the aged debtors analysis.
     *
     * @return the groups map
     */
    public final TreeMap<String, AgedDebtorsGrouping> getGroupings() {
        if (this.groupings == null) {
            this.groupings = new TreeMap<String, AgedDebtorsGrouping>();
        }
        return this.groupings;
    }

    /**
     * Gets the group name.
     *
     * @param guid the guid
     * @return the group name
     */
    public final String getGroupName(final int guid) {

        String groupName = "";

        if (StringUtils.isNotBlank(this.getGroupBy())) {
            if (this.getPeople().containsKey(guid)) {
                PersonBean person = this.getPerson(guid);

                if (person != null) {
                    groupName = person.getMembershipField("Membership Type");

                    if (StringUtils.equalsIgnoreCase(this.groupBy, "Division")) {
                        groupName = person.getMembershipField("Division");
                    }
                    if (StringUtils.equalsIgnoreCase(this.groupBy, "Status")) {
                        groupName = person.getMembershipField("Status");
                    }
                    if (StringUtils.equalsIgnoreCase(this.groupBy, "Region")) {
                        groupName = person.getRegion();
                    }
                }
            }
            if (this.getOrganisations().containsKey(guid)) {
                groupName = "Organisations";
            }
        }
        return groupName;
    }

    /**
     * Construct the periods for the aged debtors analysis.
     *
     * @return the period tree map
     */
    private TreeMap<Integer, AgedDebtorsPeriod> constructPeriods() {

        TreeMap<Integer, AgedDebtorsPeriod> newPeriods = new TreeMap<Integer, AgedDebtorsPeriod>();

        final int hour = 23;
        final int minute = 59;
        final int second = 59;

        String length = this.getPeriodLength();
        String unit = "Month";
        int calendarField = Calendar.MONTH;
        int duration = 1;

        try {
            if (length.indexOf("d") > 0) {
                // This is a day period
                unit = "Day";
                calendarField = Calendar.DAY_OF_MONTH;
                duration = Integer.parseInt(length.substring(0, length.indexOf("d")));
            }
            if (length.indexOf("m") > 0) {
                // This is a month period
                unit = "Month";
                calendarField = Calendar.MONTH;
                duration = Integer.parseInt(length.substring(0, length.indexOf("m")));
            }
            if (length.indexOf("y") > 0) {
                // This is a year period
                unit = "Year";
                calendarField = Calendar.YEAR;
                duration = Integer.parseInt(length.substring(0, length.indexOf("y")));
            }
        } catch (NumberFormatException nfe) {
            // An error occurred while parsing the period length, set to defaults
            unit = "Month";
            duration = 1;
            calendarField = Calendar.MONTH;
        }

        for (int i = 0; i < this.getNumberOfPeriods(); i++) {

            String title = "Current";
            if (i > 0) {
                int x = duration * i;
                title = String.valueOf(x) + " " + unit;

                if (x > 1) {
                    title += "s";
                }
            }

            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();

            start.setTime(getAsAtDate());
            start.add(calendarField, -duration * i);
            start.set(Calendar.HOUR, hour);
            start.set(Calendar.MINUTE, minute);
            start.set(Calendar.SECOND, second);

            end.setTimeInMillis(start.getTimeInMillis());
            // Add one second to the end date, then subtract a period of time
            end.add(Calendar.SECOND, 1);
            end.add(calendarField, -duration);

            if (i + 1 == this.getNumberOfPeriods()) {
                // Add a plus to the end of the title to signify it is the last period
                title += "+";
                // The last period has the lowest possible end date
                Date lastDate = DataFilter.parseDate("1/1/1900", false);
                end.setTimeInMillis(lastDate.getTime());
            }

            Date startDate = new Date(start.getTimeInMillis());
            Date endDate = new Date(end.getTimeInMillis());

            try {
                newPeriods.put(i, new AgedDebtorsPeriod(i, title, startDate, endDate));
            } catch (WhichDoctorBeanException wbe) {
                logger.error("Error creating aged debtors period", wbe);
            }
        }
        return newPeriods;
    }
}