package com.capgemini.reports.definition.r28report;

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 R28.
 */
class DataReportPreparatorR28 implements IDataReportPreparator<ExecutionR28> {
  private static final long serialVersionUID = 1758410914529352674L;
  private static final Logger LOGGER = LoggerFactory.getLogger(DataReportPreparatorR28.class);


  @Override
  public String print(final TcSession session, ExecutionR28 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 ExecutionR28 report) throws KurtException {
    LOGGER.debug("PrepareData started on: {}", report.getReportIdentifier().getReportTitle());

    session.setObjectPropPolicy(report.getFullPolicy());
    ItemQuery query = new ItemQuery(session);
    query.searchType("RF Part");
    List<Item> results = query.findAll();
    session.loadObjects();

    ExecutionR28.ReportDataR28 reportDataR28 = new ExecutionR28.ReportDataR28(results);
    R28Printer r28Printer = new R28Printer();
    PrintParameters printParameters = r28Printer.makeMasterTemplate(report, reportDataR28);
    r28Printer.constructDataMaps(session, printParameters, reportDataR28);
    LOGGER.debug("PrepareData ended on: {}", report.getReportIdentifier().getReportTitle());
    return printParameters;
  }

}





