bean.RecommendationBean.java Source code

Java tutorial

Introduction

Here is the source code for bean.RecommendationBean.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.faces.portal.context.LiferayFacesContext;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserNotificationEvent;
import com.liferay.portal.service.*;
import com.liferay.portal.service.persistence.UserNotificationEventUtil;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import model.*;
import service.*;

/**
 *
 * @author mis
 */
@ManagedBean
@SessionScoped
public class RecommendationBean implements Serializable {

    @PersistenceUnit
    EntityManagerFactory emf;
    @EJB
    private CoopProsRepverFacadeREST coopProsRepverFacadeREST;
    private CoopProsRepver selectedProsRepver = new CoopProsRepver();
    private CoopProsRepver prosRepver = new CoopProsRepver();
    @EJB
    private CoopRepActFacadeREST coopRepActFacadeREST;
    private CoopRepAct selectedRepAct = new CoopRepAct();
    private CoopRepAct repAct = new CoopRepAct();
    @EJB
    private CoopReportCircFacadeREST coopReportCircFacadeREST;
    private CoopReportCirc selectedReportCirc = new CoopReportCirc();
    private CoopReportCirc reportCirc = new CoopReportCirc();
    @EJB
    private CoopOrgUnitFacadeREST coopOrgUnitFacadeREST;
    private CoopOrgUnit selectedOrgUnit = new CoopOrgUnit();
    private CoopOrgUnit orgUnit = new CoopOrgUnit();
    private List<CoopOrgUnit> dcOrgUnitList;
    private List<CoopOrgUnit> ptOrgUnitList;
    private DataModel<CoopOrgUnit> dcOrgUnitModel;
    private DataModel<CoopOrgUnit> ptOrgUnitModel;
    @EJB
    private CoopProspectFacadeREST coopProspectFacadeREST;
    private CoopProspect selectedProspect = new CoopProspect();
    private CoopProspect prospect = new CoopProspect();
    private List<CoopProspect> prospectList, appProsList;
    private DataModel<CoopProspect> prospectModel;
    @EJB
    private CoopProsReportFacadeREST coopProsReportFacadeREST;
    private CoopProsReport selectedProsReport = new CoopProsReport();
    private CoopProsReport prosReport = new CoopProsReport();
    private List<CoopProsReport> prosReportList;
    private DataModel<CoopProsReport> prosReportModel;
    @EJB
    private CoopReportFacadeREST coopReportFacadeREST;
    private CoopReport selectedReport = new CoopReport();
    private CoopReport report = new CoopReport();
    private List<CoopReport> reportList;
    private DataModel<CoopReport> reportModel;
    @EJB
    private CoopReportTypeFacadeREST coopReportTypeFacadeREST;
    private CoopReportType selectedReportType = new CoopReportType();
    private CoopReportType reportType = new CoopReportType();
    private List<CoopReportType> reportTypeList;
    private DataModel<CoopReportType> reportTypeModel;
    @EJB
    private CoopInfoFacadeREST coopInfoFacadeREST;
    private CoopInfo info = new CoopInfo();
    private List<CoopInfo> infoList;
    @EJB
    private CoopMemberFacadeREST coopMemberFacadeREST;
    private CoopMember member = new CoopMember();

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    CustomViewHandler view = new CustomViewHandler();

    DateStringParse dateStringParse = new DateStringParse();
    Calendar c = Calendar.getInstance();

    private UAgentInfo agent;
    private Integer reportValue = 0;
    private String dcValue, ptValue, prospectValue, ou, dtl, dcRem, pkptlRem, memNo;
    private Boolean mode = false, mode2 = false, ptStat = false, dcStat = false, pkptlStat = false;

    long a, b, currentUserId;
    long[] currentUserOrg;
    LiferayFacesContext liferayFacesContext;
    User user, currentUser;
    List<User> all, notify;
    Date now;

    String br = System.getProperty("line.separator");
    String dcTitle = "DC Remarks:";
    String pkptlTitle = "PKPTL Remarks:";

    public static final String key = "";
    public static final String fileName = key + "-recommendation-report" + ".pdf";
    public static final String path = "../../../../../" + fileName;

    @ManagedProperty(value = "#{currentDate}")
    private Date reportDateEncoded;

    public Date getReportDateEncoded() {
        return reportDateEncoded;
    }

    public void setReportDateEncoded(Date reportDateEncoded) {
        this.reportDateEncoded = reportDateEncoded;
    }

    @ManagedProperty(value = "11")
    private Integer repTypeCode;

    public Integer getRepTypeCode() {
        return repTypeCode;
    }

    public void setRepTypeCode(Integer repTypeCode) {
        this.repTypeCode = repTypeCode;
    }

    /**
     * Creates a new instance of RecommendationBean
     */
    public RecommendationBean() {

    }

    public void init() throws SystemException, PortalException {
        report.setReportDateEncoded(reportDateEncoded);
        infoList = coopInfoFacadeREST.findAll();

        liferayFacesContext = LiferayFacesContext.getInstance();
        currentUser = liferayFacesContext.getUser();
        currentUserOrg = currentUser.getOrganizationIds();

        now = new Date();
        currentUserId = liferayFacesContext.getUserId();

        for (int counter = 0; counter <= currentUserOrg.length - 1; counter++) {
            try {
                //pkptl
                if (OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName()
                        .contains("PKPTL")) {
                    dcOrgUnitList = emf.createEntityManager()
                            .createQuery("SELECT a FROM CoopOrgUnit a WHERE a.managedBy = 'PKPTL'").getResultList();
                    appProsList = emf.createEntityManager()
                            .createQuery("SELECT a FROM CoopProspect a WHERE a.prosStat = 'A'").getResultList();
                    prospectList = emf.createEntityManager()
                            .createQuery("SELECT a FROM CoopProspect a WHERE a.prosStat = 'E'").getResultList();
                    pkptlStat = true;
                    //status2 = true;

                    ou = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName();
                    orgUnit.setOuCode(ou);

                } //dc
                if (OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName()
                        .contains("DC")) {
                    ptOrgUnitList = emf.createEntityManager()
                            .createQuery("SELECT a FROM CoopOrgUnit a WHERE a.managedBy = '"
                                    + OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter])
                                            .getName()
                                    + "'")
                            .getResultList();
                    appProsList = emf.createEntityManager().createQuery(
                            "SELECT c FROM CoopProspect c JOIN c.ouCode d WHERE c.prosStat = 'A' and d.managedBy = '"
                                    + OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter])
                                            .getName()
                                    + "'")
                            .getResultList();
                    prospectList = emf.createEntityManager().createQuery(
                            "SELECT c FROM CoopProspect c JOIN c.ouCode d WHERE c.prosStat = 'R' and d.managedBy = '"
                                    + OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter])
                                            .getName()
                                    + "'")
                            .getResultList();
                    dcStat = true;
                    //status3 = true;

                    ou = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName();
                    orgUnit.setOuCode(ou);

                } //pt 
                if (OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName().contains("PT")
                        && !(OrganizationServiceUtil.getOrganization(currentUserOrg[counter]).getName()
                                .contains("PKPTL"))) {
                    appProsList = emf.createEntityManager()
                            .createQuery(
                                    "SELECT a FROM CoopProspect a WHERE a.prosStat = 'A' AND a.ouCode.ouCode = '"
                                            + OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter])
                                                    .getName()
                                            + "'")
                            .getResultList();
                    prospectList = emf.createEntityManager()
                            .createQuery(
                                    "SELECT a FROM CoopProspect a WHERE a.prosStat = 'R' AND a.ouCode.ouCode = '"
                                            + OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter])
                                                    .getName()
                                            + "'")
                            .getResultList();
                    ptStat = true;

                    ou = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName();
                    orgUnit.setOuCode(ou);

                    a = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getOrganizationId();
                    b = OrganizationLocalServiceUtil.getParentOrganizations(a).get(0).getOrganizationId();
                    notify = UserLocalServiceUtil.getOrganizationUsers(b);
                }

                memNo = (String) emf.createEntityManager()
                        .createQuery("SELECT c.memNo FROM CoopMemberUser c WHERE c.userId = " + currentUserId)
                        .getResultList().get(0);
                member.setMemNo(memNo);

            } catch (Exception e) {
            }
        }

        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        String userAgent = externalContext.getRequestHeaderMap().get("User-Agent");
        String accept = externalContext.getRequestHeaderMap().get("Accept");
        setAgent(new UAgentInfo(userAgent, accept));

        /*
         1. pdf
         2. current/active recommendation only (prospect still a prospect || still no action)
         3. reject
         ---dc->pt
         ---pkptl->dc->pt || pkptl->dc
         4. printRecommendation td width + font + add "&nbsp;"
         5. prosrepver,proslog
         6. test other pt notify
         7. currentDate (date:/date encoded:)
            
         on edit update date encoded (prosRepver) for viewing
         */
    }

    public void beanclear() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("recommendationBean", null);
    }

    /*
     GETTER AND SETTER
     */
    public CoopProsRepverFacadeREST getCoopProsRepverFacadeREST() {
        return coopProsRepverFacadeREST;
    }

    public void setCoopProsRepverFacadeREST(CoopProsRepverFacadeREST coopProsRepverFacadeREST) {
        this.coopProsRepverFacadeREST = coopProsRepverFacadeREST;
    }

    public CoopProsRepver getSelectedProsRepver() {
        return selectedProsRepver;
    }

    public void setSelectedProsRepver(CoopProsRepver selectedProsRepver) {
        this.selectedProsRepver = selectedProsRepver;
    }

    public CoopProsRepver getProsRepver() {
        return prosRepver;
    }

    public void setProsRepver(CoopProsRepver prosRepver) {
        this.prosRepver = prosRepver;
    }

    public CoopRepActFacadeREST getCoopRepActFacadeREST() {
        return coopRepActFacadeREST;
    }

    public void setCoopRepActFacadeREST(CoopRepActFacadeREST coopRepActFacadeREST) {
        this.coopRepActFacadeREST = coopRepActFacadeREST;
    }

    public CoopRepAct getSelectedRepAct() {
        return selectedRepAct;
    }

    public void setSelectedRepAct(CoopRepAct selectedRepAct) {
        this.selectedRepAct = selectedRepAct;
    }

    public CoopRepAct getRepAct() {
        return repAct;
    }

    public void setRepAct(CoopRepAct repAct) {
        this.repAct = repAct;
    }

    public CoopReportCircFacadeREST getCoopReportCircFacadeREST() {
        return coopReportCircFacadeREST;
    }

    public void setCoopReportCircFacadeREST(CoopReportCircFacadeREST coopReportCircFacadeREST) {
        this.coopReportCircFacadeREST = coopReportCircFacadeREST;
    }

    public CoopReportCirc getSelectedReportCirc() {
        return selectedReportCirc;
    }

    public void setSelectedReportCirc(CoopReportCirc selectedReportCirc) {
        this.selectedReportCirc = selectedReportCirc;
    }

    public CoopReportCirc getReportCirc() {
        return reportCirc;
    }

    public void setReportCirc(CoopReportCirc reportCirc) {
        this.reportCirc = reportCirc;
    }

    public CoopOrgUnitFacadeREST getCoopOrgUnitFacadeREST() {
        return coopOrgUnitFacadeREST;
    }

    public void setCoopOrgUnitFacadeREST(CoopOrgUnitFacadeREST coopOrgUnitFacadeREST) {
        this.coopOrgUnitFacadeREST = coopOrgUnitFacadeREST;
    }

    public CoopOrgUnit getSelectedOrgUnit() {
        return selectedOrgUnit;
    }

    public void setSelectedOrgUnit(CoopOrgUnit selectedOrgUnit) {
        this.selectedOrgUnit = selectedOrgUnit;
    }

    public CoopOrgUnit getOrgUnit() {
        return orgUnit;
    }

    public void setOrgUnit(CoopOrgUnit orgUnit) {
        this.orgUnit = orgUnit;
    }

    public List<CoopOrgUnit> getDcOrgUnitList() {
        return dcOrgUnitList;
    }

    public void setDcOrgUnitList(List<CoopOrgUnit> dcOrgUnitList) {
        this.dcOrgUnitList = dcOrgUnitList;
    }

    public List<CoopOrgUnit> getPtOrgUnitList() {
        return ptOrgUnitList;
    }

    public void setPtOrgUnitList(List<CoopOrgUnit> ptOrgUnitList) {
        this.ptOrgUnitList = ptOrgUnitList;
    }

    public DataModel<CoopOrgUnit> getDcOrgUnitModel() {
        return dcOrgUnitModel;
    }

    public void setDcOrgUnitModel(DataModel<CoopOrgUnit> dcOrgUnitModel) {
        this.dcOrgUnitModel = dcOrgUnitModel;
    }

    public DataModel<CoopOrgUnit> getPtOrgUnitModel() {
        return ptOrgUnitModel;
    }

    public void setPtOrgUnitModel(DataModel<CoopOrgUnit> ptOrgUnitModel) {
        this.ptOrgUnitModel = ptOrgUnitModel;
    }

    public CoopProspectFacadeREST getCoopProspectFacadeREST() {
        return coopProspectFacadeREST;
    }

    public void setCoopProspectFacadeREST(CoopProspectFacadeREST coopProspectFacadeREST) {
        this.coopProspectFacadeREST = coopProspectFacadeREST;
    }

    public CoopProspect getSelectedProspect() {
        return selectedProspect;
    }

    public void setSelectedProspect(CoopProspect selectedProspect) {
        this.selectedProspect = selectedProspect;
    }

    public CoopProspect getProspect() {
        return prospect;
    }

    public void setProspect(CoopProspect prospect) {
        this.prospect = prospect;
    }

    public List<CoopProspect> getProspectList() {
        return prospectList;
    }

    public void setProspectList(List<CoopProspect> prospectList) {
        this.prospectList = prospectList;
    }

    public List<CoopProspect> getAppProsList() {
        return appProsList;
    }

    public void setAppProsList(List<CoopProspect> appProsList) {
        this.appProsList = appProsList;
    }

    public DataModel<CoopProspect> getProspectModel() {
        return prospectModel;
    }

    public void setProspectModel(DataModel<CoopProspect> prospectModel) {
        this.prospectModel = prospectModel;
    }

    public CoopProsReportFacadeREST getCoopProsReportFacadeREST() {
        return coopProsReportFacadeREST;
    }

    public void setCoopProsReportFacadeREST(CoopProsReportFacadeREST coopProsReportFacadeREST) {
        this.coopProsReportFacadeREST = coopProsReportFacadeREST;
    }

    public CoopProsReport getSelectedProsReport() {
        return selectedProsReport;
    }

    public void setSelectedProsReport(CoopProsReport selectedProsReport) {
        this.selectedProsReport = selectedProsReport;
    }

    public CoopProsReport getProsReport() {
        return prosReport;
    }

    public void setProsReport(CoopProsReport prosReport) {
        this.prosReport = prosReport;
    }

    public List<CoopProsReport> getProsReportList() {
        return prosReportList;
    }

    public void setProsReportList(List<CoopProsReport> prosReportList) {
        this.prosReportList = prosReportList;
    }

    public DataModel<CoopProsReport> getProsReportModel() {
        return prosReportModel;
    }

    public void setProsReportModel(DataModel<CoopProsReport> prosReportModel) {
        this.prosReportModel = prosReportModel;
    }

    public CoopReportFacadeREST getCoopReportFacadeREST() {
        return coopReportFacadeREST;
    }

    public void setCoopReportFacadeREST(CoopReportFacadeREST coopReportFacadeREST) {
        this.coopReportFacadeREST = coopReportFacadeREST;
    }

    public CoopReport getSelectedReport() {
        return selectedReport;
    }

    public void setSelectedReport(CoopReport selectedReport) {
        this.selectedReport = selectedReport;
    }

    public CoopReport getReport() {
        return report;
    }

    public void setReport(CoopReport report) {
        this.report = report;
    }

    public List<CoopReport> getReportList() {
        return reportList;
    }

    public void setReportList(List<CoopReport> reportList) {
        this.reportList = reportList;
    }

    public DataModel<CoopReport> getReportModel() {
        return reportModel;
    }

    public void setReportModel(DataModel<CoopReport> reportModel) {
        this.reportModel = reportModel;
    }

    public CoopReportTypeFacadeREST getCoopReportTypeFacadeREST() {
        return coopReportTypeFacadeREST;
    }

    public void setCoopReportTypeFacadeREST(CoopReportTypeFacadeREST coopReportTypeFacadeREST) {
        this.coopReportTypeFacadeREST = coopReportTypeFacadeREST;
    }

    public CoopReportType getSelectedReportType() {
        return selectedReportType;
    }

    public void setSelectedReportType(CoopReportType selectedReportType) {
        this.selectedReportType = selectedReportType;
    }

    public CoopReportType getReportType() {
        return reportType;
    }

    public void setReportType(CoopReportType reportType) {
        this.reportType = reportType;
    }

    public List<CoopReportType> getReportTypeList() {
        return reportTypeList;
    }

    public void setReportTypeList(List<CoopReportType> reportTypeList) {
        this.reportTypeList = reportTypeList;
    }

    public DataModel<CoopReportType> getReportTypeModel() {
        return reportTypeModel;
    }

    public void setReportTypeModel(DataModel<CoopReportType> reportTypeModel) {
        this.reportTypeModel = reportTypeModel;
    }

    public Integer getReportValue() {
        return reportValue;
    }

    public void setReportValue(Integer reportValue) {
        this.reportValue = reportValue;
    }

    public String getDcValue() {
        return dcValue;
    }

    public void setDcValue(String dcValue) {
        this.dcValue = dcValue;
    }

    public String getPtValue() {
        return ptValue;
    }

    public void setPtValue(String ptValue) {
        this.ptValue = ptValue;
    }

    public String getProspectValue() {
        return prospectValue;
    }

    public void setProspectValue(String prospectValue) {
        this.prospectValue = prospectValue;
    }

    public Boolean getMode() {
        return mode;
    }

    public void setMode(Boolean mode) {
        this.mode = mode;
    }

    public Boolean getMode2() {
        return mode2;
    }

    public void setMode2(Boolean mode2) {
        this.mode2 = mode2;
    }

    public UAgentInfo getAgent() {
        return agent;
    }

    public void setAgent(UAgentInfo agent) {
        this.agent = agent;
    }

    public String getOu() {
        return ou;
    }

    public void setOu(String ou) {
        this.ou = ou;
    }

    public CoopInfoFacadeREST getCoopInfoFacadeREST() {
        return coopInfoFacadeREST;
    }

    public void setCoopInfoFacadeREST(CoopInfoFacadeREST coopInfoFacadeREST) {
        this.coopInfoFacadeREST = coopInfoFacadeREST;
    }

    public CoopInfo getInfo() {
        return info;
    }

    public void setInfo(CoopInfo info) {
        this.info = info;
    }

    public List<CoopInfo> getInfoList() {
        return infoList;
    }

    public void setInfoList(List<CoopInfo> infoList) {
        this.infoList = infoList;
    }

    public String getDtl() {
        return dtl;
    }

    public void setDtl(String dtl) {
        this.dtl = dtl;
    }

    public String getDcRem() {
        return dcRem;
    }

    public void setDcRem(String dcRem) {
        this.dcRem = dcRem;
    }

    public String getPkptlRem() {
        return pkptlRem;
    }

    public void setPkptlRem(String pkptlRem) {
        this.pkptlRem = pkptlRem;
    }

    public String getBr() {
        return br;
    }

    public void setBr(String br) {
        this.br = br;
    }

    public String getDcTitle() {
        return dcTitle;
    }

    public void setDcTitle(String dcTitle) {
        this.dcTitle = dcTitle;
    }

    public String getPkptlTitle() {
        return pkptlTitle;
    }

    public void setPkptlTitle(String pkptlTitle) {
        this.pkptlTitle = pkptlTitle;
    }

    public CoopMemberFacadeREST getCoopMemberFacadeREST() {
        return coopMemberFacadeREST;
    }

    public void setCoopMemberFacadeREST(CoopMemberFacadeREST coopMemberFacadeREST) {
        this.coopMemberFacadeREST = coopMemberFacadeREST;
    }

    public CoopMember getMember() {
        return member;
    }

    public void setMember(CoopMember member) {
        this.member = member;
    }

    public String getMemNo() {
        return memNo;
    }

    public void setMemNo(String memNo) {
        this.memNo = memNo;
    }

    public Boolean getPtStat() {
        return ptStat;
    }

    public void setPtStat(Boolean ptStat) {
        this.ptStat = ptStat;
    }

    public Boolean getDcStat() {
        return dcStat;
    }

    public void setDcStat(Boolean dcStat) {
        this.dcStat = dcStat;
    }

    public Boolean getPkptlStat() {
        return pkptlStat;
    }

    public void setPkptlStat(Boolean pkptlStat) {
        this.pkptlStat = pkptlStat;
    }

    /*
     METHODS
     */
    public static String formatDate(Date currentDate) {
        if (currentDate == null) {
            currentDate = new Date();
        }
        DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
        return dateFormat.format(currentDate);
    }

    public void saveToProsReport() {
        prosReport.setProspectNo(selectedProspect);
        prosReport.setReportNum(report);
        coopProsReportFacadeREST.create(prosReport);
    }

    public void saveToProsRepver() {
        prosRepver.setProsRepNum(prosReport);
        String gvn = selectedProspect.getProspectNo() + "-" + prosReport.getProsRepNum();
        prosRepver.setGroupVerNo(gvn);
        prosRepver.setUserNum(member);
        prosRepver.setReportDtl(report.getReportDtl());
        prosRepver.setVerDate(reportDateEncoded);
        coopProsRepverFacadeREST.create(prosRepver);
    }

    public void saveToRepAct() {
        repAct.setReportNum(report);
        coopRepActFacadeREST.create(repAct);
    }

    public void saveToReport() {
        report.setReportDtl(report.getReportDtl());
        report.setReportDateFrom(report.getReportDateFrom());
        report.setReportDateTo(report.getReportDateTo());
        report.setReportDateEncoded(reportDateEncoded);
        reportType.setReportTypeCode(repTypeCode);
        report.setReportTypeCode(reportType);
        report.setReportNum(report.getReportNum());
        report.setOuCode(orgUnit);
        report.setMemNo(member);
        coopReportFacadeREST.create(report);
    }

    public void saveToReportCirc() {
        reportCirc.setReportNum(report);
        coopReportCircFacadeREST.create(reportCirc);
    }

    public String saveRecommendation() throws SystemException, DocumentException, IOException {
        saveToReport();
        saveToProsReport();
        saveToProsRepver();
        saveToRepAct();
        saveToReportCirc();
        //toPdf();
        sendMessage();
        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public void sendMessage() throws SystemException {
        for (User users : notify) {

            JSONObject payloadJSON = JSONFactoryUtil.createJSONObject();
            payloadJSON.put("userId", currentUser.getUserId());
            payloadJSON.put("fullName", currentUser.getFullName());

            UserNotificationEvent userNotificationEvent = UserNotificationEventUtil
                    .create(CounterLocalServiceUtil.increment());
            userNotificationEvent.setUuid(UUID.randomUUID().toString());
            userNotificationEvent.setCompanyId(users.getCompanyId());
            userNotificationEvent.setUserId(users.getUserId());
            userNotificationEvent.setType(CustomNotificationHandler.PORTLET_ID);//YOUR_REAL_PORTLET_ID_MUST_BE_HERE//CustomNotificationHandler.PORTLET_ID
            userNotificationEvent.setTimestamp(now.getTime());
            userNotificationEvent.setDeliverBy(users.getUserId());
            userNotificationEvent.setDelivered(false);
            userNotificationEvent.setPayload(payloadJSON.toString());
            userNotificationEvent.setArchived(false);

            UserNotificationEventLocalServiceUtil.addUserNotificationEvent(userNotificationEvent);
        }
    }

    public void updateToProsReport() {
        selectedProsReport.setProspectNo(selectedProsReport.getProspectNo());
        selectedProsReport.setReportNum(selectedProsReport.getReportNum());
        coopProsReportFacadeREST.edit(selectedProsReport);
    }

    public void updateToProsRepver() {
        prosRepver.setProsRepNum(selectedProsRepver.getProsRepNum());
        String gvn = selectedProspect.getProspectNo() + "-" + prosReport.getProsRepNum();
        prosRepver.setGroupVerNo(gvn);
        prosRepver.setUserNum(member);
        prosRepver.setReportDtl(selectedProsRepver.getReportDtl());
        prosRepver.setVerDate(reportDateEncoded);
        coopProsRepverFacadeREST.create(prosRepver);
    }

    public void updateToRepAct() {
        selectedRepAct.setReportNum(selectedRepAct.getReportNum());
        coopRepActFacadeREST.edit(selectedRepAct);
    }

    public void updateToReport() {
        selectedReport.setReportDtl(selectedReport.getReportDtl());
        selectedReport.setReportDateFrom(selectedReport.getReportDateFrom());
        selectedReport.setReportDateTo(selectedReport.getReportDateTo());
        selectedReport.setReportDateEncoded(selectedReport.getReportDateEncoded());
        reportType.setReportTypeCode(repTypeCode);
        selectedReport.setReportTypeCode(reportType);
        selectedReport.setReportNum(selectedReport.getReportNum());
        selectedReport.setOuCode(orgUnit);
        selectedReport.setMemNo(member);
        selectedReport.setMemNo(selectedReport.getMemNo());
        coopReportFacadeREST.edit(selectedReport);
    }

    public void updateToReportCirc() {
        selectedReportCirc.setReportNum(selectedReportCirc.getReportNum());
        coopReportCircFacadeREST.edit(selectedReportCirc);
    }

    public String updateRecommendation() {
        updateToReport();
        updateToProsReport();
        //updateToProsRepver();
        updateToRepAct();
        updateToReportCirc();
        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public String end() throws SystemException, PortalException {
        dtl = selectedReport.getReportDtl() + br + br + dcTitle + br + dcRem;
        selectedReport.setReportDtl(dtl);
        selectedReport.setReportDateFrom(selectedReport.getReportDateFrom());
        selectedReport.setReportDateTo(selectedReport.getReportDateTo());
        selectedReport.setReportDateEncoded(selectedReport.getReportDateEncoded());
        reportType.setReportTypeCode(repTypeCode);
        selectedReport.setReportTypeCode(reportType);
        selectedReport.setReportNum(selectedReport.getReportNum());
        selectedReport.setOuCode(orgUnit);
        selectedReport.setMemNo(member);
        coopReportFacadeREST.edit(selectedReport);

        updateToProsReport();
        //updateToProsRepver();
        updateToRepAct();
        updateToReportCirc();

        selectedProspect.setProsStat('E');
        coopProspectFacadeREST.edit(selectedProspect);

        //notify = UserLocalServiceUtil.getOrganizationUsers(21374);//pkptl//make dynamic 21374
        a = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[0]).getOrganizationId();
        b = OrganizationLocalServiceUtil.getParentOrganizations(a).get(0).getOrganizationId();
        notify = UserLocalServiceUtil.getOrganizationUsers(b);
        sendMessage();

        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public String app() throws SystemException, PortalException {
        dtl = selectedReport.getReportDtl() + br + br + pkptlTitle + br + pkptlRem;
        selectedReport.setReportDtl(dtl);
        selectedReport.setReportDateFrom(selectedReport.getReportDateFrom());
        selectedReport.setReportDateTo(selectedReport.getReportDateTo());
        selectedReport.setReportDateEncoded(selectedReport.getReportDateEncoded());
        reportType.setReportTypeCode(repTypeCode);
        selectedReport.setReportTypeCode(reportType);
        selectedReport.setReportNum(selectedReport.getReportNum());
        selectedReport.setOuCode(orgUnit);
        selectedReport.setMemNo(member);
        coopReportFacadeREST.edit(selectedReport);

        updateToProsReport();
        //updateToProsRepver();
        updateToRepAct();
        updateToReportCirc();

        selectedProspect.setProsStat('A');
        coopProspectFacadeREST.edit(selectedProspect);

        for (int w = 0; w <= OrganizationLocalServiceUtil.getOrganizationsCount() - 1; w++) {
            if (OrganizationLocalServiceUtil
                    .getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount()).get(w).getName()
                    .contains(selectedProspect.getOuCode().getOuCode())) {
                notify = UserLocalServiceUtil.getOrganizationUsers(OrganizationLocalServiceUtil
                        .getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount()).get(w)
                        .getOrganizationId());
            }
        }

        sendMessage();

        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public String rej() {
        if (pkptlStat == true) {
            dtl = selectedReport.getReportDtl() + br + br + pkptlTitle + br + pkptlRem;
            selectedReport.setReportDtl(dtl);
        } else if (dcStat == true) {
            dtl = selectedReport.getReportDtl() + br + br + dcTitle + br + dcRem;
            selectedReport.setReportDtl(dtl);
        }
        coopReportFacadeREST.edit(selectedReport);

        updateToProsReport();
        //updateToProsRepver();
        updateToRepAct();
        updateToReportCirc();

        if (pkptlStat == true) {
            selectedProspect.setProsStat('F');
            /*
             notify PT/DC
             prospect return to zoom08
             OR
             selectedProspect.setProsStat('R');
             notify PT/DC*/
        } else if (dcStat == true) {
            selectedProspect.setProsStat('F');
            /*
             notify PT
             prospect return to zoom08
             */
        }
        coopProspectFacadeREST.edit(selectedProspect);
        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public void dcValueChanged() {
        ptOrgUnitList = emf.createEntityManager()
                .createQuery("SELECT a FROM CoopOrgUnit a WHERE a.managedBy = '" + dcValue + "'").getResultList();
    }

    public void ptValueChanged() throws PortalException, SystemException {
        if ("PKPTL".equals(OrganizationLocalServiceUtil.getOrganization(currentUserOrg[0]).getName())) {
            prospectList = emf.createEntityManager().createQuery(
                    "SELECT a FROM CoopProspect a WHERE a.prosStat = 'E' AND a.ouCode.ouCode = '" + ptValue + "'")
                    .getResultList();
        } else {
            prospectList = emf.createEntityManager().createQuery(
                    "SELECT a FROM CoopProspect a WHERE a.prosStat = 'R' AND a.ouCode.ouCode = '" + ptValue + "'")
                    .getResultList();
        }
    }

    public void prospectValueChanged() {
        reportList = emf.createEntityManager().createQuery(
                "SELECT d FROM CoopProsReport c JOIN c.reportNum d JOIN c.prospectNo e WHERE d.reportTypeCode.reportTypeCode = 11 AND e.prospectNo = : "
                        + prospectValue)
                .getResultList();
        CoopProspect p = (CoopProspect) emf.createEntityManager()
                .createQuery("SELECT p FROM CoopProspect p WHERE p.prospectNo = : " + prospectValue).getResultList()
                .get(0);
        selectedProspect = p;
    }

    public void prospectValueChanged2() {
        reportList = emf.createEntityManager().createQuery(
                "SELECT d FROM CoopProsReport c JOIN c.reportNum d JOIN c.prospectNo e WHERE d.reportTypeCode.reportTypeCode = 11 AND e.prospectNo = : "
                        + prospectValue)
                .getResultList();
        CoopProspect p = (CoopProspect) emf.createEntityManager()
                .createQuery("SELECT p FROM CoopProspect p WHERE p.prospectNo = : " + prospectValue).getResultList()
                .get(0);
        selectedProspect = p;
        mode2 = false;
    }

    public void dateValueChanged() {
        CoopReport r = (CoopReport) emf.createEntityManager()
                .createQuery("SELECT r FROM CoopReport r WHERE r.reportNum = : " + reportValue).getResultList()
                .get(0);
        selectedReport = r;
    }

    public void prosReportLoad() {
        CoopProsReport r = (CoopProsReport) emf.createEntityManager()
                .createQuery(
                        "SELECT c FROM CoopProsReport c JOIN c.reportNum d WHERE d.reportNum = : " + reportValue)
                .getResultList().get(0);
        selectedProsReport = r;
    }

    public void prosRepverLoad() {
        CoopProsRepver r = (CoopProsRepver) emf.createEntityManager().createQuery(
                "SELECT c FROM CoopProsRepver c JOIN c.prosRepNum  WHERE (SELECT b FROM CoopProsReport b JOIN b.reportNum a WHERE a.reportNum = : "
                        + reportValue + ")")
                .getResultList().get(0);
        selectedProsRepver = r;
    }

    public void repActLoad() {
        CoopRepAct r = (CoopRepAct) emf.createEntityManager()
                .createQuery("SELECT c FROM CoopRepAct c JOIN c.reportNum d WHERE d.reportNum = : " + reportValue)
                .getResultList().get(0);
        selectedRepAct = r;
    }

    public void repCircLoad() {
        CoopReportCirc r = (CoopReportCirc) emf.createEntityManager()
                .createQuery(
                        "SELECT c FROM CoopReportCirc c JOIN c.reportNum d WHERE d.reportNum = : " + reportValue)
                .getResultList().get(0);
        selectedReportCirc = r;
    }

    public void loadData() {
        selectedReport = new CoopReport();
        selectedProsReport = new CoopProsReport();
        //selectedProsRepver = new CoopProsRepver();
        selectedRepAct = new CoopRepAct();
        selectedReportCirc = new CoopReportCirc();
        dateValueChanged();
        prosReportLoad();
        //prosRepverLoad();
        repActLoad();
        repCircLoad();
    }

    public String navigateView() {
        beanclear();
        return "viewRecommendation.xhtml?faces-redirect=true";
    }

    public String navigateCreate() {
        beanclear();
        return "createRecommendation.xhtml?faces-redirect=true";
    }

    public String navigatePrint() {
        return "printRecommendation.xhtml?faces-redirect=true";
    }

    public void toPdf() throws DocumentException, IOException {
        createPdf(path);
    }

    public void createPdf(String file) throws DocumentException, IOException {
        //String escaped = HtmlUtil.escape(editorVal);
        //String escapedCss = HtmlUtil.escapeCSS(editorVal);

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        document.add(new Paragraph("insert letter head"));
        document.add(new Paragraph("Recommendation Report"));
        document.add(new Paragraph("date:" + "CURRENT DATE"));
        document.add(new Paragraph("date encoded:" + reportDateEncoded));
        document.add(new Paragraph(selectedProspect.getLastName() + "," + selectedProspect.getFirstName()
                + selectedProspect.getMiddleName()));
        //document.add(new Paragraph("escaped" + "\n" + editorVal));
        //document.add(new Paragraph("escaped()" + "\n" + escaped));
        //document.add(new Paragraph("escapedCss()" + "\n" + escapedCss));
        document.close();
    }

}