/// <summary>
        /// Наполнить объектами, извлечёнными из базы данных, список в интерфейсе.
        /// </summary>
        private void ReadXpBaseCollection()
        {
            // отписка от старого обработчика
            RemoveAllItemCheckListeners();

            var criteria = GetModelCriteria();

            criteria = !ReferenceEquals(criteria, null)
                       ? CriteriaOperator.And(CheckedCollection.Criteria, criteria)
                       : CheckedCollection.Criteria;

            var dataSource = new XPCollection(CheckedCollection.Session, MemberInfo.ListElementType,
                                              criteria, CheckedCollection.Sorting.ToArray());

            var classInfo = application.Model.BOModel.GetClass(MemberInfo.ListElementTypeInfo.Type);

            if (CheckedCollection.Sorting.Count > 0)
            {
                dataSource.Sorting = CheckedCollection.Sorting;
            }
            else if (CheckedCollection.Sorting.Count == 0 &&
                     !String.IsNullOrEmpty(classInfo.DefaultProperty))
            {
                dataSource.Sorting.Add(new SortProperty(classInfo.DefaultProperty,
                                                        SortingDirection.Ascending));
            }
            else
            {
                dataSource.Sorting.Add(new SortProperty(
                                           CriteriaOperator.Parse("ToStr([This])"), SortingDirection.Ascending));
            }
            Control.DataSource    = dataSource;
            Control.DisplayMember = classInfo.DefaultProperty;

            //Выполняем условие для отображения текста в контроле.
            Control.ItemTextCriteria = GetDisplayItemCriteria();

            var removables = CheckedCollection
                             .Cast <object>()
                             .Where(item => dataSource.IndexOf(item) == -1)
                             .ToList();

            foreach (var item in removables)
            {
                CheckedCollection.BaseRemove(item);
            }

            foreach (var obj in CheckedCollection)
            {
                Control.SetItemChecked(dataSource.IndexOf(obj), true);
            }
            Control.ItemCheck += collection_ItemCheck;
        }
 protected override void ReadValueCore()
 {
     base.ReadValueCore();
     if (PropertyValue is XPBaseCollection)
     {
         Control.ItemCheck -= new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
         checkedItems       = (XPBaseCollection)PropertyValue;
         XPCollection dataSource = new XPCollection(checkedItems.Session, MemberInfo.ListElementType);
         IModelClass  classInfo  = application.Model.BOModel.GetClass(MemberInfo.ListElementTypeInfo.Type);
         if (checkedItems.Sorting.Count > 0)
         {
             dataSource.Sorting = checkedItems.Sorting;
         }
         else if (!String.IsNullOrEmpty(classInfo.DefaultProperty))
         {
             dataSource.Sorting.Add(new SortProperty(classInfo.DefaultProperty, DevExpress.Xpo.DB.SortingDirection.Ascending));
         }
         Control.DataSource    = dataSource;
         Control.DisplayMember = classInfo.DefaultProperty;
         foreach (object obj in checkedItems)
         {
             Control.SetItemChecked(dataSource.IndexOf(obj), true);
         }
         Control.ItemCheck += new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
     }
 }
        private void RefreshAvailableUsers()
        {
            if (availableUsers == null)
            {
                return;
            }

            availableUsers.Criteria = new BinaryOperator("Company", Company);

            if (employee != null && availableUsers.IndexOf(employee) == -1)
            {
                Employee = null;
            }

            availableBranch.Criteria = new BinaryOperator("Company", Company);

            if (branch != null && availableBranch.IndexOf(branch) == -1)
            {
                Branch = null;
            }
        }
        public OSOBA_RESP InsertOsobu(Session session, int EXT_APP_KOD, OSOBA osoba)
        {
            Session    sesna     = session;
            OSOBA_RESP osobaResp = new OSOBA_RESP();

            osoba.ADR_EA = EXT_APP_KOD;

            try
            {
                #region test  vyplneni vstupnich hodnot

                if (osoba.RC_IC != null)
                {
                    osoba.RC_IC = osoba.RC_IC.Trim();
                }
                if (osoba.JMENO != null)
                {
                    osoba.JMENO = osoba.JMENO.Trim();
                }
                if (osoba.PRIJMENI != null)
                {
                    osoba.PRIJMENI = osoba.PRIJMENI.Trim();
                }

                if (string.IsNullOrEmpty(osoba.RC_IC))  // na string.IsNullOrWhiteSpace(osoba.RC_IC) testovat nemusim,
                                                        //protoze je vyse Trimovan
                {
                    P_NASTAVENI nast = sesna.GetObjectByKey <P_NASTAVENI>("ICOP");
                    if ((nast == null) || ((nast != null) && (nast.HODNOTA == "1")))
                    {
                        osobaResp.result    = Result.ERROR;
                        osobaResp.ERRORMESS = "IČ/RČ musí­ být vyplněno";
                        return(osobaResp);
                    }
                }

                if (EXT_APP_KOD == null)
                {
                    throw new Exception("kód externí aplikace není zadán");
                }
                P_EXTAPP EA = sesna.GetObjectByKey <P_EXTAPP>(EXT_APP_KOD);
                if (EA == null)
                {
                    throw new Exception("chybný kód externí aplikace");
                }

                if (((osoba.TYP == TypOsoby.F) || (osoba.TYP == TypOsoby.C)) &&
                    (string.IsNullOrEmpty(osoba.RC_IC)) &&
                    (string.IsNullOrEmpty(osoba.JMENO) || string.IsNullOrEmpty(osoba.PRIJMENI) ||
                     (!osoba.DATUM_NAROZENI.HasValue))
                    )
                {
                    throw new Exception("u fyzické osoby musí být vyplněno jméno, příjmení a datum narození");
                }


                if (!string.IsNullOrEmpty(osoba.STAT))
                {
                    P_STAT stat = sesna.FindObject <P_STAT>(CriteriaOperator.Parse("Upper(STAT_NAZEV) = ?", osoba.STAT.ToUpper()));
                    if (stat == null)
                    {
                        stat = sesna.FindObject <P_STAT>(CriteriaOperator.Parse("STAT_KOD = ?", osoba.STAT.ToUpper()));
                    }

                    if (stat == null)
                    {
                        throw new Exception(string.Format("stát \"{0}\" není uveden v číselníku států", osoba.STAT));
                    }
                    else
                    {
                        osoba.STAT = stat.STAT_KOD;
                    }
                }
                else
                {
                    osoba.STAT = null;
                }


                if (osoba.ZAHRANICNI_ADRESA != null)
                {
                    if ((osoba.TYP != TypOsoby.C) && (osoba.TYP != TypOsoby.P))
                    {
                        throw new Exception(string.Format("Zahraniční adresu lze uvádět pouze pro cizince a právnické osoby."));
                    }
                    if (String.IsNullOrEmpty(osoba.ZAHRANICNI_ADRESA.STAT))
                    {
                        throw new Exception(string.Format("U zahraniční adresy musí být uveden stát."));
                    }

                    if (!String.IsNullOrEmpty(osoba.OBEC_NAZEV) || !String.IsNullOrEmpty(osoba.ULICE_NAZEV) ||
                        (osoba.CIS_DOMU != null) || (osoba.CIS_OR != null) ||
                        !String.IsNullOrEmpty(osoba.PSC) || !String.IsNullOrEmpty(osoba.STAT)
                        )
                    {
                        throw new Exception(string.Format("U zahraniční adresy nelze současně uvádět trvalou adresu."));
                    }

                    P_STAT stat = sesna.FindObject <P_STAT>(CriteriaOperator.Parse("Upper(STAT_NAZEV) = ?", osoba.ZAHRANICNI_ADRESA.STAT.ToUpper()));
                    if (stat == null)
                    {
                        stat = sesna.FindObject <P_STAT>(CriteriaOperator.Parse("STAT_KOD = ?", osoba.ZAHRANICNI_ADRESA.STAT.ToUpper()));
                    }
                    if (stat == null)
                    {
                        throw new Exception(string.Format("Stát \"{0}\" není uveden v číselníku států.", osoba.ZAHRANICNI_ADRESA.STAT));
                    }
                }


                #endregion test  vyplneni vstupnich hodnot

                using (UnitOfWork uow = new UnitOfWork(sesna.DataLayer))
                {
                    //dotaz, zda j*z neexistuje
                    if (!string.IsNullOrEmpty(osoba.RC_IC))
                    {
                        CriteriaOperator criteria = CriteriaOperator.Parse("(ADR_SICO = ? or ADR_ICO = ?)", osoba.RC_IC, osoba.RC_IC);
                        XPCollection <P_ADRESA_ROBRHS> adrExist = new XPCollection <P_ADRESA_ROBRHS>(sesna, criteria);
                        if (adrExist.Count > 0)
                        {
                            #region adresa existuje - muze existovat vice osob se stejnym ADR_SICO
                            #region adresa je vybirana v poradi

                            P_ADRESA_ROBRHS osobaExist = null;
                            int             isico      = -1;
                            int             iico       = -1;
                            for (int i = 1; i <= 4; i++)
                            {
                                if (i == 1)
                                {
                                    adrExist.Filter = CriteriaOperator.Parse("ADR_EA = ? and ADR_ADRPOPL > -1", EXT_APP_KOD);
                                }
                                if (i == 2)
                                {
                                    adrExist.Filter = CriteriaOperator.Parse("ADR_ADRPOPL > -1");
                                }
                                if (i == 3)
                                {
                                    adrExist.Filter = CriteriaOperator.Parse("ADR_EA = ?", EXT_APP_KOD);
                                }
                                if (i == 4)
                                {
                                    adrExist.Filter = null;
                                }
                                if (adrExist.Count > 0)
                                {
                                    if (adrExist.First(P_ADRESA_ROBRHS => P_ADRESA_ROBRHS.ADR_SICO == osoba.RC_IC).ADR_SICO != null)
                                    {
                                        isico = 0;
                                    }
                                    if (adrExist.First(P_ADRESA_ROBRHS => P_ADRESA_ROBRHS.ADR_ICO == osoba.RC_IC).ADR_ICO != null)
                                    {
                                        iico = 0;
                                    }
                                }

                                if (isico > -1 || iico > -1)
                                {
                                    break;
                                }
                            }
                            #endregion adresa je vybrana v poradi

                            if (isico > -1 || iico > -1)
                            {
                                osobaExist = isico >= 0 ? adrExist[isico] : adrExist[iico];

                                if ((adrExist[adrExist.IndexOf(osobaExist)].ADR_SICO == null) && (isico == -1) && (iico >= 0))
                                {
                                    adrExist[iico].ADR_SICO = osoba.RC_IC;
                                    adrExist[iico].Save();
                                }

                                osobaResp.result = Result.OK;
                                osobaResp.status = Status.EXISTS;

                                Utils.copy.CopyDlePersistentAttr <OSOBA_RESP>(osobaExist, osobaResp);
                                Utils.copy.CopyDlePersistentAttr <KONTAKTNI_ADRESA>(osobaExist, osobaResp.KONTAKTNI_ADRESA);
                                osobaResp.RC_IC = osobaExist.ADR_SICO; //0.8
                                #endregion adresa existuje
                                return(osobaResp);
                            }
                        }
                    }

                    P_ADRESA_ROBRHS adr = new P_ADRESA_ROBRHS(uow);

                    Utils.copy.CopyDlePersistentAttr <P_ADRESA_ROBRHS>(osoba, adr);
                    Utils.copy.CopyDlePersistentAttr <P_ADRESA_ROBRHS>(osoba.KONTAKTNI_ADRESA, adr);
                    if (osoba.ZAHRANICNI_ADRESA != null)
                    {
                        Utils.copy.CopyDlePersistentAttr <P_ADRESA_ROBRHS>(osoba.ZAHRANICNI_ADRESA, adr);
                    }

                    try
                    {
                        uow.CommitChanges();
                        CriteriaOperator criteria = CriteriaOperator.Parse("ADR_ICO = ?", adr.ADR_ICO);
                        XPCollection <P_ADRESA_ROBRHS> adrInserted = new XPCollection <P_ADRESA_ROBRHS>(sesna, criteria);

                        Utils.copy.CopyDlePersistentAttr <OSOBA_RESP>(adrInserted[0], osobaResp);
                        Utils.copy.CopyDlePersistentAttr <KONTAKTNI_ADRESA>(adrInserted[0], osobaResp.KONTAKTNI_ADRESA);

                        osobaResp.result = Result.OK;
                        osobaResp.status = Status.INSERTED;
                        osobaResp.ADR_ID = adrInserted[0].ADR_ID;
                        osobaResp.RC_IC  = adrInserted[0].ADR_SICO; //0.8
                        return(osobaResp);
                    }
                    catch (Exception exc)
                    {
                        uow.RollbackTransaction();
                        osobaResp.result = Result.ERROR;

                        if (exc.InnerException == null)
                        {
                            osobaResp.ERRORMESS = exc.Message;
                        }
                        else
                        {
                            osobaResp.ERRORMESS = exc.InnerException.Message;
                        }
                        return(osobaResp);

                        /*
                         * throw new Exception(String.Format("chyba \n {0}", exc.InnerException.Message));
                         */
                    }
                } //uow
            }
            catch (Exception exc)
            {
                osobaResp.result = Result.ERROR;

                if (exc.InnerException == null)
                {
                    osobaResp.ERRORMESS = exc.Message;
                }
                else
                {
                    osobaResp.ERRORMESS = exc.InnerException.Message;
                }

                Util.Util.WriteLog(osobaResp.ERRORMESS + "\n" + exc.StackTrace);

                return(osobaResp);

                /*
                 * throw new Exception(String.Format("chyba \n {0}", exc.InnerException.Message));
                 */
            }
        }
        protected override void ReadValueCore()
        {
            base.ReadValueCore();
            if (PropertyValue is XPBaseCollection)
            {
                // отписка от старого обработчика
                Control.ItemCheck -= new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
                checkedItems = (XPBaseCollection)PropertyValue;

                // дополнительные условия
                CriteriaOperator criteria = null;
                if (!string.IsNullOrEmpty(Model.DataSourceCriteria))
                {
                    criteria = CriteriaOperator.Parse(Model.DataSourceCriteria);
                }
                if (!string.IsNullOrEmpty(Model.DataSourceCriteriaProperty))
                {
                    CriteriaOperator criteria2 = null;

                    IMemberInfo propWithCriteria = MemberInfo.Owner.FindMember(Model.DataSourceCriteriaProperty);
                    criteria2 = (CriteriaOperator)propWithCriteria.GetValue(this.CurrentObject);

                    if (!ReferenceEquals(criteria2, null))
                    {
                        criteria = !ReferenceEquals(criteria, null) ? CriteriaOperator.And(criteria, criteria2) : criteria2;
                    }
                }

                // коллекция данных для списка
                XPCollection dataSource = new XPCollection(
                    checkedItems.Session,
                    MemberInfo.ListElementType,
                    !ReferenceEquals(criteria, null) ? CriteriaOperator.And(checkedItems.Criteria, criteria) : checkedItems.Criteria,
                    checkedItems.Sorting.ToArray<SortProperty>());
                IModelClass classInfo = application.Model.BOModel.GetClass(MemberInfo.ListElementTypeInfo.Type);
                if (checkedItems.Sorting.Count > 0)
                {
                    dataSource.Sorting = checkedItems.Sorting;
                }
                else if (checkedItems.Sorting.Count == 0 && !String.IsNullOrEmpty(classInfo.DefaultProperty))
                {
                    dataSource.Sorting.Add(new SortProperty(classInfo.DefaultProperty, DevExpress.Xpo.DB.SortingDirection.Ascending));
                }
                Control.DataSource = dataSource;
                Control.DisplayMember = classInfo.DefaultProperty;

                //Выполняем условие для отображения текста в контроле.
                IModelPropertyEditorDisplayItem displayItem = Model as IModelPropertyEditorDisplayItem;
                if (displayItem != null)
                {
                    if (!ReferenceEquals(displayItem.DisplayItemCriteriaProperty, null))
                    {
                        IMemberInfo propWithCriteria = MemberInfo.Owner.FindMember(displayItem.DisplayItemCriteriaProperty);
                        Control.ItemTextCriteria = (CriteriaOperator)propWithCriteria.GetValue(this.CurrentObject);
                    }
                    if (!String.IsNullOrWhiteSpace(displayItem.DisplayItemCriteriaString))
                    {
                        Control.ItemTextCriteriaString = (displayItem.DisplayItemCriteriaString);
                    }
                }

                foreach (object obj in checkedItems)
                {
                    Control.SetItemChecked(dataSource.IndexOf(obj), true);
                }
                Control.ItemCheck += new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
            }
        }