public ReproductiveStateButton(ReproductiveState state)
 {
     this.state = state;
     this.Text  = "Change to " + state.State;
 }
        public void LoadData()
        {
            ISession     session = NHibernateHelper.GetCurrentSession();
            ITransaction tx      = session.BeginTransaction();

            females = Individual.LoadAllFemales(session);

            adult   = session.Get <AgeClass>("A");
            unknown = session.Get <ReproductiveState>("U");

            BindingList <ReproductiveState> states = new BindingList <ReproductiveState>(session
                                                                                         .CreateQuery("from ReproductiveState").List <ReproductiveState>());

            this.comboBoxState.DataSource    = states;
            this.comboBoxStateNew.DataSource = new BindingList <ReproductiveState>(states);

            // Get troop visit before this date
            TroopVisit mostRecentTroopVisit = session
                                              .CreateQuery("select tv from TroopVisit as tv " +
                                                           "left join fetch tv.Troop as t " +
                                                           //"left join fetch tv.Observers " +
                                                           "where t.TroopID = :troopID and " +
                                                           "tv.Date < :date " +
                                                           "order by tv.Date desc")
                                              .SetParameter <string>("troopID", DailyData.Current.TroopVisit.Troop.TroopID)
                                              .SetParameter <DateTime>("date", DailyData.Current.TroopVisit.Date.Date)
                                              .SetMaxResults(1)
                                              .UniqueResult <TroopVisit>();

            // If we have a previous troop visit date, then lets get all the
            // previous reproductive states
            if (mostRecentTroopVisit != null)
            {
                initialReproductiveStates = (List <IndividualReproductiveState>)session
                                            .CreateQuery("select r from IndividualReproductiveState as r " +
                                                         "left join fetch r.TroopVisit as tv " +
                                                         "left join fetch tv.Observers " +
                                                         "where tv = :troopVisit ")
                                            .SetParameter <TroopVisit>("troopVisit", mostRecentTroopVisit)
                                            .SetResultTransformer(new DistinctRootEntityResultTransformer())
                                            .List <IndividualReproductiveState>();
            }

            List <IndividualReproductiveState> tempReproductivestateList = new List <IndividualReproductiveState>(initialReproductiveStates);

            // Lets also get all the current ones for today, if they are already entered
            if (DailyData.Current.RetrievedData)
            {
                todaysReproductiveStates = (List <IndividualReproductiveState>)session
                                           .CreateQuery("select r from IndividualReproductiveState as r " +
                                                        "left join fetch r.TroopVisit as tv " +
                                                        "left join fetch tv.Observers " +
                                                        "where tv = :troopVisit ")
                                           .SetParameter <TroopVisit>("troopVisit", DailyData.Current.TroopVisit)
                                           .SetResultTransformer(new DistinctRootEntityResultTransformer())
                                           .List <IndividualReproductiveState>();

                // List todays entries if there are any
                foreach (IndividualReproductiveState s in todaysReproductiveStates)
                {
                    bool replacement = false;
                    for (int i = 0; i < todaysReproductiveStates.Count; i++)
                    {
                        if (tempReproductivestateList[i].Individual.ID == s.Individual.ID)
                        {
                            tempReproductivestateList[i] = s;
                            replacement = true;
                            break;
                        }
                    }
                    if (!replacement)
                    {
                        tempReproductivestateList.Add(s);
                    }
                }
            }

            tx.Commit();

            females = females.FindAll(new Predicate <Individual>(x =>
                                                                 x.CurrentTroop(DailyData.Current.TroopVisit.Date) != null &&
                                                                 x.CurrentTroop(DailyData.Current.TroopVisit.Date).TroopID == DailyData.Current.TroopVisit.Troop.TroopID));

            // Add any new individuals from previous page
            females.AddRange(DailyData.Current.NewIndividuals.FindAll(
                                 new Predicate <Individual>(x =>
                                                            x.Sex == Individual.SexEnum.F)));

            // Remove females who already have entries
            foreach (IndividualReproductiveState irs in tempReproductivestateList)
            {
                int index = females.FindIndex(new Predicate <Individual>(x =>
                                                                         x.ID == irs.Individual.ID));
                if (index >= 0)
                {
                    females.RemoveAt(index);
                }
            }


            if (females.Count == 0)
            {
                this.comboBoxFemales.Enabled  = false;
                this.buttonAdd.Enabled        = false;
                this.comboBoxStateNew.Enabled = false;
            }
            else
            {
                this.comboBoxFemales.DataSource = females;
            }

            // Set missing individuals to unknown automatically
            // Check to see if the individual was not seen today, replace with unknown if so
            for (int i = 0; i < tempReproductivestateList.Count; i++)
            {
                IndividualReproductiveState current = tempReproductivestateList[i];

                // We do not need to replace exising unknow entries for today
                if (current.State != unknown &&
                    current.TroopVisit.Date.Date != DailyData.Current.TroopVisit.Date.Date &&
                    DailyData.Current.MissingToday.FindIndex(
                        x => x.ID == current.Individual.ID) > -1)
                {
                    IndividualReproductiveState irs = new IndividualReproductiveState();
                    irs.TroopVisit = DailyData.Current.TroopVisit;
                    irs.Individual = current.Individual;
                    irs.State      = unknown;
                    irs.Comments   = "AUTOMATICALLY GENERATED ENTRY :- Not seen on this troop visit.";
                    tempReproductivestateList[i] = irs;
                }
            }

            // Finally lets remove any individuals who have been listed as migrated
            // fropm the list
            foreach (Individual i in DailyData.Current.MigratedToday)
            {
                int index = tempReproductivestateList.FindIndex(x => x.Individual.ID == i.ID);
                if (index >= 0)
                {
                    tempReproductivestateList.RemoveAt(index);
                }
            }

            this.boundReproductiveStates  = new SortableBindingList <IndividualReproductiveState>(tempReproductivestateList);
            this.dataGridView1.DataSource = boundReproductiveStates;

            FinishedLoading(this, null);
        }
Example #3
0
        private void RunLoop()
        {
            // TO DO make the following not hard coded?
            // Variables for csv reading;
            bool header = true;
            int  lineNo = 1;

            int columnIndividual = 1;
            int columnDate       = 3;
            int columnEvent      = 6;
            int columnComment    = 7;

            List <IndividualReproductiveState> statesToInsert          = new List <IndividualReproductiveState>();
            List <ReproductiveState>           validReproductiveStates = new List <ReproductiveState>(Session
                                                                                                      .CreateQuery("select s from ReproductiveState as s")
                                                                                                      .List <ReproductiveState>());

            while (this.state != State.Stopped &&
                   this.fileStream != null &&
                   !this.fileStream.EndOfStream)
            {
                if (header)
                {
                    this.fileStream.ReadLine();
                    header = false;
                }

                // Do processing

                // Get the csv row
                string[] row = this.fileStream.ReadLine().Split(new char[] { ',' });
                lineNo++;

                // Find the event from the event column
                string demographyEvent = row[columnEvent];

                // Check to see if the event is in the list of reproductive states;
                // is so select it
                bool validEvent         = false;
                ReproductiveState state = null;
                foreach (ReproductiveState rs in validReproductiveStates)
                {
                    if (rs.State == demographyEvent)
                    {
                        validEvent = true;
                        state      = rs;
                        break;
                    }
                }

                if (validEvent)
                {
                    IndividualReproductiveState reproductiveState = new IndividualReproductiveState();

                    // Set the state
                    reproductiveState.State = state;

                    // Find the individual from the id column
                    reproductiveState.Individual = Session.Get <Individual>(row[columnIndividual]);

                    if (reproductiveState.Individual != null)
                    {
                        // Find the troop visit from the date column and the individual
                        DateTime date  = DateTime.Parse(row[columnDate]);
                        Troop    troop = reproductiveState.Individual.CurrentTroop(date);
                        if (troop != null)
                        {
                            string query = "Select tv from TroopVisit as tv where Troop = :troop and Date = :date";
                            reproductiveState.TroopVisit = Session
                                                           .CreateQuery(query)
                                                           .SetParameter <DateTime>("date", date)
                                                           .SetParameter <Troop>("troop", troop)
                                                           .UniqueResult <TroopVisit>();

                            // Find the comment from the comment column
                            reproductiveState.Comments = row[columnComment];

                            // INTEGRITY CHECK
                            // Quick check to make sure it is valid
                            if (reproductiveState.TroopVisit != null)
                            {
                                // Print some text and add the state to those to be inserted.
                                AddText("Found entry: " + reproductiveState.Individual.ToString() + " " +
                                        reproductiveState.ToString() + " " +
                                        reproductiveState.TroopVisit.ToString());
                                statesToInsert.Add(reproductiveState);
                            }
                            else
                            {
                                this.AddText("Could not find TroopVisit for " + troop
                                             + " on date " + date.ToShortDateString()
                                             + " at line " + lineNo);
                            }
                        }
                        else
                        {
                            this.AddText("Could not find Troop for '" + reproductiveState.Individual.Name
                                         + "(" + reproductiveState.ID + ") on date " + date.ToShortDateString()
                                         + " at line " + lineNo);
                        }
                    }
                    else
                    {
                        this.AddText("Could not find individual with ID '" + row[columnIndividual] + "' at line " + lineNo);
                    }
                }

                // Don't hog cpu
                Thread.Sleep(100);

                // Wait on paused
                while (this.state == State.Paused)
                {
                    Thread.Sleep(50);
                }
            }

            // Ask to begin insertion
            if (DialogResult.OK == MessageBox.Show("Found " + statesToInsert.Count
                                                   + " valid entries from selected file. Click Ok to begin insert."
                                                   , "Finished reading file", MessageBoxButtons.OKCancel, MessageBoxIcon.Information
                                                   , MessageBoxDefaultButton.Button2))
            {
                InsertEntries(statesToInsert);
            }
            StopCallback d = new StopCallback(Stop);

            Invoke(d);
        }