Exemplo n.º 1
0
 public static TagPrime GetTagPrime(this PersonDataContext dataContext,
                                    int tagPrimeId)
 {
     return(dataContext.TagPrimes
            .Where(tag => tag.PrimeId == tagPrimeId)
            .FirstOrDefault());
 }
Exemplo n.º 2
0
 private static IEnumerable <PersonFromDbWrapper> search(
     DbRequest dbRequest,
     PersonDataContext dataContext)
 {
     return(getQueryable(dbRequest, dataContext)
            .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.WhatIDo))
            .ToList());
 }
Exemplo n.º 3
0
        // Changes the ID of the tag with ID tagPrimeIdFrom to tagPrimeIdFrom.
        // (It actually does this by creating copies and deleting the old ones).
        // The previously existing tag with tagPrimeIdTo will be deleted.
        // If the from and to are the same, that tag is just deleted.
        public static void SwitchTagId(
            this PersonDataContext dataContext,
            int tagPrimeIdFrom,
            int tagPrimeIdTo)
        {
            // Make a copy of the tag with ID tagPrimeIdFrom but with
            // ID tagPrimeIdTo.
            var tagToCopy = dataContext.GetTagPrime(tagPrimeIdFrom);
            var copyOfTag = new TagPrime();

            copyOfTag.Tag  = tagToCopy.Tag;
            copyOfTag.Type = tagToCopy.Type;
            copyOfTag.AllowNonAdminsToAdd = tagToCopy.AllowNonAdminsToAdd;
            copyOfTag.PrimeId             = tagPrimeIdTo;

            var tagToDelete = dataContext.GetTagPrime(tagPrimeIdTo);

            dataContext.TagPrimes.DeleteOnSubmit(tagToDelete);
            dataContext.TagPrimes.DeleteOnSubmit(tagToCopy);
            dataContext.SubmitChanges();

            if (tagPrimeIdFrom == tagPrimeIdTo)
            {
                return;
            }
            dataContext.TagPrimes.InsertOnSubmit(copyOfTag);
            dataContext.SubmitChanges();
        }
Exemplo n.º 4
0
        // Returns the queryable object to the DB.
        private static IQueryable <Person> getQueryable(
            DbRequest dbRequest,
            PersonDataContext dataContext)
        {
            var query = dataContext.Persons.AsQueryable();

            if (dbRequest.ForceMe &&
                CurrentMisparIshi.GetCurrentMisparIshi().Length > 0)
            {
                query = query.Where(person =>
                                    person.MisparIshi == CurrentMisparIshi.GetCurrentMisparIshi());
                return(query);
            }

            if (dbRequest.Tags > 1)
            {
                query = query.Where(person =>
                                    person.Tags % dbRequest.Tags == 0);
            }

            if (dbRequest.StandardInputTextValues.Count != 0)
            {
                // Note that WhereMatches is defined in WhereMatchesQuery.cs.
                query = query.WhereMatches(
                    dbRequest.StandardInputTextValues, dbRequest.IsOnlyNumbers);
            }
            return(query);
        }
Exemplo n.º 5
0
        public PersonAndTag(string misparIshi, string tag)
        {
            if (!CurrentMisparIshi.IsCurrentUserOrAdmin(misparIshi) ||
                !TagToPrimeDictionary.TAG_TO_PRIME.ContainsKey(tag))
            {
                this.isValid = false;
                return;
            }
            this.tagPrime = TagToPrimeDictionary.TAG_TO_PRIME[tag];

            this.dataContext = new PersonDataContext();

            var personsFromDb = dataContext.Persons
                                .Where(person => person.MisparIshi.Equals(misparIshi))
                                .ToList();

            if (personsFromDb.Count() != 1)
            {
                this.isValid = false;
                return;
            }

            this.personFromDb = personsFromDb.First();

            this.isValid = true;
        }
Exemplo n.º 6
0
        public static void DeleteTag(string tagName)
        {
            lock (AdminTagAdder.LOCK)
            {
                if (!CurrentMisparIshi.IsAdmin())
                {
                    return;
                }
                if (!TagToPrimeDictionary.TAG_NAME_TO_TAG.ContainsKey(tagName))
                {
                    return;
                }
                var tagToRemoveId = TagToPrimeDictionary.TAG_NAME_TO_TAG[tagName].PrimeId;

                var dataContext = new PersonDataContext();
                var biggestTag  = dataContext.TagPrimes
                                  .OrderByDescending(tag => tag.PrimeId)
                                  .FirstOrDefault();
                var biggestTagId = biggestTag.PrimeId;
                if (biggestTagId < 2)
                {
                    return;
                }

                // Ok so we've isolated the ID of the tag we want to remove R
                // and the ID biggest tag B. We now do the following:
                // (1) Change R's text to  " ". This is a bit of a hack but elsewhere
                // the code will ensure that " " is not searchable or rendered. Note
                // that we do this by deleting it and creating a new one.
                // (2) For every person with tag R, divide their tag value by R.ID.
                //     For every person with tag B, multiple their tag value by R.ID.
                // (3) Change R's text to B's text. Delete B.
                // (4) For every person with tag B, divide their tag value by B.ID.
                // (5) Reset the dictionaries.

                // Step (1).
                dataContext.makeTagToRemoveUnsearchable(tagToRemoveId);

                // Step (2)
                dataContext.RemoveTagIdFromAllPeople(tagToRemoveId);
                dataContext.Persons
                .Where(person => person.Tags % biggestTagId == 0)
                .ToList()
                .ForEach(person => { person.Tags *= tagToRemoveId; });
                dataContext.SubmitChanges();

                // Step (3)
                dataContext.SwitchTagId(biggestTagId, tagToRemoveId);

                // Step (4)
                dataContext.RemoveTagIdFromAllPeople(tagToRemoveId);

                // Step (5)
                TagToPrimeDictionary.ResetTagToPrimeDictionaries();

                AdminChangeWriter.WriteAdminChange(
                    String.Format("Deleted tag {0}", tagName));
            }
        }
Exemplo n.º 7
0
        public IEnumerable <object> GetPerson(string input, bool shouldShowAll)
        {
            stopwatch.Start();
            var dataContext = new PersonDataContext();

            dataContext.Log = new DebugWriter();

            // Look at strings in descending size order in the hope that the strings with the fewest
            // results will come up first making each subsequent query pool smaller.
            var substrings = input.Split(' ').OrderByDescending(s => s.Length);
            IEnumerable <Person> matchingPersons = new List <Person>();
            var alreadyChecked = false;

            foreach (var substring in substrings)
            {
                if (alreadyChecked && matchingPersons.Count() == 0)
                {
                    // If here that means we already have no matches, so no reason to do anything else.
                    break;
                }

                var isNumber = isNumberRegex.IsMatch(substring);

                matchingPersons =
                    (alreadyChecked ? matchingPersons : dataContext.Persons).Where(person =>
                                                                                   isNumber ?
                                                                                   person.Mobile.Contains(substring) ||
                                                                                   person.MisparIshi.Contains(substring) ||
                                                                                   person.WorkPhone.Contains(substring)
                                : person.GivenName.Contains(substring) ||
                                                                                   person.JobTitle.Contains(substring) ||
                                                                                   person.LongWorkTitle.Contains(substring) ||
                                                                                   person.AlternateName.Contains(substring) ||
                                                                                   person.Department.Contains(substring) ||
                                                                                   person.Company.Contains(substring) ||
                                                                                   person.Location.Contains(substring)
                                                                                   );

                alreadyChecked = true;
            }

            var numberToTake  = shouldShowAll ? int.MaxValue : numberToShowInFirstTry;
            var listWasCutOff = matchingPersons.Count() > numberToTake;

            var returnObject = new List <object>();

            returnObject.Add(createMetadataObject(listWasCutOff));

            matchingPersons.OrderBy(person => person.GivenName).Take(numberToTake)
            .ToList().ForEach(person => { returnObject.Add(createPerson(person)); });

            stopwatch.Stop();
            Debug.WriteLine(stopwatch.Elapsed);
            stopwatch.Reset();

            return(returnObject);
        }
Exemplo n.º 8
0
        // Retrieves the matching people form the Database and wraps each one in a
        // PersonFromDbWrapper object.
        public static IEnumerable <PersonFromDbWrapper> GetPersonsFromDb(
            DbRequest dbRequest)
        {
            var dataContext = new PersonDataContext();

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

            return(search(dbRequest, dataContext));
        }
Exemplo n.º 9
0
 public static void RemoveTagIdFromAllPeople(
     this PersonDataContext dataContext,
     int tagPrimeId)
 {
     dataContext.Persons
     .Where(person => person.Tags % tagPrimeId == 0)
     .ToList()
     .ForEach(person => { person.Tags /= tagPrimeId; });
 }
Exemplo n.º 10
0
        private static string getGivenNameForMisparIshi(string misparIshi)
        {
            var dataContext = new PersonDataContext();

            return(dataContext.Persons
                   .Where(person => person.MisparIshi.Equals(misparIshi))
                   .Select(person => String.Format("{0} {1}",
                                                   person.GivenName, person.Surname))
                   .FirstOrDefault());
        }
Exemplo n.º 11
0
        public static IEnumerable <object> GetTags()
        {
            var dataContext = new PersonDataContext();
            var isAdmin     = CurrentMisparIshi.IsAdmin();

            return(dataContext.TagPrimes
                   .Where(tag => tag.AllowNonAdminsToAdd || isAdmin)
                   .Select(tag => new {
                tag = tag.Tag
            })
                   .ToList());
        }
Exemplo n.º 12
0
        public static void makeTagToRemoveUnsearchable(
            this PersonDataContext dataContext,
            int tagToRemoveId)
        {
            var tagToRemove       = dataContext.GetTagPrime(tagToRemoveId);
            var copyOfTagToRemove = new TagPrime();

            copyOfTagToRemove.PrimeId = tagToRemove.PrimeId;
            copyOfTagToRemove.Tag     = TagToPrimeDictionary.INVALID_TAG_NAME;

            dataContext.TagPrimes.DeleteOnSubmit(tagToRemove);
            dataContext.SubmitChanges();
            dataContext.TagPrimes.InsertOnSubmit(copyOfTagToRemove);
            dataContext.SubmitChanges();
        }
Exemplo n.º 13
0
        public static IEnumerable <object> AddTag(string tagToAdd, bool isTagForAnyone)
        {
            //if (!CurrentMisparIshi.IsAdmin())
            //{
            //    return createResponseObject(
            //        "You're not an admin, what are you doing here??");
            //}

            if (tagToAdd.Contains(" "))
            {
                return(createResponseObject(
                           "אסור להוסיף תגים עם רווחים"));
            }


            var nextPrime = TagToPrimeDictionary.GetNextPrime();

            if (nextPrime == -1)
            {
                return(createResponseObject(
                           "יש 10,000 תגים ויותר מזה לא נתמך"));
            }


            var dataContext         = new PersonDataContext();
            var alreadyExistingTags =
                dataContext.TagPrimes.Where(tag => tag.Tag.Equals(tagToAdd));

            if (alreadyExistingTags.Count() > 0)
            {
                return(createResponseObject(
                           String.Format("התג {0} כבר קיים", tagToAdd)));
            }

            var newTagPrime = new TagPrime();

            newTagPrime.PrimeId             = nextPrime;
            newTagPrime.Tag                 = tagToAdd;
            newTagPrime.AllowNonAdminsToAdd = isTagForAnyone;

            dataContext.TagPrimes.InsertOnSubmit(newTagPrime);
            dataContext.SubmitChanges();

            TagToPrimeDictionary.ResetTagToPrimeDictionaries();

            return(createResponseObject(
                       String.Format("התג {0} התווסף בהצלחה", tagToAdd)));
        }
Exemplo n.º 14
0
        public static IEnumerable <object> GetTags()
        {
            var dataContext = new PersonDataContext();
            var isAdmin     = CurrentMisparIshi.IsAdmin();
            var tagsList    = dataContext.TagPrimes
                              .Where(tag => tag.AllowNonAdminsToAdd || isAdmin)
                              .Select(tag => new {
                tag  = tag.Tag,
                type = tag.Type
            })
                              .ToList();

            var groupedTags = tagsList.GroupBy(tag => tag.type);

            return(groupedTags.Select(tagGroup => new
            {
                type = tagGroup.Key,
                tags = tagGroup.Select(singleTag => new {
                    tag = singleTag.tag
                }).OrderBy(tagWrapper => tagWrapper.tag)
            }));
        }
Exemplo n.º 15
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());
        }
Exemplo n.º 16
0
        public static void AddWhatIDo(string misparIshi, string value)
        {
            var dataContext = new PersonDataContext();

            var personsFromDb = dataContext.Persons
                                .Where(person => person.MisparIshi.Equals(misparIshi))
                                .ToList();

            if (personsFromDb.Count() != 1)
            {
                return;
            }

            var personFromDb = personsFromDb.First();

            if (!CurrentMisparIshi.IsCurrentUserOrAdmin(
                    personFromDb.MisparIshi))
            {
                return;
            }

            personFromDb.WhatIDo = value;
            dataContext.SubmitChanges();
        }
Exemplo n.º 17
0
 protected override void SetupDataContext(string connection)
 {
     context = CreateContext(connection);
 }
Exemplo n.º 18
0
        public static IEnumerable <object> AddTag(
            string tagToAdd, bool isTagForAnyone, string type)
        {
            lock (LOCK)
            {
                if (!CurrentMisparIshi.IsAdmin())
                {
                    return(createResponseObject(
                               "You're not an admin, what are you doing here??"));
                }

                if (tagToAdd.Contains(" "))
                {
                    return(createResponseObject(
                               "אסור להוסיף תגים עם רווחים"));
                }

                if (tagToAdd.Length == 0)
                {
                    return(createResponseObject("נו באמת..."));
                }

                if (type.Length == 0)
                {
                    return(createResponseObject("נא להוסיף סוג"));
                }


                var nextPrime = TagToPrimeDictionary.GetNextPrime();
                if (nextPrime == -1)
                {
                    return(createResponseObject(
                               "יש 10,000 תגים ויותר מזה לא נתמך"));
                }


                var dataContext         = new PersonDataContext();
                var alreadyExistingTags =
                    dataContext.TagPrimes.Where(tag => tag.Tag.Equals(tagToAdd));
                if (alreadyExistingTags.Count() > 0)
                {
                    return(createResponseObject(
                               String.Format("התג {0} כבר קיים", tagToAdd)));
                }

                var newTagPrime = new TagPrime();
                newTagPrime.PrimeId             = nextPrime;
                newTagPrime.Tag                 = tagToAdd;
                newTagPrime.AllowNonAdminsToAdd = isTagForAnyone;
                newTagPrime.Type                = type;

                dataContext.TagPrimes.InsertOnSubmit(newTagPrime);
                dataContext.SubmitChanges();

                TagToPrimeDictionary.ResetTagToPrimeDictionaries();

                AdminChangeWriter.WriteAdminChange(
                    String.Format("Added tag {0} of type {1}", tagToAdd, type));

                return(createResponseObject(
                           String.Format("התג {0} מסוג {1} התווסף בהצלחה", tagToAdd, type)));
            }
        }
Exemplo n.º 19
0
 public Data2()
 {
     Console.WriteLine("In Data2 Constructor");
     persons = new PersonDataContext();
 }
 public Data()
 {
     persons = new PersonDataContext();
 }
Exemplo n.º 21
0
        public PersonTest()
        {
            _personRepo = Substitute.For <PersonRepositoryImpl>();

            _dbContextInMemory = new PersonDataContext(this.GetDBOptions());
        }
Exemplo n.º 22
0
        public static void AddPersonalField(
            string misparIshi,
            string value,
            string inputType)
        {
            var dataContext = new PersonDataContext();

            var personsFromDb = dataContext.Persons
                                .Where(person => person.MisparIshi.Equals(misparIshi))
                                .ToList();

            if (personsFromDb.Count() != 1)
            {
                return;
            }

            var personFromDb = personsFromDb.First();

            if (!CurrentMisparIshi.IsCurrentUserOrAdmin(
                    personFromDb.MisparIshi))
            {
                return;
            }

            switch (inputType)
            {
            case PersonalFieldAdder.WHAT_I_DO:
                personFromDb.WhatIDo = value;
                break;

            case ADD_MOBILE:
                var normailzedMobileNumber = value.ToValidMobileNumber();
                if (normailzedMobileNumber == null)
                {
                    return;
                }
                personFromDb.Mobile = normailzedMobileNumber;
                personFromDb.MobilePhoneIsManuallyInput = true;
                break;

            case ADD_WORK_NUMBER:
                var normailzedWorkNumber = value.ToValidNonMobileNumber();
                if (normailzedWorkNumber == null)
                {
                    return;
                }
                personFromDb.WorkPhone = normailzedWorkNumber;
                personFromDb.WorkPhoneIsManuallyInput = true;
                break;

            case ADD_OTHER_NUMBER:
                var normailzedOtherNumber = value.ToValidNonMobileNumber();
                if (normailzedOtherNumber == null)
                {
                    return;
                }
                personFromDb.OtherTelephone            = normailzedOtherNumber;
                personFromDb.OtherPhoneIsManuallyInput = true;
                break;

            case SEX:
                personFromDb.Sex = value;
                personFromDb.SexIsManuallyInput = true;
                break;
            }

            dataContext.SubmitChanges();

            if (CurrentMisparIshi.IsAdminButNotCurrentUser(misparIshi))
            {
                AdminChangeWriter.WriteAdminChange(
                    String.Format(
                        "Set field of type {0} for {1} to {2}",
                        inputType,
                        personFromDb.MisparIshi,
                        value));
            }
        }
Exemplo n.º 23
0
 public PersonRepositoryImpl(PersonDataContext context)
 {
     _dbContext = context;
 }