/*
         * private void AddNewEntry(IndividualReproductiveState rs, DataGridViewRow row)
         * {
         *  IndividualReproductiveState currentState = individual.GetIndividualReproductiveStateOnDate(troopVisit.Date);
         *  this.currentStates[row.Index] = rs;
         *  this.DataGridView.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
         * }
         */
        public override void  LoadData()
        {
            List <Individual> individuals = Individual.LoadAll(Session);

            currentStates = new SortableBindingList <IndividualReproductiveState>();
            foreach (Individual i in individuals)
            {
                IndividualReproductiveState irs = i.CurrentReproductiveState(troopVisit.Date);
                if (irs != null &&
                    i.CurrentTroop() != null &&
                    i.CurrentTroop().TroopID == troopVisit.Troop.TroopID)
                {
                    currentStates.Add(irs);
                }
            }

            this.bindingSource            = new BindingSource();
            this.bindingSource.DataSource = currentStates;
            this.DataGridView.DataSource  = bindingSource;

            // Re-order columns a bit
            this.DataGridView.Columns["Id"].DisplayIndex         = 0;
            this.DataGridView.Columns["Individual"].DisplayIndex = 1;
            this.DataGridView.Columns["State"].DisplayIndex      = 2;
        }
 void dataGridView1_SelectionChanged(object sender, EventArgs e)
 {
     if (dataGridView1.SelectedRows.Count > 0)
     {
         currentIndex = dataGridView1.SelectedRows[0].Index;
         currentIrs   = (IndividualReproductiveState)dataGridView1.Rows[currentIndex].DataBoundItem;
         this.labelIndividual.Text = currentIrs.Individual.Name;
     }
 }
        protected override void RefreshRow(DataGridViewRow row)
        {
            Individual individual = currentStates[row.Index].Individual;
            //this.Session.Refresh(individual);

            IndividualReproductiveState currentState = individual.CurrentReproductiveState(troopVisit.Date);

            this.currentStates[row.Index] = currentState;
            this.DataGridView.Rows[row.Index].DefaultCellStyle.BackColor = Color.LightGray;
        }
        void ReproductiveStateButton_click(object sender, System.EventArgs e)
        {
            Individual individual = currentStates[DataGridView.CurrentRow.Index].Individual;
            IndividualReproductiveState newState = new IndividualReproductiveState();

            newState.State      = ((ReproductiveStateButton)sender).State;
            newState.TroopVisit = troopVisit;
            newState.Individual = individual;
            individual.ReproductiveStateHistory.Add(newState);
            RefreshRow(DataGridView.CurrentRow);

            //this.AddNewEntry(currentState, DataGridView.CurrentRow);
        }
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (currentIndex >= 0)
            {
                // Overwrite the selected item with the
                // specified values in the control
                IndividualReproductiveState irs = new IndividualReproductiveState();
                irs.Individual = currentIrs.Individual;
                irs.State      = (ReproductiveState)comboBoxState.SelectedItem;
                irs.TroopVisit = DailyData.Current.TroopVisit;
                irs.Comments   = textBoxComments.Text;

                boundReproductiveStates[currentIndex] = irs;
            }
        }
        protected override void RowAction(DataGridViewRow row)
        {
            Individual individual = (Individual)row.Cells["Individual"].Value;
            //IndividualReproductiveState irc = NHibernateHelper.GetCurrentSession().Get<IndividualReproductiveState>(row.Cells["Id"].Value);

            IndividualReproductiveState irs = null;

            if (troopVisit != null)
            {
                irs            = new IndividualReproductiveState();
                irs.Individual = individual;
                irs.TroopVisit = troopVisit;
            }

            ReproductiveStateEditor rce = new ReproductiveStateEditor(Session, individual, irs);

            if (rce.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                individual.ReproductiveStateHistory.Add(rce.State);
                RefreshRow(row);
            }
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            IndividualReproductiveState irs = new IndividualReproductiveState();

            irs.Individual = (Individual)comboBoxFemales.SelectedItem;
            irs.TroopVisit = DailyData.Current.TroopVisit;
            irs.State      = (ReproductiveState)comboBoxStateNew.SelectedItem;
            irs.Comments   = textBoxCommentsNew.Text;

            foreach (IndividualReproductiveState x in boundReproductiveStates)
            {
                if (x.Individual.ID == irs.Individual.ID)
                {
                    MessageBox.Show("This " + irs.Individual.Name + " already has an entry in the list. " +
                                    "Update the individuals detail with the 'Update' control.");
                    return;
                }
            }

            // Is this female already listed as Adult?
            if (irs.Individual.CurrentAgeClass() == null ||
                irs.Individual.CurrentAgeClass(DailyData.Current.TroopVisit.Date).AgeClass.ID != adult.ID)
            {
                MessageBox.Show("This " + irs.Individual.Name + " is not current listed as adult. " +
                                "A an entry will be created to change her age class to 'A'");

                IndividualAgeClass iac = new IndividualAgeClass();
                iac.AgeClass   = adult;
                iac.TroopVisit = DailyData.Current.TroopVisit;
                iac.Individual = irs.Individual;
                iac.Comments   = "AUTOMATICALLY GENERATED ENTRY :- Age class changed to adult on observation of " +
                                 " start of first reproductive cycle.";

                DailyData.Current.NewAgeClass.Add(iac);
            }

            boundReproductiveStates.Add(irs);
        }
        public bool Finish()
        {
            // We need to add a new entry foreach individual.
            foreach (IndividualReproductiveState s in boundReproductiveStates)
            {
                // If no change has been made, automatically generate an entry
                if (s.TroopVisit.ID != DailyData.Current.TroopVisit.ID)
                {
                    IndividualReproductiveState irs = new IndividualReproductiveState();
                    irs.TroopVisit = DailyData.Current.TroopVisit;
                    irs.Individual = s.Individual;


                    irs.State    = s.State;
                    irs.Comments = "AUTOMATICALLY GENERATED ENTRY :- No Change.";
                    DailyData.Current.NewReproductiveStates.Add(irs);
                }
                else if (s.ID == 0) // Add the updated entry to the list if it is new
                {
                    DailyData.Current.NewReproductiveStates.Add(s);
                }
            }
            return(true);
        }
        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);
        }
Exemplo n.º 10
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);
        }