Пример #1
0
 public bool AddFileToRoot(FileItem file)
 {
     if (file == null)
         return false;
     if (RootFolder.SubItems == null)
         RootFolder.SubItems = new List<FileItem>();
     file.PerantID = RootFolder.ID;
     RootFolder.SubItems.Add(file);
     return true;
 }
Пример #2
0
 public FileStructure(FileItem root)
 {
     if (root != null)
     {
         RootFolder = root;
     }
     else
     {
         throw new NullReferenceException("Root can't be null");
     }
 }
Пример #3
0
 /// <summary>
 /// Send add new file email notification to list of users
 /// </summary>
 /// <param name="sendTo">The lise of user this mail will be send to.</param>
 /// <param name="file">The new file properties/</param>
 /// <param name="courseName">The course name that the file added.</param>
 public static void SendAddNewFileNotification(List<User> sendTo, FileItem file, string courseName)
 {
     if (file == null)
         return;
     foreach (User user in sendTo)
     {
         if (user != null)
         {
             User owner = CMDal.GetUserBy("Id", file.OwnerId);
             string template = GetEmailTamplateByType(EmailType.NewFile);
             template = string.Format(template, user.UserName, owner.UserName, courseName, file.FileName);
             SendMail(user.Email, "New File Added to " + courseName, template);
         }
     }
 }
Пример #4
0
        public static void AddNewFile(string sessionId, int userId, string physicalPath, int courseId, FileItem file)
        {
            try
            {
                new DataAccess(ConnectionString).ExecuteQuerySP("SP_InsertNewFileToCourse",
                            "@UserID", userId,
                            "@SessionID", sessionId,
                            "@CourseID", courseId,
                            "@PhysicalPath", physicalPath==null ? string.Empty: physicalPath,
                            "@FileName", file.FileName,
                            "@FileTypeID", file.Type.ID,
                            "@parentFileID", file.PerantID,
                            "@Size", file.Size,
                            "@IsFolder", file.IsFolder);
            }
            catch (Exception)
            {

            }
        }
Пример #5
0
 private bool AddFile(FileItem toAdd, FileItem current)
 {
     if (current == null || toAdd == null)
         return false;
     if (current.ID == toAdd.PerantID)
     {
         if (current.IsFolder && current.SubItems == null)
             current.SubItems = new List<FileItem>();
         if (!current.IsFolder || current.SubItems.Contains(toAdd))
             return false;
         current.SubItems.Add(toAdd);
         return true;
     }
     if (current.SubItems != null)
     {
         foreach (FileItem item in current.SubItems)
         {
             if (AddFile(toAdd, item))
                 return true;
         }
     }
     return false;
 }
Пример #6
0
        private static FileItem GetNewFolder(bool isNull, int PerantId)
        {
            if (isNull)
                return null;
            FileType t = new FileType
            {
                ID = 1,
                ImageUrl = "folder.png",
                Extension = null,
                Description = "Folder"
            };

            FileItem fi = new FileItem
            {
                FileName = "folder" + item,
                ID = item,
                IsFolder = true,
                OwnerId = 1,
                PerantID = PerantId,
                Rate = 0,
                SubItems = new List<FileItem>(),
                Type = t
            };

            item++;
            return fi;
        }
Пример #7
0
        private static FileItem GetNewFile(bool isNull, int PerantId)
        {
            if (isNull)
                return null;
            FileType t = new FileType
            {
                ID = 2,
                ImageUrl = "doc.png",
                Extension = "doc",
                Description = "Word 2003 file"
            };

            FileItem fi = new FileItem
            {
                FileName = "file" + item + ".doc",
                ID = item,
                IsFolder = false,
                OwnerId = 1,
                PerantID = PerantId,
                Rate = 0,
                SubItems = new List<FileItem>(),
                Type = t
            };

            item++;
            return fi;
        }
Пример #8
0
        /*
            root
                -folder0
                    -folder1
                        -folder2
                            -folder3
                                -file0
                    -folder4
                        -folder5
                        -file1
                    -folder6
                        file2
                        file3
                        file4
                -folder7
                    folder8
                        file5
                        file6
                    file7
                -folder9
                    file8
                    file9
                    file10
                -file11
                -file12
         */
        private static void CreateNewStaticStructure()
        {
            item = 1;
            FileType t = new FileType
            {
                ID = 1,
                ImageUrl = "folder.png",
                Extension = null,
                Description = "Folder"
            };

            FileItem fi = new FileItem
            {
                FileName = "Root",
                ID = 0,
                IsFolder = true,
                OwnerId = 1,
                PerantID = -1,
                Rate = 0,
                SubItems = new List<FileItem>(),
                Type = t
            };
            List<FileItem> folders = new List<FileItem>();
            for (int i = 0; i < 10; i++)
            {
                folders.Add(GetNewFolder(false, -1));
            }
            List<FileItem> files = new List<FileItem>();
            for (int i = 0; i < 13; i++)
            {
                files.Add(GetNewFile(false, -1));
            }
            //root
            AddToFolder(fi, folders[0]);
                AddToFolder(folders[0], folders[1]);
                    AddToFolder(folders[1], folders[2]);
                        AddToFolder(folders[2], folders[3]);
                            AddToFolder(folders[3], files[0]);
                AddToFolder(fi, folders[4]);
                    AddToFolder(folders[4], folders[5]);
                    AddToFolder(folders[4], files[1]);
                AddToFolder(fi, folders[6]);
                    AddToFolder(folders[6], files[2]);
                    AddToFolder(folders[6], files[3]);
                    AddToFolder(folders[6], files[4]);
                AddToFolder(fi, folders[7]);
                    AddToFolder(folders[7], folders[8]);
                        AddToFolder(folders[8], files[5]);
                        AddToFolder(folders[8], files[6]);
                    AddToFolder(folders[7], files[7]);
                AddToFolder(fi, folders[9]);
                    AddToFolder(folders[9], files[8]);
                    AddToFolder(folders[9], files[9]);
                    AddToFolder(folders[9], files[10]);
                AddToFolder(fi, files[11]);
                AddToFolder(fi, files[12]);
            fs = new FileStructure(fi);
        }
Пример #9
0
 private static void AddToFolder(FileItem addTo, FileItem toAdd)
 {
     addTo.SubItems.Add(toAdd);
     toAdd.PerantID = addTo.ID;
 }
Пример #10
0
        public void TestRootSubItemsIsNull()
        {
            FileType t = new FileType
            {
                ID = 1,
                ImageUrl = "folder.png",
                Extension = null,
                Description = "Folder"
            };

            FileItem fi = new FileItem
            {
                FileName = "Root",
                ID = 0,
                IsFolder = true,
                OwnerId = 1,
                PerantID = -1,
                Rate = 0,
                SubItems = null,
                Type = t
            };
            Assert.Throws<NullReferenceException>(() => { new FileStructure(null); }, "Root can't be null");
        }
Пример #11
0
        public void TestRootIsValid()
        {
            FileType t = new FileType
            {
                ID = 1,
                ImageUrl = "folder.png",
                Extension = null,
                Description = "Folder"
            };

            FileItem fi = new FileItem
            {
                FileName = "Root",
                ID = 0,
                IsFolder = true,
                OwnerId = 1,
                PerantID = -1,
                Rate = 0,
                SubItems = new List<FileItem>(),
                Type = t
            };
            FileStructure fs = new FileStructure(fi);
            Assert.NotNull(fs.RootFolder);
        }
Пример #12
0
        public void UploadFile(UploadFileMsg msg)
        {
            string name = null;
            if (!msg.IsFolder)
            {
                string p = msg.FileName.Split('.').Last();
                name = Utilitys.SaveFileOnServer(msg.FileStream, p);
            }
            FileItem fi = new FileItem()
            {
                FileName = msg.FileName,
                OwnerId = msg.UserId,
                PerantID = msg.ParentId,
                Rate=0,
                SubItems = null,
                Type = new FileType
                {
                    ID = (int)msg.TypeId
                },
                Size = msg.Size,
                IsFolder = msg.IsFolder
            };

            CMDal.AddNewFile(msg.SessionId, msg.UserId, name, msg.CourseId, fi);
        }
Пример #13
0
 private bool IsItemExist(FileItem item, int toFind)
 {
     if (item == null || item.SubItems == null)
         return false;
     if (item.ID == toFind)
         return true;
     foreach (FileItem i in item.SubItems)
     {
         if (i.ID == toFind)
             return true;
         else if (IsItemExist(i, toFind))
             return true;
     }
     return false;
 }
Пример #14
0
 public bool AddFileByPerantID(FileItem file)
 {
     return AddFile(file, RootFolder);
 }
Пример #15
0
        private bool RemoveFile(FileItem current, int id, int userId)
        {
            if (current == null)
                return false;
            for (int i = 0; i < current.SubItems.Count; i++)
            {
                FileItem item = current.SubItems.ElementAt(i);
                if (item.ID == id && item.OwnerId == userId)
                {
                    if (item.IsFolder && item.SubItems.Count > 0)
                        return false;
                    current.SubItems.Remove(item);
                    return true;
                }
            }

            foreach (FileItem fi in current.SubItems)
            {
                if (RemoveFile(fi, id, userId))
                    return true;
            }

            return false;
        }
Пример #16
0
        public static FileStructure GetCourseFiles(string sessionId, int userId, int courseId)
        {
            try
            {
                DataTable table = new DataAccess(ConnectionString).ExecuteQuerySP("SP_GetFilesByCourseID",
                        "@SessionID", sessionId,
                        "@UserID", userId,
                        "@courseID", courseId);
                if (table == null || table.Rows.Count == 0)
                    return null;

                FileStructure struc = null;
                List<FileItem> files = new List<FileItem>();
                foreach (DataRow row in table.Rows)
                {
                    int id, ownerId, perantId, rate,typeId, size,rUsers;
                    bool isFolder;

                    ParseCellDataToInt(row["ID"], out id);
                    ParseCellDataToInt(row["UserId"], out ownerId);
                    ParseCellDataToInt(row["ParentFileID"], out perantId);
                    ParseCellDataToInt(row["Rate"], out rate);
                    ParseCellDataToInt(row["RatingUsers"], out rUsers);
                    ParseCellDataToInt(row["TypeID"], out typeId);

                    ParseCellDataToBool(row["IsFolder"], out isFolder);

                    ParseCellDataToInt(row["Size"], out size);

                    FileType type = new FileType
                    {
                        ID=typeId,
                        Description = ParseCellDataToString(row["Type"]),
                        Extension = ParseCellDataToString(row["Extension"]),
                        ImageUrl = ParseCellDataToString(row["ImageUrl"])
                    };

                    FileItem item = new FileItem
                    {
                        FileName = ParseCellDataToString(row["FileName"]),
                        Type = type,
                        SubItems=null,
                        OwnerName = ParseCellDataToString(row["UserName"]),
                        LastModify = ParseCellDataToDateTime(row["LastModify"]),
                        ID=id,
                        IsFolder=isFolder,
                        OwnerId=ownerId,
                        PerantID=perantId,
                        Rate = ((double)rate / rUsers),
                        Size=size
                    };
                    if (rate == 0 && rUsers == 0)
                        item.Rate = -1;

                    files.Add(item);
                }

                struc = new FileStructure(files.Where(x => x.PerantID == -1).First());
                files.Remove(struc.RootFolder);
                while (files.Count != 0)
                {
                    if (files.Count > 0 && struc.AddFileByPerantID(files[0]))
                        files.Remove(files[0]);
                }

                return struc;
            }
            catch (Exception)
            {
                return null;
            }
        }