internal CollarDetailsForm(Collar collar)
 {
     InitializeComponent();
     RestoreWindow();
     Collar = collar;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     SetUpHeader();
 }
 public AddCollarDeploymentForm(Collar collar = null, Animal animal = null)
 {
     InitializeComponent();
     Collar = collar;
     Animal = animal;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     LoadDefaultFormContents();
 }
 public AddArgosDeploymentForm(Collar collar = null, ArgosPlatform platform = null)
 {
     InitializeComponent();
     Collar = collar;
     Platform = platform;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     SetUpControls();  //Called before events are triggered
 }
        private bool FixOtherParameters(Collar collar, CollarParameterFile file, DateTime start, ref DateTime? end)
        {
            //I'm willing to move a existing null end date (there can only be one) back to my start date.
            //Any existing non-null enddate must have been explicitly set by the user, so they should be dealt with explicitly
            //If this situation exists, and I correct it, I am guaranteed to be fine (the new one will exist in the space created), so I can exit
            var parameter = collar.CollarParameters.SingleOrDefault(p => p.CollarParameterFile != file && p.StartDate < start && p.EndDate == null);
            if (parameter != null)
            {
                parameter.EndDate = start;
                if (!SubmitChanges())
                    return false;
            }

            //If my enddate is null (when fixing existng, or adding new), I should set my end to the earliest start time of the others that are larger than my start but smaller than my end (infinity, so all).
            //This could only happen on a fix if there was an existing integrity violation.
            //I don't try to fix a non-null end date, because that was explicitly set by the user, and should explicitly changed.
            if (end == null)
                end = collar.CollarParameters.Where(p => p.CollarParameterFile != file && start < p.StartDate)
                            .Select(p => p.StartDate).Min(); //If there is no min gets an empty enumerable, it will return null, which in this case is no change.

            //There is no situation where I would need to change an existing null start date.

            //now check if the new parameter is in conflict with any existing parameters
            DateTime? endDate = end;
            var competition = collar.CollarParameters.Where(p => p.CollarParameterFile != file).ToList();
            bool conflict = competition.Any(p => DatesOverlap(p.StartDate, p.EndDate, start, endDate));
            if (conflict)
            {
                MessageBox.Show(
                    "The other parameter assignment(s) for this collar will require manual adjustment before this file can be used on this collar",
                    "Oh No!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        private bool FixOtherDeployments(Collar collar, ArgosPlatform platform, DateTime start, ref DateTime? endDate)
        {
            //I'm willing to move a existing null end dates (there can only be one for the platform and one for the collar) back to my start date.
            //Any existing non-null enddate must have been explicitly set by the user, so they should be dealt with explicitly
            //If this situation exists, and I correct it, I am guaranteed to be fine (the new one will exist in the space created)
            var deployment1 =
                collar.ArgosDeployments.SingleOrDefault(d =>
                                                        d.ArgosPlatform != platform &&
                                                        d.StartDate < start && d.EndDate == null);
            var deployment2 =
                platform.ArgosDeployments.SingleOrDefault(d =>
                                                          d.Collar != collar &&
                                                          d.StartDate < start && d.EndDate == null);
            if (deployment1 != null)
                deployment1.EndDate = start;
            if (deployment2 != null)
                deployment2.EndDate = start;
            if (deployment1 != null || deployment2 != null)
                if (!SubmitChanges())
                    return false;

            //If my enddate is null, I should set my end to the earliest start time of the others that are larger than my start but smaller than my end (infinity, so all).
            //I don't try to fix a non-null end date, because that was explicitly set by the user, and be should explicitly changed.
            if (endDate == null)
                endDate =
                    Database.ArgosDeployments.Where(d =>
                                                    ((d.Collar == collar && d.ArgosPlatform != platform) ||
                                                     (d.Collar != collar && d.ArgosPlatform == platform)) &&
                                                    start < d.StartDate)
                            .Select(d => d.StartDate).Min(); //If min gets an empty enumerable, it will return null, which in this case is no change.

            //Since my startdate is non-null, there is no situation where I would need to change an existing null start date.

            //now check if the new deployment is in conflict with any existing deployments
            DateTime? end = endDate;
            var competition = Database.ArgosDeployments.Where(d => (d.Collar == collar && d.ArgosPlatform != platform) ||
                                                                (d.Collar != collar && d.ArgosPlatform == platform)).ToList();
            bool conflict = competition.Any(d => DatesOverlap(d.StartDate, d.EndDate, start, end));
            if (conflict)
            {
                MessageBox.Show(
                    "The other deployment(s) for this collar or platform will require manual adjustment before this platform can be used on this collar",
                    "Oh No!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
 private void CreateParameter(Collar collar, CollarParameterFile file, DateTime startDateTime)
 {
     DateTime? endDateTime = null;
     if (collar.CollarParameters.Count > 0)
         if (!FixOtherParameters(collar, file, startDateTime, ref endDateTime))
             return;
     var param = new CollarParameter
     {
         Collar = collar,
         CollarParameterFile = file,
         StartDate = startDateTime,
         EndDate = endDateTime
     };
     Database.CollarParameters.InsertOnSubmit(param);
     //Creating a parameter is optional, so we submit now, so a failure will not stop other transactions.
     if (!SubmitChanges())
         Database.CollarParameters.DeleteOnSubmit(param);
 }
 private void CreateDeployment(Collar collar, ArgosPlatform platform, DateTime startDateTime)
 {
     DateTime? endDateTime = null;
     if (platform.ArgosDeployments.Count > 0 || collar.ArgosDeployments.Count > 0)
         if (!FixOtherDeployments(collar, platform, startDateTime, ref endDateTime))
             return;
     var deploy = new ArgosDeployment
     {
         ArgosPlatform = platform,
         Collar = collar,
         StartDate = startDateTime,
         EndDate = endDateTime
     };
     Database.ArgosDeployments.InsertOnSubmit(deploy);
     //Creating a deployment is optional, so we submit now, so a failure will not stop other transactions.
     if (!SubmitChanges())
         Database.ArgosDeployments.DeleteOnSubmit(deploy);
 }
 private bool ParameterExistsWithDate(Collar collar, DateTime paramaterStart)
 {
     //possible, though not probable, or even logical, for collar to have more than one parameter set (different date ranges) with File
     //If any of the parameters match, assume we are goodone of them matches
     return collar.CollarParameters.Any(p => p.CollarParameterFile == File && p.StartDate == paramaterStart);
 }
        private Collar[] GetCollars()
        {
            var collarIds = TpfDataGridView.Rows.Cast<DataGridViewRow>().Select(r => (string) r.Cells[1].Value).ToArray();
            if (IgnoreSuffixCheckBox.Checked)
                collarIds = collarIds.Select(c => c.Length > 6 ? c.Substring(0, 6) : c).ToArray();

            var unsortedCollars =
                Database.Collars.Where(
                    c =>
                    c.CollarManufacturer == "Telonics" && collarIds.Contains(c.CollarId)).ToList();

            var sortedCollars = new Collar[TpfDataGridView.Rows.Count];
            for (int i = 0; i < TpfDataGridView.Rows.Count; i++)
            {
                sortedCollars[i] = unsortedCollars.SingleOrDefault(c => c.CollarId == collarIds[i]);
            }
            return sortedCollars;
        }
 private bool FileNotOnCollar(Collar collar)
 {
     //true if this file is not assigned to this collar
     return collar.CollarParameters.All(p => p.CollarParameterFile != File);
 }
 private void ArgosAdded(string argosId, Collar collar, DateTime start)
 {
     var platform = Database.ArgosPlatforms.FirstOrDefault(a => a.PlatformId == argosId);
     if (platform == null)
         return;
     var deploy = new ArgosDeployment
         {
             ArgosPlatform = platform,
             Collar = collar,
             StartDate = start
         };
     Database.ArgosDeployments.InsertOnSubmit(deploy);
     if (SubmitChanges())
         TpfDataChanged();
 }
 private void AddArgosDeployment(string argosId, Collar collar, DateTime start)
 {
     var platform = Database.ArgosPlatforms.FirstOrDefault(a => a.PlatformId == argosId);
     if (platform == null)
         return;
     DateTime? endDate = null;
     if (platform.ArgosDeployments.Count > 0 || collar.ArgosDeployments.Count > 0)
         if (!FixOtherArgosDeployments(collar, platform, start, ref endDate))
             return;
     var deploy = new ArgosDeployment
     {
         ArgosPlatform = platform,
         Collar = collar,
         StartDate = start,
         EndDate = endDate
     };
     Database.ArgosDeployments.InsertOnSubmit(deploy);
     if (SubmitChanges())
         TpfDataChanged();
 }
 private static bool MissingArgosDeployment(Collar collar, string argosId)
 {
     return collar.ArgosDeployments.All(a => a.PlatformId != argosId);
 }
 private static bool FrequencyMismatch(Collar collar, double frequency)
 {
     return collar.Frequency != frequency;
 }
 private string BuildCollarText(Collar collar)
 {
     string name = collar.ToString();
     var animals = from deployment in Database.CollarDeployments
                   where deployment.Collar == collar && deployment.RetrievalDate == null
                   select deployment.Animal;
     var animal = animals.FirstOrDefault();
     if (animal != null)
         name += " on " + animal;
     if (collar.DisposalDate != null)
         name = String.Format("{0} (disp:{1:M/d/yy})", name, collar.DisposalDate.Value.ToLocalTime());
     return name;
 }
 private static bool CanDeleteCollar(Collar collar)
 {
     return !collar.CollarDeployments.Any() &&
            !collar.CollarFixes.Any() &&
            !collar.CollarParameters.Any(p => p.CollarFiles.Any()) &&
            !collar.ArgosDeployments.Any();
 }