public Contract.TagResponse ManageTagObjects(string app, Contract.TagRequest req, bool tag)
        {
            Applications appEnum;

            tagRes = new TagResponse();
            if (!Applications.TryParse(app, out appEnum))
            {
                tagRes.ErrorCode = SetErrorCode(tagRes.ErrorCode, "InvalidApplicationName");
            }

            ValidateAndSaveTags((int)appEnum, req, tag);
            SaveTaggedObjects((int)appEnum, req, tag);
            return(MapTagsToTagResponse(req));
        }
        private TagResponse MapTagsToTagResponse(Contract.TagRequest req)
        {
            List <Contract.Tag> tags = new List <Contract.Tag>();

            foreach (Entities.Tag t in tagList)
            {
                tags.Add(new Contract.Tag()
                {
                    TagId     = t.TagId,
                    TagName   = t.TagName,
                    ErrorCode = t.ErrorCode,
                    IsActive  = t.IsActive
                });
            }
            tagRes.Tags          = tags;
            tagRes.TaggedObjects = toList;
            return(tagRes);
        }
        private void SaveTaggedObjects(int applicationId, Contract.TagRequest req, bool tag)
        {
            toList = new List <TaggedObject>();
            Entities.TaggedObject tagObj;
            foreach (Entities.Tag t in tagList) // In case of multiple tags
            {
                if (string.IsNullOrEmpty(t.ErrorCode))
                {
                    foreach (Contract.TaggedObject to in req.TaggedObjects)
                    {
                        if (!string.IsNullOrEmpty(to.ObjectId))
                        {
                            if (t.IsNew)
                            {
                                tagObj = new Entities.TaggedObject()
                                {
                                    TagId           = t.TagId,
                                    ObjectId        = IsObjectIdInt(applicationId) ? Int32.Parse(to.ObjectId) : 0,
                                    ObjectTextId    = !IsObjectIdInt(applicationId) ? to.ObjectId : null,
                                    CreateDatetime  = DateTime.Now,
                                    UpdatedDateTime = DateTime.Now,
                                    IsActive        = tag,
                                    ApplicationId   = applicationId
                                };
                                repository.Save(tagObj);
                            }
                            else
                            {
                                tagObj = repository.GetTaggedObject(to.ObjectId.Trim(), t.TagId, IsObjectIdInt(applicationId));
                                if (tagObj == null || tagObj.TaggedObjectId == 0)
                                {
                                    tagObj = new Entities.TaggedObject()
                                    {
                                        TagId           = t.TagId,
                                        ObjectId        = IsObjectIdInt(applicationId) ? Int32.Parse(to.ObjectId) : 0,
                                        ObjectTextId    = !IsObjectIdInt(applicationId) ? to.ObjectId : null,
                                        CreateDatetime  = DateTime.Now,
                                        UpdatedDateTime = DateTime.Now,
                                        IsActive        = tag,
                                        ApplicationId   = applicationId
                                    };
                                    repository.Save(tagObj);
                                }
                                else if (tagObj.IsActive.HasValue && tagObj.IsActive.Value != tag)
                                {
                                    tagObj.IsActive        = tag;
                                    tagObj.UpdatedDateTime = DateTime.Now;
                                    repository.Save(tagObj);
                                }

                                if (to.TagId == 0 || to.TaggedObjectId == 0)
                                {
                                    to.TaggedObjectId = tagObj.TaggedObjectId;
                                    to.TagId          = t.TagId;
                                    to.IsActive       = tag;
                                }

                                if (req.GetTaggedObjectsInResponse)
                                {
                                    toList.Add(MapTaggedObjectEntityToTaggedObjectContract(tagObj));
                                }
                            }
                        }
                        else
                        {
                            to.ErrorCode     = "InvalidObjectId";
                            tagRes.ErrorCode = SetErrorCode(tagRes.ErrorCode, "InvalidTagId");
                            if (req.GetTaggedObjectsInResponse)
                            {
                                toList.Add(to);
                            }
                        }
                    }
                }
            }
        }
        private void ValidateAndSaveTags(int applicationId, Contract.TagRequest req, bool tag)
        {
            tagList = new List <Entities.Tag>();

            if (req.Tags == null) // default is flagging
            {
                List <Contract.Tag> tList = new List <Contract.Tag>();
                tList.Add(new Contract.Tag()
                {
                    TagName = "flag"
                });
                req.Tags = tList;
            }

            foreach (Contract.Tag t in req.Tags)
            {
                Entities.Tag t1 = null;
                if (!t.TagId.HasValue || (t.TagId.HasValue && t.TagId.Value == 0))
                {
                    if (!string.IsNullOrEmpty(t.TagName))
                    {
                        t1 = repository.GetTag(t.TagName, CustomRequestContext.CurrentUser.AppUserId, applicationId);
                    }
                    else
                    {
                        tagRes.ErrorCode = SetErrorCode(tagRes.ErrorCode, "InvalidTagName");
                    }
                    if (t1 == null || t1.TagId == 0)
                    {
                        t1 = new Entities.Tag()
                        {
                            TagId   = 0,
                            TagName =
                                string.IsNullOrEmpty(t.TagName) ? string.Empty : t.TagName.Trim().ToLower(),
                            ApplicationId   = applicationId,
                            UserId          = CustomRequestContext.CurrentUser.AppUserId,
                            CreateDatetime  = DateTime.Now,
                            UpdatedDateTime = DateTime.Now,
                            IsActive        = t.IsActive ?? true,
                            ErrorCode       = string.IsNullOrEmpty(t.TagName) ? "InvalidTagName" : string.Empty,
                            IsNew           = true
                        };
                        if (string.IsNullOrEmpty(t1.ErrorCode))
                        {
                            repository.Save(t1);
                        }
                    }

                    tagList.Add(t1);
                }
                else
                {
                    t1 = repository.GetTag(t.TagId.Value, CustomRequestContext.CurrentUser.AppUserId, applicationId);
                    if (t1.TagId == 0)
                    {
                        t1.ErrorCode     = "InvalidTagId";
                        tagRes.ErrorCode = SetErrorCode(tagRes.ErrorCode, "InvalidTagId");
                    }
                    //if (t.IsActive.HasValue && t.IsActive.Value != tag)
                    //{
                    //    t1.IsActive = tag;
                    //    t1.UpdatedDateTime = DateTime.Now;
                    //    repository.Save(t1);
                    //}
                    tagList.Add(t1);
                }
            }
        }