示例#1
0
        /// <summary>
        /// put the data from imports table to actual data after constraints check
        /// </summary>
        private int ProcessFile(string fileName, ref ArrayList processedMessages, ref int idImport)
        {
            idImport = ApplicationConstants.INT_NULL_VALUE;
            int    resultChronologicalOrder      = ApplicationConstants.INT_NULL_VALUE;
            int    NonExistingAssociates         = ApplicationConstants.INT_NULL_VALUE;
            string fileNameNonExistingAssociates = string.Empty;

            //check if file is not already imported
            string[] str           = Path.GetFileName(fileName).Split('_');
            string   cleanFileName = str[0] + Path.GetExtension(fileName);

            Imports imp = new Imports(SessionManager.GetSessionValueNoRedirect(this.Page, SessionStrings.CONNECTION_MANAGER));

            bool validationResult;

            validationResult = CheckFileAlreadyExists(imp, fileName, cleanFileName, processedMessages);
            if (validationResult == false)
            {
                return(-1);
            }

            try
            {
                resultChronologicalOrder = imp.CheckChronologicalImports(cleanFileName);
            }
            catch (IndException ex)
            {
                processedMessages.Add(ex.Message);
                ChronologicalErrorsToDB(ex.Message, fileName);
                return(-1);
            }

            //write in imports table
            try
            {
                idImport = imp.InsertToImportsTable(fileName, currentUser.IdAssociate);
            }
            catch (IndException ex)
            {
                processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName));
                LogErrorToDataBase(string.Format(ApplicationMessages.IMPORT_FILE_UPLOAD_ERROR, cleanFileName, ex.Message), fileName);
                return(-1);
            }
            if (idImport < 0)
            {
                processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName));
                return(-1);
            }

            int    result         = ApplicationConstants.INT_NULL_VALUE;
            string duplicateError = string.Empty;

            bool   isConsistencyKRMError = false; //KRM stands for "key row(s) missing"
            String consistencyKRMError   = String.Empty;
            String consistencyKRMMessage = "key row(s) were missing from import";

            try
            {
                if (FilesWithCostCentersWithoutHR.Contains(fileName))
                {
                    //file is processed second time. Skip Cost Centers without Hourly Rate
                    result = imp.InsertToActualTable(fileName, currentUser.IdAssociate, idImport, true);
                }
                else
                {
                    result = imp.InsertToActualTable(fileName, currentUser.IdAssociate, idImport, false);
                }
            }
            catch (IndException ex)
            {
                result = -100;

                if (((IndException)ex).BaseException is SqlException)
                {
                    SqlException sqlBaseException = ((SqlException)((IndException)ex).BaseException);
                    if (sqlBaseException.Number == 2627) //The exception number for primary key violation
                    {
                        duplicateError = "At least one record from the file " + cleanFileName + " already exists in the database.";
                    }

                    if (sqlBaseException.Message.Contains(consistencyKRMMessage))
                    {
                        consistencyKRMError   = sqlBaseException.Message;
                        isConsistencyKRMError = true;
                    }
                }

                if (!isConsistencyKRMError) //for this we write the message below
                {
                    if (string.IsNullOrEmpty(duplicateError))
                    {
                        LogProcessErrorToDataBase(idImport, ex.Message, fileName);
                    }
                    else
                    {
                        LogProcessErrorToDataBase(idImport, duplicateError, fileName);
                    }
                }
            }


            // consistency error treatment
            int    rowsInserted = 0;
            String strError     = String.Empty;

            try
            {
                if (isConsistencyKRMError)
                {
                    strError += consistencyKRMError;

                    rowsInserted = InsertKeyRowsMissingInDB(idImport);
                    if (rowsInserted > 0)
                    {
                        DataTable dtRowsMissing = SelectKeyRowsMissingFromDB(idImport);

                        LogWriteKeyrowsMissingToLogTable(idImport, strError, fileName, dtRowsMissing);
                    }
                    else
                    {
                        LogProcessErrorToDataBase(idImport, strError, fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                LogProcessErrorToDataBase(idImport, ex.Message, fileName);
            }
            /////////////////////////////

            try
            {
                DataSet dsNonExistingAssociates = imp.SelectNonExistingAssociateNumbers(idImport);
                if (result >= 0)
                {
                    fileNameNonExistingAssociates = GetServerPath(TargetDirectoryEnum.DIRECTORY_PROCESSED) + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt";
                }
                else
                {
                    fileNameNonExistingAssociates = GetServerPath(TargetDirectoryEnum.DIRECTORY_CANCELLED) + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt";
                }

                NonExistingAssociates = ApplicationUtils.CreateAssociatesTextFile(dsNonExistingAssociates, fileNameNonExistingAssociates);
            }
            catch (Exception ex)
            {
                throw new IndException(ex);
            }
            string msg = string.Empty;

            if (result >= 0 && result != ApplicationConstants.COST_CENTER_WITHOUT_HOURLYRATE_CODE_FROM_SP)
            {
                msg = string.Format(ApplicationMessages.IMPORT_FILE_PROCESSED, cleanFileName);
                if (NonExistingAssociates >= 0)
                {
                    fileNameNonExistingAssociates = ConfigurationManager.AppSettings["UploadFolderProcessed"] + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt";
                    msg += string.Format(ApplicationMessages.UPLOAD_MISSING_ASSOCIATES + ApplicationMessages.UPLOAD_ASSOCIATES_INSERTED, NonExistingAssociates + 1, fileNameNonExistingAssociates);
                }
                processedMessages.Add(msg);
                //If file was in this collection and it has been successfully processed, then remove it
                if (FilesWithCostCentersWithoutHR.Contains(fileName))
                {
                    FilesWithCostCentersWithoutHR.Remove(fileName);
                }
                return(1);
            }
            else
            {
                int returnValue = 0;
                if (result == ApplicationConstants.COST_CENTER_WITHOUT_HOURLYRATE_CODE_FROM_SP)
                {
                    msg  = string.Format(ApplicationMessages.IMPORT_FILE_CC_WITHOUT_HR, cleanFileName);
                    msg += "<BR>" + GetLogLink(Request.ApplicationPath, 1, idImport)
                           + "<BR>" + GetDismissFile(Request.ApplicationPath, Path.GetFileName(fileName));
                    if (!FilesWithCostCentersWithoutHR.Contains(fileName))
                    {
                        FilesWithCostCentersWithoutHR.Add(fileName);
                    }
                    returnValue = 0;
                }
                else
                {
                    msg = string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName);
                    if (FilesWithCostCentersWithoutHR.Contains(fileName))
                    {
                        FilesWithCostCentersWithoutHR.Remove(fileName);
                    }
                    returnValue = -1;
                }
                if (NonExistingAssociates >= 0)
                {
                    fileNameNonExistingAssociates = ConfigurationManager.AppSettings["UploadFolderCancelled"] + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt";
                    msg += string.Format(ApplicationMessages.UPLOAD_MISSING_ASSOCIATES, NonExistingAssociates + 1, fileNameNonExistingAssociates);
                }
                processedMessages.Add(msg);
                return(returnValue);
            }
        }