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 CollarParameterFile UploadParameterFile(ProjectInvestigator owner, LookupCollarParameterFileFormat format, string filename)
        {
            LoadAndHashFile(filename);
            if (_fileContents == null)
            {
                return(null);
            }
            if (AbortBecauseDuplicate())
            {
                return(null);
            }

            var file = new CollarParameterFile
            {
                FileName = System.IO.Path.GetFileName(filename),
                LookupCollarParameterFileFormat = format,
                ProjectInvestigator             = owner,
                LookupFileStatus = (LookupFileStatus)StatusComboBox.SelectedItem,
                Contents         = _fileContents
            };

            Database.CollarParameterFiles.InsertOnSubmit(file);
            if (SubmitChanges())
            {
                return(file);
            }
            return(null);
        }
Beispiel #3
0
 internal CollarParameterFileDetailsForm(CollarParameterFile file)
 {
     InitializeComponent();
     RestoreWindow();
     File        = file;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     SetUpGeneral();
 }
 internal CollarParameterFileDetailsForm(CollarParameterFile file)
 {
     InitializeComponent();
     RestoreWindow();
     File = file;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     SetUpGeneral();
 }
Beispiel #5
0
 public AddCollarParametersForm(Collar collar = null, CollarParameterFile file = null)
 {
     InitializeComponent();
     Collar      = collar;
     File        = file;
     CurrentUser = Environment.UserDomainName + @"\" + Environment.UserName;
     LoadDataContext();
     LoadDefaultFormContents();  //Called before events are triggered
 }
        private bool UploadPpfFile(string file)
        {
            var owner  = (ProjectInvestigator)OwnerComboBox.SelectedItem;
            var format = (LookupCollarParameterFileFormat)FormatComboBox.SelectedItem;
            CollarParameterFile paramFile = UploadParameterFile(owner, format, file);

            if (paramFile == null)
            {
                return(false);
            }
            return(true);
        }
        private void CreateParameters(ProjectInvestigator owner, CollarParameterFile paramFile)
        {
            var tpfFile = new TpfFile(_fileContents);

            foreach (TpfCollar tpfCollar in tpfFile.GetCollars())
            {
                if (IgnoreSuffixCheckBox.Checked && tpfCollar.Ctn.Length > 6)
                {
                    tpfCollar.Ctn = tpfCollar.Ctn.Substring(0, 6);
                }
                //Does this collar exist?
                var collar = Database.Collars.FirstOrDefault(c => c.CollarManufacturer == "Telonics" &&
                                                             c.CollarId == tpfCollar.Ctn);
                if (collar == null && !CreateCollarsCheckBox.Checked)
                {
                    continue;
                }
                if (collar == null)
                {
                    collar = CreateTpfCollar(owner, tpfCollar.Ctn, tpfCollar.Frequency);
                    if (collar == null)
                    {
                        continue;
                    }
                }
                //Does this Argos platform exist?
                var fileHasArgos = !String.IsNullOrEmpty(tpfCollar.PlatformId) && tpfCollar.Platform == "Argos" &&
                                   !tpfCollar.PlatformId.Trim().Normalize().Equals("?", StringComparison.OrdinalIgnoreCase);
                if (fileHasArgos)
                {
                    var platform = Database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == tpfCollar.PlatformId && tpfCollar.Platform == "Argos");
                    if (platform == null && CreateCollarsCheckBox.Checked)
                    {
                        platform = CreatePlatform(tpfCollar.PlatformId);
                    }
                    if (platform != null)
                    {
                        //If not paired, then try to parit the collar and platform.  If it fails, or the the dates are wrong, they can correct that later
                        if (!Database.ArgosDeployments.Any(d => d.Collar == collar && d.ArgosPlatform == platform))
                        {
                            CreateDeployment(collar, platform, tpfCollar.TimeStamp);
                        }
                    }
                }
                CreateParameter(collar, paramFile, tpfCollar.TimeStamp);
            }
        }
        private bool UploadTpfFile(string filePath)
        {
            var owner  = (ProjectInvestigator)OwnerComboBox.SelectedItem;
            var format = (LookupCollarParameterFileFormat)FormatComboBox.SelectedItem;
            CollarParameterFile paramFile = UploadParameterFile(owner, format, filePath);

            if (paramFile == null)
            {
                return(false);
            }
            //paramfile has been successfully committed to the database.
            // CreateParameters is optional, so it should be a separate tranaction (and are not concerned with success or failure)
            if (CreateParametersCheckBox.Checked)
            {
                CreateParameters(owner, paramFile);
            }
            return(true);
        }
        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 CollarParameterFile UploadParameterFile(ProjectInvestigator owner, LookupCollarParameterFileFormat format, string filename)
        {
            LoadAndHashFile(filename);
            if (_fileContents == null)
                return null;
            if (AbortBecauseDuplicate())
                return null;

            var file = new CollarParameterFile
            {
                FileName = System.IO.Path.GetFileName(filename),
                LookupCollarParameterFileFormat = format,
                ProjectInvestigator = owner,
                LookupFileStatus = (LookupFileStatus)StatusComboBox.SelectedItem,
                Contents = _fileContents
            };
            Database.CollarParameterFiles.InsertOnSubmit(file);
            if (SubmitChanges())
                return file;
            return null;
        }
        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 void CreateParameters(ProjectInvestigator owner, CollarParameterFile paramFile)
 {
     var tpfFile = new TpfFile(_fileContents);
     foreach (TpfCollar tpfCollar in tpfFile.GetCollars())
     {
         if (IgnoreSuffixCheckBox.Checked && tpfCollar.Ctn.Length > 6)
             tpfCollar.Ctn = tpfCollar.Ctn.Substring(0, 6);
         //Does this collar exist?
         var collar = Database.Collars.FirstOrDefault(c => c.CollarManufacturer == "Telonics" &&
                                                           c.CollarId == tpfCollar.Ctn);
         if (collar == null && !CreateCollarsCheckBox.Checked)
             continue;
         if (collar == null)
         {
             collar = CreateTpfCollar(owner, tpfCollar.Ctn, tpfCollar.Frequency);
             if (collar == null)
                 continue;
         }
         //Does this Argos platform exist?
         var fileHasArgos = !String.IsNullOrEmpty(tpfCollar.PlatformId) && tpfCollar.Platform == "Argos" &&
                            !tpfCollar.PlatformId.Trim().Normalize().Equals("?", StringComparison.OrdinalIgnoreCase);
         if (fileHasArgos)
         {
             var platform = Database.ArgosPlatforms.FirstOrDefault(p => p.PlatformId == tpfCollar.PlatformId && tpfCollar.Platform == "Argos");
             if (platform == null && CreateCollarsCheckBox.Checked)
                 platform = CreatePlatform(tpfCollar.PlatformId);
             if (platform != null)
                 //If not paired, then try to parit the collar and platform.  If it fails, or the the dates are wrong, they can correct that later
                 if (!Database.ArgosDeployments.Any(d => d.Collar == collar && d.ArgosPlatform == platform))
                     CreateDeployment(collar, platform, tpfCollar.TimeStamp);
         }
         CreateParameter(collar, paramFile, tpfCollar.TimeStamp);
     }
 }
 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);
 }