示例#1
0
        public static void Run(string serverName, string userName, string password, string[] vaultNames, string viewName,
                               DateTime startDate, IDictionary <string, IEnumerable <PropertyListType> > listProperties, IProcessor processor)
        {
            _mfilesServer = new MFilesServerApplication();
            MFServerConnection result;

            try
            {
                result = _mfilesServer.Connect(MFAuthType.MFAuthTypeSpecificMFilesUser, userName, password,
                                               NetworkAddress: serverName);
            }
            catch (COMException ex)
            {
                ClassLogger.Error(ex, $"Could not connect to M-Files server");
                return;
            }
            if (result != MFServerConnection.MFServerConnectionAuthenticated)
            {
                ClassLogger.Error("Could not connect to M-Files server");
                return;
            }

            var topContext = processor.CreateContext();


            var vaultsOnServer = _mfilesServer.GetVaults();

            IList <Tuple <string, Vault, IView> > data = new List <Tuple <string, Vault, IView> >();

            foreach (var vaultName in vaultNames)
            {
                IVaultOnServer vaultOnServer;
                try
                {
                    vaultOnServer = vaultsOnServer.GetVaultByName(vaultName);
                }
                catch (COMException)
                {
                    ClassLogger.Error($"Could not find vault '{vaultName}'");
                    continue;
                }

                Vault vault = vaultOnServer.LogIn();
                if (!vault.LoggedIn)
                {
                    ClassLogger.Error($"Could not logging to vault '{vaultName}'");
                    continue;
                }

                if (listProperties.ContainsKey(vaultName))
                {
                    foreach (var listProperty in listProperties[vaultName])
                    {
                        var def =
                            vault.PropertyDefOperations.GetPropertyDefs()
                            .Cast <PropertyDef>()
                            .SingleOrDefault(x => x.Name == listProperty.PropertyName);

                        if (def == null || !def.BasedOnValueList)
                        {
                            ClassLogger.Error($"Property {listProperty.PropertyName} in {vaultName} is not a list");
                            continue;
                        }

                        ValueListItems items = vault.ValueListItemOperations.GetValueListItems(def.ValueList, true, MFExternalDBRefreshType.MFExternalDBRefreshTypeQuick);

                        foreach (ValueListItem item in items)
                        {
                            Guid guid;
                            if (!Guid.TryParse(item.DisplayID, out guid))
                            {
                                guid = Guid.NewGuid();
                            }

                            listProperty.Items.Add(new PropertyListItem(guid, item.Name));
                        }

                        topContext.ProcessListProperty(listProperty);
                    }
                }

                IView view = vault.ViewOperations.GetViews().Cast <IView>().FirstOrDefault(v => v.Name == viewName);
                if (view == null)
                {
                    ClassLogger.Warn($"Could not find view '{viewName}' in vault  '{vaultName}'");
                    continue;
                }
                data.Add(Tuple.Create(vaultName, vault, view));
            }

            IList <Task> tasks = new List <Task>();

            foreach (var taskData in data)
            {
                var task =
                    new Task(() => ProcessData(taskData.Item1, taskData.Item2, taskData.Item3, startDate, processor));
                tasks.Add(task);
                task.Start();
            }

            foreach (var task in tasks)
            {
                task.Wait();
            }
            processor.AfterProcessing();
        }