示例#1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("btnSave_Click");
            try
            {
                dgvDocSetup.EndEdit();

                List <string> queryList = new List <string>();

                foreach (DataGridViewRow row in dgvDocSetup.Rows)
                {
                    string fileName        = row.Cells[0].Value.ToString();
                    string keyword         = row.Cells[1].Value.ToString();
                    string favSelection    = row.Cells[2].Value.ToString();
                    string selection       = row.Cells[3].Value.ToString();
                    string directory       = row.Cells[4].Value.ToString();
                    string targetDirectory = row.Cells[5].Value.ToString();
                    string vpath           = row.Cells[6].Value.ToString();
                    string type            = row.Cells[7].Value.ToString();
                    string shared          = row.Cells[8].Value.ToString();
                    string sharedPerson    = shared;

                    string favorite = favSelection == "---" ? "False" : "True";

                    string autoDelete = selection == "7 days" ? DateTime.Today.AddDays(7).ToString("yyyy/MM/dd")
                        : selection == "30 days" ? DateTime.Today.AddDays(30).ToString("yyyy/MM/dd")
                        : selection == "60 days" ? DateTime.Today.AddDays(60).ToString("yyyy/MM/dd")
                        : selection == "90 days" ? DateTime.Today.AddDays(90).ToString("yyyy/MM/dd") : "2099/12/31";

                    if (keyword == "")
                    {
                        keyword = fileName;
                    }

                    //if (!File.Exists(targetDirectory))
                    File.Copy(directory, targetDirectory, true);

                    string sPath = targetDirectory;

                    if (sPath.Contains("'"))
                    {
                        sPath = sPath.Replace("'", "''");
                    }

                    if (DataUtil.IsRecordExists(targetDirectory))
                    {
                        List <string> list = DataUtil.GetSharedList(GlobalService.DbTable, targetDirectory);

                        if (list.Count > 0)
                        {
                            foreach (string item in list)
                            {
                                string tbName = "TB_" + AdUtil.GetUserId();

                                string delText = string.Format("delete from " + tbName + " where r_path = N'{0}' and r_owner = N'{1}'", sPath, GlobalService.User);
                                DataService.GetInstance().ExecuteNonQuery(delText);
                            }
                        }

                        string delOwnerText = string.Format("delete from " + GlobalService.DbTable + " where r_path = N'{0}'", sPath);
                        DataService.GetInstance().ExecuteNonQuery(delOwnerText);
                    }

                    FileInfo     info = new FileInfo(targetDirectory);
                    FileSecurity fs   = info.GetAccessControl();
                    AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(NTAccount));
                    string lastmodified = info.LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss");
                    string now          = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    string extension    = targetDirectory.Contains(".") ? targetDirectory.Substring(targetDirectory.LastIndexOf("."), targetDirectory.Length - targetDirectory.LastIndexOf(".")) : "file";

                    foreach (FileSystemAccessRule rule in rules)
                    {
                        fs.RemoveAccessRuleSpecific(rule);
                    }

                    fs.SetAccessRuleProtection(true, false);
                    fs.AddAccessRule(new FileSystemAccessRule(@"kmhk\itadmin", FileSystemRights.FullControl, AccessControlType.Allow));

                    if (GlobalService.User == "Chow Chi To(周志滔,Sammy)")
                    {
                        fs.AddAccessRule(new FileSystemAccessRule(@"kmas\as1600048", FileSystemRights.FullControl, AccessControlType.Allow));
                    }
                    if (GlobalService.User == "Ling Wai Man(凌慧敏,Velma)")
                    {
                        fs.AddAccessRule(new FileSystemAccessRule(@"kmas\as1600049", FileSystemRights.FullControl, AccessControlType.Allow));
                    }
                    if (GlobalService.User == "Ng Lau Yu, Lilith (吳柳如)")
                    {
                        fs.AddAccessRule(new FileSystemAccessRule(@"kmas\as1600051", FileSystemRights.FullControl, AccessControlType.Allow));
                    }
                    if (GlobalService.User == "Lee Miu Wah(李苗華)")
                    {
                        fs.AddAccessRule(new FileSystemAccessRule(@"kmas\as1600053", FileSystemRights.FullControl, AccessControlType.Allow));
                    }

                    fs.AddAccessRule(new FileSystemAccessRule(AdUtil.GetUserId(), FileSystemRights.FullControl, AccessControlType.Allow));

                    List <string> sharedList = new List <string>();

                    if (fileName.Contains("'"))
                    {
                        fileName = fileName.Replace("'", "''");
                    }

                    if (keyword.Contains("'"))
                    {
                        keyword = keyword.Replace("'", "''");
                    }

                    if (shared != "-")
                    {
                        sharedList = shared.Split(';').ToList();
                    }

                    //foreach (string item in sharedList)
                    // Debug.WriteLine("Item: " + item);

                    List <string> hklist = new List <string>();
                    List <string> cnlist = new List <string>();
                    List <string> vnlist = new List <string>();
                    List <string> jplist = new List <string>();

                    foreach (string item in sharedList)
                    {
                        if (UserUtil.IsCnMember(item.Trim()))
                        {
                            cnlist.Add(item.Trim());
                        }
                        else if (UserUtil.IsVnMember(item.Trim()))
                        {
                            vnlist.Add(item.Trim());
                        }
                        else if (UserUtil.IsJpMember(item.Trim()))
                        {
                            jplist.Add(item.Trim());
                        }
                        else
                        {
                            hklist.Add(item.Trim());
                        }
                    }

                    if (hklist.Count > 0)
                    {
                        foreach (string item in hklist)
                        {
                            string sharedId  = AdUtil.GetUserIdByUsername(item.Trim());
                            string tableName = "TB_" + sharedId;

                            string sharedDivision   = SystemUtil.GetDivision(item.Trim());
                            string sharedDepartment = SystemUtil.GetDepartment(item.Trim());

                            fs.AddAccessRule(new FileSystemAccessRule(sharedId, FileSystemRights.Modify, AccessControlType.Allow));

                            if (UserUtil.IsSpecialUser(item))
                            //if (item == "Chow Chi To(周志滔,Sammy)" || item == "Ling Wai Man(凌慧敏,Velma)" || item == "Chan Fai Lung(陳輝龍,Onyx)" || item == "Ng Lau Yu, Lilith (吳柳如)" ||
                            //    item == "Lee Miu Wah(李苗華)" || item == "Lee Ming Fung(李銘峯)" || item == "Ho Kin Hang(何健恒,Ken)" || item == "Yeung Wai, Gabriel (楊偉)")
                            {
                                string asText = string.Format("select as_userid from TB_USER_AS where as_user = N'{0}'", item.Trim());
                                string asId   = DataService.GetInstance().ExecuteScalar(asText).ToString().Trim();

                                fs.AddAccessRule(new FileSystemAccessRule(asId, FileSystemRights.Modify, AccessControlType.Allow));
                            }

                            string sharedVpath = sharedDivision != GlobalService.Division && vpath.StartsWith(@"\" + GlobalService.Division) ? @"\Documents" + vpath
                                : sharedDepartment != GlobalService.Department && vpath.StartsWith(@"\Common") ? @"\Documents" + vpath : vpath;

                            if (sharedVpath.Contains("'"))
                            {
                                sharedVpath = sharedVpath.Replace("'", "''");
                            }

                            string sharedText = string.Format("insert into " + tableName + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate)" +
                                                              " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}')", fileName, extension, keyword, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), lastmodified, GlobalService.User,
                                                              item.Trim(), sPath, sharedVpath, "2099/12/31");

                            queryList.Add(sharedText);
                        }
                    }

                    if (targetDirectory.StartsWith(@"\\kdthk-dm1\project\KDTHK-DM"))
                    {
                        File.SetAccessControl(targetDirectory, fs);
                    }
                    else
                    {
                        if (!targetDirectory.StartsWith(@"L:\") && !targetDirectory.StartsWith(@"M:\") && !targetDirectory.StartsWith(@"\\kdthk-dm1\project") && !targetDirectory.StartsWith(@"\\kdthk-dm1"))
                        {
                            File.SetAccessControl(targetDirectory, fs);
                        }
                    }
                    /* Commented out by Cato Yeung */

                    if (cnlist.Count > 0)
                    {
                        PermissionUtil.SetGlobalPermission(cnlist, targetDirectory, "kmcn.local");
                        SharedUtil.SharedCN(cnlist, sPath, fileName, keyword);
                    }

                    if (vnlist.Count > 0)
                    {
                        PermissionUtil.SetGlobalPermission(vnlist, targetDirectory, "kdtvn.local");
                        SharedUtil.SharedVN(vnlist, sPath, fileName, keyword);
                    }

                    if (jplist.Count > 0)
                    {
                        PermissionUtil.SetGlobalPermission(jplist, targetDirectory, "km.local");
                        SharedUtil.SharedJp(jplist, sPath, fileName, keyword);
                    }

                    try
                    {
                        List <string> receiverlist = cnlist.Concat(vnlist).Concat(jplist).ToList();
                        //receiverlist.Add(GlobalService.User);
                        if (receiverlist.Count > 0)
                        {
                            EmailUtil.SendNotificationEmail(receiverlist);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message + ex.StackTrace);
                    }
                    //SharedUtil.SharedGlobal(cnList, sPath, fileName, keyword);

                    /*sharedList = sharedList.Distinct().ToList();
                     *
                     * if (sharedList.Count > 0)
                     *  sharedPerson = string.Join(";", sharedList.ToArray());*/

                    if (vpath.Contains("'"))
                    {
                        vpath = vpath.Replace("'", "''");
                    }

                    GlobalService.RootTable.Rows.Add(fileName, keyword, lastmodified, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), GlobalService.User, shared, targetDirectory, vpath, 0, favorite, "False");

                    string ownerText = string.Format("insert into " + GlobalService.DbTable + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate)" +
                                                     " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}')", fileName, extension, keyword, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), lastmodified, GlobalService.User,
                                                     shared, sPath, vpath, autoDelete);

                    queryList.Add(ownerText);

                    if (directory.StartsWith(Environment.SpecialFolder.Desktop + @"\MyCloud Sync"))
                    {
                        File.Delete(directory);
                    }
                }
                queryList = queryList.Distinct().ToList();


                foreach (string text in queryList)
                {
                    DataService.GetInstance().ExecuteNonQuery(text);
                }
                //DataUtil.SyncDataToServer();
                GlobalService.RootTable = RootUtil.RootDataTable();
                this.DialogResult       = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);

                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }