package org.ellectra.gui.managebeans;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.WebServiceRef;
import org.ellectra.gui.utils.JSFUtils;
import org.ellectra.wscall.ManageWorkspaceService;
import org.ellectra.wschkcallass.CheckAssociationToCallService;
import org.ellectra.wshscr.HomeScreenManagerService;
import org.ellectra.wsphases.TenderUnlockingPolicyService;
import org.ellectra.wsvcall.ViewCallService;
import org.ellectra.wsviewmycalls.ViewMyCallsService;

public class searchcallsfortenders {
    @WebServiceRef(wsdlLocation = "http://localhost:18181/TenderUnlockingPolicyService/TenderUnlockingPolicyPort?wsdl")
    private TenderUnlockingPolicyService service_3;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/CheckAssociationToCallService/CheckAssociationToCallPort?wsdl")
    private CheckAssociationToCallService service_5;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ViewMyCallsService/ViewMyCallsPort?wsdl")
    private ViewMyCallsService service_4;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ManageWorkspaceService/ManageWorkspacePort?wsdl")
    private ManageWorkspaceService service_2;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/HomeScreenManagerService/HomeScreenManagerPort?wsdl")
    private HomeScreenManagerService service;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ViewCallService/ViewCallPort?WSDL")
    private ViewCallService service_1;
    
    private String dUserloginname;
    private String dUserloginsignature;
    private String dUserloginOrganization;
    private String dUserloginrole;
    private int dUserloginid;
    private int mytenderid;
    private int mycalloftender;
    String a_sc1 = "all";
    String a_sc2 = "all";
    private String a_sc3[];
    private String a_sc4[];
    String a_sc5 = "all";;
    Date a_sc6;
    String a_sc7;
    Date a_sc8;
    String a_sc9;
    String a_sc10;
    private UIData sresdata = null;
    private ArrayList<searchresultlist> asres = new ArrayList();
    int ictenderselected;
    private UIData mycallsdata = null;
    private ArrayList<searchresultlist> mycallslist = new ArrayList();    //variables used for VIEW
    private String title;
    private String description;
    private String procedureType;
    private String contractType;
    private String noticeInvolvesStr;
    private String openDate;
    private String closeDate;
    private String value;
    private String cpv;
    private String nuts;
    private String contractDuration;
    private String ojeuRefNum;
    private String prevPublications;
    private String internalRefNum;
    private String btnCreateTenderDisabled;    //variables used for EDIT
    private String voc;
    private String[] noticeInvolvesEd;
    private String procedureTypeEd;
    private Double estimatedValue;
    private String currency;
    private String durationOption;
    private Integer durationInMonths;
    private Integer durationInDays;
    private Date contractStarts;
    private Date contractEnds;
    private String a_ctw16;
    private String a_ctw17;
    private String a_ctw18;
    private Date a_ctw19;
    private String a_ctw20;
    private String a_ctw21;
    private String a_ctw22;
    private Date a_ctw23;
    private Date dateCNdispatch;
    private String openDateEd;
    private String closeDateEd;
    private Integer numOfDays;
    private String ecntransmission;
    private String econtractDocs;
    private boolean numbercd;
    private boolean datecd;    /////////////////////////////
    
    private boolean bshowcreate;
    private boolean bshowedit;
    private int searchPath = 1; //to be 0 if this bean is reached from search OR myCalls screen, else 1 if coming from other screens
    
    private boolean ctrlUpdateRendered;
    private boolean cfpsExist;
    private boolean ctrlUploadCDocs;
   
    ////////////// Evaluation params////////////////////
 
 

    public String getA_sc2() {
        java.lang.String cpvmaincode = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "MAINCPVFORSEARCH");
        // use variables session here
        if (cpvmaincode != null)
            a_sc2 = cpvmaincode;  
        else 
            a_sc2 = "all";
        return a_sc2;
    }

    public void setA_sc2(String a_sc2) {
        this.a_sc2 = a_sc2;
    }

    public void checkEvaluationStatus(searchresultlist callObject) {
        System.err.print("get in method for FR18_TenderUnlockingPolicy_ViewAll");
        GetSessionVariables();
        
        try { // Call Web Service Operation
           org.ellectra.wsphases.TenderUnlockingPolicyPortType port = service_3.getTenderUnlockingPolicyPort();
           org.ellectra.wsphases.TenderUnlockingPolicyRequest part1 = new org.ellectra.wsphases.TenderUnlockingPolicyRequest();
           part1.setAction("FR18_TenderUnlockingPolicy_ViewAll");
           part1.setActorSignature(getDUserloginsignature());
           part1.setCallForTenderID(callObject.getSrid());
           
           // TODO process result here
           org.ellectra.wsphases.TenderUnlockingPolicyResponse result = port.tenderUnlockingPolicyOperation(part1);
           List<org.ellectra.wsphases.PhaseRecordType> bpelout = result.getPhaseRecord();
           
           boolean phase1Closed = false;
           boolean phase2Closed = false;
           boolean phase3Closed = false;

           for (int i = 0; i < bpelout.size(); i++) {
                org.ellectra.wsphases.PhaseRecordType bpelitem = new org.ellectra.wsphases.PhaseRecordType();
                bpelitem = bpelout.get(i);

                String phaseStr = bpelitem.getPhase();
                int phaseID = bpelitem.getPhaseID();
                boolean unlocked = bpelitem.isPhaseUnlocked();
                boolean closed = bpelitem.isEvaluationCompleted();
                
                String currentPhase = "";
                //decide current status
                if (phaseStr.equals("PHASE_I")) {
                    if (unlocked) {
                        if (!closed)    
                            //current status = phase 1
                            callObject.setReadyForEvaluateProof(true);
                        else {
                            //current status = phase 2 OR phase 3 or END
                            //first examine 2
                            //then examine 3
                            //then examine end 
                            phase1Closed = true;
                            callObject.setReadyForEvaluateProof(false);
                            //txtProofRendered = true;
                        }
                    }
                    System.err.print("PHASE 1: unlocked = " + unlocked + ", closed = " + closed);
                } //phase 1 check
                if (phaseStr.equals("PHASE_II")) {
                    if (unlocked) {
                        if (!closed)
                            //current status = phase 2
                            callObject.setReadyForEvaluateTech(true);
                        else {
                            //current status = phase 3 or END
                            //first examine 3
                            //then examine end
                            phase2Closed = true;
                            callObject.setReadyForEvaluateTech(false);
                            //txtTechRendered = true;
                        }
                    }
                    System.err.print("PHASE 2: unlocked = " + unlocked + ", closed = " + closed);
                } //phase 2 check
                if (phaseStr.equals("PHASE_III")) {
                    if (unlocked) {
                        if (!closed)
                            //current status = phase 3
                            callObject.setReadyForEvaluateFin(true);
                        else {
                            //current status = END
                            phase3Closed = true;
                            callObject.setReadyForEvaluateFin(false);
                            //txtFinRendered = true;
                        }
                    }  
                    System.err.print("PHASE 3: unlocked = " + unlocked + ", closed = " + closed);
                } //phase 3 check
            }// end for
           //////////////////////////////////
           if (phase1Closed) {
               if (phase2Closed) {
                   if (phase3Closed) {
                       callObject.setAwarded(true);
                   }  
               }
           }
               
           
           
        } catch (Exception ex) {
           // TODO handle custom exceptions here
        }

    }
    
    
    public String populateMyCalls() {
        mycallslist.clear();
        GetSessionVariables();

        try {
            org.ellectra.wsviewmycalls.ViewMyCallsPortType port = service_4.getViewMyCallsPort();
            org.ellectra.wsviewmycalls.ViewMyCallsRequest part1 = new org.ellectra.wsviewmycalls.ViewMyCallsRequest();
            part1.setActorSignature(dUserloginsignature);

            // TODO process result here
            org.ellectra.wsviewmycalls.ViewMyCallsResponse result = port.viewMyCallsOperation(part1);
            List<org.ellectra.wsviewmycalls.MyCallType> bpelout = result.getMyCallDetails();
            System.err.print("POPULATE MYCALLS size: " + bpelout.size());
            
            for (int i = 0; i < bpelout.size(); i++) {
                org.ellectra.wsviewmycalls.MyCallType bpelitem = new org.ellectra.wsviewmycalls.MyCallType();
                bpelitem = bpelout.get(i);

                //NEED to examine user Role
                //bpelitem.getUserCallRole();

                searchresultlist callslist = new searchresultlist();
                callslist.setSrid(bpelitem.getCallForTenderID());
                callslist.setTitle(bpelitem.getCallTitle());
                callslist.setRef(bpelitem.getCallOjeuRefNum());
                
                String DATE_FORMAT2 = "dd-MM-yyyy HH:mm:ss";
                DateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT2);

                GregorianCalendar xmlClose = bpelitem.getCallCloseDate().toGregorianCalendar();
                String formattedCloseDate = sdf2.format(xmlClose.getTime());

                GregorianCalendar xmlOpen = bpelitem.getCallOpenDate().toGregorianCalendar();
                String formattedOpenDate = sdf2.format(xmlOpen.getTime());

                GregorianCalendar nowDate = new GregorianCalendar();
                
                callslist.setReadyForUnlock(false);  //"false" MUST be the initial value, also for INTRA
                callslist.setReadyForEvaluateProof(false);
                callslist.setReadyForEvaluateTech(false);
                callslist.setReadyForEvaluateFin(false);
                callslist.setAwarded(false);
                
                checkEvaluationStatus(callslist);
                
                if (xmlClose != null) {
                    callslist.setCdate(formattedCloseDate);
                    if (nowDate.after(xmlClose)) {
                        callslist.setReadyForUnlock(true);
                    }
                } else {
                    callslist.setCdate("UNSET");
                }
                if (xmlOpen != null) {
                    callslist.setPdate(formattedOpenDate);
                } else {
                    callslist.setPdate("UNSET");
                }
                mycallslist.add(callslist);
            }//for

        } catch (Exception ex) {
            ex.printStackTrace();
        // TODO handle custom exceptions here
        }

        String role = decideRole();
        if (role.equalsIgnoreCase("POA")) {
            return "showmycallspoa";
        } else if (role.equalsIgnoreCase("POO")) {
            return "showmycallspoo";
        } else if (role.equalsIgnoreCase("POE")) {
            return "showmycallspoe";
        } else {
            return "NULL";
        }
    }

//    public String performsearch() {
//        ///load the list here from search bpel
//        asres.clear();
//        try { 
//            org.ellectra.wshscr.HomeScreenManagerPortType port = service.getHomeScreenManagerPort();
//
//            org.ellectra.wshscr.Request part1 = new org.ellectra.wshscr.Request();
//            part1.setAction("CALLFORTENDER");
//            part1.setActorsignature("1234");
//            part1.setUserid(123);
//            org.ellectra.wshscr.Response result = port.homeScreenManagerOperation(part1);
//            List<org.ellectra.wshscr.Response.Return> dList = result.getReturn();
//            for (int i = 0; i < dList.size(); i++) {
//                boolean bAdd = false;
//                org.ellectra.wshscr.Response.Return dItem = dList.get(i);
//                searchresultlist ditemtoadd = new searchresultlist();
//                ditemtoadd.setSrid(dItem.getId());
//
//                String DATE_FORMAT2 = "dd-MM-yyyy HH:mm:ss";
//                DateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT2);
//                sdf2.setTimeZone(TimeZone.getDefault());
//
//                GregorianCalendar xmlClose = null; 
//                String formattedCloseDate = "";
//                if (dItem.getCd() != null) {
//                    xmlClose = dItem.getCd().toGregorianCalendar();
//                    formattedCloseDate = sdf2.format(xmlClose.getTime());
//                }
//
//                GregorianCalendar xmlOpen = null; 
//                String formattedOpenDate = "";
//                if (dItem.getPd() != null) {
//                    xmlOpen = dItem.getPd().toGregorianCalendar();
//                    //String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
//                    //java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
//                    //sdf.setTimeZone(TimeZone.getDefault());
//                    formattedOpenDate = sdf2.format(xmlOpen.getTime());
//                }
//                
//                
//                boolean bfound = false;
//                //notices involved
//                for (int ii = 0; ii < a_sc4.length; ii++) {
//                    if (a_sc4[ii] != null) {
//                        bfound = true;
//                        //check pc
//                        if (a_sc4[ii].equalsIgnoreCase("PC")) {
//                            if (dItem.isNoticeinvolvepc()) {
//                                
//                                if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                                ditemtoadd.setRef(dItem.getRefno());
//                                ditemtoadd.setTitle(dItem.getTitle());
//                                if (bAdd == false) {
//                                    asres.add(ditemtoadd);
//                                    bAdd = true;
//                                }
//                            }
//                        }
//                        if (a_sc4[ii].equalsIgnoreCase("DPS")) {
//                            if (dItem.isNoticeinvolveddp()) {
//                                if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                                ditemtoadd.setRef(dItem.getRefno());
//                                ditemtoadd.setTitle(dItem.getTitle());
//                                if (bAdd == false) {
//                                    asres.add(ditemtoadd);
//                                    bAdd = true;
//                                }
//                            }
//                        }
//                        if (a_sc4[ii].equalsIgnoreCase("FA")) {
//                            if (dItem.isNoticeinvolvedfa()) {
//                                if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                                ditemtoadd.setRef(dItem.getRefno());
//                                ditemtoadd.setTitle(dItem.getTitle());
//                                if (bAdd == false) {
//                                    asres.add(ditemtoadd);
//                                    bAdd = true;
//                                }
//                            }
//                        }
//                    }//if null
//                }//check pc ends 
//                if (bfound == false) {
//                    if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                    ditemtoadd.setRef(dItem.getRefno());
//                    ditemtoadd.setTitle(dItem.getTitle());
//                    if (bAdd == false) {
//                        asres.add(ditemtoadd);
//                        bAdd = true;
//                    }
//                }
//                //contract type
//                for (int ii2 = 0; ii2 < a_sc3.length; ii2++) {
//                    if (a_sc3[ii2] != null) {
//                        if (dItem.getContracttype().contains(a_sc3[ii2])) {
//                            if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                            ditemtoadd.setRef(dItem.getRefno());
//                            ditemtoadd.setTitle(dItem.getTitle());
//                            if (bAdd == false) {
//                                asres.add(ditemtoadd);
//                                bAdd = true;
//                            }
//                        } else {
//                            if (a_sc3[ii2].equalsIgnoreCase("all")) {
//                                if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                                ditemtoadd.setRef(dItem.getRefno());
//                                ditemtoadd.setTitle(dItem.getTitle());
//                                if (bAdd == false) {
//                                    asres.add(ditemtoadd);
//                                    bAdd = true;
//                                }
//                                ;
//                            }
//                        }
//                    }
//                }
//                //nuts
//                if (dItem.getNutcode().contains(a_sc5)) {
//                    if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                    ditemtoadd.setRef(dItem.getRefno());
//                    ditemtoadd.setTitle(dItem.getTitle());
//                    if (bAdd == false) {
//                        asres.add(ditemtoadd);
//                        bAdd = true;
//                    }
//                } else {
//                    if (a_sc5.equalsIgnoreCase("all")) {
//                        if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                        ditemtoadd.setRef(dItem.getRefno());
//                        ditemtoadd.setTitle(dItem.getTitle());
//                        if (bAdd == false) {
//                            asres.add(ditemtoadd);
//                            bAdd = true;
//                        }
//                    }
//                }
//
//                //cpv
//                if (dItem.getCpvcode().contains(a_sc2)) {
//                    if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                    ditemtoadd.setRef(dItem.getRefno());
//                    ditemtoadd.setTitle(dItem.getTitle());
//                    if (bAdd == false) {
//                        asres.add(ditemtoadd);
//                        bAdd = true;
//                    }
//                } else {
//                    if (a_sc2.equalsIgnoreCase("all")) {
//                        if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                        ditemtoadd.setRef(dItem.getRefno());
//                        ditemtoadd.setTitle(dItem.getTitle());
//                        if (bAdd == false) {
//                            asres.add(ditemtoadd);
//                            bAdd = true;
//                        }
//                    }
//                }
//                //title
//                if (dItem.getTitle().contains(a_sc1)) {
//                    if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                    ditemtoadd.setRef(dItem.getRefno());
//                    ditemtoadd.setTitle(dItem.getTitle());
//                    if (bAdd == false) {
//                        asres.add(ditemtoadd);
//                        bAdd = true;
//                    }
//                } else {
//                    if (a_sc1.equalsIgnoreCase("all")) {
//                        if (xmlClose != null) {
//                                    ditemtoadd.setCdate(formattedCloseDate);
//                                } else {
//                                    ditemtoadd.setCdate("UNSET");
//                                }
//                                if (xmlOpen != null) {
//                                    ditemtoadd.setPdate(formattedOpenDate);
//                                } else {
//                                    ditemtoadd.setPdate("UNSET");
//                                } 
//                        ditemtoadd.setRef(dItem.getRefno());
//                        ditemtoadd.setTitle(dItem.getTitle());
//                        if (bAdd == false) {
//                            asres.add(ditemtoadd);
//                            bAdd = true;
//                        }
//                    }
//                }
//            }
//        } catch (Exception ex) {
//            // TODO handle custom exceptions here
//            }
//
//        return "showresults";
//    }


    

    /**
     * Return TRUE if user (from session) is associated to call (from session) 
     * @return
     */
    public boolean userAssociatedToCall(String roleKey, int callId) {
        GetSessionVariables();
        try {
            org.ellectra.wschkcallass.CheckAssociationToCallPortType port = service_5.getCheckAssociationToCallPort();
            org.ellectra.wschkcallass.CheckAssociationToCallRequest part1 = new org.ellectra.wschkcallass.CheckAssociationToCallRequest();
            part1.setCallForTendersID(callId);
            part1.setUserAccountID(dUserloginid);
            // TODO process result here
            org.ellectra.wschkcallass.CheckAssociationToCallResponse result = port.checkAssociationToCallOperation(part1);
            System.err.print("CALL ASSOCIATION AS: [" + result.getAssociatedAs() +"]");
            boolean associated = result.isReturn();
            String associatedAs = result.getAssociatedAs();
            if (associated && associatedAs.contains(roleKey)) {
                return true;
            }
        } catch (Exception ex) {
            // TODO handle custom exceptions here
            System.err.print("SEARCH: CALL ASSOCIATION EXCEPTION");
            ex.printStackTrace();
            return false;
        }
        return false;
    }

    public String decideRole() {
        java.lang.String Poa = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "LOGINROLEPOA");
        java.lang.String Poo = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "LOGINROLEPOO");
        java.lang.String Poe = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "LOGINROLEPOE");
        java.lang.String Eo = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "LOGINROLEEO");

        if (Eo == null )
            Eo = "";
        if (Poa == null )
            Poa = "";
        if (Poo == null )
            Poo = "";
        if (Poe == null )
            Poe = "";
        if (Eo.equalsIgnoreCase("EO")) {
            return "EO";
        } else if (Poa.equalsIgnoreCase("POA")) {
            return "POA";
        } else if (Poo.equalsIgnoreCase("POO")) {
            return "POO";
        } else if (Poe.equalsIgnoreCase("POE")) {
            return "POE";
        }
        return "NULL";
    }
    
    /**
     * This function is called from searchcallsresults pages
     * Reads from data table: sresdata
     * @return
     */
    public String view() {
        searchresultlist Selected = (searchresultlist) this.getSresdata().getRowData();
        ictenderselected = Selected.getSrid();
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID", Integer.toString(ictenderselected));

        searchPath = 0;
        viewCallDetails();
        ctrlUpdateRendered = false;
        ctrlUploadCDocs = false;

        String role = decideRole();
        if (role.equalsIgnoreCase("EO")) {
            return "showctvieweo";
        } else if (role.equalsIgnoreCase("POA")) {
            if (userAssociatedToCall("ADMINISTRATIVE", ictenderselected)) {
                ctrlUploadCDocs = true;
                ctrlUpdateRendered = true;
                return "showctviewpoa";
            } else {
                return "showctviewpoo";
            }
        } else if (role.equalsIgnoreCase("POO")) {
            if (userAssociatedToCall("OPENING", ictenderselected)) 
                ctrlUploadCDocs = true;
            return "showctviewpoo";
        } else if (role.equalsIgnoreCase("POE")) {
            if (userAssociatedToCall("EVALUATING", ictenderselected)) 
                ctrlUploadCDocs = true;
            return "showctviewpoe";
        } else {
            return "showctviewguest";
        }
    }


    /**
     * This function is called from viemycalls pages
     * Reads from data table: mycallsdata
     * @return
     */
    public String viewMyCall() {
        searchresultlist selected = (searchresultlist) this.getMycallsdata().getRowData();
        ictenderselected = selected.getSrid();
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID", Integer.toString(ictenderselected));

        searchPath = 0;
        viewCallDetails();
        ctrlUpdateRendered = false;
        ctrlUploadCDocs = false;

        String role = decideRole();
        if (role.equalsIgnoreCase("EO")) {
            return "showctvieweo";
        } else if (role.equalsIgnoreCase("POA")) {
            ctrlUploadCDocs = true;
            ctrlUpdateRendered = true;
            return "showctviewpoa";
        } else if (role.equalsIgnoreCase("POO")) {
            ctrlUploadCDocs = true;
            return "showctviewpoo";
        } else if (role.equalsIgnoreCase("POE")) {
            ctrlUploadCDocs = true;
            return "showctviewpoe";
        } else {
            return "NULL";
        }
    }
    

    public void GetSessionVariables() {
        dUserloginname = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSER");
        dUserloginsignature = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINSIGNATURE");
        dUserloginOrganization = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINORGANIZATION");
        dUserloginrole = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINROLE");
        
        String szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSERID");
        if (szTemp != null)
            dUserloginid = Integer.valueOf(szTemp).intValue();
        szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "TENDERIDI");
        if (szTemp != null)
            mytenderid = Integer.valueOf(szTemp).intValue();
        szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID");
        mycalloftender = Integer.valueOf(szTemp).intValue();

    }

    public String viewCallDetails() {

        GetSessionVariables();

        System.err.print("CURRENT CALLTENDERID::::: " + getMycalloftender());
        try {
            org.ellectra.wsvcall.ViewCallPortType port = service_1.getViewCallPort();

            org.ellectra.wsvcall.ViewCallRequest part1 = new org.ellectra.wsvcall.ViewCallRequest();
            part1.setActorSignature(getDUserloginsignature());
            //part1.setActorSignature("123456");
            part1.setCallForTendersID(getMycalloftender());

            org.ellectra.wsvcall.ViewCallResponse result = port.viewCallOperation(part1);

            GregorianCalendar gc = new GregorianCalendar();
            String currentYear = String.valueOf(gc.get(GregorianCalendar.YEAR));
            setInternalRefNum(currentYear + " / " + result.getCallForTendersID());
            setOjeuRefNum("not published");
            setTitle(result.getTitle());
            setDescription(result.getDescription());

            if (result.getContractType().equals("WORKS")) {
                setContractType("Works");
            } else if (result.getContractType().equals("SUPPLIES")) {
                setContractType("Supplies");
            } else if (result.getContractType().equals("SERVICES")) {
                setContractType("Services");
            }
            if (result.getProcedureType().equals("OPEN")) {
                setProcedureType("Open");
            } else if (result.getProcedureType().equals("RESTRICTED")) {
                setProcedureType("Restricted");
            } else if (result.getProcedureType().equals("ACCELERATED")) {
                setProcedureType("Accelerated");
            } else if (result.getProcedureType().equals("NEGOTIATED")) {
                setProcedureType("Negotiated");
            } else if (result.getProcedureType().equals("ACCELERATED_NEGOTIATED")) {
                setProcedureType("Accelerated - Negotiated");
            }
            setProcedureTypeEd(result.getProcedureType());

            String involvesTmp = "";
            String[] involvesEdTmp = {"", "", ""};
            boolean pc = result.isNoticeInvolvesPC();
            if (pc) {
                involvesEdTmp[0] = "pc";
                //if (noticeInvolvesEd != null)
                //noticeInvolvesEd[getNoticeInvolvesEd().length] = "pc";
                involvesTmp += "Public Contract";
            }
            boolean dps = result.isNoticeInvolvesDPS();
            if (dps) {
                involvesEdTmp[1] = "dps";
                //if (noticeInvolvesEd != null)
                //noticeInvolvesEd[getNoticeInvolvesEd().length] = "dps";
                if (!involvesTmp.equals("")) {
                    involvesTmp += ", " + "Dynamic Purchasing System";
                } else {
                    involvesTmp += "Dynamic Purchasing System";
                }
            }
            boolean fa = result.isNoticeInvolvesFA();
            if (fa) {
                involvesEdTmp[2] = "fa";
                //if (noticeInvolvesEd != null)
                //noticeInvolvesEd[getNoticeInvolvesEd().length] = "fa";
                if (!involvesTmp.equals("")) {
                    involvesTmp += ", " + "Framework Agreement";
                } else {
                    involvesTmp += "Framework Agreement";
                }
            }
            setNoticeInvolvesStr(involvesTmp);
            setNoticeInvolvesEd(involvesEdTmp);

            String DATE_FORMAT2 = "dd-MM-yyyy HH:mm:ss";
            DateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT2);
            
            String DATE_FORMAT = "dd/MM/yyyy";
            DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            //DateFormat localFormat = DateFormat.getDateInstance();
            
            GregorianCalendar xmlOpen = null; 
            String formattedOpenDate = "";
            if (result.getOpenCallDate() != null) {
                xmlOpen = result.getOpenCallDate().toGregorianCalendar();
                //sdf2.setTimeZone(TimeZone.getDefault());
                formattedOpenDate = sdf2.format(xmlOpen.getTime());
                setOpenDateEd(sdf.format(xmlOpen.getTime()));
            }
            setOpenDate(formattedOpenDate);

            GregorianCalendar xmlClose = null;
            String formattedCloseDate = "";
            if (result.getCloseCallDate() != null) {
                xmlClose = result.getCloseCallDate().toGregorianCalendar();
                //sdf3.setTimeZone(TimeZone.getDefault());
                formattedCloseDate = sdf2.format(xmlClose.getTime());
                setCloseDateEd(sdf.format(xmlClose.getTime()));
            }
            setCloseDate(formattedCloseDate);

           
            if (result.getCNsubmissionDate() != null) {
                GregorianCalendar xmlCNsubmission = result.getCNsubmissionDate().toGregorianCalendar();
                Date cnSubmissionDate = xmlCNsubmission.getTime();
                setDateCNdispatch(cnSubmissionDate);
            }


            if (xmlClose != null) {
                GregorianCalendar nowDate = new GregorianCalendar();
                if (nowDate.before(xmlClose)) {
                    btnCreateTenderDisabled = "false";
                //linkUnlockRendered = false;
                } else {
                    //linkUnlockRendered = true;
                    btnCreateTenderDisabled = "true";
                }
            } else {
                //linkUnlockRendered = false;
                btnCreateTenderDisabled = "true";
            }

            setCurrency(result.getCurrency());
            Double estimatedValue = null;;
            if (result.getEstimatedValue() != null) {
                estimatedValue = result.getEstimatedValue();
                setEstimatedValue(estimatedValue);
                String valueString = String.valueOf(estimatedValue);
                setValue(valueString + " " + result.getCurrency());
            }
            
            setCpv(result.getCPV());
            setNuts(result.getNUTS());

            ///////////////////////////////////////////////////////////////////
            //CASE 1
            String duration = "";
            int months = result.getDurationInMonths();
            int days = result.getDurationInDays();

            GregorianCalendar xmlStart = null;
            Date startDate = null;
            if (result.getContractStarts() != null) {
                xmlStart = result.getContractStarts().toGregorianCalendar();
                startDate = xmlStart.getTime();
            }
            GregorianCalendar xmlEnd = null;
            Date endDate = null;
            if (result.getContractEnds() != null) {
                xmlEnd = result.getContractEnds().toGregorianCalendar();
                endDate = xmlEnd.getTime();
            }

            if (xmlStart != null || xmlEnd != null) {
                if (xmlStart != null) {
                    setContractStarts(startDate);
                    if (endDate != null) {
                        setContractEnds(endDate);
                        duration = calculateDuration(startDate, endDate);
                    } else {
                        duration = "Contract starts at: " + startDate;
                    }
                } else {
                    setContractEnds(endDate);
                    duration = "Contract ends at: " + endDate;
                }
            } else if (months > 0 || days > 0) {
                if (months > 0) {
                    setDurationInMonths(months);
                    String durationMonths = String.valueOf(result.getDurationInMonths());
                    duration = durationMonths + " months";
                }
                if (days > 0) {
                    setDurationInDays(days);
                    String durationDays = String.valueOf(result.getDurationInDays());
                    if (!duration.equals("")) {
                        duration = duration + " and " + durationDays + " days";
                    } else {
                        duration = durationDays + " days";
                    }
                }
            }
            setContractDuration(duration);

            String pinA = result.getPINcode();
            String partA = "";
            String partB = "";
            String partC = "";

            String prevPublicationsTmp = "";
            if (pinA != null && (!pinA.trim().equals(""))) {
                if (pinA.length() == 17) {
                    partA = pinA.substring(0, 4);
                    partB = pinA.substring(7, 10);
                    partC = pinA.substring(11, pinA.length());
                    setA_ctw16(partA);
                    setA_ctw17(partB);
                    setA_ctw18(partC);
                    GregorianCalendar xmlPinA = result.getPINsubmitted().toGregorianCalendar();
                    Date pinADate = xmlPinA.getTime();
                    setA_ctw19(pinADate);

                    prevPublicationsTmp = pinA;
                }
            }

            String pinB = result.getOtherPINcode();
            if (pinB != null && (!pinB.trim().equals(""))) {
                if (pinB.length() == 17) {
                    partA = pinB.substring(0, 4);
                    partB = pinB.substring(7, 10);
                    partC = pinB.substring(11, pinB.length());
                    setA_ctw20(partA);
                    setA_ctw21(partB);
                    setA_ctw22(partC);
                    GregorianCalendar xmlPinB = result.getOtherPINsubmitted().toGregorianCalendar();
                    Date pinBDate = xmlPinB.getTime();
                    setA_ctw23(pinBDate);

                    if (!prevPublicationsTmp.equals("")) {
                        prevPublicationsTmp += ", " + pinB;
                    } else {
                        prevPublicationsTmp = pinB;
                    }
                }
            }
            if (prevPublicationsTmp.equals("")) {
                setPrevPublications("none");
            } else {
                setPrevPublications(prevPublicationsTmp);
            }
            if (result.isETransmissionOfCN()) {
                setEcntransmission("yes");
            } else {
                setEcntransmission("no");
            }
            if (result.isEContractDocs()) {
                setEcontractDocs("yes");
            } else {
                setEcontractDocs("no");
            }
            if (result.getOpenTendersAfterClosingDate() != null) {
                setNumOfDays(result.getOpenTendersAfterClosingDate());    
            }
            
            //DateFormat sdf4 = new SimpleDateFormat(DATE_FORMAT2);
            //sdf4.setTimeZone(TimeZone.getDefault());
            //String formattedStartDate = sdf4.format(xmlStart.getTime());
            //Date date1 = ConvertToDate(formattedStartDate);
            //DateFormat sdf5 = new SimpleDateFormat(DATE_FORMAT2);
            //sdf5.setTimeZone(TimeZone.getDefault());
            //String formattedEndDate = sdf5.format(xmlEnd.getTime());
            //Date date2 = ConvertToDate(formattedEndDate);
           
            cfpsExist = result.isCfpClosed();
            
            System.err.print("ok user: " + dUserloginname);
            //ok ok
            return "success";
        } catch (Exception ex) {
            // TODO handle custom exceptions here
            ex.printStackTrace();
            System.err.println("ViewCallDetails Result error = " + ex.getLocalizedMessage() + " -- " + ex.getMessage());
            return "failure";
        }

    }

    
    
    public String cancel() {
        return "cancel";
    }

    /**
     * in order to Update the Call Workspace, check that the call hasn't been closed
     * @return
     */
    public String editlink() {
        String openDateStr = getOpenDate();
        String DATE_FORMAT = "dd-MM-yyyy";
        Date openDate = ConvertToDate(openDateStr, DATE_FORMAT);
        Date nowDate = new Date();


        if (nowDate.before(openDate)) {
            return "goToEditCall";
        } else {
            return "NULL";
        }
    }

    public String editWorkspace() {
        System.err.println("get in edit....");

        GetSessionVariables();

        try {
            org.ellectra.wscall.ManageWorkspacePortType port = service_2.getManageWorkspacePort();

            org.ellectra.wscall.ManageWorkspaceRequest part1 = new org.ellectra.wscall.ManageWorkspaceRequest();
            part1.setAction("FR5_Workspace_Update");
            part1.setActorSignature(getDUserloginsignature());
            part1.setCallForTendersID(getMycalloftender());

            //INTERNAL REFERENCE NUMBER
            //GregorianCalendar gc = new GregorianCalendar();
            //String currentYear = String.valueOf(gc.get(GregorianCalendar.YEAR));
            //setInternalRefNum(currentYear + " / " + getMycalloftender());

            part1.setTitle(getTitle());
            part1.setContractType(getContractType().toUpperCase());
            part1.setDescription(getDescription());
            part1.setProcedureType(getProcedureTypeEd());
            part1.setCurrency(getCurrency());

            part1.setNoticeInvolvesPC(false);
            part1.setNoticeInvolvesDPS(false);
            part1.setNoticeInvolvesFA(false);
            for (int i = 0; i < noticeInvolvesEd.length; i++) {
                if (noticeInvolvesEd[i].equals("pc")) {
                    part1.setNoticeInvolvesPC(true);
                }
                if (noticeInvolvesEd[i].equals("dps")) {
                    part1.setNoticeInvolvesDPS(true);
                }
                if (noticeInvolvesEd[i].equals("fa")) {
                    part1.setNoticeInvolvesFA(true);
                }
            }

            if (estimatedValue != null) {
                part1.setEstimatedValue(estimatedValue);
            }
            if (durationInMonths != null) {
                part1.setDurationInMonths(durationInMonths);
            }
            if (durationInDays != null) {
                part1.setDurationInDays(durationInDays);
            }
            if (contractStarts != null) {
                part1.setContractStarts(ConvertDateToXML(contractStarts));
            }
            if (contractEnds != null) {
                part1.setContractEnds(ConvertDateToXML(contractEnds));
            }
            if (getEcntransmission().equals("yes")) {
                part1.setETransmissionOfCN(true);
            } else {
                part1.setETransmissionOfCN(false);
            }
            if (getEcontractDocs().equals("yes")) {
                part1.setEContractDocs(true);
            } else {
                part1.setEContractDocs(false);
            }
            if (getDateCNdispatch() != null) {
                part1.setCNsubmissionDate(ConvertDateToXML(dateCNdispatch));
            }
            String DATE_FORMAT = "dd/MM/yyyy";
            if (getOpenDateEd().length() > 2) {
                Date openDate = ConvertToDate(getOpenDateEd(), DATE_FORMAT);
                part1.setOpenCallDate(ConvertDateToXML(openDate));
            }

            if (getCloseDateEd().length() > 2) {
                Date closeDate = ConvertToDate(getCloseDateEd(), DATE_FORMAT);
                part1.setCloseCallDate(ConvertDateToXML(closeDate));
            }

            if (numOfDays != null) {
                part1.setOpenTendersAfterClosingDate(numOfDays);            ///////////////////////////////////////////////////////////    
            }
            part1.setCPV(cpv);
            //part1.setVOC(voc);
            part1.setNUTS(nuts);
            //  part1.setOjeuReferenceNumber("NA");

            String regExpA = "\\d{4}";
            String regExpB = "\\d{3}";
            String regExpC = "\\d{6}";

            if (getA_ctw16() != null && getA_ctw17() != null && getA_ctw18() != null && getA_ctw19() != null) {
                if (Pattern.matches(regExpA, getA_ctw16()) && Pattern.matches(regExpB, getA_ctw17()) && Pattern.matches(regExpC, getA_ctw18())) {
                    part1.setPINcode(getA_ctw16() + "/S " + getA_ctw17() + "-" + getA_ctw18());
                    part1.setPINsubmitted(ConvertDateToXML(getA_ctw19()));
                }
            }
            //else
            //part1.setPINcode("NA");

            if (getA_ctw20() != null && getA_ctw21() != null && getA_ctw22() != null && getA_ctw23() != null) {
                if (Pattern.matches(regExpA, getA_ctw20()) && Pattern.matches(regExpB, getA_ctw21()) && Pattern.matches(regExpC, getA_ctw22())) {
                    part1.setOtherPINcode(getA_ctw20() + "/S " + getA_ctw21() + "-" + getA_ctw22());
                    part1.setOtherPINsubmitted(ConvertDateToXML(getA_ctw23()));
                }
            }
            //else
            //part1.setOtherPINcode("NA");

            org.ellectra.wscall.ManageWorkspaceResponse result = port.manageWorkspaceOperation(part1);
            if (result.isReturn()) {
                return "update";
            } else {
                return "failure";
            }


        } catch (Exception ex) {
            // TODO handle custom exceptions here
            System.err.println("Result = " + ex.getLocalizedMessage());
            return "failure";
        }


    }
    ///////////////DATE HANDLING///////////////
    private XMLGregorianCalendar ConvertDateToXML(Date xmldate) {

        try {
            String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);

            SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);


            pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
            pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);


            GregorianCalendar GreCal = new GregorianCalendar(pdt);
            GreCal.setTime(xmldate);

            XMLGregorianCalendar xmlCaltemp = DatatypeFactory.newInstance().newXMLGregorianCalendar(GreCal);
            return xmlCaltemp;

        } catch (DatatypeConfigurationException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private Date ConvertToDate(String dateStr, String format) {
        try {

            String mydatestring = dateStr;
            //String DATE_FORMAT = "dd-MM-yyyy";

            Date dt = new SimpleDateFormat(format).parse(mydatestring);
            return dt;
        } catch (ParseException ex) {
            ex.printStackTrace();
            return new Date();
        }

    }

    public String calculateDuration(Date fromDate, Date toDate) {
        int years = 0;
        int months = 0;
        int days = 0;

        //Date date2 = ConvertToDate(fromDate); //startDate
        Calendar date2cal = Calendar.getInstance();
        date2cal.setTime(fromDate);

        //Date date1 = ConvertToDate(toDate); //endDate
        Calendar date1cal = Calendar.getInstance();
        date1cal.setTime(toDate);

        while (date2cal.get(Calendar.MONTH) != date1cal.get(Calendar.MONTH)) {
            months++;
            date2cal.add(Calendar.MONTH, 1);
        }
        if (date2cal.get(Calendar.DAY_OF_MONTH) > date1cal.get(Calendar.DAY_OF_MONTH)) {
            months--;
            date2cal.add(Calendar.MONTH, -1);
        }
        while (date2cal.get(Calendar.DAY_OF_MONTH) != date1cal.get(Calendar.DAY_OF_MONTH)) {
            days++;
            date2cal.add(Calendar.DATE, 1);
        }
        while (date2cal.get(Calendar.YEAR) < date1cal.get(Calendar.YEAR)) {
            years++;
            date2cal.add(Calendar.YEAR, 1);
        }

        String duration = "";
        if (years > 0) {
            duration = duration + years + " years ";
        }
        if (months > 0) {
            duration = duration + months + " months ";
        }
        if (days > 0) {
            duration = duration + days + " days";
        }
        return duration;

    }

    public void calculate() {
        if (getDateCNdispatch() != null) {
            Date cnDate = getDateCNdispatch();
            Calendar c = Calendar.getInstance();
            c.setTime(cnDate);

            String DATE_FORMAT = "dd/MM/yyyy";
            DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

            c.add(Calendar.DATE, 5);
            System.out.println("OPEN CALL: " + sdf.format(c.getTime()));
            setOpenDateEd(sdf.format(c.getTime()));


            c.add(Calendar.DATE, 52);
            if (getEcntransmission().equalsIgnoreCase("yes")) {
                c.add(Calendar.DATE, -7);
            }
            if (getEcontractDocs().equalsIgnoreCase("yes")) {
                c.add(Calendar.DATE, -5);
            }

            System.out.println("CLOSE CALL: " + sdf.format(c.getTime()));
            setCloseDateEd(sdf.format(c.getTime()));
        }
    }

    public int substractDates(Date date1, Date date2) {

//            GregorianCalendar gc1 = new GregorianCalendar(2003,9,15), //Yes, '9'
//			  gc2 = new GregorianCalendar(2004,0,1); //Yes, '0'
//	System.out.println((gc2.get(Calendar.YEAR)-gc1.get(Calendar.YEAR))*12+
//			    gc2.get(Calendar.MONTH)-gc1.get(Calendar.MONTH));

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);
        c2.setTime(date2);

        int months = c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
        return months;
    //gc2.get(Calendar.YEAR)-gc1.get(Calendar.YEAR))*12+
    //		    gc2.get(Calendar.MONTH)-gc1.get(Calendar.MONTH));


    }

    ///////////////////////////////    
//    private boolean iscfpavailable() {
//        GetSessionVariables();
//        try { // Call Web Service Operation
//            org.ellectra.wsccfp.ViewCfpCriteriaPortType port = service_3.getViewCfpCriteriaPort();
//
//            org.ellectra.wsccfp.ViewCfpCriteriaRequest part1 = new org.ellectra.wsccfp.ViewCfpCriteriaRequest();
//            part1.setActorSignature(dUserloginsignature);
//            part1.setCallForTenderID(mycalloftender);
//            org.ellectra.wsccfp.ViewCfpCriteriaResponse result = port.viewCfpCriteriaOperation(part1);
//            return result.isReturn();
//        } catch (Exception ex) {
//            // TODO handle custom exceptions here
//            return false;
//        }
//
//    }

    public boolean isBshowcreate() {
        //if (!cfpsExist)
            //bshowcreate = true;
        return !cfpsExist;
    }

    public void setBshowcreate(boolean bshowcreate) {
        this.bshowcreate = bshowcreate;
    }

    public boolean isBshowedit() {
        //if (cfpsExist)
            //bshowedit = true;
        return cfpsExist;
    }

    public void setBshowedit(boolean bshowedit) {
        this.bshowedit = bshowedit;
    }
///////////////////////////////    
    public UIData getSresdata() {
        return sresdata;
    }

    public void setSresdata(UIData sresdata) {
        this.sresdata = sresdata;
    }

    public ArrayList<searchresultlist> getAsres() {
        return asres;
    }

    public void setAsres(ArrayList<searchresultlist> asres) {
        this.asres = asres;
    }

    public UIData getMycallsdata() {
        return mycallsdata;
    }

    public void setMycallsdata(UIData mycallsdata) {
        this.mycallsdata = mycallsdata;
    }

    public ArrayList<searchresultlist> getMycallslist() {
        populateMyCalls();
        return mycallslist;
    }

    public void setMycallslist(ArrayList<searchresultlist> mycallslist) {
        this.mycallslist = mycallslist;
    }
    ///////////////////////////////////////
    public String[] getA_sc3() {
        return a_sc3;
    }

    public void setA_sc3(String[] a_sc3) {
        this.a_sc3 = a_sc3;
    }

    public String[] getA_sc4() {
        return a_sc4;
    }

    public void setA_sc4(String[] a_sc4) {
        this.a_sc4 = a_sc4;
    }

    public String getA_sc1() {
        return a_sc1;
    }

    public void setA_sc1(String a_sc1) {
        this.a_sc1 = a_sc1;
    }

    public String getA_sc5() {
        return a_sc5;
    }

    public void setA_sc5(String a_sc5) {
        this.a_sc5 = a_sc5;
    }

    public Date getA_sc6() {
        return a_sc6;
    }

    public void setA_sc6(Date a_sc6) {
        this.a_sc6 = a_sc6;
    }

    public String getA_sc7() {
        return a_sc7;
    }

    public void setA_sc7(String a_sc7) {
        this.a_sc7 = a_sc7;
    }

    public Date getA_sc8() {
        return a_sc8;
    }

    public void setA_sc8(Date a_sc8) {
        this.a_sc8 = a_sc8;
    }

    public String getA_sc9() {
        return a_sc9;
    }

    public void setA_sc9(String a_sc9) {
        this.a_sc9 = a_sc9;
    }

    public String getA_sc10() {
        return a_sc10;
    }

    public void setA_sc10(String a_sc10) {
        this.a_sc10 = a_sc10;
    }

    public String getDUserloginname() {
        return dUserloginname;
    }

    public void setDUserloginname(String dUserloginname) {
        this.dUserloginname = dUserloginname;
    }

    public String getDUserloginsignature() {
        return dUserloginsignature;
    }

    public void setDUserloginsignature(String dUserloginsignature) {
        this.dUserloginsignature = dUserloginsignature;
    }

    public String getDUserloginOrganization() {
        return dUserloginOrganization;
    }

    public void setDUserloginOrganization(String dUserloginOrganization) {
        this.dUserloginOrganization = dUserloginOrganization;
    }

    public String getDUserloginrole() {
        return dUserloginrole;
    }

    public void setDUserloginrole(String dUserloginrole) {
        this.dUserloginrole = dUserloginrole;
    }

    public int getDUserloginid() {
        return dUserloginid;
    }

    public void setDUserloginid(int dUserloginid) {
        this.dUserloginid = dUserloginid;
    }

    public int getMytenderid() {
        return mytenderid;
    }

    public void setMytenderid(int mytenderid) {
        this.mytenderid = mytenderid;
    }

    public int getMycalloftender() {
        return mycalloftender;
    }

    public void setMycalloftender(int mycalloftender) {
        this.mycalloftender = mycalloftender;
    }

    public String getTitle() {
        System.err.println("IN GET TITLE: SEARCH PATH VALUE: [" + searchPath + "]");
        //if (searchPath != 0) {
            viewCallDetails();
            
            String role = decideRole();
            System.err.println("ROLE IS: " + role);
            if (role.equalsIgnoreCase("POA")) {
                if (userAssociatedToCall("ADMINISTRATIVE", getMycalloftender())) {
                    System.err.println("ASSOCIATED");
                    ctrlUploadCDocs = true;
                    ctrlUpdateRendered = true;
                } 
                else System.err.println("NOT_ASSOCIATED");
                    
            } 
        //}
        return title;
    }
    
    

    


    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getProcedureType() {
        return procedureType;
    }

    public void setProcedureType(String procedureType) {
        this.procedureType = procedureType;
    }

    public String getContractType() {
        return contractType;
    }

    public void setContractType(String contractType) {
        this.contractType = contractType;
    }

    public String getNoticeInvolvesStr() {
        return noticeInvolvesStr;
    }

    public void setNoticeInvolvesStr(String noticeInvolves) {
        this.noticeInvolvesStr = noticeInvolves;
    }

    public String getOpenDate() {
        return openDate;
    }

    public void setOpenDate(String openDate) {
        this.openDate = openDate;
    }

    public String getCloseDate() {
        return closeDate;
    }

    public void setCloseDate(String closeDate) {
        this.closeDate = closeDate;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getCpv() {
        return cpv;
    }

    public void setCpv(String cpv) {
        this.cpv = cpv;
    }

    public String getNuts() {
        return nuts;
    }

    public void setNuts(String nuts) {
        this.nuts = nuts;
    }

    public String getContractDuration() {
        return contractDuration;
    }

    public void setContractDuration(String contractDuration) {
        this.contractDuration = contractDuration;
    }

    public String getOjeuRefNum() {
        return ojeuRefNum;
    }

    public void setOjeuRefNum(String ojeuRefNum) {
        this.ojeuRefNum = ojeuRefNum;
    }

    public String getPrevPublications() {
        return prevPublications;
    }

    public void setPrevPublications(String prevPublications) {
        this.prevPublications = prevPublications;
    }

    public String getInternalRefNum() {
        return internalRefNum;
    }

    public void setInternalRefNum(String internalRefNum) {
        this.internalRefNum = internalRefNum;
    }

    public String getBtnCreateTenderDisabled() {
        return btnCreateTenderDisabled;
    }

    public void setBtnCreateTenderDisabled(String btnCreateTenderDisabled) {
        this.btnCreateTenderDisabled = btnCreateTenderDisabled;
    }

    public int getSearchPath() {
        return searchPath;
    }

    public void setSearchPath(int searchPath) {
        this.searchPath = searchPath;
    }
    ////////////////////////////////////////////////////////////////////////////
    /////////////////// EDIT VARIABLES /////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    public void eventStartDate(ValueChangeEvent vce) {
        contractStarts = (Date) vce.getNewValue();
    }

    public void eventEndDate(ValueChangeEvent vce) {
        contractEnds = (Date) vce.getNewValue();
    }

    public void eventSelectDurationOption(ValueChangeEvent vce) {
        durationOption = (String) vce.getNewValue();
        if (durationOption.equals("1")) {
            setNumbercd(true);
            setDatecd(false);
        } else if (durationOption.equals("2")) {
            setNumbercd(false);
            setDatecd(true);
        }
    }

    public void validatePartA(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
        String partA = (String) value;
        String regExpA = "\\d{4}";
        if (!Pattern.matches(regExpA, partA)) {
            FacesMessage message = new FacesMessage("Code's first field doesn't match the pattern DDDD");
            context.addMessage("searchcallsfortenders", message);
            throw new ValidatorException(message);
        }
    }

    public void validatePartB(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
        String partB = (String) value;
        String regExpB = "\\d{3}";
        if (!Pattern.matches(regExpB, partB)) {
            FacesMessage message = new FacesMessage("Code's second field doesn't match the pattern DDD");
            context.addMessage("searchcallsfortenders", message);
            throw new ValidatorException(message);
        }
    }

    public void validatePartC(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
        String partC = (String) value;
        String regExpC = "\\d{6}";
        if (!Pattern.matches(regExpC, partC)) {
            FacesMessage message = new FacesMessage("Code's third field doesn't match the pattern DDDDDD");
            context.addMessage("searchcallsfortenders", message);
            throw new ValidatorException(message);
        }
    }

    public Date getContractStarts() {
        System.err.println("START: " + contractStarts);
        return contractStarts;
    }

    public void setContractStarts(Date contractStarts) {
        this.contractStarts = contractStarts;
    }

    public Date getContractEnds() {
        System.err.println("END: " + contractEnds);
        return contractEnds;
    }

    public void setContractEnds(Date contractEnds) {
        this.contractEnds = contractEnds;
    }

    public Date getDateCNdispatch() {
        return dateCNdispatch;
    }

    public void setDateCNdispatch(Date dateCNdispatch) {
        this.dateCNdispatch = dateCNdispatch;
    }

    public String getOpenDateEd() {
        System.err.println("GET OPEN DATE STR: " + openDateEd);
        return openDateEd;
    }

    public void setOpenDateEd(String openDateEd) {
        this.openDateEd = openDateEd;
    }

    public String getCloseDateEd() {
        System.err.println("GET CLOSE DATE STR: " + closeDateEd);
        return closeDateEd;
    }

    public void setCloseDateEd(String closeDateEd) {
        this.closeDateEd = closeDateEd;
    }

    public String[] getNoticeInvolvesEd() {
        return noticeInvolvesEd;
    }

    public void setNoticeInvolvesEd(String[] noticeInvolvesEd) {
        this.noticeInvolvesEd = noticeInvolvesEd;
    }

    public Double getEstimatedValue() {
        return estimatedValue;
    }

    public void setEstimatedValue(Double estimatedValue) {
        this.estimatedValue = estimatedValue;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public Integer getDurationInMonths() {
        return durationInMonths;
    }

    public void setDurationInMonths(Integer durationInMonths) {
        this.durationInMonths = durationInMonths;
    }

    public Integer getDurationInDays() {
        return durationInDays;
    }

    public void setDurationInDays(Integer durationInDays) {
        this.durationInDays = durationInDays;
    }

    public String getProcedureTypeEd() {
        return procedureTypeEd;
    }

    public void setProcedureTypeEd(String procedureTypeEd) {
        this.procedureTypeEd = procedureTypeEd;
    }

    public String getA_ctw16() {
        return a_ctw16;
    }

    public void setA_ctw16(String a_ctw16) {
        this.a_ctw16 = a_ctw16;
    }

    public String getA_ctw17() {
        return a_ctw17;
    }

    public void setA_ctw17(String a_ctw17) {
        this.a_ctw17 = a_ctw17;
    }

    public String getA_ctw18() {
        return a_ctw18;
    }

    public void setA_ctw18(String a_ctw18) {
        this.a_ctw18 = a_ctw18;
    }

    public Date getA_ctw19() {
        return a_ctw19;
    }

    public void setA_ctw19(Date a_ctw19) {
        this.a_ctw19 = a_ctw19;
    }

    public String getA_ctw20() {
        return a_ctw20;
    }

    public void setA_ctw20(String a_ctw20) {
        this.a_ctw20 = a_ctw20;
    }

    public String getA_ctw21() {
        return a_ctw21;
    }

    public void setA_ctw21(String a_ctw21) {
        this.a_ctw21 = a_ctw21;
    }

    public String getA_ctw22() {
        return a_ctw22;
    }

    public void setA_ctw22(String a_ctw22) {
        this.a_ctw22 = a_ctw22;
    }

    public Date getA_ctw23() {
        return a_ctw23;
    }

    public void setA_ctw23(Date a_ctw23) {
        this.a_ctw23 = a_ctw23;
    }

    public Integer getNumOfDays() {
        return numOfDays;
    }

    public void setNumOfDays(Integer numOfDays) {
        this.numOfDays = numOfDays;
    }

    public String getEcntransmission() {
        return ecntransmission;
    }

    public void setEcntransmission(String ecntransmission) {
        this.ecntransmission = ecntransmission;
    }

    public String getEcontractDocs() {
        return econtractDocs;
    }

    public void setEcontractDocs(String econtractDocs) {
        this.econtractDocs = econtractDocs;
    }

    public boolean isNumbercd() {
        return numbercd;
    }

    public void setNumbercd(boolean numbercd) {
        this.numbercd = numbercd;
    }

    public boolean isDatecd() {
        return datecd;
    }

    public void setDatecd(boolean datecd) {
        this.datecd = datecd;
    }

    public String getDurationOption() {
        return durationOption;
    }

    public void setDurationOption(String durationOption) {
        this.durationOption = durationOption;
    }

    public String getVoc() {
        return voc;
    }

    public void setVoc(String voc) {
        this.voc = voc;
    }

    public boolean isCtrlUpdateRendered() {
        return ctrlUpdateRendered;
    }

    public void setCtrlUpdateRendered(boolean ctrlUpdateRendered) {
        this.ctrlUpdateRendered = ctrlUpdateRendered;
    }

    public boolean isCfpsExist() {
        return cfpsExist;
    }

    public void setCfpsExist(boolean cfpsExist) {
        this.cfpsExist = cfpsExist;
    }

    public boolean isCtrlUploadCDocs() {
        return ctrlUploadCDocs;
    }

    public void setCtrlUploadCDocs(boolean ctrlUploadCDocs) {
        this.ctrlUploadCDocs = ctrlUploadCDocs;
    }




    
}
