Пример #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());
        }
Пример #2
0
        public virtual CCFileList[] SearchFiles(String[] rootPaths, bool copySrcFiles, bool subFolders, int sortType, int maxLock, String[] keyExtensions, params String[] subExtensions)
#endif
        {
            if (!processing)
            {
                try
                {
                    stopSearch = false;
                    processing = true;
                    maxLock    = Math.Max(maxLock, 1);
                    List <CCFileList> res = new List <CCFileList>();
                    int counter           = 0;

                    CCFileList filesCol = new CCFileList();

                    //-- Get all files (to sort as specified) --\\
                    foreach (String rootPath in rootPaths)
                    {
                        if (stopSearch)
                        {
                            return(null);
                        }

                        filesCol.AddFiles(GetFileList(rootPath, sortType, subFolders, keyExtensions).ToArray());
                    }


                    //-- Lock all possible files --\\
                    if (filesCol != null && filesCol.FileCount > 0)
                    {
                        List <String> files = new List <String>(filesCol.Files);
                        if (sortType > 1)
                        {
                            files.Sort(new FlexListComparer(sortType, false));
                        }

                        //-- Iterate all possible files, break after locking the specified amount --\\
                        for (int i = 0; i < files.Count; i++)
                        {
                            try
                            {
                                Application.DoEvents();
                                if (stopSearch)
                                {
                                    return(null);
                                }

                                //-- Prepare to fire  'OnPreFileLock'  event --\\
                                String lstFile = files[i];
                                if (!PreFileLock(ref lstFile))
                                {
                                    continue;
                                }

                                CCFileList fileLck = new CCFileList(lstFile);
                                fileLck.OnPreFileLock  -= OnPreFileLock;
                                fileLck.OnPreFileLock  += OnPreFileLock;
                                fileLck.OnPostFileLock -= OnPostFileLock;
                                fileLck.OnPostFileLock += OnPostFileLock;

                                //-- Try to lock file name --\\
                                if (fileLck.LockFiles(CurrentProfile.LockExtension, false, copySrcFiles, false))
                                {
                                    counter++;
                                    PostFileLock(fileLck.KeyFile); //-- fire 'OnPostFileLock' event --\\

                                    #region                        //-- Get other attachment files --\\
                                    if (subExtensions != null && subExtensions.Length > 0)
                                    {
                                        //-- NF: 2012-07-06, Don't search same extension as key --\\
                                        List <String> subExts = new List <string>(subExtensions);
                                        for (int iSub = subExts.Count - 1; iSub >= 0; iSub--)
                                        {
                                            foreach (String sKy in keyExtensions)
                                            {
                                                if (String.Compare(subExts[iSub], sKy, true) == 0)
                                                {
                                                    subExts.RemoveAt(iSub);
                                                    break;
                                                }
                                            }
                                        }

                                        //-- Get attachments --\\
                                        CCFileList lckList = new CCFileList(GetFileList(Path.GetDirectoryName(fileLck.KeyFile), Path.GetFileNameWithoutExtension(fileLck.KeyFile), subFolders, subExts.ToArray()));
                                        if (lckList.FileCount > 0)
                                        {
                                            try
                                            {
                                                lckList.AddFiles(CurrentProfile.LockExtension, fileLck.KeyFile);
                                                lckList.SetKeyFile(fileLck.KeyFile);
                                                fileLck = lckList;

                                                if (!lckList.LockFiles(CurrentProfile.LockExtension, false, copySrcFiles, false))
                                                {
                                                    throw new Exception(String.Format("Failed locking collection additional \\attachment files: " + String.Join(Path.PathSeparator.ToString(), lckList.Files)));
                                                }
                                            }
                                            catch (Exception et)
                                            {
                                                ILog.LogError(et);
                                                lckList.RollbackLock(CurrentProfile.LockExtension); //-- reverse extension state \ rollback file ext. --\\
                                            }
                                        }
                                    }

                                    res.Add(fileLck);
                                    #endregion
                                }
                            }
                            catch (Exception ep)
                            {
                                ILog.LogError(ep);
                            }

                            //-- Break after max count, to lock as many files specified --\\
                            if (counter >= maxLock)
                            {
                                break;
                            }
                        }
                    }

                    return(res.ToArray());
                }
                catch (Exception ex)
                {
                    ILog.LogError(ex);
                }
            }
Пример #3
0
        /*
         * /// <summary>
         * /// Create an eFlow collection from the specified DataTable.
         * /// </summary>
         * /// <param name="collectionData">The collection data in the specified data table.</param>
         * /// <param name="applicationName">The eFlow application name to insert the collection to.</param>
         * /// <param name="copySourceFiles">Copy the source file when true (as opposed to move).</param>
         * /// <param name="errMsg">Will return the error message if any error occured.</param>
         * /// <returns>The error code, 1 = sucsess.</returns>
         * public bool TestCreateCollection(String filePath, String stationName, String applicationName)
         * {
         *  try
         *  {
         *      List<ITisPageParams> pages = new List<ITisPageParams>();
         *      ITisClientServicesModule csm = csmManager.GetCsm(applicationName, stationName, true);
         *      pages.Add(csm.Setup.get_FlowByIndex(0).get_FormByIndex(0).get_PageByIndex(0));
         *      CCCollection.CCPage pg = new CCCollection.CCPage(null, pages[0], "Demo");
         *      CCCollection col = new CCCollection();
         *      for (int i = 0; i < 7; i++)
         *      {
         *          col.AddForm(new CCCollection.CCForm(null, null, csm.Setup.get_FlowByIndex(0).get_FormByIndex(0).Name, pg));
         *      }
         *      col.ImagePath = filePath;// @"C:\Program Files\TIS\eFlow 4.5\Sample Applications\SimpleDemo\Office7Pages.TIF";
         *     // col.FlowType = "OfficeStore";
         *
         *      //-- Create and define the Creator class --\\
         *      using (CCreator crt = new CCreator())
         *      {
         *          crt.ValidatePagePerImagePage = false;
         *          crt.CurrentProfile = CCConfiguration.CCConfigurationData.FromXml("Default2");
         *          int errCode=0;
         *         String[] res= crt.CreateCollections(csm,  out errCode, col);
         *         if (res.Length > 0)
         *         {
         *             return true;
         *         }
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      ILog.LogError(ex);
         *  }
         *  return false;
         * }
         */

        /// <summary>
        /// Create an eFlow collection from the specified DataTable.
        /// </summary>
        /// <param name="collectionData">The collection data in the specified data table.</param>
        /// <param name="applicationName">The eFlow application name to insert the collection to.</param>
        /// <param name="copySourceFiles">Copy the source file\s when true (as opposed to move).</param>
        /// <param name="errMsg">Will return the error message if any error occured.</param>
        /// <returns>The error code, 1 = sucsess.</returns>
        public bool CreateCollection(DataTable collectionData, String applicationName, bool copySourceFiles, out int errCode, out String errMsg)
        {
            //-- Set returning values --\\
            errCode = (int)CCEnums.CCErrorCodes.E0000;
            errMsg  = null;
            String[]     createdFiles = null;
            CCCollection coll         = null;

            try
            {
                #region //-- Load and validate profile --\\
                if (config == null)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0101;
                    errMsg  = String.Format("{0}:, error code [{1}]", CCConstants.E0101, errCode);
                    throw new Exception(errMsg);
                }

                if (String.IsNullOrEmpty(applicationName))
                {
                    applicationName = CCEnums.CCNames.Default.ToString();
                }
                currCfg = config.GetConfiguration(applicationName);

                if (currCfg == null)
                {
                    errCode = (int)CCEnums.CCErrorCodes.E0102;
                    errMsg  = String.Format("{0}: [{1}], error code [{2}]", CCConstants.E0102, applicationName, errCode);
                    throw new Exception(errMsg);
                }
                #endregion

                //-- Create and define the Creator class --\\
                using (CCreator crt = new CCreator())
                {
                    crt.CurrentProfile.CopySourceFiles = copySourceFiles;
                    if (currCfg != null)
                    {
                        crt.CurrentProfile.LockExtension = currCfg.LockExtension;
                    }

                    ITisClientServicesModule csm = csmManager.GetCsm(String.IsNullOrEmpty(currCfg.LoginApplication) ? applicationName : currCfg.LoginApplication, currCfg.LoginStation, true);

                    if (csm == null)
                    {
                        errCode = (int)CCEnums.CCErrorCodes.E0210;
                        errMsg  = String.Format("{0}, Application name: [{1}], Station name: [{2}] error code [{3}]", CCConstants.E0210, String.IsNullOrEmpty(currCfg.LoginApplication) ? applicationName ?? String.Empty : currCfg.LoginApplication ?? String.Empty, currCfg.LoginStation ?? String.Empty, errCode);
                        throw new Exception(errMsg);
                    }

                    //-- Load CCCollection from a data table (and validate all the data) --\\
                    coll = CCDataTable.FromDataTable(currCfg, out errCode, out errMsg, copySourceFiles, out createdFiles, collectionData);

                    if (errCode != 1)
                    {
                        errMsg = String.Format("{0}, table name: [{1}], error code [{2}]", CCConstants.E0091, collectionData != null ? collectionData.TableName ?? String.Empty : String.Empty, errCode);
                        throw new Exception(errMsg);
                    }

                    if (coll == null)
                    {
                        errCode = (int)CCEnums.CCErrorCodes.E0091;
                        errMsg  = String.Format("{0}, table name: [{1}], error code [{2}]", CCConstants.E0091, collectionData != null ? collectionData.TableName ?? String.Empty : String.Empty, errCode);
                        throw new Exception(errMsg);
                    }

                    try
                    {
                        //--<< Call eFlow to create the collection >>--\\
                        String[] resCols = crt.CreateCollections(csm, out errCode, coll);
                        if (resCols == null || resCols.Length != 1)
                        {
                            errCode = errCode <= 0 ? (int)CCEnums.CCErrorCodes.E0092: errCode;
                            errMsg  = crt.LastErrors;
                            throw new Exception(String.Format("{0}, {1}, error code [{2}]", CCConstants.E0092, errMsg, errCode));
                        }
                        else
                        {
                            ILog.LogInfo("Done creating collection [{0}] in eFlow system", resCols[0]);
                            errCode = (int)CCEnums.CCErrorCodes.E0001;
                            errMsg  = resCols[0];//-- Return the name of the created collection --\\
                            //\\ return errCode == (int)CCEnums.CCErrorCodes.E0001;
                        }
                    }
                    catch (Exception ec)
                    {
                        errCode = (int)CCEnums.CCErrorCodes.E0092;
                        errMsg  = crt.LastErrors;
                        if (String.IsNullOrEmpty(errMsg))
                        {
                            errMsg = CCConstants.E0092 + "," + ec.Message;
                        }
                        throw new Exception(String.Format("{0}, {1}, {2}, error code [{3}]", CCConstants.E0092, errMsg, ec.Message, errCode));
                    }
                }
            }
            catch (Exception ex)
            {
                //\\ if (errCode < (int)CCEnums.CCErrorCodes.E0000) errCode = (int)CCEnums.CCErrorCodes.E0000;
                ILog.LogError(ex);
                //-- Move collections to errror folder --\\
                if (coll != null && coll.Files.Length > 0 && currCfg != null && !String.IsNullOrEmpty(currCfg.ErrorFolderPath))
                {
                    CCFileList.MoveToFolder(currCfg.ErrorFolderPath, coll.Files);
                }
                if (currCfg == null || currCfg.ThrowAllExceptions)
                {
                    throw ex;
                }
            }
            finally
            {
                //-- Delete files created by this method --\\ 
                if (copySourceFiles && createdFiles != null && createdFiles.Length > 0)
                {
                    foreach (String sf in createdFiles)
                    {
                        try
                        {
                            if (File.Exists(sf ?? String.Empty))
                            {
                                File.SetAttributes(sf, FileAttributes.Normal);
                                File.Delete(sf);
                            }
                        }
                        catch (Exception ex)
                        {
                            ILog.LogError(ex);
                            throw ex;
                        }
                    }
                }
            }
            //\\  return false;
            return(errCode == (int)CCEnums.CCErrorCodes.E0001);
        }