Пример #1
0
        public DbRequest MakeDbRequest(string input, bool shouldShowAll)
        {
            input = input.TrimEnd('?');
            // input comes in as "מה המספר של עומר"

            input = input.Replace(key1Lookup, "");
            // Now input is "מספר של עומר"

            var inputSplitOnKey2 = key2Lookup.Split(input, 2);

            // inputSplitOnKey2 is ["עומר" ,"מספר" ]
            if (inputSplitOnKey2.Length != 2)
            {
                return(null);
            }

            lookupField = inputSplitOnKey2[0];


            lookupValue = inputSplitOnKey2[1].TrimEnd(' ');
            lookupValue = inputSplitOnKey2[1].TrimStart(' ');
            var dbRequest = new DbRequest(lookupValue, shouldShowAll);

            return(dbRequest.IsValid ? dbRequest : null);
        }
Пример #2
0
        public DbRequest MakeDbRequest(string input, bool shouldShowAll)
        {
            input = input.TrimEnd('!');
            input = input.TrimEnd('?');

            // input comes in as "עד מתי שמעון"
            // Now input is "שמעון"
            input = input.Replace(key1Lookup, "");

            lookupField = "תאריך שחרור";

            // queryParts is ["שמעון" ]
            string[] queryParts = input.Split(' ');

            // build the lookup value from all query parts except of the last one
            string value = "";

            for (int i = 0; i < queryParts.Length; i++)
            {
                value += ' ' + queryParts[i];
            }
            value.TrimStart(' ');

            lookupValue = value;

            var dbRequest = new DbRequest(lookupValue, shouldShowAll);

            return(dbRequest.IsValid ? dbRequest : null);
        }
Пример #3
0
        // (1) Validate the query and return an empty list if the query is invalid.
        // (2) Convert the input query to a SQL query.
        // (3) Post-process each SQL output to convert it to the JSON the client
        //     expects.
        // (4) Return a list containing a Metadata object and the list of JSON
        //     objects for each person.
        public IEnumerable <object> GetPersons(string input, bool shouldShowAll)
        {
            var translatedInput = EnglishToHebrew.maybeConvertToHebrew(input);

            DbRequest dbRequest     = null;
            ITemplate templateToUse = null;

            foreach (ITemplate template in Templates.CreateTemplateList())
            {
                if (!template.MatchingRegex().IsMatch(translatedInput))
                {
                    continue;
                }
                dbRequest = template.MakeDbRequest(translatedInput, shouldShowAll);
                if (dbRequest == null)
                {
                    continue;
                }
                templateToUse = template;
                break;
            }

            if (dbRequest == null || templateToUse == null)
            {
                return(new object[] { });
            }

            var returnObjects  = createMatchingPersonsList(dbRequest, templateToUse);
            var metadataObject =
                createMetadataObject(templateToUse, returnObjects, dbRequest, input, translatedInput);

            returnObjects.Insert(0, metadataObject);

            return(returnObjects);
        }
Пример #4
0
        private static Expression getExpressionForStrings <T>(
            DbRequest dbRequest, ParameterExpression parameter)
        {
            var birthday = getTodayBirthdayString();

            return(dbRequest.StandardInputValues
                   .Select(value =>
            {
                if (value.Equals(TermExtractions.BIRTHDAY))
                {
                    var today = getTodayBirthdayString();
                    return BIRTHDAY_STRING.ToExpression <T>(
                        STRING_EQUALS_METHOD, today, parameter);
                }

                // For the partial match strings, we search without quotation marks.
                var valueForPartialStrings = value.Replace("\"", "");
                var partialMatches = STRING_PARTIAL.Select(member =>
                                                           member.ToExpression <T>(
                                                               "Contains", valueForPartialStrings, parameter));

                var startsWithMatches = STRING_STARTS_WITH
                                        .Select(member => member.ToExpression <T>(
                                                    "StartsWith", value, parameter));

                var fullMatches = STRING_FULL_MATCH.Select(
                    member => member.ToExpression <T>(
                        STRING_EQUALS_METHOD, value, parameter));

                return partialMatches.Union(startsWithMatches)
                .Union(fullMatches).Aggregate(Expression.OrElse);
            }).Aggregate(Expression.AndAlso));
        }
Пример #5
0
        // Retrieves the matching people form the Database and wraps each one in a
        // PersonFromDbWrapper object.
        // Query the DB for matching persons. The current algorithm is to select
        // every person for whom every distinct word that is searched appears in
        // at least one field in their row.
        // Then post-process the list to generate JSON that the client knows how
        // to receive.
        public static IEnumerable <PersonFromDbWrapper> GetPersonsFromDb(
            DbRequest dbRequest)
        {
            var dataContext = new PersonDataContext();

            dataContext.Log = new DebugWriter(); // Toggle comment to log SQL.

            return(search(dbRequest, dataContext));
        }
Пример #6
0
        public static IQueryable <T> WhereMatches <T>(
            this IQueryable <T> source,
            DbRequest dbRequest)
        {
            var parameter = Expression.Parameter(typeof(T), "r");
            var body      = dbRequest.IsOnlyNumbers ?
                            getExpressionForNumbers <T>(dbRequest, parameter)
                : getExpressionForStrings <T>(dbRequest, parameter);
            var predicate = Expression.Lambda <Func <T, bool> >(body, parameter);

            return(source.Where(predicate));
        }
Пример #7
0
 private static Expression getExpressionForNumbers <T>(
     DbRequest dbRequest, ParameterExpression parameter)
 {
     return(dbRequest.StandardInputValues
            .Select(value =>
     {
         var valueToSearch = value.ReplaceAll("-", "");
         return NUMBER_PARTIAL.Select(member =>
                                      member.ToExpression <T>("Contains", valueToSearch, parameter))
         .Aggregate(Expression.OrElse);
     }).Aggregate(Expression.AndAlso));
 }
Пример #8
0
        public DbRequest MakeDbRequest(string input, bool shouldShowAll)
        {
            input = input.TrimEnd('?');
            // input comes in as "מתי עומר נולד"
            // Now input is "עומר נולד"
            input = input.Replace(key1Lookup, "");
            // queryParts is ["עומר" ,"נולד" ]
            string[] queryParts = input.Split(' ');

            // try to find a relevant lookup field based on the last query parts
            List <string> fields     = null;
            int           fieldIndex = queryParts.Length - 1;

            for (int i = queryParts.Length - 1; i > 0; i--)
            {
                fields = FieldsAliases.getJsonFields(queryParts[i]);

                if (fields != null && fields.Count > 0)
                {
                    // the recieved field is relevant for this template
                    if (fields[0] == FieldsAliases.BIRTHDAY ||
                        fields[0] == FieldsAliases.END_OF_SERVICE)
                    {
                        lookupField = queryParts[i];
                        fieldIndex  = i;
                    }
                }
            }

            // if a field has been found, set the db request
            if (lookupField != null)
            {
                string value = "";
                // build the lookup value from all query parts except of the last one
                for (int i = 0; i < fieldIndex; i++)
                {
                    value += " " + queryParts[i];
                }

                lookupValue = value;

                var dbRequest = new DbRequest(lookupValue, shouldShowAll);
                return(dbRequest.IsValid ? dbRequest : null);
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
        private List <object> createMatchingPersonsList(DbRequest dbRequest, ITemplate templateToUse)
        {
            // Post process the selected entities; adjusting and renaming some fields.
            var personJsonsFromDb = DbReader.GetPersonsFromDb(dbRequest)
                                    .Select(person => new PersonJsonWrapper(person, templateToUse.getLookupField()))
                                    .ToList();

            var processedPersonJsons = templateToUse.ProcessPersonJsons(personJsonsFromDb);

            return(personJsonsFromDb
                   .OrderByDescending(person => person.IsMe)
                   .ThenByDescending(person => person.Mail)
                   .ThenBy(person => person.Name)
                   .Select(person => person.JsonFromClient)
                   .ToList());
        }
Пример #10
0
        private object createMetadataObject(ITemplate template,
                                            IEnumerable <object> persons,
                                            DbRequest dbRequest, string originalInput, string translatedInput)
        {
            // TODO(josh): This is a cheating heuristic that is occasionally
            // incorrect.
            // Fix - Request one more number than you're displaying.
            var listWasCutOff =
                !dbRequest.ShouldShowAll &&
                persons.Count() == dbRequest.NumberToTake;

            return(new {
                //query = template.MetdataDisplayValue(),
                templateData = template.AddMetadata(),
                shouldShowSeeMore = listWasCutOff,
                isAdmin = CurrentMisparIshi.IsAdmin(),
                originalInput = originalInput,
                translatedInput = translatedInput,
                nonAdminsCanAddTags = false
            });
        }
Пример #11
0
        private static IEnumerable <PersonFromDbWrapper> search(
            DbRequest dbRequest,
            PersonDataContext dataContext)
        {
            var query = dataContext.Persons.AsQueryable();

            if (dbRequest.Tags > 1)
            {
                query = query.Where(person =>
                                    person.Tags % dbRequest.Tags == 0);
            }
            if (dbRequest.StandardInputValues.Count != 0)
            {
                query = query.WhereMatches(dbRequest);
            }
            // Note that WhereMatches is defined in WhereMatchesQuery.cs.
            return(query
                   .Take(dbRequest.NumberToTake)
                   .Select(person => new PersonFromDbWrapper(
                               person.MisparIshi,
                               person.GivenName,
                               person.Surname,
                               person.Mail,
                               person.Mobile,
                               person.JobTitle,
                               person.WorkPhone,
                               person.OtherTelephone,
                               person.Fax,
                               person.HomeTelephone,
                               person.LongWorkTitle,
                               person.Picture,
                               person.BirthdayDisplayString,
                               person.Darga,
                               person.Sex,
                               person.Tags,
                               person.EndOfService,
                               person.WhatIDo))
                   .ToList());
        }
Пример #12
0
        public DbRequest MakeDbRequest(string input, bool shouldShowAll)
        {
            string actionString;

            foreach (List <string> currWords in actionWordsLookup)
            {
                for (int i = 1; (i < currWords.Count) && (string.IsNullOrWhiteSpace(lookupAction)); i++)
                {
                    for (int j = 0; (j < toWordsLookup.Count) && (string.IsNullOrWhiteSpace(lookupAction)); j++)
                    {
                        actionString = currWords[i] + " " + toWordsLookup[j];
                        if (input.Contains(actionString))
                        {
                            lookupAction = currWords[0];
                            input        = input.Replace(actionString, "");
                        }
                    }
                }
            }
            lookupValue = input;
            var dbRequest = new DbRequest(lookupValue, shouldShowAll);

            return(dbRequest.IsValid ? dbRequest : null);
        }