package com.capgemini.reports.teamcenter.queries.saved;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.queries.saved.AbstractReplicatedQuery;
import com.teamcenter.soa.client.model.strong.ItemRevision;

/**
 * This class is able to look for Unit Revision objects in Teamcenter.
 */
public class UnitQuery extends AbstractReplicatedQuery<ItemRevision, UnitQuery> {

    /**
     * Use this String in the Constructor to obtain Units that are going (or have gone) through a Workflow with this name.
     */
    static final String UNIT_VALIDATION_UNIT_PAR_SO = "UNIT-Validation UNIT par SO";
    /**
     * Query name of Teamcenter for request in Unit objects.
     */
    private static final String QUERY_SUFFIX_UNIT = "Unit";
    private static final String QUERY_SUFFIX_UNIT_WITH_WKF = "Unit_With_WKF";
    private static final String TASK_NAME = "task_name";

    /**
     * Constructor to build a Query for Unit objects.
     *
     * @param session the session for the connection
     */
    public UnitQuery(final TcSession session) {
        super(session, QUERY_SUFFIX_UNIT);
    }

    /**
     * Constructor to build a Query for Unit objects.
     *
     * @param session the session for the connection
     * @param wkfName <b>(optional)</b> Provide the name of a Workflow active or done on this unit. Input may be <code>null</code> or empty to deactivate this criteria.<br>
     *                If the input is not empty, then the Unit MUST have a workflow with the provided name.<br>
     *                <i>Exemple value:</i> {@link UnitQuery#UNIT_VALIDATION_UNIT_PAR_SO}
     */
    public UnitQuery(final TcSession session, final String wkfName) {
        super(session, (wkfName == null || wkfName.isEmpty()) ? QUERY_SUFFIX_UNIT : QUERY_SUFFIX_UNIT_WITH_WKF);
        if (wkfName != null && !wkfName.isEmpty()) {
            this.addParam(TASK_NAME, wkfName);
        }
    }

    @Override
    protected UnitQuery selfCast() {
        return this;
    }

}
