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

Java tutorial

Introduction

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

Source

/*******************************************************************************
 * Copyright (c) 2009 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 com.sfs.Formatter;
import com.sfs.whichdoctor.formatter.OutputFormatter;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * The Class ItemBean.
 *
 * @author David Harrison 30th May 2003 9th April 2005
 */
public class ItemBean extends WhichDoctorBean {

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

    /** The Constant MAX_WEIGHTING. */
    private static final int MAX_WEIGHTING = 10;

    /** The object1 guid. */
    private int object1GUID;

    /** The object2 guid. */
    private int object2GUID;

    /** The reference guid. */
    private int referenceGUID;

    /** The permission. */
    private String permission;

    /** The start date. */
    private Date startDate;

    /** The end date. */
    private Date endDate;

    /** The weighting. */
    private int weighting;

    /** The order index. */
    private String orderIndex;

    /** The name. */
    private String name;

    /** The title. */
    private String title;

    /** The comment. */
    private String comment;

    /** The item type. */
    private String itemType;

    /** The identity. */
    private WhichDoctorCoreIdentityBean identity;

    /**
     * Instantiates a new item bean.
     */
    public ItemBean() {
    }

    /**
     * Instantiates a new item bean.
     *
     * @param person the person
     */
    public ItemBean(final PersonBean person) {
        if (person != null) {
            this.name = OutputFormatter.toMemberName(person) + " (" + person.getPersonIdentifier() + ")";
            this.object1GUID = person.getGUID();
            this.orderIndex = person.getOrderIndex();
        }
    }

    /**
     * Instantiates a new item bean.
     *
     * @param organisation the organisation
     */
    public ItemBean(final OrganisationBean organisation) {
        if (organisation != null) {
            this.name = organisation.getName();
            this.object1GUID = organisation.getGUID();
            this.orderIndex = organisation.getOrderIndex();
        }
    }

    /**
     * Instantiates a new item bean.
     *
     * @param financeObject the finance object
     */
    public ItemBean(final WhichDoctorCoreFinanceBean financeObject) {
        if (financeObject != null) {
            this.name = financeObject.getAbbreviation() + " " + financeObject.getNumber() + " - "
                    + financeObject.getDescription();
            this.object1GUID = financeObject.getGUID();
            this.orderIndex = financeObject.getOrderIndex();
        }
    }

    /**
     * Instantiates a new item bean.
     *
     * @param rotation the rotation
     */
    public ItemBean(final RotationBean rotation) {
        if (rotation != null) {
            this.name = OutputFormatter.toMemberName(rotation.getPerson()) + ": " + rotation.getDescription()
                    + "  (" + Formatter.convertDate(rotation.getStartDate()) + ")";
            this.object1GUID = rotation.getGUID();
            this.orderIndex = rotation.getOrderIndex();
        }
    }

    /**
     * Instantiates a new item bean.
     *
     * @param group the group
     */
    public ItemBean(final GroupBean group) {
        if (group != null) {
            this.name = OutputFormatter.toFormattedGroup(group);
            this.object1GUID = group.getGUID();
            this.orderIndex = group.getOrderIndex();
        }
    }

    /**
     * Sets the object1 guid.
     *
     * @param object1GUIDVal the new object1 guid
     */
    public final void setObject1GUID(final int object1GUIDVal) {
        this.object1GUID = object1GUIDVal;
    }

    /**
     * Gets the object1 guid.
     *
     * @return the object1 guid
     */
    public final int getObject1GUID() {
        return this.object1GUID;
    }

    /**
     * Sets the object2 guid.
     *
     * @param object2GUIDVal the new object2 guid
     */
    public final void setObject2GUID(final int object2GUIDVal) {
        this.object2GUID = object2GUIDVal;
    }

    /**
     * Gets the object2 guid.
     *
     * @return the object2 guid
     */
    public final int getObject2GUID() {
        return this.object2GUID;
    }

    /**
     * The referenceGUID refers to an associated object, e.g. Rotation. An item
     * with an associated ReferenceGUID should not be manually edited as its
     * fields are dependent on that of the referenced object.
     *
     * @param referenceGUIDVal the reference guid
     */
    public final void setReferenceGUID(final int referenceGUIDVal) {
        this.referenceGUID = referenceGUIDVal;
    }

    /**
     * Gets the reference guid.
     *
     * @return the reference guid
     */
    public final int getReferenceGUID() {
        return this.referenceGUID;
    }

    /**
     * Sets the permission.
     *
     * @param permissionVal the new permission
     */
    public final void setPermission(final String permissionVal) {
        this.permission = permissionVal;
    }

    /**
     * Gets the permission.
     *
     * @return the permission
     */
    public final String getPermission() {

        String permissionVal = this.permission + " item";

        if (StringUtils.equalsIgnoreCase(this.permission, "systemAdmin")) {
            permissionVal = this.permission;
        }
        if (StringUtils.equalsIgnoreCase(getItemType(), "Mentor")) {
            permissionVal = "training";
        }
        if (StringUtils.equalsIgnoreCase(getItemType(), "Employment")
                || StringUtils.equalsIgnoreCase(getItemType(), "Employee")
                || StringUtils.equalsIgnoreCase(getItemType(), "Employer")) {
            permissionVal = "employee";
        }
        return permissionVal;
    }

    /**
     * Sets the weighting.
     *
     * @param weightingVal the new weighting
     */
    public final void setWeighting(final int weightingVal) {
        this.weighting = weightingVal;
    }

    /**
     * Gets the weighting.
     *
     * @return the weighting
     */
    public final int getWeighting() {
        return this.weighting;
    }

    /**
     * Sets the order index.
     *
     * @param orderIndexVal the order index
     * @param orderNumberVal the order number
     */
    public final void setOrderIndex(final String orderIndexVal, final int orderNumberVal) {
        String prefix = "";
        if (this.weighting < MAX_WEIGHTING) {
            prefix += "0";
        }
        prefix += String.valueOf(this.weighting);
        this.orderIndex = prefix + "_" + orderIndexVal + "_" + String.valueOf(orderNumberVal);
    }

    /**
     * Gets the order index.
     *
     * @return the order index
     */
    public final String getOrderIndex() {
        return this.orderIndex;
    }

    /**
     * Sets the name.
     *
     * @param nameVal the new name
     */
    public final void setName(final String nameVal) {
        this.name = nameVal;
    }

    /**
     * Gets the name.
     *
     * @return the name
     */
    public final String getName() {
        if (this.name == null) {
            this.name = "";
        }
        return this.name;
    }

    /**
     * Sets the title.
     *
     * @param titleVal the new title
     */
    public final void setTitle(final String titleVal) {
        this.title = titleVal;
    }

    /**
     * Gets the title.
     *
     * @return the title
     */
    public final String getTitle() {
        if (this.title == null) {
            this.title = "";
        }
        return this.title;
    }

    /**
     * Sets the comment.
     *
     * @param commentVal the new comment
     */
    public final void setComment(final String commentVal) {
        this.comment = commentVal;
    }

    /**
     * Gets the comment.
     *
     * @return the comment
     */
    public final String getComment() {
        if (this.comment == null) {
            this.comment = "";
        }
        return this.comment;
    }

    /**
     * Sets the start date.
     *
     * @param startDateVal the new start date
     */
    public final void setStartDate(final Date startDateVal) {
        this.startDate = startDateVal;
    }

    /**
     * Gets the start date.
     *
     * @return the start date
     */
    public final Date getStartDate() {
        return this.startDate;
    }

    /**
     * Sets the end date.
     *
     * @param endDateVal the new end date
     */
    public final void setEndDate(final Date endDateVal) {
        this.endDate = endDateVal;
    }

    /**
     * Gets the end date.
     *
     * @return the end date
     */
    public final Date getEndDate() {
        return this.endDate;
    }

    /**
     * Sets the item type.
     *
     * @param itemTypeVal the new item type
     */
    public final void setItemType(final String itemTypeVal) {
        this.itemType = itemTypeVal;
    }

    /**
     * Gets the item type.
     *
     * @return the item type
     */
    public final String getItemType() {
        return this.itemType;
    }

    /**
     * Sets the identity.
     *
     * @param identityRef the new identity
     */
    public final void setIdentity(final WhichDoctorCoreIdentityBean identityRef) {
        this.identity = identityRef;
    }

    /**
     * Gets the identity.
     *
     * @return the identity
     */
    public final WhichDoctorCoreIdentityBean getIdentity() {
        return this.identity;
    }

    /**
     * Display.
     *
     * @param startDateVal the start date
     * @param endDateVal the end date
     *
     * @return true, if successful
     */
    public final boolean display(final Date startDateVal, final Date endDateVal) {
        boolean display = false;

        boolean startOk = true;
        boolean endOk = true;
        if (this.startDate != null && endDateVal != null) {
            if (this.startDate.compareTo(endDateVal) > 0) {
                // item starts after EndDate - do not show
                startOk = false;
            }
        }

        if (this.endDate != null && startDateVal != null) {
            if (this.endDate.compareTo(startDateVal) < 0) {
                // item ends before the StartDate - do not show
                endOk = false;
            }
        }
        if (startOk && endOk) {
            display = true;
        }
        return display;
    }

    /**
     * Gets the information.
     *
     * @param defaultTitle the default title
     * @param preferences the preferences
     *
     * @return the information
     */
    public final String getInformation(final String defaultTitle, final PreferencesBean preferences) {
        StringBuffer information = new StringBuffer();

        StringBuffer strDescription = new StringBuffer();
        String strStartDate = "";
        String strEndDate = "";

        if (StringUtils.isNotBlank(this.getTitle())) {
            strDescription.append(this.getTitle());
        }
        if (StringUtils.isNotBlank(defaultTitle) && StringUtils.isBlank(strDescription.toString())) {
            strDescription.append(defaultTitle);
        }
        if (strDescription.length() > 0 && this.getReferenceGUID() > 0) {
            strDescription.append(" <a href=\"");
            strDescription.append(preferences.buildUrl("read", "rotations", "guid", this.getReferenceGUID()));
            strDescription.append("\">&raquo; view</a>");
        }
        if (strDescription.length() > 0) {
            strDescription.append("<br />");
        }

        if (this.getStartDate() != null) {
            strStartDate = Formatter.convertDate(this.getStartDate());
        }
        if (this.getEndDate() != null) {
            strEndDate = Formatter.convertDate(this.getEndDate());
        }

        if (strDescription.length() > 0 || strStartDate.length() > 0 || strEndDate.length() > 0) {

            information.append(strDescription.toString());

            if (strStartDate.length() > 0 || strEndDate.length() > 0) {
                information.append(" <span class=\"informationText\">(");
                information.append(strStartDate);
                information.append(" &raquo; ");
                information.append(strEndDate);
                information.append(")</span>");
            }
        }
        return information.toString();
    }
}