Esempio n. 1
0
        HadithReferenceDefinition[] ReadReferenceDefinitions(XElement rootNode)
        {
            var result = new List <HadithReferenceDefinition>();
            var referenceDefinitionsNode = rootNode.Element("referenceDefinitions");

            if (referenceDefinitionsNode != null)
            {
                foreach (XElement referenceDefinitionNode in referenceDefinitionsNode.Elements("referenceDefinition"))
                {
                    bool   isPrimary   = bool.Parse(referenceDefinitionNode.Element("isPrimary").Value);
                    string code        = referenceDefinitionNode.Element("code").Value;
                    string name        = referenceDefinitionNode.Element("name").Value;
                    string valuePrefix = referenceDefinitionNode.Element("valuePrefix")?.Value;
                    var    partNames   = new List <string>();
                    var    partsNode   = referenceDefinitionNode.Element("parts");
                    foreach (XElement partNode in partsNode.Elements("part"))
                    {
                        partNames.Add(partNode.Value);
                    }
                    var definition = new HadithReferenceDefinition(
                        isPrimary: isPrimary,
                        code: code,
                        name: name,
                        partNames: partNames,
                        valuePrefix: valuePrefix);
                    result.Add(definition);
                }
            }
            return(result.ToArray());
        }
Esempio n. 2
0
        public IEnumerable <HadithViewModel> Create(IEnumerable <Hadith> hadiths)
        {
            var result = new List <HadithViewModel>();

            foreach (Hadith hadith in hadiths)
            {
                string           collectionCode = hadith.References[0].CollectionCode;
                HadithCollection collection     = HadithCollectionRepository.Get(collectionCode);
                var references = new List <HadithReferenceViewModel>();
                foreach (HadithReference reference in hadith.References)
                {
                    HadithReferenceDefinition referenceDefinition =
                        collection.GetReferenceDefinition(reference.ReferenceCode);
                    var referenceViewModel = new HadithReferenceViewModel(
                        collectionCode: collection.Code,
                        collectionName: collection.Name,
                        indexCode: referenceDefinition.Code,
                        indexName: referenceDefinition.Name,
                        partNamesAndValues: reference.ToNameValuePairs(referenceDefinition));
                    references.Add(referenceViewModel);
                }
                var viewModel = new HadithViewModel(
                    collectionName: collection.Name,
                    hadith: hadith,
                    references: references);
                result.Add(viewModel);
            }
            return(result);
        }
 void WriteHadithReferenceDefinition(HadithReferenceDefinition definition)
 {
     using (Xml.WriteElement("referenceDefinition"))
     {
         Xml.WriteElementString("isPrimary", definition.IsPrimary + "");
         Xml.WriteElementString("code", definition.Code);
         Xml.WriteElementString("name", definition.Name);
         Xml.WriteElementString("valuePrefix", definition.ValuePrefix);
         using (Xml.WriteElement("parts"))
         {
             foreach (string partName in definition.PartNames)
             {
                 Xml.WriteElementString("part", partName);
             }
         }
     }
 }
Esempio n. 4
0
        public Hadith_DrillDown(
            string collectionCode,
            string indexCode,
            string path)
        {
            if (string.IsNullOrWhiteSpace(collectionCode))
            {
                throw new ArgumentNullException(nameof(collectionCode));
            }
            if (string.IsNullOrWhiteSpace(indexCode))
            {
                throw new ArgumentNullException(nameof(indexCode));
            }

            path                = path ?? "";
            this.Collection     = SharedData.Document.HadithDocument[collectionCode];
            ReferenceDefinition = Collection.GetReferenceDefinition(indexCode);
            if (ReferenceDefinition == null)
            {
                throw new KeyNotFoundException(nameof(indexCode));
            }

            this.NextKeyPartSelection = new List <string>();
            this.SelectedKeyParts     =
                path.Split('/')
                .Where(x => !string.IsNullOrEmpty(x))
                .Select(x => x.Trim())
                .Select(x =>
            {
                string[] keyAndValue = x.Split('-');
                return(new KeyValuePair <string, string>(keyAndValue[0], keyAndValue[1]));
            }
                        )
                .ToList();

            int referencePartIndex = 0;

            HadithsInCurrentSelection =
                Collection
                .Hadiths
                .Select(x => new KeyValuePair <HadithReference, Hadith>(x.GetReference(ReferenceDefinition.Code), x))
                .Where(x => x.Key != null)
                .OrderBy(x => x.Key);
            foreach (var keyPartAndValue in SelectedKeyParts)
            {
                if (string.Compare(keyPartAndValue.Key, ReferenceDefinition.PartNames[referencePartIndex], true) != 0)
                {
                    throw new ArgumentException(string.Format("Expected key part {0} but found {1}", ReferenceDefinition.PartNames[referencePartIndex], keyPartAndValue.Key));
                }

                HadithsInCurrentSelection =
                    HadithsInCurrentSelection
                    .Where(x => string.Compare(x.Key[referencePartIndex], keyPartAndValue.Value, true) == 0)
                    .ToList();
                referencePartIndex++;
            }
            if (referencePartIndex < ReferenceDefinition.PartNames.Length)
            {
                NextKeyPartName      = ReferenceDefinition.PartNames[referencePartIndex];
                NextKeyPartSelection = HadithsInCurrentSelection.Select(x => x.Key[referencePartIndex]).Distinct();
            }
        }
Esempio n. 5
0
        public ActionResult Index(string collectionCode, string referenceCode,
                                  string referenceValue1, string referenceValue2, string referenceValue3)
        {
            HadithCollection collection = HadithCollectionRepository.Get(collectionCode);

            if (collection == null)
            {
                return(HttpNotFound());
            }

            var referencePartNamesAndValues = new List <(string referencePartName, int value, string suffix)>();

            if (!string.IsNullOrWhiteSpace(referenceValue1))
            {
                referencePartNamesAndValues.Add(HadithReference.SplitNameAndValue(referenceValue1));
            }
            if (!string.IsNullOrWhiteSpace(referenceValue2))
            {
                referencePartNamesAndValues.Add(HadithReference.SplitNameAndValue(referenceValue2));
            }
            if (!string.IsNullOrWhiteSpace(referenceValue3))
            {
                referencePartNamesAndValues.Add(HadithReference.SplitNameAndValue(referenceValue3));
            }

            IEnumerable <string> referencePartNames = referencePartNamesAndValues
                                                      .Select(x => x.referencePartName);
            HadithReferenceDefinition referenceDefinition = collection.GetReferenceDefinition(referenceCode);

            if (referenceDefinition == null && referenceCode.Contains("-"))
            {
                return(RedirectForMissingIndexCode(collectionCode, referenceCode, collection, referencePartNamesAndValues, referencePartNames, ref referenceDefinition));
            }

            if (referenceDefinition == null || !referenceDefinition.PatternMatch(referencePartNames))
            {
                return(HttpNotFound());
            }

            IEnumerable <(int value, string suffix)> referenceValues =
                referencePartNamesAndValues.Select(x => (x.value, x.suffix));
            IEnumerable <HadithReference> hadithReferences =
                HadithRepository.GetReferences(
                    collectionCode: collectionCode,
                    referenceCode: referenceCode,
                    values: referenceValues);

            IEnumerable <string> urlReferenceParts = referencePartNamesAndValues
                                                     .Select(x => x.value + x.suffix)
                                                     .Select((value, i) => referenceDefinition.PartNames[i] + "-" + value);
            string partsAsUrl = string.Join("/", urlReferenceParts);
            string urlSoFar   = $"/hadith/{collectionCode}/{referenceCode}/{partsAsUrl}";

            if (urlSoFar.EndsWith("/"))
            {
                urlSoFar = urlSoFar.Substring(0, urlSoFar.Length - 1);
            }
            var headerViewModel = new HadithIndexHeaderViewModel(
                selectedReferenceCode: referenceCode,
                urlSoFar: urlSoFar,
                collection: collection,
                referencePartNamesAndValues: urlReferenceParts);

            if (referencePartNames.Count() == referenceDefinition.PartNames.Count)
            {
                IEnumerable <int>             hadithIds        = hadithReferences.Select(x => x.HadithId);
                IEnumerable <Hadith>          hadiths          = HadithRepository.GetHadiths(hadithIds);
                IEnumerable <HadithViewModel> hadithViewModels = HadithViewModelFactory.Create(hadiths);
                var viewModel = new HadithsViewModel(headerViewModel, hadithViewModels);
                return(View("Hadiths", viewModel));
            }
            else
            {
                string nextReferencePartName = referenceDefinition.PartNames[referencePartNames.Count()];
                Func <HadithReference, string> getNextValue;
                switch (referencePartNames.Count())
                {
                case 0:
                    getNextValue = x => x.ReferenceValue1 + x.ReferenceValue1Suffix;
                    break;

                case 1:
                    getNextValue = x => x.ReferenceValue2 + x.ReferenceValue2Suffix;
                    break;

                case 2:
                    getNextValue = x => x.ReferenceValue3 + x.ReferenceValue3Suffix;
                    break;

                default:
                    throw new NotImplementedException();
                }
                // If the next level is the final level (the hadith itself) then remove the suffix
                // from the final part so that all hadiths with the same reference but different
                // suffixes are shown on screen at once.
                hadithReferences = hadithReferences
                                   .Select(x => x.ExcludingFinalSuffix())
                                   .Distinct()
                                   .OrderBy(x => x);
                // Get the next available values
                IEnumerable <string> nextReferencePartValues = hadithReferences
                                                               .Select(getNextValue)
                                                               .Distinct();
                var viewModel = new BrowseHadithIndexViewModel(
                    hadithIndexHeaderViewModel: headerViewModel,
                    nextReferencePartName: nextReferencePartName,
                    nextReferencePartValueSelection: nextReferencePartValues);
                return(View("BrowseHadithIndex", viewModel));
            }
        }