package com.capgemini.reports.definition.r27report;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.framework.teamcenter.queries.saved.modelobject.ItemQuery;
import com.capgemini.reports.execution.IDataReportPreparator;
import com.capgemini.reports.execution.strategies.IExecutionStrategy;
import com.capgemini.reports.printing.PrintParameters;
import com.teamcenter.soa.client.model.strong.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * This class will allow to prepare data and findAll back data for making report R27.
 */
class DataReportPreparatorR27 implements IDataReportPreparator<ExecutionR27> {
  private static final long serialVersionUID = 1758410914529352674L;
  private static final Logger LOGGER = LoggerFactory.getLogger(DataReportPreparatorR27.class);


  @Override
  public String print(final TcSession session, ExecutionR27 execution, IExecutionStrategy executionStrategy) throws KurtException {
    PrintParameters printParameters = prepareData(session, execution);
    executionStrategy.print(printParameters, execution);
    return executionStrategy.getReportFileName(execution) + printParameters.getOuputFileExtension();
  }


  private PrintParameters prepareData(final TcSession session, final ExecutionR27 report) throws KurtException {
    LOGGER.debug("PrepareData started on: {}", report.getReportIdentifier().getReportTitle());

    session.setObjectPropPolicy(report.getFullPolicy());
    ItemQuery query = new ItemQuery(session);
    query.searchType("Alternate;DET;Part Aero;Part Mat;Part No Aero;Standard");
    List<Item> results = query.findAll();
    session.loadObjects();

    ExecutionR27.ReportDataR27 reportDataR27 = new ExecutionR27.ReportDataR27(results);
    R27Printer r27Printer = new R27Printer();
    PrintParameters printParameters = r27Printer.makeMasterTemplate(report, reportDataR27);
    r27Printer.constructDataMaps(session, printParameters, reportDataR27);
    LOGGER.debug("PrepareData ended on: {}", report.getReportIdentifier().getReportTitle());
    return printParameters;
  }

}





