package com.fp.frontend.controller.pgeneral.safe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.dto.query.DtoQuery;
import com.fp.dto.query.SubQuery;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.safe.TsafeAudit;

/**
 *
 * @author Jacqueline Arias
 *
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class SafeAuditController extends AbstractController<TsafeAudit> {

    public SafeAuditController() throws Exception {
        super(TsafeAudit.class);
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    private void postConstruct() {
        this.init();
    }

    private void init() {
        try {
            this.recperpage = 15;
            this.lrecord = new ArrayList<>();
            this.beanalias = "TSAFEAUDIT";
            super.create();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    protected void querydatabase() {
        // TODO Auto-generated method stub
        try {

            Request request = this.callerhelper.getRequest();
            DtoQuery dto = this.getDtoSafeAudit();

            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(this.beanalias, dto);
            request.setQueryTables(mtables);
            Response resp = this.callerhelper.executeQuery(request);
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.lrecord = new ArrayList<TsafeAudit>();
                MessageHelper.setMessageError(resp);
            } else {
                this.lrecord = (List<TsafeAudit>) resp.get(this.beanalias);
                super.postQuery(this.lrecord);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }

    }

    public DtoQuery getDtoSafeAudit() throws Exception {

        DtoQuery dto = super.getDtoQuery(true);

        dto.setOrderby("t.pk");
        SubQuery subqueryBranch = new SubQuery("TgeneBranch", "description", "branchdesc",
                "i.pk.branchcode = t.branchcode and i.pk.companycode = t.companycode and i.pk.companycode="
                + super.getLoginController().getRequest().getCompany().toString());
        dto.addSubQuery(subqueryBranch);

        SubQuery subqueryOffice = new SubQuery("TgeneOffice", "description", "officedesc",
                "i.pk.officecode = t.officecode and i.pk.branchcode = t.branchcode and i.pk.companycode = t.companycode and i.pk.companycode="
                + super.getLoginController().getRequest().getCompany().toString());
        dto.addSubQuery(subqueryOffice);

        SubQuery subqueryPerson = new SubQuery("TcustPersonDetail", "name", "personname",
                "i.pk.personcode = t.personcode and i.pk.dateto = to_date('2999-12-31', 'yyyy-MM-dd') and i.companycode = t.companycode and i.companycode="
                + super.getLoginController().getRequest().getCompany().toString());
        dto.addSubQuery(subqueryPerson);

        SubQuery subqueryTransaction = new SubQuery("TgeneTransaction", "name", "transactiondesc",
                "i.pk.transactioncode = t.transactioncode and i.pk.transactionmodule = t.transactionmodule");
        dto.addSubQuery(subqueryTransaction);

        return dto;
    }

}
