public void RunApplication(string[] args)
        {
            if (args.Length == 0)
            {
                Platform.Log(LogLevel.Error, "Name of data file to import must be supplied as first argument.");
                return;
            }

            try
            {
                using (StreamReader reader = File.OpenText(args[0]))
                {
                    List <string> lines = null;
                    while ((lines = ReadLines(reader, DEFAULT_BATCH_SIZE)).Count > 0)
                    {
                        using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
                        {
                            ((IUpdateContext)PersistenceScope.CurrentContext).ChangeSetRecorder.OperationName = this.GetType().FullName;
                            Import(lines, (IUpdateContext)PersistenceScope.CurrentContext);
                            scope.Complete();
                        }
                    }
                }
            }
            catch (EntityValidationException e)
            {
                Log(LogLevel.Error, e.Message);
            }
            catch (Exception e)
            {
                Log(LogLevel.Error, e.ToString());
            }
        }
        protected sealed override void ImportCore(IEnumerable <IImportItem> items)
        {
            var enumerator = items.GetEnumerator();

            for (var more = true; more;)
            {
                using (var scope = new PersistenceScope(PersistenceContextType.Update))
                {
                    var context = (IUpdateContext)PersistenceScope.CurrentContext;
                    context.ChangeSetRecorder.OperationName = this.GetType().FullName;

                    for (var j = 0; j < ItemsPerUpdateTransaction && more; j++)
                    {
                        more = enumerator.MoveNext();
                        if (more)
                        {
                            var item = enumerator.Current;
                            var data = (TDataContract)Read(item.Read(), typeof(TDataContract));
                            Import(data, context);
                        }
                    }
                    scope.Complete();
                }
            }
        }
        public void RunApplication(string[] args)
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
            {
                ((IUpdateContext)PersistenceScope.CurrentContext).ChangeSetRecorder.OperationName = this.GetType().FullName;
                ImportFromPlugins((IUpdateContext)PersistenceScope.CurrentContext);

                scope.Complete();
            }
        }
Exemple #4
0
        public void RunApplication(string[] args)
        {
            SetupCommandLine cmdLine = new SetupCommandLine();

            try
            {
                cmdLine.Parse(args);

                using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
                {
                    ((IUpdateContext)PersistenceScope.CurrentContext).ChangeSetRecorder.OperationName = GetType().FullName;

                    // import authority tokens
                    AuthorityTokenImporter tokenImporter = new AuthorityTokenImporter();
                    IList <AuthorityToken> allTokens     = tokenImporter.ImportFromPlugins((IUpdateContext)PersistenceScope.CurrentContext);


                    // create the sys admin group, which has all tokens assigned by default
                    string[] tokenStrings = CollectionUtils.Map <AuthorityToken, string, List <string> >(allTokens,
                                                                                                         t => t.Name).ToArray();
                    AuthorityGroupDefinition adminGroupDef = new AuthorityGroupDefinition(cmdLine.SysAdminGroup, cmdLine.SysAdminGroup, false,
                                                                                          tokenStrings);
                    AuthorityGroupImporter groupImporter = new AuthorityGroupImporter();

                    IList <AuthorityGroup> groups =
                        groupImporter.Import(new AuthorityGroupDefinition[] { adminGroupDef }, (IUpdateContext)PersistenceScope.CurrentContext);

                    // find the admin group entity that was just created
                    AuthorityGroup adminGroup = CollectionUtils.SelectFirst(groups,
                                                                            g => g.Name == cmdLine.SysAdminGroup);

                    // create the "sa" user
                    CreateSysAdminUser(adminGroup, cmdLine, PersistenceScope.CurrentContext, Console.Out);

                    // optionally import other default authority groups defined in other plugins
                    if (cmdLine.ImportDefaultAuthorityGroups)
                    {
                        groupImporter.ImportFromPlugins((IUpdateContext)PersistenceScope.CurrentContext);
                    }

                    scope.Complete();
                }
            }
            catch (CommandLineException e)
            {
                Console.WriteLine(e.Message);
                cmdLine.PrintUsage(Console.Out);
            }
        }
        public void RunApplication(string[] args)
        {
            var cmdLine = new SetupCommandLine();

            try
            {
                cmdLine.Parse(args);

                using (var scope = new PersistenceScope(PersistenceContextType.Update))
                {
                    ((IUpdateContext)PersistenceScope.CurrentContext).ChangeSetRecorder.OperationName = GetType().FullName;

                    // import authority tokens
                    var tokenImporter = new AuthorityTokenImporter();
                    var allTokens     = tokenImporter.ImportFromPlugins((IUpdateContext)PersistenceScope.CurrentContext);
                    var tokenStrings  = CollectionUtils.Map <AuthorityToken, string, List <string> >(allTokens, t => t.Name).ToArray();

                    // import built-in groups
                    var builtInAuthorityGroups = new[]
                    {
                        GetSysAdminGroupDefinition(tokenStrings),
                        BuiltInAuthorityGroups.SystemAccounts
                    };

                    var groupImporter = new AuthorityGroupImporter();
                    var groups        = groupImporter.Import(builtInAuthorityGroups, (IUpdateContext)PersistenceScope.CurrentContext);

                    // create the "sa" user
                    var adminGroup = CollectionUtils.SelectFirst(groups, g => g.Name == BuiltInAuthorityGroups.Administrators.Name);
                    CreateSysAdminUser(adminGroup, cmdLine, PersistenceScope.CurrentContext, Console.Out);

                    // optionally import other default authority groups defined in other plugins
                    if (cmdLine.ImportDefaultAuthorityGroups)
                    {
                        groupImporter.ImportFromPlugins((IUpdateContext)PersistenceScope.CurrentContext);
                    }

                    scope.Complete();
                }
            }
            catch (CommandLineException e)
            {
                Console.WriteLine(e.Message);
                cmdLine.PrintUsage(Console.Out);
            }
        }
Exemple #6
0
        protected override IEnumerable <IExportItem> ExportCore()
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Read))
            {
                IReadContext    context     = (IReadContext)PersistenceScope.CurrentContext;
                IMetadataBroker metaBroker  = context.GetBroker <IMetadataBroker>();
                IEnumBroker     enumBroker  = context.GetBroker <IEnumBroker>();
                List <Type>     enumClasses = CollectionUtils.Sort(metaBroker.ListEnumValueClasses(),
                                                                   delegate(Type x, Type y) { return(x.FullName.CompareTo(y.FullName)); });
                foreach (Type enumClass in enumClasses)
                {
                    EnumerationData data = new EnumerationData();
                    data.EnumerationClass = enumClass.FullName;
                    data.Members          = CollectionUtils.Map <EnumValue, EnumerationMemberData>(enumBroker.Load(enumClass, true),
                                                                                                   delegate(EnumValue v) { return(new EnumerationMemberData(v)); });

                    yield return(new ExportItem(data));
                }

                scope.Complete();
            }
        }
        protected sealed override IEnumerable <IExportItem> ExportCore()
        {
            var more = true;

            for (var row = 0; more; row += ItemsPerReadTransaction)
            {
                using (var scope = new PersistenceScope(PersistenceContextType.Read))
                {
                    var context = (IReadContext)PersistenceScope.CurrentContext;
                    var items   = GetItemsForExport(context, row, ItemsPerReadTransaction);
                    foreach (var entity in items)
                    {
                        var data = Export(entity, context);
                        yield return(new ExportItem(data));
                    }

                    // there may be more rows if the last query returned any items
                    more = (items.Count > 0);
                    scope.Complete();
                }
            }
        }
Exemple #8
0
        protected override void ImportCore(IEnumerable <IImportItem> items)
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
            {
                IUpdateContext context = (IUpdateContext)PersistenceScope.CurrentContext;
                context.ChangeSetRecorder.OperationName = this.GetType().FullName;

                IMetadataBroker metaBroker  = context.GetBroker <IMetadataBroker>();
                IEnumBroker     enumBroker  = context.GetBroker <IEnumBroker>();
                IList <Type>    enumClasses = metaBroker.ListEnumValueClasses();

                foreach (IImportItem item in items)
                {
                    EnumerationData data = (EnumerationData)Read(item.Read(), typeof(EnumerationData));

                    // find the enum class
                    Type enumClass = CollectionUtils.SelectFirst(enumClasses,
                                                                 delegate(Type ec)
                    {
                        return(ec.FullName == data.EnumerationClass);
                    });

                    if (enumClass == null)
                    {
                        Platform.Log(LogLevel.Error, string.Format("{0} is not a valid enumeration class name.", data.EnumerationClass));
                        continue;
                    }

                    IList <EnumValue> existingValues = enumBroker.Load(enumClass, true);
                    foreach (EnumerationMemberData md in data.Members)
                    {
                        // check if a conflicting value exists
                        // (this can happen if there is existing data in the db with the same value but different code)
                        EnumValue conflict = CollectionUtils.SelectFirst(existingValues,
                                                                         delegate(EnumValue v) { return(v.Code != md.Code && v.Value == md.Value); });

                        if (conflict != null)
                        {
                            Platform.Log(LogLevel.Error, string.Format("{0} value {1} conflicts with existing value {2} and will not be imported.",
                                                                       data.EnumerationClass, md.Code, conflict.Code));
                            continue;
                        }

                        // check if the value already exists
                        EnumValue value = CollectionUtils.SelectFirst(existingValues,
                                                                      delegate(EnumValue v) { return(v.Code == md.Code); });

                        if (value == null)
                        {
                            // value does not exist - add it
                            value = enumBroker.AddValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
                            existingValues.Add(value);
                        }
                        else
                        {
                            // value exists - update it
                            enumBroker.UpdateValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
                        }
                    }

                    context.SynchState();
                }

                scope.Complete();
            }
        }
        protected override IEnumerable<IExportItem> ExportCore()
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Read))
            {
                IReadContext context = (IReadContext) PersistenceScope.CurrentContext;
                IMetadataBroker metaBroker = context.GetBroker<IMetadataBroker>();
                IEnumBroker enumBroker = context.GetBroker<IEnumBroker>();
                List<Type> enumClasses = CollectionUtils.Sort(metaBroker.ListEnumValueClasses(),
                    delegate (Type x, Type y) { return x.FullName.CompareTo(y.FullName);});
                foreach (Type enumClass in enumClasses)
                {
                    EnumerationData data = new EnumerationData();
                    data.EnumerationClass = enumClass.FullName;
                    data.Members = CollectionUtils.Map<EnumValue, EnumerationMemberData>(enumBroker.Load(enumClass, true),
                        delegate(EnumValue v) { return new EnumerationMemberData(v); });

                    yield return new ExportItem(data);
                }

                scope.Complete();
            }
        }
        protected override void ImportCore(IEnumerable<IImportItem> items)
        {
            using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update))
            {
                IUpdateContext context = (IUpdateContext) PersistenceScope.CurrentContext;
                context.ChangeSetRecorder.OperationName = this.GetType().FullName;

                IMetadataBroker metaBroker = context.GetBroker<IMetadataBroker>();
                IEnumBroker enumBroker = context.GetBroker<IEnumBroker>();
                IList<Type> enumClasses = metaBroker.ListEnumValueClasses();

                foreach (IImportItem item in items)
                {
                    EnumerationData data = (EnumerationData) Read(item.Read(), typeof(EnumerationData));

                    // find the enum class
                    Type enumClass = CollectionUtils.SelectFirst(enumClasses,
                        delegate(Type ec)
                        {
                            return ec.FullName == data.EnumerationClass;
                        });

					if(enumClass == null)
					{
						Platform.Log(LogLevel.Error, string.Format("{0} is not a valid enumeration class name.", data.EnumerationClass));
						continue;
					}

					IList<EnumValue> existingValues = enumBroker.Load(enumClass, true);
					foreach (EnumerationMemberData md in data.Members)
					{
						// check if a conflicting value exists
						// (this can happen if there is existing data in the db with the same value but different code)
						EnumValue conflict = CollectionUtils.SelectFirst(existingValues,
							delegate(EnumValue v) { return v.Code != md.Code && v.Value == md.Value; });

						if(conflict != null)
						{
							Platform.Log(LogLevel.Error, string.Format("{0} value {1} conflicts with existing value {2} and will not be imported.",
								data.EnumerationClass, md.Code, conflict.Code));
							continue;
						}

						// check if the value already exists
						EnumValue value = CollectionUtils.SelectFirst(existingValues,
							delegate(EnumValue v) { return v.Code == md.Code; });

						if (value == null)
						{
							// value does not exist - add it
							value = enumBroker.AddValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
							existingValues.Add(value);
						}
						else
						{
							// value exists - update it
							enumBroker.UpdateValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated);
						}
					}

					context.SynchState();
				}

                scope.Complete();
            }
        }
Exemple #11
0
        internal static SystemIdentityStore Load()
        {
            if (_store != null)
            {
                return(_store);
            }

            lock (_syncroot)
            {
                if (_store == null)
                {
                    const string userName      = "******";
                    var          serializer    = new XmlSerializer(typeof(SystemIdentityStore));
                    var          documentName  = typeof(SystemIdentityStore).FullName;
                    var          versionString = VersionUtils.ToPaddedVersionString(new Version(0, 0), false, false);

                    var criteria = new ConfigurationDocumentSearchCriteria();
                    criteria.User.EqualTo(userName);
                    criteria.DocumentName.EqualTo(documentName);
                    criteria.DocumentVersionString.EqualTo(versionString);

                    SystemIdentityStore store = null;
                    using (var scope = new PersistenceScope(PersistenceContextType.Read))
                    {
                        var broker   = scope.Context.GetBroker <IConfigurationDocumentBroker>();
                        var document = broker.Find(criteria).FirstOrDefault();
                        if (document != null)
                        {
                            try
                            {
                                using (var reader = new StringReader(document.Body.DocumentText))
                                    store = (SystemIdentityStore)serializer.Deserialize(reader);
                            }
                            catch (Exception)
                            {
                                store = null;
                            }
                        }
                        scope.Complete();
                    }

                    if (store == null || store.SecretKey == null || store.SecretKey.Length == 0)
                    {
                        if (store == null)
                        {
                            store = new SystemIdentityStore();
                        }
                        store.SecretKey = new byte[128];
                        using (var crng = new RNGCryptoServiceProvider())
                            crng.GetBytes(store.SecretKey);

                        using (var scope = new PersistenceScope(PersistenceContextType.Update))
                            using (var writer = new StringWriter())
                            {
                                serializer.Serialize(writer, store);

                                var broker   = scope.Context.GetBroker <IConfigurationDocumentBroker>();
                                var document = broker.Find(criteria).FirstOrDefault();
                                if (document != null)
                                {
                                    document.Body.DocumentText = writer.ToString();
                                }
                                else
                                {
                                    document = new ConfigurationDocument(documentName, versionString, userName, null);
                                    document.Body.DocumentText = writer.ToString();
                                    scope.Context.Lock(document, DirtyState.New);
                                }
                                scope.Complete();
                            }
                    }

                    Interlocked.Exchange(ref _store, store);
                }
                return(_store);
            }
        }