public DocumentDBDataReader()
        {
            var dict = new Dictionary<HighchartsHelper.DocumentTypes, IEnumerable<Document>>();
            _documentClient = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentServiceEndpoint"]), ConfigurationManager.AppSettings["DocumentKey"]);

            _database = _documentClient.CreateDatabaseQuery().Where(db => db.Id == ConfigurationManager.AppSettings["DocumentDatabase"]).AsEnumerable().FirstOrDefault();

            if (_database == null)
                throw new ApplicationException("Error: DocumentDB database does not exist");

            // Check to verify a document collection with the id=FamilyCollection does not exist
            _documentCollection = _documentClient.CreateDocumentCollectionQuery(_database.CollectionsLink).Where(c => c.Id == ConfigurationManager.AppSettings["DocumentCollection"]).AsEnumerable().FirstOrDefault();

            if (_documentCollection == null)
                throw new ApplicationException("Error: DocumentDB collection does not exist");


            try
            {
                _documentClient.CreateUserDefinedFunctionAsync(_documentCollection.SelfLink, new UserDefinedFunction
                {
                    Id = "ISDEFINED",
                    Body = @"function ISDEFINED(doc, prop) {
                            return doc[prop] !== undefined;
                        }"
                });  
            }
            catch (Exception)
            {
                //fail silently for now..
            }
        }
Exemplo n.º 2
0
        public void Initialize(string modelsDirectory)
        {
            Database databaseId = new Database {
                Id = _databaseId
            };
            ResourceResponse <Database> database = _database.CreateDatabaseIfNotExistsAsync(databaseId).Result;
            Uri databaseUri = UriFactory.CreateDatabaseUri(_databaseId);

            string settingKey      = Constant.AppSettingKey.DatabaseCollectionThroughputUnits;
            string throughputUnits = AppSetting.GetValue(settingKey);

            RequestOptions collectionOptions = new RequestOptions()
            {
                OfferThroughput = int.Parse(throughputUnits)
            };

            DocumentCollection documentCollection = new DocumentCollection()
            {
                Id = Constant.Database.Collection.ContentInsight
            };

            ResourceResponse <DocumentCollection> collection = _database.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, collectionOptions).Result;
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, documentCollection.Id);

            string collectionDirectory = string.Concat(modelsDirectory, @"\", documentCollection.Id);

            string jsFile = string.Concat(collectionDirectory, @"\Functions\isTimecodeFragment.js");
            UserDefinedFunction function = new UserDefinedFunction()
            {
                Id   = "isTimecodeFragment",
                Body = File.ReadAllText(jsFile)
            };

            _database.CreateUserDefinedFunctionAsync(collectionUri, function);

            jsFile = string.Concat(collectionDirectory, @"\Procedures\getTimecodeFragment.js");
            StoredProcedure procedure = new StoredProcedure()
            {
                Id   = "getTimecodeFragment",
                Body = File.ReadAllText(jsFile)
            };

            _database.CreateStoredProcedureAsync(collectionUri, procedure);

            documentCollection = new DocumentCollection()
            {
                Id = Constant.Database.Collection.ProcessorConfig
            };

            collection    = _database.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, collectionOptions).Result;
            collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, documentCollection.Id);

            collectionDirectory = string.Concat(modelsDirectory, @"\", documentCollection.Id);

            jsFile    = string.Concat(collectionDirectory, @"\Procedures\getProcessorConfig.js");
            procedure = new StoredProcedure()
            {
                Id   = "getProcessorConfig",
                Body = File.ReadAllText(jsFile)
            };
            _database.CreateStoredProcedureAsync(collectionUri, procedure);

            string presetsDirectory = string.Concat(modelsDirectory, @"\ProcessorPresets");

            string[] mediaProcessors = Directory.GetDirectories(presetsDirectory);
            foreach (string mediaProcessor in mediaProcessors)
            {
                string[] processorPresets = Directory.GetFiles(mediaProcessor);
                foreach (string processorPreset in processorPresets)
                {
                    if (processorPreset.EndsWith(Constant.Media.FileExtension.Json, StringComparison.OrdinalIgnoreCase))
                    {
                        StreamReader   streamReader = new StreamReader(processorPreset);
                        JsonTextReader presetReader = new JsonTextReader(streamReader);
                        JObject        presetConfig = JObject.Load(presetReader);
                        UpsertDocument(documentCollection.Id, presetConfig);
                    }
                }
            }
        }