private RelationshipEventWrapper ToRelationshipEventWrapper(RelationshipEvent relationshipEvent)
        {
            var result = new RelationshipEventWrapper(relationshipEvent);


            var historyCategory = DaoFactory.GetListItemDao().GetByID(relationshipEvent.CategoryID);

            if (historyCategory != null)
                result.Category = ToHistoryCategoryWrapper(historyCategory);

            if (relationshipEvent.EntityID > 0)
                result.Entity = ToEntityWrapper(relationshipEvent.EntityType, relationshipEvent.EntityID);

            result.Files = DaoFactory.GetRelationshipEventDao().GetFiles(relationshipEvent.ID).ConvertAll(file => new FileWrapper(file));

            if (relationshipEvent.ContactID > 0)
            {
                var relativeContact = DaoFactory.GetContactDao().GetByID(relationshipEvent.ContactID);

                if (relativeContact != null)
                    result.Contact = ToContactBaseWrapper(relativeContact);
            }

            result.CanEdit = CRMSecurity.CanAccessTo(relationshipEvent);

            return result;
        }
        private IEnumerable<RelationshipEventWrapper> ToListRelationshipEventWrapper(List<RelationshipEvent> itemList)
        {

            if (itemList.Count == 0) return new List<RelationshipEventWrapper>();

            var result = new List<RelationshipEventWrapper>();

            var contactIDs = new List<int>();
            var eventIDs = new List<int>();
            var categoryIDs = new List<int>();
            var entityWrappersIDs = new Dictionary<EntityType, List<int>>();


            foreach (var item in itemList)
            {
                eventIDs.Add(item.ID);

                if (!categoryIDs.Contains(item.CategoryID))
                    categoryIDs.Add(item.CategoryID);

                if (item.ContactID > 0 && !contactIDs.Contains(item.ContactID))
                    contactIDs.Add(item.ContactID);

                if (item.EntityID > 0)
                {
                    if (!entityWrappersIDs.ContainsKey(item.EntityType))
                        entityWrappersIDs.Add(item.EntityType, new List<int>
                                                                   {
                                                                       item.EntityID
                                                                   });
                    else if (!entityWrappersIDs[item.EntityType].Contains(item.EntityID))
                        entityWrappersIDs[item.EntityType].Add(item.EntityID);
                }
            }

            var entityWrappers = new Dictionary<String, EntityWrapper>();

            foreach (EntityType entityType in entityWrappersIDs.Keys)
            {

                switch (entityType)
                {
                    case EntityType.Opportunity:
                        DaoFactory.GetDealDao().GetByID(entityWrappersIDs[entityType].Distinct().ToArray())
                            .ForEach(item =>
                                         {

                                             if (item == null) return;

                                             entityWrappers.Add(
                                                 String.Format("{0}_{1}", (int)entityType, item.ID),
                                                 new EntityWrapper
                                                     {
                                                         EntityId = item.ID,
                                                         EntityTitle = item.Title,
                                                         EntityType = "opportunity"
                                                     });
                                         });
                        break;
                    case EntityType.Case:
                        DaoFactory.GetCasesDao().GetByID(entityWrappersIDs[entityType].ToArray())
                            .ForEach(item =>
                                         {

                                             if (item == null) return;

                                             entityWrappers.Add(
                                                 String.Format("{0}_{1}", (int)entityType, item.ID),
                                                 new EntityWrapper
                                                     {
                                                         EntityId = item.ID,
                                                         EntityTitle = item.Title,
                                                         EntityType = "case"
                                                     });
                                         });
                        break;
                    default:
                        throw new ArgumentException();
                }
            }

            var categories = DaoFactory.GetListItemDao().GetItems(categoryIDs.ToArray()).ToDictionary(x => x.ID, x => ToHistoryCategoryWrapper(x));

            var historyCategoryItemsCount = DaoFactory.GetListItemDao().GetRelativeItemsCount(ListType.HistoryCategory);

            foreach (var x in historyCategoryItemsCount)
            {
                
                if (categories.ContainsKey(x.Key))
                    categories[x.Key].RelativeItemsCount = x.Value;

            }


            var files = DaoFactory.GetRelationshipEventDao().GetFiles(eventIDs.ToArray());

            var contacts = DaoFactory.GetContactDao().GetContacts(contactIDs.ToArray()).ToDictionary(item => item.ID,
                                                                                                     item =>
                                                                                                     ToContactBaseWrapper(item));

            foreach (var item in itemList)
            {
                var eventObjWrap = new RelationshipEventWrapper(item);

                if (contacts.ContainsKey(item.ContactID))
                    eventObjWrap.Contact = contacts[item.ContactID];

                if (item.EntityID > 0)
                {

                    var entityStrKey = String.Format("{0}_{1}", (int)item.EntityType, item.EntityID);

                    if (entityWrappers.ContainsKey(entityStrKey))
                        eventObjWrap.Entity = entityWrappers[entityStrKey];

                }

                if (files.ContainsKey(item.ID))
                    eventObjWrap.Files = files[item.ID].ConvertAll(file => new FileWrapper(file));
                else
                    eventObjWrap.Files = new List<FileWrapper>();

                if (categories.ContainsKey(item.CategoryID))
                    eventObjWrap.Category = categories[item.CategoryID];


                result.Add(eventObjWrap);

            }

            return result;
        }