Esempio n. 1
0
        /// <summary>
        /// 保存题目数据文件到磁盘
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="forms">Request.Forms</param>
        /// <param name="httpFiles">Request.Files</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveProblemData(Int32 problemID, NameValueCollection forms, HttpFileCollectionBase httpFiles)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            SortedDictionary <String, String> dictData = new SortedDictionary <String, String>();

            if (httpFiles != null)
            {
                for (Int32 i = 0; i < httpFiles.Count; i++)
                {
                    HttpPostedFileBase file = httpFiles[i];

                    if (String.IsNullOrEmpty(file.FileName))
                    {
                        return(MethodResult.FailedAndLog("Filename can not be NULL!"));
                    }

                    FileInfo fi = new FileInfo(file.FileName);
                    if (httpFiles.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 && !".in".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (httpFiles.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0 && !".out".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase) && !".ans".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (file.ContentLength <= 0)
                    {
                        return(MethodResult.FailedAndLog("You can not upload empty file!"));
                    }

                    StreamReader sr = new StreamReader(file.InputStream);
                    dictData.Add(httpFiles.GetKey(i), sr.ReadToEnd());
                }
            }

            if (forms != null)
            {
                for (Int32 i = 0; i < forms.Count; i++)
                {
                    if (forms.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 || forms.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        dictData.Add(forms.GetKey(i), forms[i]);
                    }
                }
            }

            if (dictData.Count == 0)
            {
                return(MethodResult.FailedAndLog("No data was added!"));
            }

            if (dictData.Count % 2 != 0)
            {
                return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
            }

            ProblemDataWriter writer = new ProblemDataWriter();

            foreach (KeyValuePair <String, String> pair in dictData)
            {
                if (pair.Key.IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    String input  = pair.Value;
                    String output = String.Empty;

                    if (!dictData.TryGetValue(pair.Key.ToLower().Replace("in", "out"), out output))
                    {
                        return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
                    }

                    writer.WriteData(input, output);
                }
            }

            String fileNewName = problemID.ToString() + ".zip";
            String savePath    = Path.Combine(ConfigurationManager.ProblemDataPath, fileNewName);

            Byte[] data = writer.WriteTo();
            File.WriteAllBytes(savePath, data);

            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.SuccessAndLog("Admin create problem data, id = {0}", problemID.ToString()));
        }
Esempio n. 2
0
        /// <summary>
        /// 设置指定ID的题目类型对
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="sourceIDs">旧逗号分隔的题目类型ID</param>
        /// <param name="targetIDs">新逗号分隔的题目类型ID</param>
        /// <returns>是否成功设置</returns>
        public static IMethodResult AdminUpdateProblemCategoryItems(Int32 problemID, String sourceIDs, String targetIDs)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            if ((!String.IsNullOrEmpty(sourceIDs) && !RegexVerify.IsNumericIDs(sourceIDs)) || (!String.IsNullOrEmpty(targetIDs) && !RegexVerify.IsNumericIDs(targetIDs)))
            {
                return(MethodResult.InvalidRequest(RequestType.ProblemCategory));
            }

            StringBuilder deleteIDs = new StringBuilder();
            StringBuilder insertIDs = new StringBuilder();
            List <String> sourceid  = (String.IsNullOrEmpty(sourceIDs) ? new List <String>() : new List <String>(sourceIDs.Split(',')));
            List <String> targetid  = (String.IsNullOrEmpty(targetIDs) ? new List <String>() : new List <String>(targetIDs.Split(',')));

            for (Int32 i = 0; i < targetid.Count; i++)//删除sourceIDs和targetIDs中相同的数据
            {
                for (Int32 j = 0; j < sourceid.Count; j++)
                {
                    if (String.Equals(targetid[i], sourceid[j], StringComparison.OrdinalIgnoreCase))
                    {
                        targetid.RemoveAt(i);
                        sourceid.RemoveAt(j);
                        i--;
                        j--;
                        break;
                    }
                }
            }

            for (Int32 i = 0; i < sourceid.Count; i++)//设置deleteIDs
            {
                if (i > 0)
                {
                    deleteIDs.Append(',');
                }
                deleteIDs.Append(sourceid[i]);
            }

            for (Int32 i = 0; i < targetid.Count; i++)//设置insertIDs
            {
                if (i > 0)
                {
                    insertIDs.Append(',');
                }
                insertIDs.Append(targetid[i]);
            }

            Boolean ret = true;

            if (deleteIDs.Length > 0)
            {
                ret &= (ProblemCategoryItemRepository.Instance.DeleteEntities(problemID, deleteIDs.ToString()) > 0);
            }
            if (insertIDs.Length > 0)
            {
                ret &= (ProblemCategoryItemRepository.Instance.InsertEntity(problemID, insertIDs.ToString()) > 0);
            }

            if (!ret)
            {
                return(MethodResult.FailedAndLog("No problem's category was updated!"));
            }

            return(MethodResult.SuccessAndLog("Admin update problem's category, id = {0}, new category = {1}", problemID.ToString(), targetIDs));
        }