LibiadaWeb.Models.Repositories.Sequences.ElementRepository.ToDbElements C# (CSharp) Method

ToDbElements() public method

The to db elements.
/// Thrown if alphabet element is not found in db. ///
public ToDbElements ( Alphabet alphabet, int notationId, bool createElements ) : long[]
alphabet Alphabet /// The alphabet. ///
notationId int /// The notation id. ///
createElements bool /// The create elements. ///
return long[]
        public long[] ToDbElements(Alphabet alphabet, int notationId, bool createElements)
        {
            if (!ElementsInDb(alphabet, notationId))
            {
                if (createElements)
                {
                    CreateLackingElements(alphabet, notationId);
                }
                else
                {
                    throw new Exception("At least one element of alphabet is not found in database.");
                }
            }

            var staticNotation = Aliases.Notation.StaticNotations.Contains(notationId);

            var stringElements = alphabet.Select(element => element.ToString()).ToList();

            var elements = staticNotation ?
                            CachedElements.Where(e => e.NotationId == notationId && stringElements.Contains(e.Value)).ToList() :
                            db.Element.Where(e => e.NotationId == notationId && stringElements.Contains(e.Value)).ToList();

            return (from stringElement in stringElements
                    join element in elements
                    on stringElement equals element.Value
                    select element.Id).ToArray();
        }

Usage Example

        /// <summary>
        /// Creates literature sequence in database.
        /// </summary>
        /// <param name="commonSequence">
        /// The common sequence.
        /// </param>
        /// <param name="sequenceStream">
        /// The sequence stream.
        /// </param>
        /// <param name="language">
        /// The language id.
        /// </param>
        /// <param name="original">
        /// The original.
        /// </param>
        /// <param name="translator">
        /// The translator id.
        /// </param>
        /// <param name="dropPunctuation">
        /// Flag indicating if punctuation should be removed from text.
        /// </param>
        public void Create(CommonSequence commonSequence, Stream sequenceStream, Language language, bool original, Translator translator, bool dropPunctuation = false)
        {
            string    stringSequence = FileHelper.ReadSequenceFromStream(sequenceStream);
            BaseChain chain;

            if (commonSequence.Notation == Notation.Letters)
            {
                stringSequence = stringSequence.ToUpper();
                if (dropPunctuation)
                {
                    stringSequence = new string(stringSequence.Where(c => !char.IsPunctuation(c)).ToArray());
                }
                chain = new BaseChain(stringSequence);
            }
            else
            {
                // file always contains empty string at the end
                // TODO: rewrite this, add empty string check at the end or write a normal trim
                string[] text = stringSequence.Split(new[] { '\n', '\r', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                chain = new BaseChain(text.Select(e => (ValueString)e).Cast <IBaseObject>().ToList());
            }

            MatterRepository.CreateOrExtractExistingMatterForSequence(commonSequence);

            long[] alphabet = ElementRepository.ToDbElements(chain.Alphabet, commonSequence.Notation, true);
            Create(commonSequence, original, language, translator, alphabet, chain.Building);
        }
All Usage Examples Of LibiadaWeb.Models.Repositories.Sequences.ElementRepository::ToDbElements