package com.capgemini.reports.definition.r11report.checker;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.queries.saved.modelobject.ItemQuery;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.capgemini.reports.reports.parameters.Generics;
import com.capgemini.reports.reports.parameters.checker.GenericExistenceChecker;
import com.teamcenter.soa.client.model.strong.Item;

import java.util.List;

/**
 * This class checks if a Generics (which represents a Generic and/or GenericAssemb) exists within the TC database for a R11 report.
 */
public class R11GenericExistenceChecker extends GenericExistenceChecker {

    /**
     * Constructor.
     *
     * @param generics {@link Generics} - the generic
     */
    public R11GenericExistenceChecker(final Generics generics) {
        super(generics);
    }

    // generic assemb not allowed!
    @Override
    protected void genericExists(final TcSession session) throws InvalidUserInputException {
        List<Item> list = new ItemQuery(session).searchID(super.generics.getGeneric()).searchGeneric().findAll();
        List<Item> listGenericAssemb = new ItemQuery(session).searchID(generics.getGeneric()).searchGenericAssemb().findAll();

        if (list == null || list.isEmpty()) {
            if (!(listGenericAssemb == null || listGenericAssemb.isEmpty())) {
                throw new InvalidUserInputException(UserInputErrorEnum.THE_GENERIC_MUST_BELONG_TO_THE_TECHNICAL_VIEW);
            } else {
                throw new InvalidUserInputException(getTechnicalErrorMsg());
            }
        }
    }
}
