/*
 * RowInfoListBuilder.java
 *
 * Created on May 25, 2006, 12:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package usda.weru.util.diff.listbuilders;

import usda.weru.mcrew.CropObject;
import usda.weru.mcrew.JulianCalendar;
import usda.weru.mcrew.OperationObject;
import usda.weru.mcrew.RowInfo;
import usda.weru.util.diff.*;

/**
 *
 * @author Joseph Levin
 */
public class RowInfoListBuilder extends ObjectListBuilder {

    /**
     *
     * @param engine
     * @param a
     * @param b
     * @return
     * @throws IncomparableObjectsException
     */
    @Override
    public DiffNode buildList(DiffEngine engine, Object a, Object b) throws IncomparableObjectsException {
        test(RowInfo.class, a, b);
        return applyExtras(buildList(engine, (RowInfo) a, (RowInfo) b));
    }

    /**
     *
     * @param engine
     * @param rowA
     * @param rowB
     * @return
     */
    public DiffNode buildList(DiffEngine engine, RowInfo rowA, RowInfo rowB) {
        DiffNode node = new DiffNode("Row Info", rowA, rowB);

        if (rowA != null) {
            node.setA(" ");
        }
        if (rowB != null) {
            node.setB(" ");
        }

        //Date, Operation, Crop
        JulianCalendar dateA = null;
        JulianCalendar dateB = null;
        OperationObject opA = null;
        OperationObject opB = null;
        CropObject cropA = null;
        CropObject cropB = null;
        String nameA = null;
        String nameB = null;

        if (rowA != null) {
            dateA = rowA.getDate();
            opA = (OperationObject) rowA.getDataObject("operation");
            cropA = opA.getCrop();

            nameA = dateA.toString() + " : " + opA.getOperationName();
            if (cropA != null) {
                nameA += " : " + cropA.getCropName();
            }
        }
        if (rowB != null) {
            dateB = rowB.getDate();
            opB = (OperationObject) rowB.getDataObject("operation");
            cropB = opB.getCrop();

            nameB = dateB.toString() + " : " + opB.getOperationName();
            if (cropB != null) {
                nameB += " : " + cropB.getCropName();
            }
        }

        node.setA(nameA);
        node.setB(nameB);

        if (rowA != null && rowB != null) {
            node.addChild("Date", dateA, dateB);
            node.queueChild(opA, opB);
            if (cropA != null || cropB != null) {
                node.queueChild(cropA, cropB);
            }
        }

        return node;
    }

}
