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.EPMTask;

/**
 * Search for a Workflow.
 */
public final class WorkflowOnUnitQuery extends AbstractReplicatedQuery<EPMTask, WorkflowOnUnitQuery> {

    static final String UNIT_VALIDATION_UNIT_PAR_SO = "UNIT-Validation UNIT par SO";

    /**
     * Query name of Teamcenter for requesting a Workflow, by giving the ID of the Unit it was launched upon.
     */
    private static final String QUERY_SUFFIX_WKF = "WKF_ValidationUnit_on_Unit";
    private static final String PARAM_SUFFIX_WKF_NAME = REPCOL_PREFIX + "wkf_name";
    private static final String PARAM_SUFFIX_UNIT_ID = REPCOL_PREFIX + "unit_id";

    private WorkflowOnUnitQuery(final TcSession session, final String wkfSearchName) {
        super(session, wkfSearchName);
    }

    /**
     * Construct a Query that will look for any Workflow named 'UNIT-Validation UNIT par SO' that was launched on the specified Unit.
     *
     * @param session the session
     * @param unitID  the ID of the Unit that the Workflow was working on
     * @return the Query
     */
    public static WorkflowOnUnitQuery searchValidationUnitParSO(final TcSession session, final String unitID) {
        WorkflowOnUnitQuery workflowQuery = new WorkflowOnUnitQuery(session, QUERY_SUFFIX_WKF);
        workflowQuery.addParam(PARAM_SUFFIX_WKF_NAME, UNIT_VALIDATION_UNIT_PAR_SO);
        workflowQuery.addParam(PARAM_SUFFIX_UNIT_ID, unitID);
        return workflowQuery;
    }

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

}
