public void TestCoreStandardFromIdentifier()
        {
            StandardIdentifier identifier = StandardIdentifierTranslation.StandardStringToStandardIdentifier(elaStandardString);
            var coreStandardsRowCcss      = ImmutableArray.Create(
                CoreStandardsRow.Create(
                    subjectCode: "ELA",
                    key: "3-l|4-6|6.SL.2",
                    name: "",
                    description: "CCSS Desc",
                    levelType: "CCSS",
                    identifier: identifier));

            var coreStandardsRowTarget = ImmutableArray.Create(
                CoreStandardsRow.Create(
                    subjectCode: "ELA",
                    key: "4-6|3-6",
                    name: "",
                    description: "Target Desc",
                    levelType: "Target",
                    identifier: identifier));

            CoreStandardsXml coreStandardsXml = new CoreStandardsXml(coreStandardsRowTarget, coreStandardsRowCcss);
            CoreStandards    core             = StandardIdentifierTranslation.CoreStandardFromIdentifier(coreStandardsXml, identifier);

            Assert.NotNull(core);
            Assert.Equal(core.CommonCoreStandardsId, "6.SL.2");
            Assert.Equal(core.TargetId, "4-6");
            Assert.Equal(core.TargetIdLabel, "4");
            Assert.Equal(core.TargetDescription, "Target Desc");
            Assert.Equal(core.CommonCoreStandardsDescription, "CCSS Desc");
        }
        public static CoreStandards CoreStandardFromIdentifier(
            CoreStandardsXml standardsXml,
            StandardIdentifier itemIdentifier)
        {
            CoreStandardsRow targetRow = null;
            CoreStandardsRow ccssRow   = null;

            if (standardsXml != null && standardsXml.TargetRows.Any())
            {
                targetRow = standardsXml.TargetRows
                            .FirstOrDefault(t =>
                                            StandardIdentifierTargetComparer.Instance.Equals(t.StandardIdentifier, itemIdentifier));
            }

            if (standardsXml != null && standardsXml.CcssRows.Any())
            {
                ccssRow = standardsXml.CcssRows
                          .FirstOrDefault(t =>
                                          StandardIdentifierCcssComparer.Instance.Equals(t.StandardIdentifier, itemIdentifier));
            }

            return(CoreStandards.Create(
                       targetId: itemIdentifier?.Target,
                       targetIdLabel: itemIdentifier?.ToTargetId(),
                       commonCoreStandardsId: itemIdentifier?.CommonCoreStandard,
                       commonCoreStandardsDescription: ccssRow?.Description,
                       targetDescription: targetRow?.Description,
                       claimId: itemIdentifier?.ToClaimId(),
                       publication: itemIdentifier?.Publication));
        }
        private static CoreStandards ApplyPatchToCoreStandards(StandardIdentifier identifier,
                                                               CoreStandards coreStandards,
                                                               CoreStandardsXml standardsXml,
                                                               ItemPatch patch)
        {
            string claimNumber = Regex.Match(input: patch.Claim, pattern: @"\d+").Value;

            if (identifier == null)
            {
                identifier = StandardIdentifier.Create(claim: claimNumber, target: patch.Target);
            }
            else
            {
                string target = (!string.IsNullOrEmpty(patch.Target)) ? patch.Target : identifier.Target;
                claimNumber = (!string.IsNullOrEmpty(claimNumber)) ? claimNumber : identifier.Claim;
                identifier  = identifier.WithClaimAndTarget(claimNumber, target);
            }

            string targetDesc = (!string.IsNullOrEmpty(patch.TargetDescription)) ? patch.TargetDescription : coreStandards?.TargetDescription;
            string ccssDesc   = (!string.IsNullOrEmpty(patch.CCSSDescription)) ? patch.CCSSDescription : coreStandards?.CommonCoreStandardsDescription;

            coreStandards = StandardIdentifierTranslation.CoreStandardFromIdentifier(standardsXml, identifier);
            coreStandards = coreStandards.WithTargetCCSSDescriptions(targetDesc, ccssDesc);

            return(coreStandards);
        }
        /// <summary>
        /// Digests a collection of ItemMetadata objects and a collection of ItemContents objects into a collection of ItemDigest objects.
        /// Matches the ItemMetadata and ItemContents objects based on their ItemKey fields.
        /// </summary>
        public static ImmutableArray <SampleItem> ToSampleItems(
            ImmutableArray <ItemDigest> digests,
            IList <MergedAccessibilityFamily> resourceFamilies,
            IList <InteractionType> interactionTypes,
            IList <Subject> subjects,
            CoreStandardsXml standardsXml,
            IList <ItemPatch> patches,
            IList <BrailleFileInfo> brailleFileInfo,
            AppSettings settings
            )
        {
            var sampleItems = digests.Select(d =>
                                             ToSampleItem(
                                                 itemDigest: d,
                                                 standardsXml: standardsXml,
                                                 subjects: subjects,
                                                 interactionTypes: interactionTypes,
                                                 resourceFamilies: resourceFamilies,
                                                 patches: patches,
                                                 brailleFileInfo: brailleFileInfo,
                                                 settings: settings
                                                 ))
                              .ToImmutableArray();

            return(sampleItems);
        }
        public static SampleItemsContext LoadContext(AppSettings appSettings, ILogger logger)
        {
            CoreStandardsXml standardsXml     = LoadCoreStandards(appSettings.SettingsConfig.CoreStandardsXMLPath);
            var accessibilityResourceFamilies = LoadAccessibility(appSettings.SettingsConfig.AccommodationsXMLPath);
            var interactionGroup = LoadInteractionGroup(appSettings.SettingsConfig.InteractionTypesXMLPath);
            ImmutableArray <Subject> subjects = LoadSubjects(appSettings.SettingsConfig.ClaimsXMLPath, interactionGroup.InteractionFamilies);

            var itemDigests = LoadItemDigests(appSettings).Result;

            var itemPatchPath   = appSettings.SettingsConfig.PatchXMLPath;
            var itemPatchRoot   = XmlSerialization.DeserializeXml <ItemPatchRoot>(filePath: itemPatchPath);
            var brailleFileInfo = BrailleManifestReader.GetBrailleFileInfo(appSettings).Result;

            var sampleItems = SampleItemTranslation.ToSampleItems(
                digests: itemDigests,
                settings: appSettings,
                resourceFamilies: accessibilityResourceFamilies,
                interactionTypes: interactionGroup.InteractionTypes,
                subjects: subjects,
                patches: itemPatchRoot.Patches,
                standardsXml: standardsXml,
                brailleFileInfo: brailleFileInfo);

            var itemCards = sampleItems
                            .Select(i => i.ToItemCardViewModel())
                            .ToImmutableArray();

            var aboutInteractionTypes = LoadAboutInteractionTypes(interactionGroup);

            SampleItemsContext context = new SampleItemsContext(
                sampleItems: sampleItems,
                itemCards: itemCards,
                interactionTypes: interactionGroup.InteractionTypes,
                subjects: subjects,
                appSettings: appSettings,
                aboutInteractionTypes: aboutInteractionTypes);

            logger.LogInformation($"Loaded {sampleItems.Length} sample items");
            logger.LogInformation($"Context loaded successfully");

            return(context);
        }
        /// <summary>
        /// Translates metadata, itemcontents and lookups to item digest
        /// </summary>
        public static SampleItem ToSampleItem(
            ItemDigest itemDigest,
            CoreStandardsXml standardsXml,
            IList <Subject> subjects,
            IList <InteractionType> interactionTypes,
            IList <MergedAccessibilityFamily> resourceFamilies,
            IList <ItemPatch> patches,
            IList <BrailleFileInfo> brailleFileInfo,
            AppSettings settings)
        {
            var supportedPubs       = settings.SettingsConfig.SupportedPublications;
            var rubrics             = GetRubrics(itemDigest, settings);
            var brailleItemCodes    = GetBrailleItemCodes(itemDigest.ItemKey, brailleFileInfo);
            var braillePassageCodes = GetBraillePassageCodes(itemDigest, brailleFileInfo);
            var interactionType     = interactionTypes.FirstOrDefault(t => t.Code == itemDigest.InteractionTypeCode);
            var grade            = GradeLevelsUtils.FromString(itemDigest.GradeCode);
            var patch            = patches.FirstOrDefault(p => p.ItemId == itemDigest.ItemKey);
            var copiedItemPatch  = patches.FirstOrDefault(p => p.BrailleCopiedId == itemDigest.ItemKey.ToString());
            var subject          = subjects.FirstOrDefault(s => s.Code == itemDigest.SubjectCode);
            var depthOfKnowledge = itemDigest.DepthOfKnowledge;
            var itemType         = itemDigest.ItemType;

            var fieldTestUseAttribute = itemDigest.ItemMetadataAttributes?.FirstOrDefault(a => a.Code == "itm_FTUse");
            var fieldTestUse          = FieldTestUse.Create(fieldTestUseAttribute, itemDigest.SubjectCode);

            StandardIdentifier identifier    = StandardIdentifierTranslation.ToStandardIdentifier(itemDigest, supportedPubs);
            CoreStandards      coreStandards = StandardIdentifierTranslation.CoreStandardFromIdentifier(standardsXml, identifier);
            int?copiedFromItem = null;

            if (patch != null)
            {
                int tmp;
                copiedFromItem   = int.TryParse(patch.BrailleCopiedId, out tmp) ? (int?)tmp : null;
                depthOfKnowledge = !string.IsNullOrEmpty(patch.DepthOfKnowledge) ? patch.DepthOfKnowledge : depthOfKnowledge;
                itemType         = !string.IsNullOrEmpty(patch.ItemType) ? patch.ItemType : itemType;
                coreStandards    = ApplyPatchToCoreStandards(identifier, coreStandards, standardsXml, patch);
            }

            if (copiedItemPatch != null)
            {
                var copyBrailleItemCodes = GetBrailleItemCodes(copiedItemPatch.ItemId, brailleFileInfo);
                brailleItemCodes = brailleItemCodes.Concat(copyBrailleItemCodes).Distinct().ToImmutableArray();
            }

            if (patch != null && !string.IsNullOrEmpty(patch.QuestionNumber))
            {
                fieldTestUse = ApplyPatchFieldTestUse(fieldTestUse, patch);
            }

            var  claim         = subject?.Claims.FirstOrDefault(t => t.ClaimNumber == coreStandards.ClaimId);
            bool brailleOnly   = copiedFromItem.HasValue;
            bool isPerformance = fieldTestUse != null && itemDigest.AssociatedPassage.HasValue;

            if (itemDigest.AssociatedPassage.HasValue)
            {
                braillePassageCodes = brailleFileInfo
                                      .Where(f => f.ItemKey == itemDigest.AssociatedPassage.Value)
                                      .Select(b => b.BrailleType).ToImmutableArray();
            }
            else
            {
                braillePassageCodes = ImmutableArray.Create <string>();
            }

            bool aslSupported = AslSupported(itemDigest);

            var groups = GetAccessibilityResourceGroups(itemDigest, resourceFamilies, grade,
                                                        isPerformance, aslSupported, claim, interactionType, brailleItemCodes, settings);

            string interactionTypeSubCat = "";

            settings.SettingsConfig.InteractionTypesToItem.TryGetValue(itemDigest.ToString(), out interactionTypeSubCat);

            SampleItem sampleItem = new SampleItem(
                itemType: itemType,
                itemKey: itemDigest.ItemKey,
                bankKey: itemDigest.BankKey,
                targetAssessmentType: itemDigest.TargetAssessmentType,
                depthOfKnowledge: depthOfKnowledge,
                sufficentEvidenceOfClaim: itemDigest.SufficentEvidenceOfClaim,
                associatedStimulus: itemDigest.AssociatedStimulus,
                aslSupported: aslSupported,
                allowCalculator: itemDigest.AllowCalculator,
                isPerformanceItem: isPerformance,
                accessibilityResourceGroups: groups,
                rubrics: rubrics,
                interactionType: interactionType,
                subject: subject,
                claim: claim,
                grade: grade,
                coreStandards: coreStandards,
                fieldTestUse: fieldTestUse,
                interactionTypeSubCat: interactionTypeSubCat,
                brailleItemCodes: brailleItemCodes,
                braillePassageCodes: braillePassageCodes,
                brailleOnlyItem: brailleOnly,
                copiedFromitem: copiedFromItem);

            return(sampleItem);
        }
        public void TestToSampleItems()
        {
            var digests = ImmutableArray.Create(digest);
            ImmutableArray <AccessibilityFamilyResource> noPartialResources = ImmutableArray.Create <AccessibilityFamilyResource>();
            var subjectMath = ImmutableArray.Create("MATH");
            AccessibilityFamily noPartialResourcesFamily = new AccessibilityFamily(
                subjects: subjectMath,
                grades: GradeLevels.Grade5,
                resources: noPartialResources);

            MergedAccessibilityFamily resourceFamily = AccessibilityResourceTranslation.MergeGlobalResources(noPartialResourcesFamily, Resources);
            var resourceFamilies = ImmutableArray.Create(resourceFamily);

            Claim1 = new Claim("claim1", "1", "ELA Claim 1");
            Claim2 = new Claim("claim2", "2", "3");
            ItMath = new InteractionType("2", "EQ", "", 2);
            ItEla  = new InteractionType("1", "WER", "", 1);
            Math   = Subject.Create("MATH", "Mathematics", "Math", ImmutableArray.Create(Claim2), ImmutableArray.Create(ItMath.Code));
            Ela    = Subject.Create("ELA", "English", "ELA", ImmutableArray.Create(Claim1), ImmutableArray.Create(ItEla.Code));
            var interactionTypes = ImmutableArray.Create(ItEla, ItMath);
            var subjects         = ImmutableArray.Create(Ela, Math);

            StandardIdentifier identifier = StandardIdentifierTranslation.StandardStringToStandardIdentifier("SBAC-ELA-v1:3-L|4-6|6.SL.2");
            var coreStandardsRowCcss      = ImmutableArray.Create(
                CoreStandardsRow.Create(
                    subjectCode: "ELA",
                    key: "3-l|4-6|6.SL.2",
                    name: "",
                    description: "CCSS Desc",
                    levelType: "CCSS",
                    identifier: identifier));

            var coreStandardsRowTarget = ImmutableArray.Create(
                CoreStandardsRow.Create(
                    subjectCode: "ELA",
                    key: "4-6|3-6",
                    name: "",
                    description: "Target Desc",
                    levelType: "Target",
                    identifier: identifier));

            CoreStandardsXml coreStandardsXml = new CoreStandardsXml(coreStandardsRowTarget, coreStandardsRowCcss);
            var itemPatch       = ImmutableArray.Create(new ItemPatch());
            var brailleFileInfo = new List <BrailleFileInfo>();
            var items           = SampleItemTranslation.ToSampleItems(digests, resourceFamilies, interactionTypes, subjects, coreStandardsXml, itemPatch, brailleFileInfo, appSettings);

            Assert.NotNull(items);
            Assert.Equal(items[0].AccessibilityResourceGroups[0].AccessibilityResources[0].Label, Resources[0].Label);
            Assert.Equal(items[0].AccessibilityResourceGroups[0].AccessibilityResources[0].Order, Resources[0].Order);
            Assert.Equal(items[0].AccessibilityResourceGroups[0].AccessibilityResources[0].ResourceCode, Resources[0].ResourceCode);
            Assert.Equal(items[0].AccessibilityResourceGroups[0].AccessibilityResources[0].ResourceTypeId, Resources[0].ResourceTypeId);
            Assert.Equal(items[0].SufficentEvidenceOfClaim, metadata.Metadata.SufficientEvidenceOfClaim);
            Assert.Equal(items[0].TargetAssessmentType, metadata.Metadata.TargetAssessmentType);
            Assert.Equal(items[0].Grade, GradeLevelsUtils.FromString(digest.GradeCode));
            Assert.Equal(items[0].ItemKey, digest.ItemKey);
            Assert.Equal(items[0].BankKey, digest.BankKey);
            Assert.Equal(items[0].CoreStandards.CommonCoreStandardsDescription, coreStandardsRowCcss[0].Description);
            Assert.Equal(items[0].CoreStandards.TargetDescription, coreStandardsRowTarget[0].Description);
            Assert.Equal(items[0].InteractionType.Code, ItMath.Code);
            Assert.Equal(items[0].InteractionType.Label, ItMath.Label);
            Assert.Equal(items[0].Subject.Code, digest.SubjectCode);
        }