com.sfs.whichdoctor.formatter.GroupFormatter.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.formatter.GroupFormatter.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.formatter;

import com.sfs.Formatter;
import com.sfs.whichdoctor.beans.GroupBean;
import com.sfs.whichdoctor.beans.ItemBean;
import com.sfs.whichdoctor.beans.MemoBean;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;

public class GroupFormatter {

    public static String getField(final GroupBean group, final String field, final String format) {

        String value = "";
        if (group == null || field == null) {
            return value;
        }

        if (field.compareTo("Tags") == 0) {
            value = OutputFormatter.toTagList(group.getTags());
        }

        if (field.compareTo("GUID") == 0) {
            if (group.getGUID() > 0) {
                value = String.valueOf(group.getGUID());
            }
        }

        if (field.compareTo("GroupId") == 0) {
            if (group.getId() > 0) {
                value = String.valueOf(group.getId());
            }
        }
        if (field.compareTo("Group Name") == 0) {
            value = group.getName();
        }
        if (field.compareTo("Group Description") == 0) {
            value = StringUtils.replace(group.getDescription(), "\n", "<br />");
        }
        if (field.compareTo("Group Type") == 0) {
            value = group.getType();
        }
        if (field.compareTo("Created By") == 0) {
            value = group.getCreatedBy();
        }
        if (field.compareTo("Date Created") == 0) {
            if (group.getCreatedDate() != null) {
                value = Formatter.conventionalDate(group.getCreatedDate());
            }
        }
        if (field.compareTo("Modified By") == 0) {
            value = group.getModifiedBy();
        }
        if (field.compareTo("Date Modified") == 0) {
            if (group.getModifiedDate() != null) {
                value = Formatter.conventionalDate(group.getModifiedDate());
            }
        }
        if (value == null) {
            value = "";
        }
        return value;
    }

    public static String getField(final GroupBean group, final Object object, final String section,
            final String field, final String format) {

        String value = "";
        if (section == null) {
            return value;
        }
        if (field == null) {
            return value;
        }
        if (field.compareTo("GUID") == 0) {
            value = String.valueOf(group.getGUID());
        }
        if (field.compareTo("Group Name") == 0) {
            value = group.getName();
        }
        if (field.compareTo("Group Type") == 0) {
            value = group.getType();
        }
        if (section.compareTo("Items") == 0) {
            ItemBean item = (ItemBean) object;

            if (field.compareTo("Item Name") == 0) {
                value = item.getName();
            }
            if (field.compareTo("Item Title") == 0) {
                value = item.getTitle();
            }
            if (field.compareTo("Item Comment") == 0) {
                value = StringUtils.replace(item.getComment(), "\n", "<br />");
            }
            if (field.compareTo("Item Weighting") == 0) {
                value = String.valueOf(item.getWeighting());
            }
            if (field.compareTo("Item Start") == 0) {
                value = Formatter.conventionalDate(item.getStartDate());
            }
            if (field.compareTo("Item End") == 0) {
                value = Formatter.conventionalDate(item.getEndDate());
            }
        }

        if (section.compareTo("Memos") == 0) {
            MemoBean memo = (MemoBean) object;

            if (field.compareTo("Type") == 0) {
                value = memo.getType();
            }
            if (field.compareTo("Date Created") == 0) {
                value = Formatter.conventionalDate(memo.getCreatedDate());
            }
            if (field.compareTo("Date Expires") == 0) {
                value = Formatter.conventionalDate(memo.getExpires());
            }
            if (field.compareTo("Message") == 0) {
                value = memo.getMessage();
            }
        }

        if (value == null) {
            value = "";
        }
        return value;
    }

    public static Collection<Object> getCollection(final GroupBean group, final String section) {

        Collection<Object> collection = new ArrayList<Object>();

        if (section != null) {
            if (section.compareTo("Items") == 0) {
                if (group.getItems() != null) {
                    for (String key : group.getItems().keySet()) {
                        ItemBean item = group.getItems().get(key);
                        collection.add(item);
                    }
                }
            }

            if (section.compareTo("Memos") == 0) {
                if (group.getMemo() != null) {
                    for (MemoBean memo : group.getMemo()) {
                        collection.add(memo);
                    }
                }
            }
        }
        return collection;
    }
}