package com.capgemini.framework.teamcenter.object.runtime;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.EmptyNomenclatureException;
import com.teamcenter.soa.client.model.strong.BOMLine;
import com.teamcenter.soa.client.model.strong.BOMWindow;

public class TcBomWindow implements TcRuntimeBusinessObject, AutoCloseable {

    private final BOMWindow bomWindow;
    private final TcSession session;

    public TcBomWindow(final TcSession session, final BOMWindow bomWindow) {
        this.session = session;
        this.bomWindow = bomWindow;
    }

    public BOMLine topLine() throws EmptyNomenclatureException {
        session.loadProperties(bomWindow, "top_line");
        BOMLine topLine = (BOMLine) bomWindow.get_top_line();
        session.loadProperties(topLine, "bl_line_object");
        if (topLine == null || topLine.get_bl_line_object() == null) {
            throw new EmptyNomenclatureException("The top item provided does not seem to be effective under the selected configuration");
        }
        return topLine;
    }

    @Override
    public void close() {
        session.getStructureManagementService().closeBOMWindows(new BOMWindow[]{bomWindow});
    }
}
