SortBy.java

package com.tradecloud.domain.model.deal;

import java.util.HashMap;
import java.util.Map;

public enum SortBy {

    ESTIMATED_SETTLEMENT_DATE(0, "estimatedSettlementDate.localDate", "Estimated Settlement Date"),
    DEAL_DATE(1, "dealDate", "Deal Date"),
    MATURITY_DATE(4, "maturityDate", "Deal Maturity Date"),
    INVOICED_DATE(2, "invoicedDate", "Invoiced Date"),
    SETTLED_DATE(3, "settlementDate", "Settled Date");

    private final Byte key;
    private final String sortPropertyName;
    private final String description;

    private static final Map<Byte, SortBy> lookup = new HashMap<Byte, SortBy>();

    static {
        for (SortBy d : SortBy.values())
            lookup.put(d.getKey(), d);
    }

    SortBy(int key, String sortPropertyName, String description) {
        this.key = (byte) key;
        this.sortPropertyName = sortPropertyName;
        this.description = description;
    }

    public byte getKey() {
        return key;
    }

    public String getSortPropertyName() {
        return sortPropertyName;
    }

    public String getDescription() {
        return description;
    }

    public static SortBy get(int key) {
        return lookup.get((byte) key);
    }

    public static String toXML() {
        SortBy[] sortbys = values();
        StringBuilder builder = new StringBuilder("<sortbys>");
        if (sortbys.length > 0) {
            for (SortBy sortby : sortbys) {
                //we do not want maturity date to be part of flex screen.
                if (sortby == MATURITY_DATE) {
                    continue;
                }
                builder.append("<sortby>");
                builder.append("<code>");
                builder.append(sortby.getKey());
                builder.append("</code>");
                builder.append("<name>");
                builder.append(sortby.getDescription());
                builder.append("</name>");
                builder.append("</sortby>");
            }
        }
        builder.append("</sortbys>");
        return builder.toString();
    }
}