package com.capgemini.reports.definition.r07report;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.changeobjects.change.TcChangeNoticeRevision;
import com.capgemini.reports.changeobjects.change.TcChangeRequestRevision;
import com.capgemini.reports.teamcenter.queries.saved.ChangeNoticeR07Query;
import com.capgemini.reports.teamcenter.queries.saved.ChangeRequestR07Query;
import com.teamcenter.soa.client.model.strong.ItemRevision;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is able to request the ChangeRequests or the ChangeNotices associated to a part or generic.
 */
public class CRExtractorHelper {

    private final TcSession session;

    /**
     * Builds an instance of CR helper.
     *
     * @param session a teamcenter session
     */
    public CRExtractorHelper(final TcSession session) {
        this.session = session;
    }

    /**
     * Return map with changeRequest and the list of changeNotice for each changeRequest.
     *
     * @param itemID         ID of a generic or a part.
     * @param itemRevisionID Revision of a generic or a part.
     * @return map.
     */
    public List<TcChangeRequestRevision> getCrOnPart(final String itemID, final String itemRevisionID) {
        List<TcChangeRequestRevision> changeRequestRevs = new ArrayList<>();
        for (ItemRevision changeRequestRev : new ChangeRequestR07Query(session).searchIDAndRevision(itemID, itemRevisionID).findAll()) {
            changeRequestRevs.add(new TcChangeRequestRevision(session, changeRequestRev));
        }
        return changeRequestRevs;
    }

    /**
     * Return list of changeNotice which is not associated to a changeRequest but directly to the part or generic.
     *
     * @param itemID         ID of a generic or a part.
     * @param itemRevisionID Revision of a generic or a part.
     * @return list.
     */
    public List<TcChangeNoticeRevision> getCnOnPart(final String itemID, final String itemRevisionID) {
        List<TcChangeNoticeRevision> changeNoticeRevs = new ArrayList<>();
        for (ItemRevision changeNoticeRev : new ChangeNoticeR07Query(session).searchIDAndRevision(itemID, itemRevisionID).findAll()) {
            changeNoticeRevs.add(new TcChangeNoticeRevision(session, changeNoticeRev));
        }
        return changeNoticeRevs;
    }
}
