NotNullGetNameToStringStyle.java

package com.tradecloud.dto.base;

import com.tradecloud.common.base.StaticDataEntityBase;
import com.tradecloud.domain.party.base.Company;
import com.tradecloud.domain.party.base.OrganisationalUnitCompany;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.builder.StandardToStringStyle;

/**
 * This is an attempt to make a ToStringStyle that does no output null values,
 * and only outputs the name fields of Tradecloud classes, instead of their
 * toString values.
 *
 * @author jon
 */
public class NotNullGetNameToStringStyle extends StandardToStringStyle {

    public static StandardToStringStyle STYLE = new NotNullGetNameToStringStyle();

    private static final DateFormat DF = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
        if (value != null) {
            if (value instanceof StaticDataEntityBase) {
                super.appendDetail(buffer, fieldName, ((StaticDataEntityBase) value).getName());
            }
            if (value instanceof Company) {
                super.appendDetail(buffer, fieldName, ((Company) value).getName());
            }
            if (value instanceof OrganisationalUnitCompany) {
                super.appendDetail(buffer, fieldName, ((OrganisationalUnitCompany) value).getCompany().getName());
            }
            if (value instanceof Date) {
                super.appendDetail(buffer, fieldName, DF.format((Date) value));
            }
        }
    }

}