public Boolean AttachmentEditVisibility(Int64 idAttachment, Boolean hide)
        {
            Domain.TicketFile attachment = Manager.Get <Domain.TicketFile>(idAttachment);
            if (attachment == null)
            {
                return(false);
            }
            if (attachment.Visibility == Domain.Enums.FileVisibility.hiddenMessage)
            {
                return(false);
            }

            attachment.Visibility = (hide) ?
                                    Domain.Enums.FileVisibility.hidden :
                                    Domain.Enums.FileVisibility.visible;

            attachment.UpdateMetaInfo(CurrentLitePerson, UC.IpAddress, UC.ProxyIpAddress);

            Manager.SaveOrUpdate(attachment);
            return(true);
        }
        // VERIFICARE: QUALI SONO LE ACTION CHE SERVONO IN GENERALE E QUALI SONO QUELLE CHE SERVONO AI TICKET NELLO SPECIFICO?
        public List <StandardActionType> GetAllowedStandardAction(ModuleObject source, ModuleObject destination, int idUser, int idRole, int idCommunity, Dictionary <string, long> moduleUserLong = null, Dictionary <string, string> moduleUserString = null)
        {
            TK.TicketUser usr;

            List <StandardActionType> actions = new List <StandardActionType>();

            //Verificare, eventualmente aggiungere controllo sul tipo.
            TK.Message msg = Manager.Get <TK.Message>(source.ObjectLongID);

            if (moduleUserLong != null && moduleUserLong.ContainsKey(ModuleTicket.UniqueCode))
            {
                usr = this.UserGet(moduleUserLong[ModuleTicket.UniqueCode]);
            }
            else
            {
                litePerson person = Manager.Get <litePerson>(idUser);
                usr = this.UserGetfromPerson(person.Id);
            }



            TK.TicketFile attach = (from TK.TicketFile a in msg.Attachments
                                    where a.Item != null && a.Item.Id == destination.ObjectLongID
                                    select a).Skip(0).Take(1).ToList().FirstOrDefault();

            //CONTROLLARE: in teoria SOLO x download...
            if (idCommunity <= 0)
            {
                idCommunity = destination.CommunityID;
            }


            //---------------         Begin DEBUG        ------------------
            //Debug.WriteLine("IdUser: "******"idRole: " + idRole.ToString());
            //Debug.WriteLine("idCommunity: " + idCommunity.ToString());
            //if(source != null)
            //{
            //    Debug.WriteLine("source - Com ID: " + source.CommunityID.ToString());
            //    Debug.WriteLine("source - Obj ID: " + source.ObjectLongID.ToString());
            //    Debug.WriteLine("source - Obj Type: " + source.ObjectTypeID.ToString());
            //}
            //else
            //{
            //    Debug.WriteLine("source: NULL!");
            //}
            //if (destination != null)
            //{
            //    Debug.WriteLine("destination - Com Id: " + destination.CommunityID.ToString());
            //    Debug.WriteLine("destination - Obj ID: " + destination.ObjectLongID.ToString());
            //    Debug.WriteLine("destination - Obj Type: " + destination.ObjectTypeID.ToString());
            //}
            //else
            //{
            //    Debug.WriteLine("destination: NULL!");
            //}
            //if(person == null)
            //    Debug.WriteLine("person: NULL");
            //if (usr == null)
            //    Debug.WriteLine("usr: NULL");
            //if(msg == null)
            //    Debug.WriteLine("msg: ");
            //else
            //{
            //    if(msg.Attachments == null)
            //        Debug.WriteLine("msg: Attachments = NULL!" );
            //    else
            //    {
            //        Debug.WriteLine("msg: Attachments = " + msg.Attachments.Count());
            //        foreach(TK.TicketFile fl in msg.Attachments)
            //        {
            //            Debug.WriteLine("msg - Attachments TkFile: " + fl.Id + " - " + fl.Name);
            //            if(fl.File != null)
            //            {
            //                Debug.WriteLine("msg - Attachments File: " + fl.File.Id + " - " + fl.File.Name);
            //            }
            //            Debug.WriteLine("");
            //        }
            //    }
            //}
            //if (attach == null)
            //    Debug.WriteLine("attach: NULL");


            //---------------         END DEBUG        ------------------


            //SE non esiste messaggio, utente o allegati: non ho permessi.
            if (msg == null || usr == null || attach == null)
            {
                return(actions);
            }

            //Se sono PROPRIETARIO del messaggio: ho TUTTI i permessi!
            if (msg.Creator != null && msg.Creator.Id == usr.Id)
            {
                //SE il messaggio è in DRAFT, posso anche cancellare il file!
                if (msg.IsDraft)
                {
                    actions.Add(StandardActionType.Delete);
                }

                //SONO creatore del messaggio in DRAFT, comunque ho TUTTI i permessi.
                actions.Add(StandardActionType.EditMetadata);
                actions.Add(StandardActionType.Play);
                actions.Add(StandardActionType.DownloadItem);
                actions.Add(StandardActionType.ViewPersonalStatistics);
                actions.Add(StandardActionType.ViewAdvancedStatistics);

                actions.Add(StandardActionType.ViewDescription);
                actions.Add(StandardActionType.ViewPreview);

                //return actions;
            }
            else if (!msg.IsDraft && usr.Person != null && msg.Ticket.CreatedBy.Id == usr.Person.Id)
            //SONO il CREATORE del Ticket
            {
                actions.Add(StandardActionType.EditMetadata);
                actions.Add(StandardActionType.Play);
                actions.Add(StandardActionType.DownloadItem);
                actions.Add(StandardActionType.ViewPersonalStatistics);
                actions.Add(StandardActionType.ViewAdvancedStatistics);

                actions.Add(StandardActionType.ViewDescription);
                actions.Add(StandardActionType.ViewPreview);
            }
            else if (!msg.IsDraft && this.UserHasManResTicketPermission(msg.Ticket.Id, usr.Id))
            //SONO un manager/REsolver del Ticket
            {
                actions.Add(StandardActionType.EditMetadata);
                actions.Add(StandardActionType.Play);
                actions.Add(StandardActionType.DownloadItem);
                actions.Add(StandardActionType.ViewPersonalStatistics);
                actions.Add(StandardActionType.ViewAdvancedStatistics);

                actions.Add(StandardActionType.ViewDescription);
                actions.Add(StandardActionType.ViewPreview);
            }

            if (actions.Count() <= 0 && (msg.Ticket.Owner.Id == usr.Id && msg.Visibility && attach.Visibility == TK.Enums.FileVisibility.visible))
            {
                //Se sono MANAGER/RESOLVER ho tutti i permessi

                //SE sono associato al Ticket o ad una categoria associata al ticket (sono manager o resolver del ticket),
                //ho tutti i permessi sul file allegato al messaggio?
                //actions.Add(StandardActionType.EditMetadata);
                actions.Add(StandardActionType.Play);
                actions.Add(StandardActionType.DownloadItem);
                //actions.Add(StandardActionType.ViewPersonalStatistics);
                //actions.Add(StandardActionType.ViewAdvancedStatistics);

                actions.Add(StandardActionType.ViewDescription);
                actions.Add(StandardActionType.ViewPreview);
            }

            //else
            //{
            //    //Non sono MANAGER/RESOLVER, nè proprietario del messaggio: non ho permessi!!!
            //    //if(msg.Visibility == true && attach.Visibility == TK.Enums.FileVisibility.visible)
            //    //{
            //    //    //Il messaggio ED il FILE sono VISIBILI!

            //    //    //actions.Add(StandardActionType.EditMetadata);
            //    //    //actions.Add(StandardActionType.Play);
            //    //    //actions.Add(StandardActionType.DownloadItem);
            //    //    //actions.Add(StandardActionType.ViewPersonalStatistics);
            //    //    //actions.Add(StandardActionType.ViewAdvancedStatistics);

            //    //    //actions.Add(StandardActionType.ViewDescription);
            //    //    //actions.Add(StandardActionType.ViewPreview);
            //    //}
            //}

            return(actions);
        }
        public Boolean AttachmentDelete(Int64 idAttachment, Int64 TkUserId, String baseFilePath, String baseThumbnailPath)
        {
            Boolean deleted = true;

            if (idAttachment < 0)
            {
                return(false);
            }
            try
            {
                Manager.BeginTransaction();
                Domain.TicketFile attachment = Manager.Get <Domain.TicketFile>(idAttachment);
                if (attachment == null)
                {
                    return(true);
                }
                if (attachment.Message == null || attachment.Message.Ticket == null ||
                    !(attachment.Message.IsDraft || attachment.Message.Ticket.IsDraft) ||
                    attachment.Message.Creator == null ||
                    attachment.Message.Creator.Id != TkUserId)
                {
                    return(false);
                }

                List <String> filesToRemove = new List <string>();

                if (attachment.Item != null && attachment.Item.IsInternal)
                {
                    filesToRemove.Add(ServiceRepository.GetItemDiskFullPath(baseFilePath, attachment.Item));
                    filesToRemove.Add(ServiceRepository.GetItemThumbnailFullPath(baseThumbnailPath, attachment.Item));
                    Manager.DeletePhysical(attachment.Item);
                    if (attachment.Version != null)
                    {
                        Manager.DeletePhysical(attachment.Version);
                    }

                    if (attachment.Link != null)
                    {
                        Manager.DeletePhysical(attachment.Link);
                    }
                }
                else if (attachment.Link != null)
                {
                    Manager.DeletePhysical(attachment.Link);
                }

                Manager.DeletePhysical(attachment);
                Manager.Commit();

                if (filesToRemove.Any(f => !String.IsNullOrEmpty(f)))
                {
                    lm.Comol.Core.File.Delete.Files(filesToRemove.Where(f => !String.IsNullOrEmpty(f)).ToList());
                }
            }
            catch (Exception ex)
            {
                deleted = false;
                if (Manager.IsInTransaction())
                {
                    Manager.RollBack();
                }
            }
            return(deleted);
        }