Example #1
0
    /**
     * 输入:指定用户的userId
     * 输出:指定用户的所有兴趣标签的列表Tag列表
     * 功能:获取指定用户的所有兴趣标签的列表
     */
    public static List <GroupNode> getAllInterestLabelsByUserId(int userId)
    {
        List <GroupNode> tagGroupList = new List <GroupNode>();
        User             user         = new User(userId, "", "", "");
        List <int>       tagIdList    = User2TagManager.getTagListByUserId(user);
        List <Tag>       tagList      = TagManager.getTagsByIdList(tagIdList);

        if (tagList == null)
        {
            return(null);
        }
        foreach (Tag tag in tagList)
        {
            if (tag.IsPrivate == 0)
            {
                continue;
            }
            GroupNode groupNode = new GroupNode();
            groupNode.Id              = tag.TagId;
            groupNode.IsPrimaryGroup  = false;
            groupNode.IsInterestLabel = true;
            groupNode.NodeName        = tag.TagName;
            groupNode.PrimaryGroupId  = tag.GroupId;
            tagGroupList.Add(groupNode);
        }
        return(tagGroupList);
    }
Example #2
0
    private static string deleteRecord()
    {
        string output = "deleteRecord\n";

        for (int i = 0; i < count; i++)
        {
            User2Tag u2t = new User2Tag(sampleUser.UserId, ids[i]);
            if (!User2TagManager.deleteRecord(u2t))
            {
                output += "Error! 删除关联(TagId为" + ids[i] + ",用户id为" + sampleUser.UserId + ")失败!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 删除关联(TagId为" + ids[i] + ",用户id为" + sampleUser.UserId + ")成功!\n";
            }
            Tag t = new Tag(ids[i], "", "", 1, DateTime.Now, 1);
            if (!TagManager.deleteTag(t))
            {
                output += "Error! 删除测试Tag失败(TagId为" + ids[i] + ")!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 删除测试Tag成功(TagId为" + ids[i] + ")!\n";
            }
        }

        return(output);
    }
Example #3
0
    private static string addRecord()
    {
        string output = "addRecord\n";

        Random r = new Random();

        for (int i = 0; i < count; i++)
        {
            Tag t = new Tag("", "", 1, DateTime.Now, 1);
            ids[i] = TagManager.addTag(t);
            if (ids[i] < 0)
            {
                output += "Error! 新增测试Tag失败!返回值为" + ids[i] + "\n";
                errorCount++;
                continue;
            }

            output += "Ok! 新增测试Tag成功!返回值为" + ids[i] + "\n";
            User2Tag u2t = new User2Tag(sampleUser.UserId, ids[i]);
            if (!User2TagManager.addRecord(u2t))
            {
                output += "Error! 将id为" + ids[i] + "的Tag关联到id为" + sampleUser.UserId + "的用户时失败!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 将id为" + ids[i] + "的Tag关联到id为" + sampleUser.UserId + "的用户时成功!\n";
            }
        }
        return(output);
    }
Example #4
0
    private static void deleteTag()
    {
        output += "deleteTag\n";

        List <int> list = User2TagManager.getTagListByUserId(sampleUser);

        foreach (int id in list)
        {
            User2Tag u2t2 = new User2Tag(sampleUser.UserId, id);
            if (!FocusAndCreateTag.deleteTag(u2t2))
            {
                output += "Error! 删除User2Tag记录失败!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 删除User2Tag记录成功!\n";
            }
        }
        List <int> list2 = User2TagManager.getTagListByUserId(sampleUser);

        int list2_len = 0;

        if (list2 != null)
        {
            list2_len = list2.Count;
        }

        if (list2_len == list.Count)
        {
            output += "Error! 调用deleteTag前后getTagListByUserId返回的列表长度相同!未能真正删除User2Tag记录!\n";
        }
    }
Example #5
0
    private static void addTag()
    {
        output += "addTag\n";

        Random r = new Random();

        // insert
        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);

            if (!FocusAndCreateTag.addTag(pgs[i].GroupId, "intre" + no, "intre" + no, sampleUser.UserId))
            {
                output += "Error! 新增兴趣标签失败!返回false。\n";
                errorCount++;
                continue;
            }
        }

        // check
        List <int> list = User2TagManager.getTagListByUserId(sampleUser);

        if (list.Count < count)
        {
            output += "Error! 通过getTagListByUserId返回列表长度(=" + list.Count + ")小于测试用例数量!\n";
            errorCount++;
        }
        else
        {
            output += "Ok! 通过getTagListByUserId返回列表长度(=" + list.Count + ")不少于测试用例数量!\n";
        }
    }
Example #6
0
    private static bool clean()
    {
        for (int i = 0; i < count; i++)
        {
            // unlink user to sub groups
            User2Tag u2t = new User2Tag(sampleUser.UserId, tags[i].TagId);
            if (!User2TagManager.deleteRecord(u2t))
            {
                output += "Error! 删除User2Tag记录失败!测试无法继续进行。请先解决User2TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // unlink user to interest labels
            User2Tag u2t2 = new User2Tag(sampleUser.UserId, interLabels[i].TagId);
            if (!User2TagManager.deleteRecord(u2t2))
            {
                output += "Error! 删除User2Tag记录失败!测试无法继续进行。请先解决User2TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // delete public sub groups
            if (!TagManager.deleteTag(tags[i]))
            {
                output += "Error! 删除测试公共子分类失败!测试无法继续进行。请先解决TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // delete interest labels
            if (!TagManager.deleteTag(interLabels[i]))
            {
                output += "Error! 删除测试兴趣标签失败!测试无法继续进行。请先解决TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // delete primary groups
            if (!PrimaryGroupMananger.deleteRecord(pgs[i]))
            {
                output += "Error! 删除测试主分类失败!测试无法继续进行。请先解决PrimaryGroupManager中的错误。\n";
                errorCount++;
                return(false);
            }
        }

        if (!UserManager.deleteRecord(sampleUser))
        {
            output += "Error! 删除用户\"test\"失败!测试无法继续进行。请先解决UserManager中的错误。\n";
            errorCount++;
            return(false);
        }

        return(true);
    }
Example #7
0
    private static bool prepare()
    {
        Random r = new Random();

        // add global user
        sampleUser        = new User("test", "test", "test");
        sampleUser.UserId = UserManager.addUser(sampleUser);
        if (sampleUser.UserId < 0)
        {
            output += "Error! 新增用户\"test\"失败!返回值为" + sampleUser.UserId + "。测试无法继续进行。请先解决UserManager中的错误。\n";
            errorCount++;
            return(false);
        }

        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);

            // add primary groups
            pgs[i]         = new PrimaryGroups(100, no + "");
            pgs[i].GroupId = PrimaryGroupMananger.addRecord(pgs[i]);
            if (pgs[i].GroupId < 0)
            {
                output += "Error! 新增主分类失败!返回值为" + pgs[i].GroupId + "。测试无法继续进行。请先解决PrimaryGroupManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // add public sub groups
            tags[i]       = new Tag("sub" + no, "sub" + no, pgs[i].GroupId, DateTime.Now, 0);
            tags[i].TagId = TagManager.addTag(tags[i]);
            if (tags[i].TagId < 0)
            {
                output += "Error! 新增公共子分类失败!返回值为" + tags[i].TagId + "。测试无法继续进行。请先解决TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // link user to sub groups
            User2Tag u2t = new User2Tag(sampleUser.UserId, tags[i].TagId);
            if (!User2TagManager.addRecord(u2t))
            {
                output += "Error! 新增User2Tag记录失败!测试无法继续进行。请先解决User2TagManager中的错误。\n";
                errorCount++;
                return(false);
            }
        }

        return(true);
    }
Example #8
0
    /**
     * 输入:新建兴趣标签所属的主分类,兴趣标签的名称,关键词列表(多个关键词之间用空格分隔),用户的userId
     * 输出:成功返回true,失败返回false
     * 功能:在指定的主分类下新建用户私有的兴趣标签
     * 注意:要对primaryGroupId的取值范围做检查
     */
    public static bool addTag(int primaryGroupId, string tagName, string tagKeys, int userId)
    {
        // 建立Tag
        Tag t     = new Tag(-1, tagName, tagKeys, primaryGroupId, DateTime.Now, 1);
        int tagId = TagManager.addTag(t);

        if (tagId < 0)
        {
            return(false);
        }
        // 建立用户到tag的链接
        User2Tag u2t = new User2Tag(userId, tagId);

        return(User2TagManager.addRecord(u2t));
    }
Example #9
0
    private static string getTagListByUserId()
    {
        string     output = "getTagListByUserId\n";
        List <int> list   = User2TagManager.getTagListByUserId(sampleUser);

        if (list == null)
        {
            output += "Error! 调用getTagListByUserId失败,返回null。\n";
            errorCount++;
        }
        else
        {
            output += "Ok! 调用getTagListByUserId成功,返回的tag数为" + list.Count + "。\n";
        }

        return(output);
    }
Example #10
0
    /**
     * 输入:一个User2Tag的model
     * 输出:成功返回true,失败返回false
     * 功能:删除指定的User2Tag记录
     */
    public static bool deleteTag(User2Tag u2t)
    {
        if (!User2TagManager.deleteRecord(u2t))
        {
            return(false);
        }
        // 如果是兴趣标签,则除了删除User2Tag表中记录,还要删除Tag表中这个兴趣标签本身
        Tag t = new Tag();

        t.TagId = u2t.TagId;
        Tag t2 = TagManager.getTag(t);

        if (t2.IsPrivate == 1)
        {
            return(TagManager.deleteTag(t2));
        }
        else
        {
            return(true);
        }
    }
Example #11
0
    /**
     * 输入:指定用户的userId
     * 输出:该用户关注的所有公共分类(主分类+子分类)的列表
     * 功能:获取所有主分类的所有子分类的列表
     * 注意:返回的结果中包括主分类下的各个“其他”子分类,额外地,还要通过各个“其他”子分类还原出用户关注的主分类,并添加到结果列表中。
     */
    public static List <GroupNode> getFocusedPublicGroupsByUserId(int userId)
    {
        List <GroupNode> focusedPublicGroupList = new List <GroupNode>();
        User             user      = new User(userId, "", "", "");
        List <int>       tagIdList = User2TagManager.getTagListByUserId(user);
        List <Tag>       tagList   = TagManager.getTagsByIdList(tagIdList);

        if (tagList == null)
        {
            return(null);
        }
        foreach (Tag tag in tagList)
        {
            if (tag.IsPrivate == 1)
            {
                continue;
            }
            GroupNode groupNode = new GroupNode();
            groupNode.Id              = tag.TagId;
            groupNode.IsPrimaryGroup  = false;
            groupNode.IsInterestLabel = false;
            groupNode.NodeName        = tag.TagName;
            groupNode.PrimaryGroupId  = tag.GroupId;
            focusedPublicGroupList.Add(groupNode);
            if (tag.TagName.Equals("其他"))
            {
                GroupNode groupNode2 = new GroupNode();
                groupNode2.Id              = tag.GroupId;
                groupNode2.IsPrimaryGroup  = true;
                groupNode2.IsInterestLabel = false;
                PrimaryGroups primaryGroup = new PrimaryGroups();
                primaryGroup.GroupId      = tag.GroupId;
                primaryGroup              = PrimaryGroupMananger.selectRecord(primaryGroup);
                groupNode2.NodeName       = primaryGroup.GroupName;
                groupNode2.PrimaryGroupId = tag.GroupId;
                focusedPublicGroupList.Add(groupNode2);
            }
        }
        return(focusedPublicGroupList);
    }
Example #12
0
    /**
     * 输入:用户关注的所有tag(这里只有子分类,不会出现兴趣标签)的tagId列表(包括所有的“其他”子分类),用户的id
     * 输出:成功返回true,失败返回false
     * 功能:记录用户关注的子分类信息到数据库
     * 注意:进行数据库操作时,要先删除该用户的所有非兴趣标签类型的关注,再重新添加用户关注的子分类。
     *       对兴趣标签的增删都有专门的方法进行操作,这不是本方法的涉足范围,本方法不可以增删用户自己的兴趣标签,
     *       只能对用户关于公共分类(主分类和子分类)的关注链接进行操作!!!
     *
     * 还有任何不理解的地方请果断问清楚。
     */
    public static bool saveFocus(List <int> tagIds, int userId)
    {
        //拿到指定用户旧的关注列表(包括子分类和兴趣标签)
        User u = new User();

        u.UserId = userId;
        User       u2         = UserManager.getUserById(u);
        List <int> oldTagList = User2TagManager.getTagListByUserId(u2);

        //根据列表信息,删除用户对子分类的全部关注,只留下对兴趣标签的关注
        if (oldTagList != null)
        {
            foreach (int tempTagId in oldTagList)
            {
                Tag t = new Tag();
                t.TagId = tempTagId;
                Tag t2 = TagManager.getTag(t);
                if (t2.IsPrivate == 0)
                {
                    // 删除用户关注
                    User2Tag u2t = new User2Tag(userId, tempTagId);
                    User2TagManager.deleteRecord(u2t);
                }
            }
        }

        //将用户的新的对子分类的关注列表存入User2Tag表中
        foreach (int tempNewTagId in tagIds)
        {
            User2Tag u2t = new User2Tag(userId, tempNewTagId);
            if (!User2TagManager.addRecord(u2t))
            {
                continue;
            }
        }
        return(true);
    }
Example #13
0
    /*
     * 输入:第一个参数用户的userId,第二个参数有两种可能:
     *       1、若第二个参数不为null,则表示一个普通主分类下的“其他”子分类的GroupNode实例(仅使用其中的primaryGroupId字段);
     *       2、若第二个参数为null,则表示“其他”主分类。
     * 输出:Article的列表
     * 功能:这个方法的功能比较难理解。以下是大致实现思路:
     *       1、如果第二个参数不为null:
     *          (1)根据参数gn的primaryGroupId字段确定主分类,记为M;
     *          (2)拿到M下的所有文章列表,记为Q;
     *          (3)根据userId查找该用户关注了M下的哪些子分类和兴趣标签;
     *          (4)利用getArticleListByDynamicSearch函数拿到在M下该用户关注的所有子分类和兴趣标签对应的文章的并集,记为R;
     *          (5)返回值为 Q - R
     *       2、如果第二个参数为null,比较简单,只需返回所有文章中没有被分到任何主分类的文章的列表即可
     * 用途说明:当用户点击某个主分类下“其他”子分类或“其他”主分类查看该分类下所有文章时,此函数返回该分类所有文章的s列表
     */
    public static List <Article> getArticleListOfOthers(int userId, GroupNode gn)
    {
        if (gn != null)
        {
            // 1、如果第二个参数不为null:
            PrimaryGroups pg = PrimaryGroupMananger.selectRecord(new PrimaryGroups(gn.PrimaryGroupId, ""));
            if (pg == null)
            {
                return(null);
            }
            List <Article> al = ArticleManager.getArticleListByPrimaryGroup(pg);
            if (al == null)
            {
                return(null);
            }
            User u = new User();
            u.UserId = userId;
            List <int> tagList = User2TagManager.getTagListByUserId(u);
            if (tagList != null)
            {
                int tagListLength = tagList.Count;
                for (int i = 0; i < tagListLength; i++)
                {
                    Tag t = new Tag();
                    t.TagId = tagList[i];
                    t       = TagManager.getTag(t);
                    if (t.TagName != "其他")
                    {
                        List <Article> articleListInTag = ArticleManager.getArticleListByDynamicSearch(t);
                        if (articleListInTag != null)
                        {
                            int articleListInTagLength = articleListInTag.Count;
                            for (int k = 0; k < articleListInTagLength; k++)
                            {
                                Article a        = articleListInTag[k];
                                int     alLength = al.Count;
                                for (int p = 0; p < alLength; p++)
                                {
                                    if (al[p].ArticleId == a.ArticleId)
                                    {
                                        al.RemoveAt(p);
                                        alLength -= 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(al);
        }
        else
        {
            // 2、如果第二个参数为null,只需返回所有文章中没有被分到任何主分类的文章的列表即可
            List <Article> al = new List <Article>();

            List <string[]> allPG = PrimaryGroupMananger.getAllGroups();
            if (allPG != null)
            {
                for (int i = 0; i < allPG.Count; i++)
                {
                    PrimaryGroups pg = new PrimaryGroups();
                    pg.GroupId   = Convert.ToInt32(allPG[i][0]);
                    pg.GroupName = allPG[i][1];
                    List <Article> onePGAL = ArticleManager.getArticleListByPrimaryGroup(pg);
                    if (onePGAL != null)
                    {
                        for (int j = 0; j < onePGAL.Count; j++)
                        {
                            al.Add(onePGAL[j]);
                        }
                    }
                }
            }

            User u = new User();
            u.UserId = userId;
            List <int> tagList = User2TagManager.getTagListByUserId(u);
            if (tagList != null)
            {
                int tagListLength = tagList.Count;
                for (int i = 0; i < tagListLength; i++)
                {
                    Tag t = new Tag();
                    t.TagId = tagList[i];
                    t       = TagManager.getTag(t);

                    List <Article> articleListInTag = ArticleManager.getArticleListByDynamicSearch(t);
                    if (articleListInTag != null)
                    {
                        int articleListInTagLength = articleListInTag.Count;
                        for (int k = 0; k < articleListInTagLength; k++)
                        {
                            Article a        = articleListInTag[k];
                            int     alLength = al.Count;
                            for (int p = 0; p < alLength; p++)
                            {
                                if (al[p].ArticleId == a.ArticleId)
                                {
                                    al.RemoveAt(p);
                                    alLength -= 1;
                                }
                            }
                        }
                    }
                }
            }

            return(al);
        }
    }
Example #14
0
    private static void saveFocus()
    {
        output += "saveFocus\n";

        List <int> list1 = new List <int>();
        List <int> list2 = new List <int>();

        for (int i = 0; i < count / 2; i++)
        {
            list1.Add(tags[i].TagId);
        }
        for (int i = count / 2; i < count; i++)
        {
            list2.Add(tags[i].TagId);
        }

        // 第一次测试
        output += "第一次测试:\n";
        if (!FocusAndCreateTag.saveFocus(list1, sampleUser.UserId))
        {
            output += "Error! saveFocus调用失败!返回false\n";
            errorCount++;
            return;
        }
        else
        {
            output += "Ok! saveFocus调用成功!返回true\n";
        }

        // 第一次测试成果检查
        List <int> result1 = User2TagManager.getTagListByUserId(sampleUser);

        if (result1 == null)
        {
            output += "Error! saveFocus执行失败!getTagListByUserId返回值为null。\n";
            errorCount++;
            return;
        }
        if (result1.Count < count / 2 - 1)
        {
            output += "Error! 通过getTagListByUserId返回列表长度(=" + result1.Count + ")小于测试用例数量!\n";
            errorCount++;
        }
        else
        {
            output += "Ok! 通过getTagListByUserId返回列表长度(=" + result1.Count + ")不少于测试用例数量!\n";
        }

        // 第二次测试
        output += "第二次测试:\n";
        if (!FocusAndCreateTag.saveFocus(list2, sampleUser.UserId))
        {
            output += "Error! saveFocus调用失败!返回false\n";
            errorCount++;
            return;
        }
        else
        {
            output += "Ok! saveFocus调用成功!返回true\n";
        }

        // 第二次测试成果检查
        List <int> result2 = User2TagManager.getTagListByUserId(sampleUser);

        if (result2 == null)
        {
            output += "Error! saveFocus执行失败!getTagListByUserId返回值为null。\n";
            errorCount++;
            return;
        }
        if (result2.Count < count / 2 - 1)
        {
            output += "Error! 通过getTagListByUserId返回列表长度(=" + result2.Count + ")小于测试用例数量!\n";
            errorCount++;
        }
        else
        {
            output += "Ok! 通过getTagListByUserId返回列表长度(=" + result2.Count + ")不少于测试用例数量!\n";
        }
    }