Ejemplo n.º 1
0
        /// <summary>
        /// Loads Rock data that's used globally by the transform
        /// </summary>
        /// <param name="lookupContext">The lookup context.</param>
        private static void LoadRockData(RockContext lookupContext = null)
        {
            lookupContext = lookupContext ?? new RockContext();

            // initialize file providers
            DatabaseProvider   = new Database();
            FileSystemProvider = new FileSystem();

            // core-specified attribute guid for setting file root path
            RootPathAttribute = AttributeCache.Read(new Guid("3CAFA34D-9208-439B-A046-CB727FB729DE"));

            // core-specified blacklist files
            FileTypeBlackList = (GlobalAttributesCache.Read().GetValue("ContentFiletypeBlacklist")
                                 ?? string.Empty).Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);

            // clean up blacklist
            FileTypeBlackList = FileTypeBlackList.Select(a => a.ToLower().TrimStart(new char[] { '.', ' ' }));
            FileTypes         = new BinaryFileTypeService(lookupContext).Queryable().AsNoTracking().ToList();

            // get all the types we'll be importing
            var binaryTypeSettings = ConfigurationManager.GetSection("binaryFileTypes") as NameValueCollection;

            // create any custom types defined in settings that don't exist yet
            foreach (var typeKey in binaryTypeSettings.AllKeys)
            {
                if (!FileTypes.Any(f => f.Name == typeKey))
                {
                    var newFileType = new BinaryFileType();
                    lookupContext.BinaryFileTypes.Add(newFileType);
                    newFileType.Name         = typeKey;
                    newFileType.Description  = typeKey;
                    newFileType.AllowCaching = true;

                    var typeValue = binaryTypeSettings[typeKey];
                    if (typeValue != null)
                    {
                        // #TODO: support additional storage types (like AWS?)
                        newFileType.StorageEntityTypeId = typeValue.Equals("Database") ? DatabaseStorageTypeId : FileSystemStorageTypeId;
                        newFileType.Attributes          = new Dictionary <string, AttributeCache>();
                        newFileType.AttributeValues     = new Dictionary <string, AttributeValueCache>();

                        // save changes to binary type to get an ID
                        lookupContext.SaveChanges();

                        var newRootPath = new AttributeValue()
                        {
                            AttributeId = RootPathAttribute.Id,
                            EntityId    = newFileType.Id,
                            Value       = typeValue
                        };

                        newFileType.Attributes.Add(RootPathAttribute.Key, RootPathAttribute);
                        newFileType.AttributeValues.Add(RootPathAttribute.Key, new AttributeValueCache(newRootPath));

                        // save attribute values with the current type ID
                        lookupContext.AttributeValues.Add(newRootPath);
                    }

                    lookupContext.SaveChanges();
                    FileTypes.Add(newFileType);
                }
            }

            // load attributes on file system types to get the default storage location
            foreach (var type in FileTypes)
            {
                type.LoadAttributes(lookupContext);

                if (type.StorageEntityTypeId == FileSystemStorageTypeId && binaryTypeSettings.AllKeys.Any(k => type.Name.Equals(k)))
                {
                    // override the configured storage location since we can't handle relative paths
                    type.AttributeValues["RootPath"].Value = binaryTypeSettings[type.Name];
                }
            }

            // get a list of all the imported people keys
            var personAliasList = new PersonAliasService(lookupContext).Queryable().AsNoTracking().ToList();

            ImportedPeople = personAliasList.Select(pa =>
                                                    new PersonKeys()
            {
                PersonAliasId    = pa.Id,
                PersonId         = pa.PersonId,
                PersonForeignId  = pa.ForeignId,
                PersonForeignKey = pa.ForeignKey
            }).ToList();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads Rock data that's used globally by the transform
        /// </summary>
        private void LoadRockData(RockContext lookupContext = null)
        {
            lookupContext = lookupContext ?? new RockContext();

            // initialize storage providers and file types
            LoadStorageProviders();

            FileTypes = new BinaryFileTypeService(lookupContext).Queryable().AsNoTracking().ToList();

            // core-specified blacklist files
            FileTypeBlackList = (GlobalAttributesCache.Read().GetValue("ContentFiletypeBlacklist")
                                 ?? string.Empty).Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);

            // clean up blacklist
            FileTypeBlackList = FileTypeBlackList.Select(a => a.ToLower().TrimStart(new char[] { '.', ' ' }));

            // get all the file types we'll be importing
            var binaryTypeSettings = ConfigurationManager.GetSection("binaryFileTypes") as NameValueCollection;

            // create any custom types that don't exist yet
            foreach (var typeKey in binaryTypeSettings.AllKeys)
            {
                var fileType = FileTypes.FirstOrDefault(f => f.Name == typeKey);

                // create new binary file type if it doesn't exist
                if (fileType == null)
                {
                    fileType              = new BinaryFileType();
                    fileType.Name         = typeKey;
                    fileType.Description  = typeKey;
                    fileType.AllowCaching = true;

                    var typeValue = binaryTypeSettings[typeKey];
                    if (typeValue != null)
                    {
                        var storageProvider = StorageProviders.FirstOrDefault(p => p.TypeName.RemoveWhitespace().EndsWith(typeValue.RemoveWhitespace()));
                        if (storageProvider != null)
                        {
                            // ensure the storage provider is active
                            fileType.StorageEntityTypeId = storageProvider.EntityType.Id;
                            lookupContext.BinaryFileTypes.Add(fileType);
                            lookupContext.SaveChanges();
                            FileTypes.Add(fileType);
                        }
                        else
                        {
                            LogException("Binary File Import", string.Format("{0} must use the name of a configured storage provider.", typeKey));
                        }
                    }
                    else
                    {
                        LogException("Binary File Import", string.Format("{0} must specify the storage provider type.", typeKey));
                    }
                }
            }

            // load attributes on file types
            foreach (var type in FileTypes)
            {
                type.LoadAttributes(lookupContext);
            }

            // get a list of all the imported people keys
            var personAliasList = new PersonAliasService(lookupContext).Queryable().AsNoTracking().ToList();

            ImportedPeople = personAliasList.Select(pa =>
                                                    new PersonKeys()
            {
                PersonAliasId = pa.Id,
                PersonId      = pa.PersonId,
                IndividualId  = pa.ForeignId,
            }).ToList();
        }