Пример #1
0
        public void RemoveLabelsForLink(long userId, long linkId)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.UserProfile.userId != userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, linkId);
            }

            List <Label> removableLabels = new List <Label>();

            foreach (Label label in link.Labels)
            {
                if (label.Links.Contains(link))
                {
                    removableLabels.Add(label);
                }
            }

            foreach (Label label in removableLabels)
            {
                label.Links.Remove(link);
                link.Labels.Remove(label);

                LinkDao.Update(link);

                if (label.Links.Count > 0)
                {
                    try
                    {
                        LabelDao.Update(label);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
                else
                {
                    try
                    {
                        LabelDao.Remove(label.labelId);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
            }
        }
Пример #2
0
        public void SetLabelsForLink(long userId, long linkId, List <string> labelTexts)
        {
            if (!UserDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.userId != userId)
            {
                throw new UserNotAuthorizedException <LinkDetails>(userId, "linkId", linkId);
            }

            List <Label> labelsToSet = new List <Label>();

            foreach (string labelText in labelTexts.Distinct())
            {
                if (labelText != null)
                {
                    string trimmedLabelText = labelText.Trim();

                    if (trimmedLabelText != "")
                    {
                        Label label;
                        try
                        {
                            label = LabelDao.FindByText(trimmedLabelText);
                        }
                        catch (InstanceNotFoundException <Label> )
                        {
                            label = Label.CreateLabel(-1, trimmedLabelText);
                            LabelDao.Create(label);
                        }
                        catch (DuplicateInstanceException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                        if (!labelsToSet.Contains(label))
                        {
                            labelsToSet.Add(label);
                        }
                    }
                }
            }

            List <Label> labelsAlreadyForLink;

            try
            {
                labelsAlreadyForLink = LabelDao.FindForLink(linkId);
            }
            catch (InstanceNotFoundException <Label> )
            {
                labelsAlreadyForLink = new List <Label>();
            }
            foreach (Label labelAlreadyForLink in labelsAlreadyForLink)
            {
                if (!labelsToSet.Contains(labelAlreadyForLink))
                {
                    link.Labels.Remove(labelAlreadyForLink);

                    if (labelAlreadyForLink.Links.Count == 1)
                    {
                        labelAlreadyForLink.Links.Remove(link);
                        try
                        {
                            LabelDao.Update(labelAlreadyForLink);
                        }
                        catch (InstanceNotFoundException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            LabelDao.Remove(labelAlreadyForLink.labelId);
                        }
                        catch (InstanceNotFoundException <Label> ex)
                        {
                            throw new InternalErrorException(ex);
                        }
                    }
                }
                else
                {
                    labelsToSet.Remove(labelAlreadyForLink);
                }
            }

            foreach (Label label in labelsToSet)
            {
                if (!link.Labels.Contains(label))
                {
                    link.Labels.Add(label);

                    label.Links.Add(link);
                    try
                    {
                        LabelDao.Update(label);
                    }
                    catch (InstanceNotFoundException <Label> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                }
            }

            try
            {
                LinkDao.Update(link);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InternalErrorException(ex);
            }
        }