Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
 public Label FindLabel(string text)
 {
     return(LabelDao.FindByText(text));
 }