Beispiel #1
0
        public bool CanRead(FileEntry file, Guid userId)
        {
            using (var scope = DIHelper.Resolve())
            {
                var daoFactory = scope.Resolve <DaoFactory>();
                var invoice    = daoFactory.InvoiceDao.GetByFileId(Convert.ToInt32(file.ID));
                if (invoice != null)
                {
                    return(CRMSecurity.CanAccessTo(invoice, userId));
                }

                var reportFile = daoFactory.ReportDao.GetFile(Convert.ToInt32(file.ID), userId);
                if (reportFile != null)
                {
                    return(true);
                }

                var eventIds = FilesIntegration.GetTagDao().GetTags(file.ID, FileEntryType.File, TagType.System)
                               .Where(x => x.TagName.StartsWith("RelationshipEvent_"))
                               .Select(x => Convert.ToInt32(x.TagName.Split(new[] { '_' })[1]))
                               .ToList();

                if (!eventIds.Any())
                {
                    return(false);
                }

                var eventItem = daoFactory.RelationshipEventDao.GetByID(eventIds.First());
                return(CRMSecurity.CanAccessTo(eventItem, userId));
            }
        }
        public bool CanRead(FileEntry file, Guid userId)
        {
            var eventDao   = Global.DaoFactory.GetRelationshipEventDao();
            var tagDao     = FilesIntegration.GetTagDao();
            var invoiceDao = Global.DaoFactory.GetInvoiceDao();

            var invoice = invoiceDao.GetByFileId(Convert.ToInt32(file.ID));

            if (invoice != null)
            {
                return(CRMSecurity.CanAccessTo(invoice));
            }
            else
            {
                var eventIds = tagDao.GetTags(file.ID, FileEntryType.File, TagType.System)
                               .Where(x => x.TagName.StartsWith("RelationshipEvent_"))
                               .Select(x => Convert.ToInt32(x.TagName.Split(new[] { '_' })[1]));

                if (!eventIds.Any())
                {
                    return(false);
                }

                var eventItem = eventDao.GetByID(eventIds.First());
                return(CRMSecurity.CanAccessTo(eventItem));
            }
        }
        public void AttachFile(ProjectEntity entity, object fileId, bool notify)
        {
            if (!ProjectSecurity.CanReadFiles(entity.Project))
            {
                return;
            }

            File file;

            using (var dao = FilesIntegration.GetTagDao())
            {
                dao.SaveTags(new Tag(entity.GetType().Name + entity.ID, TagType.System, Guid.Empty)
                {
                    EntryType = FileEntryType.File, EntryId = fileId
                });
                file = FileEngine.GetFile(fileId, 0);
                FileEngine.GenerateImageThumb(file);
            }

            if (notify && !Factory.DisableNotifications)
            {
                var senders = GetSubscribers(entity);
                NotifyClient.Instance.SendNewFile(senders, entity, file.Title);
            }
        }
        public List <int> RemoveFile(File file)
        {
            CRMSecurity.DemandDelete(file);

            List <int> eventIDs;

            using (var tagdao = FilesIntegration.GetTagDao())
            {
                var tags = tagdao.GetTags(file.ID, FileEntryType.File, TagType.System).ToList().FindAll(tag => tag.TagName.StartsWith("RelationshipEvent_"));
                eventIDs = tags.Select(item => Convert.ToInt32(item.TagName.Split(new[] { '_' })[1])).ToList();
            }
            using (var dao = FilesIntegration.GetFileDao())
            {
                dao.DeleteFile(file.ID);
            }

            foreach (var eventID in eventIDs)
            {
                if (GetFiles(eventID).Count == 0)
                {
                    Db.ExecuteNonQuery(Update("crm_relationship_event").Set("have_files", false).Where("id", eventID));
                }
            }

            Db.ExecuteNonQuery(Update("crm_invoice").Set("file_id", 0).Where("file_id", file.ID));

            return(eventIDs);
        }
        private object[] GetFilesIDs(int[] contactID, EntityType entityType, int entityID)
        {
            if (entityID > 0 && entityType != EntityType.Opportunity && entityType != EntityType.Case)
            {
                throw new ArgumentException();
            }


            var sqlQuery = Query("crm_relationship_event").Select("id");

            if (contactID != null && contactID.Length > 0)
            {
                sqlQuery.Where(Exp.In("contact_id", contactID));
            }

            if (entityID > 0)
            {
                sqlQuery.Where(Exp.Eq("entity_id", entityID) & Exp.Eq("entity_type", (int)entityType));
            }

            sqlQuery.Where(Exp.Eq("have_files", true));

            var tagNames = Db.ExecuteList(sqlQuery).ConvertAll(row => String.Format("RelationshipEvent_{0}", row[0]));

            using (var tagdao = FilesIntegration.GetTagDao())
            {
                return(tagdao.GetTags(tagNames.ToArray(), TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray());
            }
        }
        public void AttachFiles(int eventID, int[] fileIDs)
        {
            if (fileIDs.Length == 0)
            {
                return;
            }

            using (var dao = FilesIntegration.GetTagDao())
            {
                var tags = fileIDs.ToList().ConvertAll(fileID => new Tag("RelationshipEvent_" + eventID, TagType.System, Guid.Empty)
                {
                    EntryType = FileEntryType.File, EntryId = fileID
                });

                dao.SaveTags(tags.ToArray());
            }

            if (fileIDs.Length > 0)
            {
                using (var db = GetDb())
                {
                    db.ExecuteNonQuery(Update("crm_relationship_event").Set("have_files", true).Where("id", eventID));
                }
            }
        }
Beispiel #7
0
        public IEnumerable <File> GetFiles(ProjectEntity entity)
        {
            if (entity == null)
            {
                return(new List <File>());
            }

            if (!ProjectSecurity.CanReadFiles(entity.Project))
            {
                return(new List <File>());
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var ids   = tagdao.GetTags(entity.GetType().Name + entity.ID, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();
                    var files = 0 < ids.Length ? filedao.GetFiles(ids) : new List <File>();

                    var rootId = FileEngine.GetRoot(entity.Project.ID);

                    //delete tags when file moved from project folder
                    files.Where(file => !file.RootFolderId.Equals(rootId)).ToList()
                    .ForEach(file =>
                    {
                        DetachFile(entity, file.ID);
                        files.Remove(file);
                    });

                    files.ForEach(r => r.Access = FileEngine.GetFileShare(r, entity.Project.ID));
                    return(files);
                }
        }
        public Dictionary <int, List <File> > GetFiles(int[] eventID)
        {
            if (eventID == null || eventID.Length == 0)
            {
                throw new ArgumentException("eventID");
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var findedTags = tagdao.GetTags(eventID.Select(item => String.Concat("RelationshipEvent_", item)).ToArray(),
                                                    TagType.System).Where(t => t.EntryType == FileEntryType.File);

                    var filesID = findedTags.Select(t => t.EntryId).ToArray();

                    var files = 0 < filesID.Length ? filedao.GetFiles(filesID) : new List <File>();

                    var filesTemp = new Dictionary <object, File>();

                    files.ForEach(item =>
                    {
                        if (!filesTemp.ContainsKey(item.ID))
                        {
                            filesTemp.Add(item.ID, item);
                        }
                    });

                    return(findedTags.Where(x => filesTemp.ContainsKey(x.EntryId)).GroupBy(x => x.TagName).ToDictionary(x => Convert.ToInt32(x.Key.Split(new[] { '_' })[1]),
                                                                                                                        x => x.Select(item => filesTemp[item.EntryId]).ToList()));
                }
        }
Beispiel #9
0
 public List <int> GetEventsByFile(int id)
 {
     using (var tagdao = FilesIntegration.GetTagDao())
     {
         var tags = tagdao.GetTags(id, FileEntryType.File, TagType.System).ToList().FindAll(tag => tag.TagName.StartsWith("RelationshipEvent_"));
         return(tags.Select(item => Convert.ToInt32(item.TagName.Split(new[] { '_' })[1])).ToList());
     }
 }
Beispiel #10
0
        public void DeleteBatchDeals(List <Deal> deals)
        {
            if (deals.Count == 0)
            {
                return;
            }

            var dealID = deals.Where(CRMSecurity.CanAccessTo).Select(x => x.ID).ToArray();

            // Delete relative  keys
            _cache.Insert(_dealCacheKey, String.Empty);


            using (var db = GetDb())
            {
                var tagNames = db.ExecuteList(Query("crm_relationship_event").Select("id").Where(Exp.Eq("have_files", true) & Exp.In("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)))
                               .Select(row => String.Format("RelationshipEvent{0}", row[0])).ToArray();

                using (var tx = db.BeginTransaction(true))
                {
                    db.ExecuteNonQuery(Delete("crm_field_value").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)));

                    db.ExecuteNonQuery(new SqlDelete("crm_entity_contact")
                                       .Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                    db.ExecuteNonQuery(Delete("crm_relationship_event")
                                       .Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                    db.ExecuteNonQuery(Delete("crm_task")
                                       .Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                    db.ExecuteNonQuery(new SqlDelete("crm_entity_tag").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                    db.ExecuteNonQuery(Delete("crm_deal").Where(Exp.In("id", dealID)));

                    tx.Commit();
                }

                deals.ForEach(deal => CoreContext.AuthorizationManager.RemoveAllAces(deal));

                if (tagNames.Length == 0)
                {
                    return;
                }

                using (var tagdao = FilesIntegration.GetTagDao())
                    using (var filedao = FilesIntegration.GetFileDao())
                    {
                        var filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                        foreach (var filesID in filesIDs)
                        {
                            filedao.DeleteFolder(filesID);
                            filedao.DeleteFile(filesID);
                        }
                    }
            }
        }
Beispiel #11
0
 public void DetachFileFromEntity(EntityType entityType, int entityId, object fileId)
 {
     using (var dao = FilesIntegration.GetTagDao())
     {
         dao.RemoveTags(new Tag(entityType.ToString() + entityId, TagType.System, Guid.Empty)
         {
             EntryType = FileEntryType.File, EntryId = fileId
         });
     }
 }
Beispiel #12
0
 public void DetachFileFromTask(int taskId, int fileId)
 {
     using (var dao = FilesIntegration.GetTagDao())
     {
         dao.RemoveTags(new Tag("Task" + taskId, TagType.System, Guid.Empty)
         {
             EntryType = FileEntryType.File, EntryId = fileId
         });
     }
 }
Beispiel #13
0
        public virtual void DeleteBatchCases(int[] casesID)
        {
            if (casesID == null || !casesID.Any())
            {
                return;
            }

            var cases = GetCases(casesID).Where(CRMSecurity.CanAccessTo).ToList();

            //// Delete relative  keys
            _cache.Insert(_caseCacheKey, String.Empty);

            var tagNames = DbManager.ExecuteList(Query("crm_relationship_event").Select("id")
                                                 .Where(Exp.Eq("have_files", true) &
                                                        Exp.In("entity_id", casesID) &
                                                        Exp.Eq("entity_type", (int)EntityType.Case)))
                           .Select(row => String.Format("RelationshipEvent{0}", row[0])).ToArray();

            using (var tx = DbManager.BeginTransaction(true))
            {
                DbManager.ExecuteNonQuery(Delete("crm_field_value")
                                          .Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));

                DbManager.ExecuteNonQuery(Delete("crm_relationship_event")
                                          .Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));

                DbManager.ExecuteNonQuery(Delete("crm_task")
                                          .Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));

                DbManager.ExecuteNonQuery(new SqlDelete("crm_entity_tag").Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));

                DbManager.ExecuteNonQuery(Delete("crm_case").Where(Exp.In("id", casesID)));

                tx.Commit();
            }

            cases.ForEach(item => CoreContext.AuthorizationManager.RemoveAllAces(item));

            if (tagNames.Length == 0)
            {
                return;
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                    foreach (var filesID in filesIDs)
                    {
                        filedao.DeleteFolder(filesID);
                        filedao.DeleteFile(filesID);
                    }
                }
        }
Beispiel #14
0
        public virtual void DeleteDeal(int dealID)
        {
            var deal = GetByID(dealID);

            CRMSecurity.DemandAccessTo(deal);

            // Delete relative  keys
            _cache.Insert(_dealCacheKey, String.Empty);


            var tagNames = DbManager.ExecuteList(Query("crm_relationship_event").Select("id").Where(Exp.Eq("have_files", true) & Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)))
                           .Select(row => String.Format("RelationshipEvent{0}", row[0])).ToArray();

            using (var tx = DbManager.BeginTransaction(true))
            {
                DbManager.ExecuteNonQuery(Delete("crm_field_value").Where(Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)));

                DbManager.ExecuteNonQuery(new SqlDelete("crm_entity_contact")
                                          .Where(Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                DbManager.ExecuteNonQuery(Delete("crm_relationship_event")
                                          .Where(Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                DbManager.ExecuteNonQuery(Delete("crm_task")
                                          .Where(Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                DbManager.ExecuteNonQuery(new SqlDelete("crm_entity_tag").Where(Exp.Eq("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));

                DbManager.ExecuteNonQuery(Delete("crm_deal").Where(Exp.Eq("id", dealID)));

                tx.Commit();
            }

            CoreContext.AuthorizationManager.RemoveAllAces(deal);

            if (tagNames.Length == 0)
            {
                return;
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                    var store = FilesIntegration.GetStore();

                    foreach (var filesID in filesIDs)
                    {
                        filedao.DeleteFolder(filesID);
                        filedao.DeleteFile(filesID);
                    }
                }
        }
Beispiel #15
0
 public void AttachFileToMessage(int messageId, object fileId)
 {
     using (var dao = FilesIntegration.GetTagDao())
     {
         dao.SaveTags(new Tag("Message" + messageId, TagType.System, Guid.Empty)
         {
             EntryType = FileEntryType.File, EntryId = fileId
         });
         GenerateImageThumb(GetFile(fileId, 0));
     }
 }
Beispiel #16
0
 public void AttachFileToEntity(EntityType entityType, int entityId, object fileId)
 {
     using (var dao = FilesIntegration.GetTagDao())
     {
         dao.SaveTags(new Tag(entityType.ToString() + entityId, TagType.System, Guid.Empty)
         {
             EntryType = FileEntryType.File, EntryId = fileId
         });
         GenerateImageThumb(GetFile(fileId, 0));
     }
 }
Beispiel #17
0
 public void AttachFileToTask(int taskId, int fileId)
 {
     using (var dao = FilesIntegration.GetTagDao())
     {
         dao.SaveTags(new Tag("Task" + taskId, TagType.System, Guid.Empty)
         {
             EntryType = FileEntryType.File, EntryId = fileId
         });
         GenerateImageThumb(GetFile(fileId, 0));
     }
 }
Beispiel #18
0
        public void DetachFile(ProjectEntity entity, object fileId)
        {
            if (!ProjectSecurity.CanReadFiles(entity.Project))
            {
                return;
            }

            using (var dao = FilesIntegration.GetTagDao())
            {
                dao.RemoveTags(new Tag(entity.GetType().Name + entity.ID, TagType.System, Guid.Empty)
                {
                    EntryType = FileEntryType.File, EntryId = fileId
                });
            }
        }
Beispiel #19
0
 public List <File> GetMessageFiles(Message message)
 {
     if (message == null)
     {
         return(new List <File>());
     }
     using (var tagdao = FilesIntegration.GetTagDao())
         using (var filedao = FilesIntegration.GetFileDao())
         {
             var ids   = tagdao.GetTags("Message" + message.ID, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();
             var files = 0 < ids.Length ? filedao.GetFiles(ids) : new List <File>();
             files.ForEach(r => r.Access = GetFileShare(r, message.Project.ID));
             SetThumbUrls(files);
             return(files);
         }
 }
Beispiel #20
0
        public List <File> GetEntityFiles(ProjectEntity entity)
        {
            if (entity == null)
            {
                return(new List <File>());
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var ids   = tagdao.GetTags(entity.GetType().Name + entity.ID, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();
                    var files = 0 < ids.Length ? filedao.GetFiles(ids) : new List <File>();
                    files.ForEach(r => r.Access = GetFileShare(r, entity.Project.ID));
                    SetThumbUrls(files);
                    return(files);
                }
        }
        public List <File> GetFiles(int eventID)
        {
            if (eventID == 0)
            {
                throw new ArgumentException("eventID");
            }

            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var ids   = tagdao.GetTags(String.Concat("RelationshipEvent_", eventID), TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();
                    var files = 0 < ids.Length ? filedao.GetFiles(ids) : new List <File>();

                    files.ForEach(CRMSecurity.SetAccessTo);

                    return(files.ToList());
                }
        }
        private bool CanReadFile(FileEntry entry, Guid userId)
        {
            if (entry.FileEntryType == FileEntryType.Folder)
            {
                return(false);
            }

            if (!CRMSecurity.IsAvailableForUser(userId))
            {
                return(false);
            }

            using (var scope = DIHelper.Resolve())
            {
                var daoFactory = scope.Resolve <DaoFactory>();
                var invoice    = daoFactory.InvoiceDao.GetByFileId(Convert.ToInt32(entry.ID));
                if (invoice != null)
                {
                    return(CRMSecurity.CanAccessTo(invoice, userId));
                }

                var reportFile = daoFactory.ReportDao.GetFile(Convert.ToInt32(entry.ID), userId);
                if (reportFile != null)
                {
                    return(CRMSecurity.IsAdministrator(userId));
                }

                using (var tagDao = FilesIntegration.GetTagDao())
                {
                    var eventIds = tagDao.GetTags(entry.ID, FileEntryType.File, TagType.System)
                                   .Where(x => x.TagName.StartsWith("RelationshipEvent_"))
                                   .Select(x => Convert.ToInt32(x.TagName.Split(new[] { '_' })[1]))
                                   .ToList();

                    if (!eventIds.Any())
                    {
                        return(false);
                    }

                    var eventItem = daoFactory.RelationshipEventDao.GetByID(eventIds.First());
                    return(CRMSecurity.CanAccessTo(eventItem, userId));
                }
            }
        }
Beispiel #23
0
        private void DeleteBatchDealsExecute(List <Deal> deals)
        {
            if (deals == null || !deals.Any())
            {
                return;
            }

            var dealID = deals.Select(x => x.ID).ToArray();

            object[] filesIDs;

            using (var db = GetDb())
                using (var tagdao = FilesIntegration.GetTagDao())
                {
                    var tagNames = db.ExecuteList(Query("crm_relationship_event").Select("id")
                                                  .Where(Exp.Eq("have_files", true) & Exp.In("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)))
                                   .Select(row => String.Format("RelationshipEvent_{0}", row[0])).ToArray();
                    filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                    using (var tx = db.BeginTransaction(true))
                    {
                        db.ExecuteNonQuery(Delete("crm_field_value").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", (int)EntityType.Opportunity)));
                        db.ExecuteNonQuery(new SqlDelete("crm_entity_contact").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));
                        db.ExecuteNonQuery(Delete("crm_relationship_event").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));
                        db.ExecuteNonQuery(Delete("crm_task").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));
                        db.ExecuteNonQuery(new SqlDelete("crm_entity_tag").Where(Exp.In("entity_id", dealID) & Exp.Eq("entity_type", EntityType.Opportunity)));
                        db.ExecuteNonQuery(Delete("crm_deal").Where(Exp.In("id", dealID)));

                        tx.Commit();
                    }

                    deals.ForEach(deal => CoreContext.AuthorizationManager.RemoveAllAces(deal));
                }

            using (var filedao = FilesIntegration.GetFileDao())
            {
                foreach (var filesID in filesIDs)
                {
                    filedao.DeleteFolder(filesID);
                    filedao.DeleteFile(filesID);
                }
            }
        }
Beispiel #24
0
        private void DeleteBatchCasesExecute(List <Cases> caseses)
        {
            var casesID = caseses.Select(x => x.ID).ToArray();

            using (var db = GetDb())
                using (var tagdao = FilesIntegration.GetTagDao())
                {
                    var tagNames = db.ExecuteList(Query("crm_relationship_event").Select("id")
                                                  .Where(Exp.Eq("have_files", true) & Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)))
                                   .Select(row => String.Format("RelationshipEvent_{0}", row[0])).ToArray();
                    var filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                    using (var tx = db.BeginTransaction(true))
                    {
                        db.ExecuteNonQuery(Delete("crm_field_value").Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));
                        db.ExecuteNonQuery(Delete("crm_relationship_event").Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));
                        db.ExecuteNonQuery(Delete("crm_task").Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));
                        db.ExecuteNonQuery(new SqlDelete("crm_entity_tag").Where(Exp.In("entity_id", casesID) & Exp.Eq("entity_type", (int)EntityType.Case)));
                        db.ExecuteNonQuery(Delete("crm_case").Where(Exp.In("id", casesID)));

                        tx.Commit();
                    }

                    caseses.ForEach(item => CoreContext.AuthorizationManager.RemoveAllAces(item));

                    if (0 < tagNames.Length)
                    {
                        using (var filedao = FilesIntegration.GetFileDao())
                        {
                            foreach (var filesID in filesIDs)
                            {
                                filedao.DeleteFolder(filesID);
                                filedao.DeleteFile(filesID);
                            }
                        }
                    }
                }
        }
Beispiel #25
0
        public virtual void DeleteBatchContact(int[] contactID)
        {
            if (contactID == null || contactID.Length == 0)
            {
                return;
            }

            // Delete relative  keys
            _cache.Insert(_contactCacheKey, String.Empty);

            var contacts = GetContacts(contactID).Where(CRMSecurity.CanAccessTo).ToList();

            if (contacts.Count == 0)
            {
                return;
            }

            var personsID    = new List <int>();
            var companyID    = new List <int>();
            var newContactID = new List <int>();

            foreach (var contact in contacts)
            {
                newContactID.Add(contact.ID);

                if (contact is Company)
                {
                    companyID.Add(contact.ID);
                }
                else
                {
                    personsID.Add(contact.ID);
                }
            }

            contactID = newContactID.ToArray();

            using (var tx = DbManager.BeginTransaction())
            {
                DbManager.ExecuteNonQuery(Delete("crm_field_value").Where(Exp.In("entity_id", contactID)
                                                                          & Exp.In("entity_type", new[] { (int)EntityType.Contact, (int)EntityType.Person, (int)EntityType.Company })));

                DbManager.ExecuteNonQuery(Delete("crm_task").Where(Exp.In("contact_id", contactID)));
                DbManager.ExecuteNonQuery(new SqlDelete("crm_entity_tag")
                                          .Where(Exp.In("entity_id", contactID) & Exp.Eq("entity_type", (int)EntityType.Contact)));

                DbManager.ExecuteNonQuery(Delete("crm_relationship_event").Where(Exp.In("contact_id", contactID)));
                DbManager.ExecuteNonQuery(Update("crm_deal").Set("contact_id", 0).Where(Exp.In("contact_id", contactID)));

                if (companyID.Count > 0)
                {
                    DbManager.ExecuteNonQuery(Update("crm_contact").Set("company_id", 0).Where(Exp.In("company_id", companyID)));
                }

                if (personsID.Count > 0)
                {
                    RemoveRelative(null, EntityType.Person, personsID.ToArray());
                }

                RemoveRelative(contactID, EntityType.Any, null);

                DbManager.ExecuteNonQuery(Delete("crm_contact_info").Where(Exp.In("contact_id", contactID)));

                DbManager.ExecuteNonQuery(Delete("crm_contact").Where(Exp.In("id", contactID)));

                tx.Commit();
            }

            contacts.ForEach(contact => CoreContext.AuthorizationManager.RemoveAllAces(contact));


            using (var tagdao = FilesIntegration.GetTagDao())
                using (var filedao = FilesIntegration.GetFileDao())
                {
                    var tagNames = DbManager.ExecuteList(Query("crm_relationship_event").Select("id").Where(Exp.In("contact_id", contactID) & Exp.Eq("have_files", true)))
                                   .Select(row => String.Format("RelationshipEvent{0}", row[0])).ToArray();

                    if (tagNames.Length == 0)
                    {
                        return;
                    }

                    var filesIDs = tagdao.GetTags(tagNames, TagType.System).Where(t => t.EntryType == FileEntryType.File).Select(t => t.EntryId).ToArray();

                    var store = FilesIntegration.GetStore();

                    foreach (var filesID in filesIDs)
                    {
                        filedao.DeleteFolder(filesID);
                        filedao.DeleteFile(filesID);
                    }
                }
        }