Пример #1
0
        public static bool uploadFiletoVaultFolder(VDF.Vault.Currency.Connections.Connection connection, string destinationTempFolder, string folderID, string comment, string fileName, byte[] fileBytes)
        {
            try
            {
                VDF.Vault.Currency.Entities.Folder fld   = FolderHelper.getFolderusingID(connection, folderID);
                List <FileAssocParam> fileAssocParamList = new List <FileAssocParam>();
                FileAssocParam        fileAssocArray     = new FileAssocParam();
                FileAssocParam[]      paramArray         = fileAssocParamList.ToArray();
                BOM _bom = new BOM();
                //string destinationFile = System.IO.Path.GetTempPath() + fileName;
                System.IO.File.WriteAllBytes(destinationTempFolder, fileBytes);
                VDF.Currency.FilePathAbsolute FPA = new VDF.Currency.FilePathAbsolute(destinationTempFolder);
                FileIteration FI = connection.FileManager.AddFile(fld, comment, paramArray, _bom, FileClassification.None, false, FPA);

                //cehck if file exists
                if (FI.FolderId.ToString() == folderID)
                {
                    ZGHCC.writeLog("File uplaoded and it Exists: " + folderID + " | " + comment);
                    boolRes = true;
                }
                else
                {
                    boolRes = false;
                }
            }
            catch (SystemException ex)
            {
                ZGHCC.writeLog("uploadFiletoVaultFolder EX: " + ex.ToString());
                statusMess = null;
                Debug.Write("\nError: " + ex.ToString());
                statusMess = "Error: \n" + ex.ToString();
                boolRes    = false;
            }
            return(boolRes);
        }
Пример #2
0
        private void GetFiles()
        {
            topAssemblyName = selectedFile.EntityName;
            VDF.Vault.Settings.AcquireFilesSettings acquireFileSettings = new VDF.Vault.Settings.AcquireFilesSettings(vaultConnection);
            acquireFileSettings.AddFileToAcquire(selectedFile, acquireFileSettings.DefaultAcquisitionOption);
            acquireFileSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = true;
            acquireFileSettings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = true;
            acquireFileSettings.DefaultAcquisitionOption = VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download;
            FilesResults = vaultConnection.FileManager.AcquireFiles(acquireFileSettings);

            foreach (VDF.Vault.Results.FileAcquisitionResult fileResults in FilesResults.FileResults)
            {
                if (fileResults.File.EntityName == topAssemblyName)
                {
                    if (!InList(fileResults.File.EntityName, referenceDocs))
                    {
                        ReferenceDoc newDoc = new ReferenceDoc();
                        newDoc.PartName         = fileResults.File.EntityName;
                        newDoc.PartPath         = fileResults.LocalPath.ToString();
                        newDoc.DrawingName      = System.IO.Path.ChangeExtension(fileResults.File.EntityName, ".idw");
                        newDoc.DrawingPath      = System.IO.Path.ChangeExtension(fileResults.LocalPath.ToString(), ".idw");
                        newDoc.VaultDrawingPath = newDoc.DrawingPath.Replace("C:\\_Vault_Working_Folder", "$").Replace("\\", "/");
                        referenceDocs.Add(newDoc);
                    }
                    VDF.Currency.FilePathAbsolute filePathAbs = fileResults.LocalPath;
                    assemblyPath = filePathAbs.ToString();
                }

                if (IsDoc(fileResults.File.EntityName) && fileResults.File.EntityName != topAssemblyName)
                {
                    if (!InList(fileResults.File.EntityName, referenceDocs))
                    {
                        ReferenceDoc newDoc = new ReferenceDoc();
                        newDoc.PartName         = fileResults.File.EntityName;
                        newDoc.PartPath         = fileResults.LocalPath.ToString();
                        newDoc.DrawingName      = System.IO.Path.ChangeExtension(fileResults.File.EntityName, ".idw");
                        newDoc.DrawingPath      = System.IO.Path.ChangeExtension(fileResults.LocalPath.ToString(), ".idw");
                        newDoc.VaultDrawingPath = newDoc.DrawingPath.Replace("C:\\_Vault_Working_Folder", "$").Replace("\\", "/");
                        referenceDocs.Add(newDoc);
                    }
                }
            }

            referenceDocs.Sort((x, y) => x.DrawingName.CompareTo(y.DrawingName));

            string[] vaultDrawingFiles = new string[referenceDocs.Count];
            int      i = 0;

            foreach (ReferenceDoc rd in referenceDocs)
            {
                if (CheckForDups(rd.DrawingName, referenceDocs))
                {
                    vaultDrawingFiles[i] = rd.VaultDrawingPath;
                    i++;
                }
            }

            drawingFiles = vaultConnection.WebServiceManager.DocumentService.FindLatestFilesByPaths(vaultDrawingFiles);

            List <VDF.Vault.Currency.Entities.FileIteration> fileIters = new List <VDF.Vault.Currency.Entities.FileIteration>();

            foreach (ACW.File vFile in drawingFiles)
            {
                if (vFile.Name != null)
                {
                    fileIters.Add(new VDF.Vault.Currency.Entities.FileIteration(vaultConnection, vFile));
                }
            }

            VDF.Vault.Settings.AcquireFilesSettings drawingFileSetting = new VDF.Vault.Settings.AcquireFilesSettings(vaultConnection);

            foreach (VDF.Vault.Currency.Entities.FileIteration fi in fileIters)
            {
                if (fi.EntityName != null)
                {
                    drawingFileSetting.AddFileToAcquire(fi, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download);
                }
            }

            VDF.Vault.Results.AcquireFilesResults drawingFileResults = vaultConnection.FileManager.AcquireFiles(drawingFileSetting);

            foreach (ReferenceDoc rd in referenceDocs)
            {
                InventorApprentice.ApprenticeServerDocument doc;
                doc         = apprentice.Open(rd.PartPath);
                rd.Revision = GetRevision(doc);
                rd.PDFName  = System.IO.Path.GetFileNameWithoutExtension(rd.PartName) + "rev--" + rd.Revision + ".pdf";
            }
        }
Пример #3
0
        private void CheckForUpdates(string serverName, string vaultName, Connection conn)
        {
            if (m_restartPending)
            {
                return;
            }

            Settings settings = Settings.Load();

            // user previously indicated not to download updates and not to be prompted
            if (settings.NeverDownload)
            {
                return;
            }

            string defaultFolder = conn.WebServiceManager.KnowledgeVaultService.GetVaultOption(DEFAULT_FOLDER_OPTION);

            if (defaultFolder == null || defaultFolder.Length == 0)
            {
                return;
            }
            if (!defaultFolder.EndsWith("/"))
            {
                defaultFolder += "/";
            }

            string deploymentPath = defaultFolder + PACKAGE_NAME;

            Autodesk.Connectivity.WebServices.File [] files = conn.WebServiceManager.DocumentService.FindLatestFilesByPaths(
                deploymentPath.ToSingleArray());

            if (files == null || files.Length == 0 || files[0].Id <= 0 || files[0].Cloaked)
            {
                return; // no package found
            }
            VaultEntry entry = settings.GetOrCreateEntry(serverName, vaultName);

            if (entry != null && entry.LastUpdate >= files[0].CkInDate)
            {
                return;  // we are up to date
            }
            StringBuilder updateList = new StringBuilder();

            string          deploymentXml   = conn.WebServiceManager.KnowledgeVaultService.GetVaultOption(DEPLOYMENT_CONFIG_OPTION);
            DeploymentModel deploymentModel = DeploymentModel.Load(deploymentXml);

            if (deploymentModel == null || deploymentModel.Containers == null)
            {
                return;
            }

            foreach (DeploymentContainer container in deploymentModel.Containers)
            {
                updateList.AppendLine(container.DisplayName);

                foreach (DeploymentItem item in container.DeploymentItems)
                {
                    updateList.AppendLine("- " + item.DisplayName);
                }

                updateList.AppendLine();
            }

            AskDialog    ask    = new AskDialog(updateList.ToString());
            DialogResult result = ask.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (ask.AskResult == AskDialog.AskResultEnum.No)
                {
                    return;
                }
                else if (ask.AskResult == AskDialog.AskResultEnum.Never)
                {
                    settings.NeverDownload = true;
                    settings.Save();
                    return;
                }
            }
            else
            {
                return;
            }

            // if we got here, the user cliecked yes.


            string tempPath = Path.Combine(Path.GetTempPath(), "Thunderdome");

            if (!tempPath.EndsWith("\\"))
            {
                tempPath += "\\";
            }

            if (Directory.Exists(tempPath))
            {
                Directory.Delete(tempPath, true);
            }
            DirectoryInfo dirInfo = Directory.CreateDirectory(tempPath);

            UtilSettings utilSettings = new UtilSettings();

            utilSettings.TempFolder = tempPath;

            // the first arg should be the EXE path
            utilSettings.VaultClient = Environment.GetCommandLineArgs()[0];

            string zipPath = Path.Combine(tempPath, PACKAGE_NAME);

            VDF.Vault.Currency.Entities.FileIteration vdfFile = new VDF.Vault.Currency.Entities.FileIteration(conn, files[0]);
            VDF.Currency.FilePathAbsolute             vdfPath = new VDF.Currency.FilePathAbsolute(zipPath);
            AcquireFilesSettings acquireSettings = new AcquireFilesSettings(conn, false);

            acquireSettings.AddFileToAcquire(vdfFile, AcquireFilesSettings.AcquisitionOption.Download, vdfPath);
            AcquireFilesResults acquireResults = conn.FileManager.AcquireFiles(acquireSettings);

            foreach (FileAcquisitionResult acquireResult in acquireResults.FileResults)
            {
                if (acquireResult.Exception != null)
                {
                    throw acquireResult.Exception;
                }
            }

            // clear the read-only bit
            System.IO.File.SetAttributes(zipPath, FileAttributes.Normal);

            FastZip zip = new FastZip();

            zip.ExtractZip(zipPath, tempPath, null);

            MasterController mc = new MasterController(conn, vaultName);

            mc.SetMoveOperations(tempPath, utilSettings);

            utilSettings.Save();

            MessageBox.Show("Updates downloaded. " +
                            "You need exit the Vault client to complete the update process. " + Environment.NewLine +
                            "The Vault Client will restart when the update is complete.",
                            "Exit Required");

            m_restartPending = true;

            entry.LastUpdate = files[0].CkInDate;
            settings.Save();
        }
        public JobOutcome Execute(IJobProcessorServices context, IJob job)
        {
            try
            {
                Inventor.InventorServer mInv = context.InventorObject as InventorServer;

                #region validate execution rules

                //pick up this job's context
                mConnection = context.Connection;
                Autodesk.Connectivity.WebServicesTools.WebServiceManager mWsMgr = mConnection.WebServiceManager;
                long   mEntId    = Convert.ToInt64(job.Params["EntityId"]);
                string mEntClsId = job.Params["EntityClassId"];

                // only run the job for files; handle the scenario, that an item lifecycle transition accidently submitted the job
                if (mEntClsId != "FILE")
                {
                    context.Log(null, "This job type is for files only.");
                    return(JobOutcome.Failure);
                }

                // only run the job for ipt and iam file types,
                List <string> mFileExtensions = new List <string> {
                    ".ipt", ".iam", "idw", "dwg"
                };
                ACW.File mFile = mWsMgr.DocumentService.GetFileById(mEntId);
                if (!mFileExtensions.Any(n => mFile.Name.EndsWith(n)))
                {
                    context.Log(null, "Skipped Job execution as file type did not match iLogic enabled files (ipt, iam, idw/dwg");
                    return(JobOutcome.Success);
                }
                //run iLogic for Inventor DWG file types/skip AutoCAD DWG files
                ACW.PropDef[] mPropDefs = mWsMgr.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE");
                ACW.PropDef   mPropDef  = null;
                ACW.PropInst  mPropInst = null;
                if (mFile.Name.EndsWith(".dwg"))
                {
                    mPropDef  = mPropDefs.Where(n => n.SysName == "Provider").FirstOrDefault();
                    mPropInst = (mWsMgr.PropertyService.GetPropertiesByEntityIds("FILE", new long[] { mFile.Id })).Where(n => n.PropDefId == mPropDef.Id).FirstOrDefault();
                    if (mPropInst.Val.ToString() != "Inventor DWG")
                    {
                        context.Log(null, "Skipped Job execution as DWG type did not match Inventor DWG");
                        return(JobOutcome.Success);
                    }
                }

                ApplicationAddIns mInvSrvAddIns = mInv.ApplicationAddIns;
                ApplicationAddIn  iLogicAddIn   = mInvSrvAddIns.ItemById["{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"];

                if (iLogicAddIn != null && iLogicAddIn.Activated != true)
                {
                    iLogicAddIn.Activate();
                }

                dynamic mAutomation = iLogicAddIn.Automation;

                if (mAutomation == null)
                {
                    Trace.WriteLine("iLogic-AddIn automation is not available; exiting job processing");
                    context.Log(null, "iLogic-AddIn automation is not available");
                    return(JobOutcome.Failure);
                }

                #endregion validate execution rules

                #region VaultInventorServer IPJ activation

                //override InventorServer default project settings by your Vault specific ones
                Inventor.DesignProjectManager mInvIpjManager;
                Inventor.DesignProject        mInvDfltProject, mInvVltProject;
                String   mIpjPath      = "";
                String   mWfPath       = "";
                String   mIpjLocalPath = "";
                ACW.File mIpj;
                VDF.Vault.Currency.Entities.FileIteration mIpjFileIter = null;

                //validate ipj setting, a single, enforced ipj is expected
                if (mWsMgr.DocumentService.GetEnforceWorkingFolder() && mWsMgr.DocumentService.GetEnforceInventorProjectFile())
                {
                    mIpjPath = mWsMgr.DocumentService.GetInventorProjectFileLocation();
                    mWfPath  = mWsMgr.DocumentService.GetRequiredWorkingFolderLocation();
                    //Set mWfPath to alternate temporary working folder if needed, e.g. to delete all files after job execution
                }
                else
                {
                    context.Log(null, "Job requires both settings enabled: 'Enforce Workingfolder' and 'Enforce Inventor Project'.");
                    return(JobOutcome.Failure);
                }
                //download and activate the Inventor Project file in VaultInventorServer
                mIpj = (mWsMgr.DocumentService.FindLatestFilesByPaths(new string[] { mIpjPath })).FirstOrDefault();

                try
                {
                    String[] mIpjFullFileName = mIpjPath.Split(new string[] { "/" }, StringSplitOptions.None);
                    String   mIpjFileName     = mIpjFullFileName.LastOrDefault();

                    //define download settings for the project file
                    VDF.Vault.Settings.AcquireFilesSettings mAcqrIpjSettings = new VDF.Vault.Settings.AcquireFilesSettings(mConnection);
                    mAcqrIpjSettings.LocalPath = new VDF.Currency.FolderPathAbsolute(mWfPath);
                    mIpjFileIter = new VDF.Vault.Currency.Entities.FileIteration(mConnection, mIpj);
                    mAcqrIpjSettings.AddFileToAcquire(mIpjFileIter, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download);

                    //download project file and get local path
                    VDF.Vault.Results.AcquireFilesResults   mDownLoadResult;
                    VDF.Vault.Results.FileAcquisitionResult fileAcquisitionResult;
                    mDownLoadResult       = mConnection.FileManager.AcquireFiles(mAcqrIpjSettings);
                    fileAcquisitionResult = mDownLoadResult.FileResults.FirstOrDefault();
                    mIpjLocalPath         = fileAcquisitionResult.LocalPath.FullPath;

                    //activate this Vault's ipj
                    mInvIpjManager  = mInv.DesignProjectManager;
                    mInvDfltProject = mInvIpjManager.ActiveDesignProject;
                    mInvVltProject  = mInvIpjManager.DesignProjects.AddExisting(mIpjLocalPath);
                    mInvVltProject.Activate();

                    //[Optionally:] get Inventor Design Data settings and download all related files ---------
                }
                catch
                {
                    context.Log(null, "Job was not able to activate Inventor project file. - Note: The ipj must not be checked out by another user.");
                    return(JobOutcome.Failure);
                }
                #endregion VaultInventorServer IPJ activation


                #region download source file(s)

                //build download options including DefaultAcquisitionOptions
                VDF.Vault.Currency.Entities.FileIteration mFileIteration    = new VDF.Vault.Currency.Entities.FileIteration(mConnection, mFile);
                VDF.Vault.Currency.Entities.FileIteration mNewFileIteration = null;
                VDF.Vault.Settings.AcquireFilesSettings   mAcqrFlsSettings;
                VDF.Vault.Results.AcquireFilesResults     mAcqrFlsResults2;
                VDF.Vault.Results.FileAcquisitionResult   mFileAcqsResult2;
                string mLocalFileFullName = "", mExt = "";

                if (mFileIteration.IsCheckedOut == true && mFileIteration.IsCheckedOutToCurrentUser == false)
                {
                    //exit the job, as the job user is not able to edit the file reserved to another user
                    mInvDfltProject.Activate();
                    context.Log(null, "Job stopped execution as the source file to process is checked-out by another user.");
                    return(JobOutcome.Failure);
                }
                //download only
                if (mFileIteration.IsCheckedOut == true && mFileIteration.IsCheckedOutToCurrentUser == true)
                {
                    mAcqrFlsSettings = CreateAcquireSettings(false);
                    mAcqrFlsSettings.AddFileToAcquire(mFileIteration, mAcqrFlsSettings.DefaultAcquisitionOption);
                    mAcqrFlsResults2   = this.mConnection.FileManager.AcquireFiles(mAcqrFlsSettings);
                    mNewFileIteration  = mFileIteration;
                    mFileAcqsResult2   = mAcqrFlsResults2.FileResults.Where(n => n.File.EntityName == mFileIteration.EntityName).FirstOrDefault();
                    mLocalFileFullName = mFileAcqsResult2.LocalPath.FullPath;
                    mExt = System.IO.Path.GetExtension(mLocalFileFullName);
                }
                //checkout and download
                if (mFileIteration.IsCheckedOut == false)
                {
                    try
                    {
                        mAcqrFlsSettings = CreateAcquireSettings(true); //checkout (don't checkout related children)
                        mAcqrFlsSettings.AddFileToAcquire(mFileIteration, mAcqrFlsSettings.DefaultAcquisitionOption);
                        mAcqrFlsResults2  = this.mConnection.FileManager.AcquireFiles(mAcqrFlsSettings);
                        mFileAcqsResult2  = mAcqrFlsResults2.FileResults.Where(n => n.File.EntityName == mFileIteration.EntityName).FirstOrDefault();
                        mNewFileIteration = mFileAcqsResult2.NewFileIteration;
                        mAcqrFlsSettings  = null;
                        mAcqrFlsSettings  = CreateAcquireSettings(false);//download (include related children)
                        mAcqrFlsSettings.AddFileToAcquire(mNewFileIteration, mAcqrFlsSettings.DefaultAcquisitionOption);
                        mAcqrFlsResults2   = this.mConnection.FileManager.AcquireFiles(mAcqrFlsSettings);
                        mLocalFileFullName = mFileAcqsResult2.LocalPath.FullPath;
                        mExt = System.IO.Path.GetExtension(mLocalFileFullName);
                    }
                    catch (Exception)
                    {
                        mInvDfltProject.Activate();
                        context.Log(null, "Job stopped execution as the source file to process did not download or check-out.");
                        return(JobOutcome.Failure);
                    }
                }

                #endregion download source file(s)

                #region capture dependencies
                //we need to return all relationships during later check-in
                List <ACW.FileAssocParam> mFileAssocParams = new List <ACW.FileAssocParam>();
                ACW.FileAssocArray        mFileAssocArray  = mWsMgr.DocumentService.GetLatestFileAssociationsByMasterIds(new long[] { mFile.MasterId },
                                                                                                                         ACW.FileAssociationTypeEnum.None, false, ACW.FileAssociationTypeEnum.All, false, false, false, true).FirstOrDefault();
                if (mFileAssocArray.FileAssocs != null)
                {
                    foreach (ACW.FileAssoc item in mFileAssocArray.FileAssocs)
                    {
                        ACW.FileAssocParam mFileAssocParam = new ACW.FileAssocParam();
                        mFileAssocParam.CldFileId         = item.CldFile.Id;
                        mFileAssocParam.ExpectedVaultPath = item.ExpectedVaultPath;
                        mFileAssocParam.RefId             = item.RefId;
                        mFileAssocParam.Source            = item.Source;
                        mFileAssocParam.Typ = item.Typ;
                        mFileAssocParams.Add(mFileAssocParam);
                    }
                }

                #endregion capture dependencies

                #region iLogic Configuration

                //avoid unplanned rule execution triggered by the document itself
                mAutomation.RulesOnEventsEnabled = false;
                mAutomation.RulesEnabled         = false;

                //set the iLogic Advanced Configuration Settings
                dynamic mFileOptions = mAutomation.FileOptions;
                mFileOptions.AddinDirectory = mSettings.iLogicAddinDLLs;
                //add the job extension app path and configured external rule directories to the FileOptions.ExternalRuleDirectories for iLogic
                List <string> mExtRuleDirs = new List <string>();
                mExtRuleDirs.Add(mAppPath);
                mExtRuleDirs.AddRange(mSettings.ExternalRuleDirectories.Split(',').ToList <string>());
                mFileOptions.ExternalRuleDirectories = mExtRuleDirs.ToArray();

                //enable iLogic logging
                dynamic mLogCtrl = mAutomation.LogControl;
                switch (mSettings.iLogicLogLevel)
                {
                case "None":
                    mLogCtrl.Level = 0;
                    break;

                case "Trace":
                    mLogCtrl.Level = 1;
                    break;

                case "Debug":
                    mLogCtrl.Level = 2;
                    break;

                case "Info":
                    mLogCtrl.Level = 3;
                    break;

                case "Warn":
                    mLogCtrl.Level = 4;
                    break;

                case "Error":
                    mLogCtrl.Level = 5;
                    break;

                case "Fatal":
                    mLogCtrl.Level = 6;
                    break;

                default:
                    mLogCtrl.Level = 5;
                    break;
                }

                //enable iLogic to save a log file for each job Id.
                string mILogicLogFileFullName = "";
                if (mLogCtrl.Level != 0)
                {
                    string mLogName = job.Id + "_" + mFile.Name + "_iLogicSampleJob.log";
                    System.IO.DirectoryInfo mLogDirInfo = new System.IO.DirectoryInfo(mSettings.iLogicLogDir);
                    if (mLogDirInfo.Exists == false)
                    {
                        mLogDirInfo = System.IO.Directory.CreateDirectory(mSettings.iLogicLogDir);
                    }
                    mILogicLogFileFullName = System.IO.Path.Combine(mLogDirInfo.FullName, mLogName);
                }



                //read rule execution settings
                string mVaultRule       = mSettings.VaultRuleFullFileName;
                string mExtRule         = mSettings.ExternalRuleName;
                string mExtRuleFullName = null;
                string mIntRulesOption  = mSettings.InternalRulesOption;

                if (mVaultRule != "")
                {
                    ACW.File mRuleFile = mWsMgr.DocumentService.FindLatestFilesByPaths(new string[] { mVaultRule }).FirstOrDefault();
                    //build download options including DefaultAcquisitionOptions
                    VDF.Vault.Currency.Entities.FileIteration mRuleFileIter = new VDF.Vault.Currency.Entities.FileIteration(mConnection, mRuleFile);

                    VDF.Vault.Settings.AcquireFilesSettings mAcqrRuleSettings = CreateAcquireSettings(false);

                    mAcqrRuleSettings.AddFileToAcquire(mRuleFileIter, mAcqrRuleSettings.DefaultAcquisitionOption);

                    //download
                    VDF.Vault.Results.AcquireFilesResults mAcqrRuleResults = this.mConnection.FileManager.AcquireFiles(mAcqrRuleSettings);
                    //pick-up the new file iteration in case of check-out
                    VDF.Vault.Results.FileAcquisitionResult mRuleAcqResult = mAcqrRuleResults.FileResults.Where(n => n.File.EntityName == mRuleFileIter.EntityName).FirstOrDefault();
                    if (mRuleAcqResult.LocalPath != null)
                    {
                        mExtRuleFullName = mRuleAcqResult.LocalPath.FullPath;
                        System.IO.FileInfo fileInfo = new System.IO.FileInfo(mExtRuleFullName);
                        if (fileInfo.Exists == false)
                        {
                            context.Log(null, "Job downloaded rule file but exited due to missing rule file: " + mExtRuleFullName + ".");
                            mConnection.FileManager.UndoCheckoutFile(mNewFileIteration);
                            return(JobOutcome.Failure);
                        }
                        else
                        {
                            mExtRule = fileInfo.Name;
                            if (!mExtRuleDirs.Any(n => fileInfo.DirectoryName.Equals(n)))
                            {
                                context.Log(null, "Job downloaded rule file but exited due to missing iLogic External Rule Directory configuration: Add the path"
                                            + fileInfo.DirectoryName + " to the list of External Rule Directories.");
                                mConnection.FileManager.UndoCheckoutFile(mNewFileIteration);
                                return(JobOutcome.Failure);
                            }
                        }
                    }
                    else
                    {
                        context.Log(null, "Job could not download configured rule file and exited. Compare the 'VaultRuleFullFileName' setting and available rule in Vault.");
                        return(JobOutcome.Failure);
                    }
                }

                #endregion iLogic Configuration

                #region Run iLogic Rule(s)

                //Open Inventor Document
                Document mDoc = mInv.Documents.Open(mLocalFileFullName);

                //use case  - apply external rule with arguments; additional Vault UDP, status or any information might fill rule arguments
                if (mExtRule != "")
                {
                    //required rule arguments to continue Vault interaction within the rule (iLogic-Vault library)
                    NameValueMap ruleArguments = mInv.TransientObjects.CreateNameValueMap();
                    ruleArguments.Add("ServerName", mConnection.Server);
                    ruleArguments.Add("VaultName", mConnection.Vault);
                    ruleArguments.Add("UserId", mConnection.UserID);
                    ruleArguments.Add("Ticket", mConnection.Ticket);

                    //additional rule arguments to build rule conditions evaluating Vault lifecycle information, properties, etc.
                    if (mSettings.PropagateProps == "True")
                    {
                        ACW.PropInst[] mSourcePropInsts = mWsMgr.PropertyService.GetPropertiesByEntityIds("FILE", new long[] { mFileIteration.EntityIterationId });
                        string         mPropDispName;
                        string         mThumbnailDispName = mPropDefs.Where(n => n.SysName == "Thumbnail").FirstOrDefault().DispName;
                        foreach (ACW.PropInst item in mSourcePropInsts)
                        {
                            mPropDispName = mPropDefs.Where(n => n.Id == item.PropDefId).FirstOrDefault().DispName;
                            //filter thumbnail property, as iLogic RuleArguments will fail reading it.
                            if (mPropDispName != mThumbnailDispName)
                            {
                                ruleArguments.Add(mPropDispName, item.Val);
                            }
                        }
                    }

                    //call external rule with arguments; return value = 0 in case of successful execution
                    mRuleSuccess = mAutomation.RunExternalRuleWithArguments(mDoc, mExtRule, ruleArguments);
                    if (mRuleSuccess != 0)
                    {
                        context.Log(null, "Job failed due to failure in external rule: " + mExtRule + ".");
                        mDoc.Close(true);
                        mConnection.FileManager.UndoCheckoutFile(mNewFileIteration);
                        mLogCtrl.SaveLogAs(mILogicLogFileFullName);
                        return(JobOutcome.Failure);
                    }
                    else
                    {
                        mAllRulesTextWrp = "External Rule: " + mExtRule;
                    }
                    mDoc.Save2(false);
                }


                //use case - run all, all active or filtered document rules
                dynamic        mDocRules    = mAutomation.Rules(mDoc);
                List <dynamic> mRulesToExec = new List <dynamic>();

                switch (mSettings.InternalRulesOption)
                {
                case "None":
                    break;

                case "Active":
                    if (mDocRules != null)
                    {
                        foreach (dynamic rule in mDocRules)
                        {
                            if (rule.IsActive == true)
                            {
                                mRulesToExec.Add(rule);
                            }
                        }
                    }
                    break;

                case "All":
                    if (mDocRules != null)
                    {
                        foreach (dynamic rule in mDocRules)
                        {
                            mRulesToExec.Add(rule);
                        }
                    }
                    break;

                default:
                    foreach (dynamic rule in mDocRules)
                    {
                        if (rule.Name.Contains(mSettings.InternalRulesOption))
                        {
                            mRulesToExec.Add(rule);
                        }
                    }
                    break;
                }
                if (mRulesToExec.Count >= 1)
                {
                    foreach (dynamic rule in mRulesToExec)
                    {
                        mRuleSuccess = mAutomation.RunRule(mDoc, rule.Name);
                        if (mRuleSuccess != 0)
                        {
                            context.Log(null, "Job failed due to failure in internal rule: " + rule.Name + ".");
                            mDoc.Close(true);
                            mConnection.FileManager.UndoCheckoutFile(mNewFileIteration);
                            mLogCtrl.SaveLogAs(mILogicLogFileFullName);
                            return(JobOutcome.Failure);
                        }
                        else
                        {
                            mAllRules.Add(rule.Name);
                        }
                    }
                }

                mDoc.Save2(false);
                mDoc.Close(true);
                mLogCtrl.SaveLogAs(mILogicLogFileFullName);

                if (mAllRules.Count > 0)
                {
                    mAllRulesTextWrp += "\n\r Internal Rule(s):";
                    foreach (string name in mAllRules)
                    {
                        mAllRulesTextWrp += "\r\n " + name;
                    }
                }

                #endregion Run iLogic Rules


                #region Check-in result

                // checkin new file version
                VDF.Currency.FilePathAbsolute vdfPath = new VDF.Currency.FilePathAbsolute(mLocalFileFullName);
                FileIteration mUploadedFile           = null;
                try
                {
                    if (mFileAssocParams.Count > 0)
                    {
                        mUploadedFile = mConnection.FileManager.CheckinFile(mNewFileIteration, "Created by Custom Job executing iLogic : " + mAllRulesTextWrp,
                                                                            false, mFileAssocParams.ToArray(), null, true, null, mFileIteration.FileClassification, false, vdfPath);
                    }
                    else
                    {
                        mUploadedFile = mConnection.FileManager.CheckinFile(mNewFileIteration, "Created by Custom Job executing iLogic : " + mAllRulesTextWrp,
                                                                            false, null, null, false, null, mFileIteration.FileClassification, false, vdfPath);
                    }
                }
                catch
                {
                    context.Log(null, "Job could not check-in updated file: " + mUploadedFile.EntityName + ".");
                    return(JobOutcome.Failure);
                }
                #endregion check-in result

                #region reset

                mInvDfltProject.Activate();
                //delete temporary working folder if imlemented here

                #endregion reset

                return(JobOutcome.Success);
            }
            catch (Exception ex)
            {
                context.Log(ex, "Job failed by unhandled exception: " + ex.ToString() + " ");
                return(JobOutcome.Failure);
            }
        }