public ApasRegularView UpdateApasRegular(ApasRegularView obj)
    {
        //security and design reconsideration
        ApasRegular objReg = new ApasRegular();
        objReg.Id = obj.Id;
        objReg.LoadData();
        objReg.ColorCode = obj.ColorCode;
        objReg.Save();

        EntityConverter ecov = new EntityConverter();
        return ecov.ConvertRegular(objReg);
    }
        //public ArrayList SearchPaymentAdviceByDay
        //(long idPAList, string strDay, int branchId){
        //    try{
        //        return SearchPaymentAdviceByDayImpl(idPAList, strDay, branchId);
        //    }catch(Exception e){
        //        string source = "APAS";
        //        string log = "Application";
        //        string strEvent = "Exception";
        //        string machine = ".";
        //        if(!EventLog.SourceExists(source,machine)){
        //            EventLog.CreateEventSource(source, log, machine);
        //        }
        //        EventLog eLog = new EventLog(log, machine, source);
        //        eLog.WriteEntry(e.StackTrace);
        //        throw e;
        //    }
        //}
        public ArrayList SearchPaymentAdviceByDay(long idPAList, string strDay, int branchId)
        {
            //1. Create criteria to query PAs in the chosen day
            //2. Aggregate PAs under their principal RegularView
            //2.1. If principal class is not in the chosen day, don't display
            //2.2. If non-principal classes are in the chosen day, add a dummy reference

            ISession ses = NHibernateManager.GetCurrentSession();
            EntityConverter ecov = new EntityConverter();

            Dictionary<int, ApasRegularView> result = new Dictionary<int, ApasRegularView>();
            ApasRegularView pReg;
            ApasRegular tempReg;
            int pRegId;

            PALockManager lMgr = new PALockManager(
               ApasAccessControlManager.GetCurrentInstance().LogonUser.Id,
               PALockKey);

            List<SearchPredicate> criteria = new List<SearchPredicate>();
            StringSearchPredicate sp = new StringSearchPredicate();
            sp.FieldName = "strDayRegular";
            sp.TableName = "Regular";
            sp.Value = strDay;
            sp.PredicateTemplate = " {0} = {1} ";

            criteria.Add(sp);

            IntegerSearchPredicate ip = new IntegerSearchPredicate();
            ip.FieldName = "intIdBranch";
            ip.TableName = "Branch";
            ip.Value = branchId;
            ip.PredicateTemplate = " {0} = {1} ";

            criteria.Add(ip);

            IList<PaymentAdvice> pList = executeQuery(idPAList, criteria);

            //if no result, return
            if (pList.Count == 0)
                return new ArrayList();

            //aggregate PAs under respective ApasRegularViews
            foreach (PaymentAdvice objPA in pList)
            {

                //check if chosen day of week is principal day for the objPA.
                //Principal day = leftmost day of week start from monday
                int[] aryDow = new int[objPA.ClassFees.Count];
                ApasRegular[] aryReg = new ApasRegular[aryDow.Length];

                int i = 0;
                foreach (ClassFee objCF in objPA.ClassFees.Values)
                {
                    tempReg = objCF.GetAssociatedClass();

                    aryDow[i] = convertDow(tempReg.Day);
                    aryReg[i] = tempReg;
                    i++;
                }

                Array.Sort(aryDow, aryReg);

                pRegId = aryReg[0].Id; //principal class Id

                //add all the classes in the chosen day to result dictionary
                foreach (ApasRegular objAR in aryReg)
                {
                    if (objAR.Day.ToLower() == strDay.ToLower() &&
                        !result.ContainsKey(objAR.Id))
                        result.Add(objAR.Id, ecov.ConvertRegular(objAR));
                }

                //check if principal class is in chosen day
                if (result.TryGetValue(pRegId, out pReg))
                {
                    //lock
                    lMgr.Hold(objPA.Id);

                    ses.Evict(objPA);

                    //requery the objPA
                    PaymentAdvice principalPA = new PaymentAdvice(objPA.Id, false, PALockKey);

                    //add to class
                    pReg.PaymentAdvices.Add(ecov.ConvertPaymentAdvice(principalPA));

                }

                foreach (ClassFee objCF in objPA.ClassFees.Values)
                {
                    //add dummy references
                    if (objCF.IdRegular != pRegId &&
                        result.ContainsKey(objCF.IdRegular))
                    {
                        result[objCF.IdRegular].PaymentAdviceReferences.Add
                        (ecov.ConvertPaymentAdviceReference(objPA, ecov.ConvertRegular(aryReg[0])));
                    }
                }

            }

            //put the result into an arraylist
            ArrayList resultArray = new ArrayList();
            foreach (ApasRegularView objARV in result.Values)
            {
                resultArray.Add(objARV);
            }

            return resultArray;
        }
        /* OBSOLETE METHOD, new method below */
        /*
        public ArrayList SearchPaymentAdviceByDay
            (long idPAList, string strDay)
        {

            ISession ses = NHibernateManager.GetCurrentSession();
            ITransaction trans;
            EntityConverter ecov=new EntityConverter();

            //string idsToLock="";

            Dictionary<int, ApasRegularView> result = new Dictionary<int, ApasRegularView>();
            ApasRegularView pReg;
            ApasRegular tempReg;
            int pRegId;

            PALockManager lMgr = new PALockManager(
               ApasAccessControlManager.GetCurrentInstance().LogonUser.Id,
               PALockKey);

            //release all previous locks
            lMgr.Release("", new List<SqlParameter>());

            //query
            trans = ses.BeginTransaction();
            IList<PaymentAdvice> pList =
                ses.GetNamedQuery
                ("Apas.Business.ApasSearchManager.SearchPaymentAdviceByDay")
                .SetAnsiString("day", strDay)
                .SetInt64("idPAList", idPAList)
                .List<PaymentAdvice>();
            trans.Commit();

            //if no result, return
            if (pList.Count == 0)
                return new ArrayList();

            //dun lock now because not all the PAs queried are principal records
            ////Lock
            //foreach (PaymentAdvice objPA in pList)
            //{
            //    idsToLock += objPA.Id + ",";
            //}
            //idsToLock = idsToLock.Substring(0, idsToLock.Length - 1);
            //idsToLock = "(" + idsToLock + ")";

            //lMgr.Hold(" WHERE intIdPA in " + idsToLock, new List<SqlParameter>());

            //trans = ses.BeginTransaction();
            ////re-query for updated "hold" status
            //pList =
            //    ses.GetNamedQuery
            //    ("Apas.Business.ApasSearchManager.SearchPaymentAdviceByDay")
            //    .SetAnsiString("day", strDay)
            //    .SetInt64("idPAList", idPAList)
            //    .List<PaymentAdvice>();
            //trans.Commit();

            //do eager fetching, erm.. not necessary
            //foreach (PaymentAdvice objPA in pList)
            //{

            //    //do eager fetching on collections
            //    NHibernateUtil.Initialize(objPA.ClassFees);
            //    NHibernateUtil.Initialize(objPA.OtherFees);
            //}

            //aggregate PAs under respective ApasRegularViews
            foreach (PaymentAdvice objPA in pList)
            {

                //check if chosen day of week is principal day for the objPA.
                //Principal day = leftmost day of week start from monday
                int dow = convertDow(strDay);
                int[] aryDow = new int[objPA.ClassFees.Count];
                ApasRegular[] aryReg = new ApasRegular[aryDow.Length];

                int i = 0;
                foreach (ClassFee objCF in objPA.ClassFees.Values)
                {
                    tempReg = objCF.GetAssociatedClass();

                    aryDow[i] = convertDow(tempReg.Day);
                    aryReg[i] = tempReg;
                    i++;
                }

                Array.Sort(aryDow, aryReg);

                pRegId = aryReg[0].Id ; //principal class Id

                //add all the classes in the chosen day to result dictionary
                foreach (ApasRegular objAR in aryReg)
                {
                    if (objAR.Day.ToLower() == strDay.ToLower() &&
                        !result.ContainsKey(objAR.Id))
                        result.Add(objAR.Id, ecov.ConvertRegular(objAR));
                }

                //check if this PA is in principal class
                if (result.TryGetValue(pRegId, out pReg))
                {
                    //lock
                    lMgr.Hold(objPA.Id);

                    ses.Evict(objPA);

                    //requery the objPA
                    PaymentAdvice principalPA= new PaymentAdvice(objPA.Id, false, PALockKey);

                    //add to class
                    pReg.PaymentAdvices.Add(ecov.ConvertPaymentAdvice(principalPA));

                }

                foreach (ClassFee objCF in objPA.ClassFees.Values)
                {
                    //add dummy references
                    if (objCF.IdRegular != pRegId &&
                        result.ContainsKey(objCF.IdRegular))
                    {
                        result[objCF.IdRegular].PaymentAdviceReferences.Add
                        (ecov.ConvertPaymentAdviceReference(objPA, ecov.ConvertRegular(aryReg[0])));
                    }
                }

            }

            //put the result into an arraylist
            ArrayList resultArray = new ArrayList();
            foreach (ApasRegularView objARV in result.Values)
            {
                resultArray.Add(objARV);
            }

            return resultArray;
        }
        */
        public ArrayList SearchPaymentAdviceByCriteria(long idPAList, List<SearchPredicate> criteria)
        {
            //1. execute the criteria to get PAList
            //2. Aggregate PAList under respective principal RegularClassView
            ISession ses = NHibernateManager.GetCurrentSession();
            EntityConverter ecov = new EntityConverter();

            Dictionary<int, ApasRegularView> result = new Dictionary<int, ApasRegularView>();
            ApasRegularView pReg;
            ApasRegular tempReg;
            int pRegId;

            PALockManager lMgr = new PALockManager(
               ApasAccessControlManager.GetCurrentInstance().LogonUser.Id,
               PALockKey);

            IList<PaymentAdvice> pList = executeQuery(idPAList, criteria);

            //if no result, return
            if (pList.Count == 0)
                return new ArrayList();

            //aggregate PAs under respective ApasRegularViews
            foreach (PaymentAdvice objPA in pList)
            {

                //Principal day = leftmost day of week start from monday
                int[] aryDow = new int[objPA.ClassFees.Count];
                ApasRegular[] aryReg = new ApasRegular[aryDow.Length];

                int i = 0;
                foreach (ClassFee objCF in objPA.ClassFees.Values)
                {
                    tempReg = objCF.GetAssociatedClass();

                    aryDow[i] = convertDow(tempReg.Day);
                    aryReg[i] = tempReg;
                    i++;
                }

                Array.Sort(aryDow, aryReg);

                pRegId = aryReg[0].Id; //principal class Id

                //add principal class to result
                if (!result.ContainsKey(pRegId))
                    result.Add(pRegId, ecov.ConvertRegular(aryReg[0]));

                //add PA under its principal class
                if (result.TryGetValue(pRegId, out pReg))
                {
                    //lock
                    lMgr.Hold(objPA.Id);

                    ses.Evict(objPA);

                    //requery the objPA
                    PaymentAdvice principalPA = new PaymentAdvice(objPA.Id, false, PALockKey);

                    //add to class
                    pReg.PaymentAdvices.Add(ecov.ConvertPaymentAdvice(principalPA));

                }

            }

            //put the result into an arraylist
            ArrayList resultArray = new ArrayList();
            foreach (ApasRegularView objARV in result.Values)
            {
                resultArray.Add(objARV);
            }

            return resultArray;
        }