Ejemplo n.º 1
0
        /// <summary>
        /// Read and load pages data into the collection definition.
        /// </summary>
        /// <param name="cfg">The configuration profile to use.</param>
        /// <param name="errCode">Returns the error code of this operation.</param>
        /// <param name="errMsg">Returns the error string of this operation</param>
        /// <param name="expectedNumberOfPages">The expected nuumber of pages in the collection.</param>
        /// <param name="coll">The collection definition to add the data to.</param>
        /// <param name="colData">The data table containing the data for the collection.</param>
        /// <returns>An array oif file paths for files that were created in this method</returns>
        private static String[] ReadPagesData(CCConfiguration.CCConfigurationData cfg, out int errCode, out String errMsg, int expectedNumberOfPages, ref CCCollection coll, DataTable colData)
        {
            errCode = (int)CCEnums.CCErrorCodes.E0000;
            errMsg  = null;
            List <String> result = new List <String>();

            try
            {
                //-- Validate expected page count --\\
                if (expectedNumberOfPages <= 0)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0051;
                    errMsg  = String.Format("{0}, error code [{1}]", CCConstants.E0051, errCode);
                    throw new Exception(errMsg);
                }

                int numberOfImagePages = CCUtils.GetImagePageCount(coll.ImagePath, null);

                //-- Validate page count --\\
                if (numberOfImagePages <= 0)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0061;
                    errMsg  = String.Format("{0}, file path [{1}], error code [{2}]", CCConstants.E0061, coll.ImagePath, errCode);
                    throw new Exception(errMsg);
                }

                //-- Validate page count against expected page count --\\
                if (numberOfImagePages != expectedNumberOfPages)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0050;
                    errMsg  = String.Format("{4}. expected number of pages [{1}], actual image count [{2}], file path [{3}], error code [{0}]", errCode, expectedNumberOfPages, numberOfImagePages, coll.ImagePath, CCConstants.E0050);
                    throw new Exception(errMsg);
                }

                List <CCCollection.CCForm> forms = new List <CCCollection.CCForm>();

                for (int pageCount = 1; pageCount <= expectedNumberOfPages; pageCount++)
                {
                    Dictionary <String, String> lstDct = new Dictionary <String, String>();
                    List <String> attachments          = new List <String>();
                    DataRow[]     pageRows             = colData.Select(String.Format("{0}={1}", CCEnums.CCTableColumns.Level, IsStringData(colData.Columns[CCEnums.CCTableColumns.Level.ToString()]) ? "'" + pageCount.ToString() + "'" :  pageCount.ToString()));
                    if (pageRows == null || pageRows.Length <= 0)
                    {
                        pageRows = colData.Select(String.Format("{0}='{1}'", CCEnums.CCTableColumns.Level, pageCount));
                    }

                    //-- Process pages data --\\
                    if (pageRows != null && pageRows.Length > 0)
                    {
                        List <int> pageNumbers = new List <int>();
                        String     startDate   = null;
                        String     endDate     = null;

                        //-- Iterate pages rows --\\
                        foreach (DataRow dr in pageRows)
                        {
                            //-- Get the page number the data is itntended to --\\
                            int pageNumber = CCUtils.StrToIntDef(dr[CCEnums.CCTableColumns.Level.ToString()].ToString(), -1);
                            if (!pageNumbers.Contains(pageNumber))
                            {
                                pageNumbers.Add(pageNumber);
                            }

                            //-- << Read pages data >>--\\
                            if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.PageType.ToString(), true) == 0)
                            {
                                //-- PageType --\\
                                CCUtils.AddSetDictionaryItem(CCEnums.CCPagesDataType.PageType.ToString(), dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                            }
                            else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.StartDate.ToString(), true) == 0)
                            {
                                //-- StartDate --\\
                                startDate = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                                CCUtils.AddSetDictionaryItem(CCEnums.CCPagesDataType.StartDate.ToString(), startDate, excpetionOnDictDuplicates, ref lstDct);
                            }
                            else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.EndDate.ToString(), true) == 0)
                            {
                                //-- EndDate --\\
                                endDate = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                                CCUtils.AddSetDictionaryItem(CCEnums.CCPagesDataType.EndDate.ToString(), endDate, excpetionOnDictDuplicates, ref lstDct);
                            }
                            else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.Attachments.ToString(), true) == 0)
                            {
                                //-- Attachments --\\
                                String val = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                                if (!String.IsNullOrEmpty(val) && !File.Exists(val))
                                {
                                    errCode = (int)CCEnums.CCErrorCodes.E0065;
                                    errMsg  = String.Format("{1}, file path[{2}], error code [{0}]", errCode, CCConstants.E0065, val ?? String.Empty);
                                    throw new Exception(errMsg);
                                }
                                else
                                {
                                    if (!attachments.Contains(val))
                                    {
                                        attachments.Add(val);
                                    }
                                }
                            }
                            else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.XmlPrdPath.ToString(), true) == 0)
                            {
                                //-- XmlPrdPath --\\
                                int    errC          = 0;
                                String errS          = null;
                                String sourceXmlPath = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();

                                //-- Convert XML to PRD  and add as page attachment --\\
                                String prdPath = DeserializePRD(out errC, out errS, pageNumber, false, sourceXmlPath);
                                if (!String.IsNullOrEmpty(prdPath))
                                {
                                    if (File.Exists(sourceXmlPath))
                                    {
                                        //-- Lock PRD file --\\
                                        String lockPrd = CCFileList.GetLockedFilePath(prdPath, cfg.LockExtension);
                                        if (String.Compare(lockPrd, prdPath, true) != 0)
                                        {
                                            if (File.Exists(lockPrd))
                                            {
                                                File.SetAttributes(lockPrd, FileAttributes.Normal);
                                                File.Delete(lockPrd);
                                            }

                                            File.SetAttributes(prdPath, FileAttributes.Normal);
                                            File.Move(prdPath, lockPrd);
                                        }

                                        File.Delete(sourceXmlPath);
                                        if (!attachments.Contains(lockPrd))
                                        {
                                            attachments.Add(lockPrd);
                                        }
                                        if (!result.Contains(lockPrd))
                                        {
                                            result.Add(lockPrd);
                                        }
                                    }
                                }
                            }
                            else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCPagesDataType.MetaData.ToString(), true) == 0)
                            {
                                //-- Additional MetaData--\\
                                String tagName = dr[CCEnums.CCTableColumns.Key.ToString()] is DBNull || String.IsNullOrEmpty(dr[CCEnums.CCTableColumns.Key.ToString()].ToString()) ? null : dr[CCEnums.CCTableColumns.Key.ToString()].ToString();
                                if (!String.IsNullOrEmpty(tagName))
                                {
                                    CCUtils.AddSetDictionaryItem(tagName, dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                                }
                            }
                        }

                        //-- Validate page dates --\\
                        if (!ValidateDates(startDate, endDate, out errCode, out errMsg))
                        {
                            //-- Validate start and end time\date --\\
                            errCode = (int)CCEnums.CCErrorCodes.E0020;
                            errMsg  = String.Format("{0}, {1} [{2}],{3} [{4}], page number [{5}], error code [{6}]", CCConstants.E0020, CCEnums.CCPagesDataType.StartDate, startDate, CCEnums.CCPagesDataType.EndDate, endDate, pageCount, errCode);
                            throw new Exception(errMsg);
                        }
                    }

                    //-- Create a form and page, NF 2011-11-07 added support for multipage per form --\\
                    CCCollection.CCForm frm = cfg.MultiPagePerForm ? forms.Count > 0 ? forms[0] : new CCCollection.CCForm(null, null, null) : new CCCollection.CCForm(null, null, null);


                    if (!cfg.MultiPagePerForm || (cfg.MultiPagePerForm && forms.Count <= 0))
                    {
                        frm.NamedUserTags.NativeDictionary = lstDct;                                                                       //-- Add page Named user tags to Form user tags --\\
                    }
                    CCCollection.CCPage pg = new CCCollection.CCPage();
                    pg.Attachments = attachments.ToArray();
                    pg.NamedUserTags.NativeDictionary = lstDct;
                    if (!cfg.MultiPagePerForm || forms.Count <= 0)
                    {
                        frm.Pages = new CCCollection.CCPage[] { pg };
                    }
                    else
                    {
                        List <CCCollection.CCPage> pgs = new List <CCCollection.CCPage>(frm.Pages);
                        pgs.Add(pg);
                        frm.Pages = pgs.ToArray();
                    }

                    if (!cfg.MultiPagePerForm || forms.Count <= 0)
                    {
                        forms.Add(frm);
                    }
                }

                //-- Do some validations --\\
                if (expectedNumberOfPages != forms.Count)
                {
                    if (!cfg.MultiPagePerForm || forms.Count <= 0)
                    {
                        errCode = (int)CCEnums.CCErrorCodes.E0090;
                        errMsg  = String.Format("Error code [{0}], {1},expected number of pages [{2}], eflow form count [{3}]", errCode, CCConstants.E0090, expectedNumberOfPages, forms.Count);
                        throw new Exception(errMsg);
                    }
                    else
                    {
                        if (forms[0].Pages.Length != expectedNumberOfPages)
                        {
                            errCode = (int)CCEnums.CCErrorCodes.E0090;
                            errMsg  = String.Format("Error code [{0}], {1},expected number of pages [{2}] in one form, eflow form count [{3}]", errCode, CCConstants.E0090, expectedNumberOfPages, forms.Count);
                            throw new Exception(errMsg);
                        }
                    }
                }


                errCode    = (int)CCEnums.CCErrorCodes.E0001;
                coll.Forms = forms.ToArray();
            }
            catch (Exception ex)
            {
                ILog.LogError(ex, false);

                if (String.IsNullOrEmpty(errMsg))
                {
                    errMsg = CCConstants.E0000 + "," + ex.Message;
                }
                throw (ex);
            }
            return(result.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read header data.
        /// </summary>
        /// <param name="cfg">The configuration profile to use.</param>
        /// <param name="errCode">The return error code, 1  is OK, anything else not.</param>
        /// <param name="errMsg">Will contain the error data if any error occures.</param>
        /// <param name="expectedNumberOfPages">Returns the expected number of pages.</param>
        /// <param name="dbCollectionData">The data table with rows to get Hedaer info.</param>
        /// <returns>A CCCollection when successfull, null when failed,</returns>
        public static CCCollection ReadHeaderData(CCConfiguration.CCConfigurationData cfg, out int errCode, out String errMsg, out int expectedNumberOfPages, DataTable dbCollectionData)
        {
            errCode = -1;
            errMsg  = null;
            expectedNumberOfPages = 0;

            try
            {
                if (dbCollectionData == null || dbCollectionData.Rows.Count <= 0 || dbCollectionData.Columns.Count < 4)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0041;
                    errMsg  = String.Format("{0}, error code [{1}]", CCConstants.E0041, errCode);
                    throw new Exception(errMsg);
                }

                //-- Get header rows --\\
                DataRow[] headerRows = dbCollectionData.Select(String.Format("{0}={1}", CCEnums.CCTableColumns.Level, IsStringData(dbCollectionData.Columns[CCEnums.CCTableColumns.Level.ToString()]) ? "'0'":"0"));
                if (headerRows == null || headerRows.Length <= 0)
                {
                    headerRows = dbCollectionData.Select(String.Format("{0}='0'", CCEnums.CCTableColumns.Level));
                }


                //-- Process header data --\\
                if (headerRows != null && headerRows.Length > 0)
                {
                    CCCollection res = new CCCollection();
                    Dictionary <String, String> lstDct = new Dictionary <String, String>();
                    List <String> attachments          = new List <String>();
                    String        startDate            = null;
                    String        endDate = null;
                    bool          hasFlow = false;


                    foreach (DataRow dr in headerRows)
                    {
                        //-- Extract columns data --\\
                        if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.Name.ToString(), true) == 0)
                        {
                            //-- Name --\\
                            res.Name = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.FlowType.ToString(), true) == 0)
                        {
                            //-- FlowType --\\
                            hasFlow      = true;
                            res.FlowType = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.AbsolutePriority.ToString(), true) == 0)
                        {
                            //-- AbsolutePriority --\\
                            double absPrio = 0d;
                            if (double.TryParse(dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), out absPrio))
                            {
                                res.AbsolutePriority = absPrio;
                            }
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.StartDate.ToString(), true) == 0)
                        {
                            //-- StartDate --\\
                            startDate = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.StartDate.ToString(), startDate, excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.EndDate.ToString(), true) == 0)
                        {
                            //-- EndDate --\\
                            endDate = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.EndDate.ToString(), endDate, excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.DeviceID.ToString(), true) == 0)
                        {
                            //-- DeviceID --\\
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.DeviceID.ToString(), dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.UserID.ToString(), true) == 0)
                        {
                            //-- UserID --\\
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.UserID.ToString(), dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.MachineID.ToString(), true) == 0)
                        {
                            //-- MachineID --\\
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.MachineID.ToString(), dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.TotalPagesInBatch.ToString(), true) == 0)
                        {
                            //-- TotalPagesInBatch --\\ 
                            expectedNumberOfPages = CCUtils.StrToIntDef(dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), 0);
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.TotalPagesInBatch.ToString(), expectedNumberOfPages.ToString(), excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.ImagePath.ToString(), true) == 0)
                        {
                            //-- ImagePath --\\
                            res.ImagePath = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.Attachments.ToString(), true) == 0)
                        {
                            //-- Attachments --\\
                            String val = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                            if (!String.IsNullOrEmpty(val) && !File.Exists(val))
                            {
                                errCode = (int)CCEnums.CCErrorCodes.E0065;
                                errMsg  = String.Format("{0},  file path [{1}], error code [{2}]", CCConstants.E0065, val ?? String.Empty, errCode);
                                throw new Exception(errMsg);
                            }
                            else
                            {
                                if (!attachments.Contains(val))
                                {
                                    attachments.Add(val);
                                }
                            }
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.BatchType.ToString(), true) == 0)
                        {
                            //-- BatchType --\\
                            CCUtils.AddSetDictionaryItem(CCEnums.CCHedaerDataType.BatchType.ToString(), dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.Priority.ToString(), true) == 0)
                        {
                            //-- Priority --\\
                            String pr = dr[CCEnums.CCTableColumns.Data.ToString()].ToString().ToUpper();
                            if (!String.IsNullOrEmpty(pr) && !Regex.IsMatch(pr, CCConstants.RX_PRIORITY))
                            {
                                errCode = (int)CCEnums.CCErrorCodes.E0070;
                                errMsg  = String.Format("{0}, specified value [{1}], error code [{2}]", CCConstants.E0070, pr, errCode);
                                throw new Exception(errMsg);
                            }
                            else
                            {
                                res.Priority = pr == WorkflowPriorityLevel.AboveNormal.ToString().Substring(0, 1) || pr.Replace(" ", String.Empty) == WorkflowPriorityLevel.AboveNormal.ToString().ToUpper()  ? WorkflowPriorityLevel.AboveNormal :
                                               pr == WorkflowPriorityLevel.High.ToString().Substring(0, 1) || pr == WorkflowPriorityLevel.High.ToString().ToUpper() ? WorkflowPriorityLevel.High :
                                               pr == WorkflowPriorityLevel.Low.ToString().Substring(0, 1) || pr == WorkflowPriorityLevel.Low.ToString().ToUpper() ? WorkflowPriorityLevel.Low : WorkflowPriorityLevel.Normal;
                            }
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.ApplicationName.ToString(), true) == 0)
                        {
                            //-- ApplicationName --\\
                            res.LoginApplication = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.StationName.ToString(), true) == 0)
                        {
                            //-- StationName --\\
                            res.LoginStation = dr[CCEnums.CCTableColumns.Data.ToString()].ToString();
                        }
                        else if (String.Compare(dr[CCEnums.CCTableColumns.DataType.ToString()].ToString(), CCEnums.CCHedaerDataType.MetaData.ToString(), true) == 0)
                        {
                            //-- Additional MetaData --\\
                            String tagName = dr[CCEnums.CCTableColumns.Key.ToString()] is DBNull || String.IsNullOrEmpty(dr[CCEnums.CCTableColumns.Key.ToString()].ToString()) ? null : dr[CCEnums.CCTableColumns.Key.ToString()].ToString();
                            if (!String.IsNullOrEmpty(tagName))
                            {
                                CCUtils.AddSetDictionaryItem(tagName, dr[CCEnums.CCTableColumns.Data.ToString()].ToString(), excpetionOnDictDuplicates, ref lstDct);
                            }
                        }
                    }

                    if (!hasFlow && !String.IsNullOrEmpty(cfg.FlowType))
                    {
                        //-- Take flow type from configuration, if defined --\\
                        res.FlowType = cfg.FlowType;
                    }

                    #region //-- Do some validations --\\
                    if (expectedNumberOfPages <= 0)
                    {
                        //-- Validate expected number of pages value --\\
                        errCode = (int)CCEnums.CCErrorCodes.E0051;
                        errMsg  = String.Format("{0}, error code [{1}]", CCConstants.E0051, errCode);
                        throw new Exception(errMsg);
                    }
                    else if (!File.Exists(res.ImagePath))
                    {
                        //-- Validate image path --\\
                        errCode = (int)CCEnums.CCErrorCodes.E0060;
                        errMsg  = String.Format("{0}, file path [{1}], error code [{2}]", CCConstants.E0060, res.ImagePath ?? String.Empty, errCode);
                        throw new Exception(errMsg);
                    }
                    else if (!String.IsNullOrEmpty(res.Name) && !Regex.IsMatch(res.Name, CCConstants.RX_COLLECTION_NAME))
                    {
                        //-- Validate collection name --\\
                        errCode = (int)CCEnums.CCErrorCodes.E0010;
                        errMsg  = String.Format("{0}, error code [{1}]", CCConstants.E0010, errCode);
                        throw new Exception(errMsg);
                    }
                    else if (!ValidateDates(startDate, endDate, out errCode, out errMsg))
                    {
                        //-- Validate start and end time\date --\\
                        errCode = (int)CCEnums.CCErrorCodes.E0020;
                        errMsg  = String.Format("{0}, {1} [{2}],{3} [{4}], error code [{5}]", CCConstants.E0020, CCEnums.CCHedaerDataType.StartDate, startDate, CCEnums.CCHedaerDataType.EndDate, endDate, errCode);
                        throw new Exception(errMsg);
                    }
                    #endregion


                    //-- Finish setting data --\\
                    res.Attachments = attachments.ToArray();
                    res.NamedUserTags.NativeDictionary = lstDct;

                    //-- Done gathering data from all header rows --\\
                    errCode = (int)CCEnums.CCErrorCodes.E0001;
                    return(res);
                }
                else
                {
                    errCode = 999;
                    errMsg  = String.Format("{0}, error code [{1}]", CCConstants.E0040, errCode);
                    throw new Exception(errMsg);
                }
            }
            catch (Exception ex)
            {
                ILog.LogError(ex, false);
                throw (ex);
            }
        }