/// <summary>
        /// Creates a new object by copying data from an species observation object.
        /// </summary>
        /// <param name="obs">The species observation object.</param>
        /// <param name="speciesObservationFieldDescriptions">The species observation field descriptions.</param>
        /// <returns></returns>
        public static SpeciesObservationViewModel CreateFromSpeciesObservation(
            ISpeciesObservation obs,
            ISpeciesObservationFieldDescriptionsViewModel speciesObservationFieldDescriptions)
        {
            var model = new SpeciesObservationViewModel();

            model.ObservationId = obs.Id.ToString(CultureInfo.InvariantCulture);
            model.CopyDataFromObservationToModel(obs, model, speciesObservationFieldDescriptions);
            //model.Projects.AddRange(GetSampleProjects()); // used for testing purpose
            return(model);
        }
        /// <summary>
        /// Copies the project parameters to model.
        /// </summary>
        /// <param name="observation">The observation.</param>
        /// <param name="model">The model.</param>
        /// <param name="speciesObservationFieldDescriptions"></param>
        private void CopyProjectParametersToModel(
            ISpeciesObservation observation,
            SpeciesObservationViewModel model,
            ISpeciesObservationFieldDescriptionsViewModel speciesObservationFieldDescriptions)
        {
            model.Projects = new List <ProjectViewModel>();
            if (observation.Project == null || observation.Project.ProjectParameters == null)
            {
                return;
            }

            var projectParametersDictionary = new Dictionary <int, List <ISpeciesObservationProjectParameter> >();

            foreach (var projectParameter in observation.Project.ProjectParameters)
            {
                if (!projectParametersDictionary.ContainsKey(projectParameter.ProjectId))
                {
                    projectParametersDictionary.Add(projectParameter.ProjectId, new List <ISpeciesObservationProjectParameter>());
                }

                projectParametersDictionary[projectParameter.ProjectId].Add(projectParameter);
            }

            // loop all projects
            foreach (var pair in projectParametersDictionary)
            {
                ProjectViewModel projectViewModel;
                if (!speciesObservationFieldDescriptions.ProjectsDictionary.TryGetValue(pair.Key, out projectViewModel))
                {
                    projectViewModel = CreateProjectViewModel(pair);
                }
                else
                {
                    projectViewModel = projectViewModel.Clone();
                    // map values
                    foreach (var projectParameter in pair.Value)
                    {
                        projectViewModel.ProjectParameters[projectParameter.PropertyIdentifier].Value = projectParameter.Value;
                    }
                }

                model.Projects.Add(projectViewModel);
            }
        }
        /// <summary>
        /// Gets the species observation view model.
        /// </summary>
        /// <param name="observationId">The observation id.</param>
        /// <returns>A species observation view model.</returns>
        public SpeciesObservationViewModel GetSpeciesObservationViewModel(int observationId)
        {
            var         displayCoordinateSystem = mySettings.Presentation.Map.DisplayCoordinateSystem;
            List <long> speciesObservationIds   = new List <long> {
                observationId
            };
            SpeciesObservationList speciesObservationList = CoreData.SpeciesObservationManager.GetSpeciesObservations(userContext, speciesObservationIds, displayCoordinateSystem);
            SpeciesObservationFieldDescriptionViewManager fieldDescriptionViewManager = new SpeciesObservationFieldDescriptionViewManager(userContext, mySettings);
            SpeciesObservationFieldDescriptionsViewModel  fieldDescriptionsViewModel  = fieldDescriptionViewManager.CreateSpeciesObservationFieldDescriptionsViewModel();

            if (speciesObservationList.Count > 0)
            {
                ISpeciesObservation         speciesObservation          = speciesObservationList[0];
                SpeciesObservationViewModel speciesObservationViewModel =
                    SpeciesObservationViewModel.CreateFromSpeciesObservation(speciesObservation, fieldDescriptionsViewModel);
                return(speciesObservationViewModel);
            }

            return(null);
        }
        /// <summary>
        /// Gets a string with the scientific name, author and common name.
        /// </summary>
        /// <param name="obs">The obsevation.</param>
        /// <returns>A string with the scientific name, author and common name.</returns>
        public static string GetLabel(this ISpeciesObservation obs)
        {
            if (obs == null || obs.Taxon.ScientificName.IsEmpty())
            {
                return("");
            }

            var str = new StringBuilder();

            str.Append(obs.Taxon.ScientificName);
            //if (string.IsNullOrEmpty(obs.Taxon.Author))
            //{
            //    str.Append(" " + obs.Author);
            //}

            //if (obs.Taxon.CommonName.IsNotEmpty())
            //{
            //    str.Append(", " + obs.CommonName);
            //}
            return(str.ToString());
        }
        /// <summary>
        /// Generate species observation properties to a text file.
        /// </summary>
        /// <returns></returns>
        public ActionResult GenerateObservationProperties()
        {
            string filePath = string.Empty;

            try
            {
                IUserContext userContext = CoreData.UserManager.GetApplicationContext();
                // Serach griffelblomfluga to extract properties from
                var obsSearchCriteria = new SpeciesObservationSearchCriteria {
                    TaxonIds = new List <int> {
                        100573
                    }, IncludePositiveObservations = true
                };
                var coordinateSystem = new CoordinateSystem {
                    Id = CoordinateSystemId.WGS84
                };

                userContext.CurrentRole = userContext.CurrentRoles[0];
                var obsList = new SpeciesObservationList();
                SpeciesObservationFieldList fieldList = new SpeciesObservationFieldList();
                if (obsSearchCriteria.TaxonIds.Count > 0)
                {
                    obsList = CoreData.SpeciesObservationManager.GetSpeciesObservations(userContext, obsSearchCriteria, coordinateSystem);
                }

                ISpeciesObservation observation = obsList[0];

                Dictionary <string, string> tableData = new Dictionary <string, string>();
                tableData = ExtractProperties(observation);
                filePath  = GetFilePath("SpeciesObservationProperties" + System.DateTime.Now.ToShortDateString());
                SaveFileToDisk(filePath, tableData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            ViewData["FilePath"] = filePath;
            return(View());
        }
 /// <summary>
 /// Get property value out of class and add properties to a list.
 /// </summary>
 /// <param name="speciesObservation"></param>
 /// <param name="propertyInfos"></param>
 /// <param name="value"></param>
 /// <param name="propertyList"> </param>
 private static void AddClassPropertiesToList(ISpeciesObservation speciesObservation, PropertyInfo[] propertyInfos, object value, Dictionary <string, string> propertyList)
 {
     for (int j = 0; j < propertyInfos.Length; j++)
     {
         PropertyInfo speciesSubPropertyInfo = propertyInfos[j];
         //Console.WriteLine(propertyInfo.Name + "-" + subPropInfo.Name);
         ISpeciesObservation speciesClass = speciesObservation;
         object propValue    = null;
         string propertyName = null;
         propertyName = speciesSubPropertyInfo.Name;
         // We must know what tye of class the sub property orginates from to get correct value.
         if (value is ISpeciesObservationConservation)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Conservation, null);
         }
         else if (value is ISpeciesObservationEvent)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Event, null);
         }
         else if (value is ISpeciesObservationGeologicalContext)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.GeologicalContext, null);
         }
         else if (value is ISpeciesObservationIdentification)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Identification, null);
         }
         else if (value is ISpeciesObservationLocation)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Location, null);
         }
         else if (value is ISpeciesObservationMeasurementOrFact)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.MeasurementOrFact, null);
         }
         else if (value is ISpeciesObservationOccurrence)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Occurrence, null);
         }
         else if (value is ISpeciesObservationProject)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Project, null);
         }
         else if (value is ISpeciesObservationResourceRelationship)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.ResourceRelationship, null);
         }
         else if (value is ISpeciesObservationTaxon)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Taxon, null);
         }
         else
         {
             propValue = "New value: not implemeted in code";
         }
         // Add property to list
         string propertyValueAsString = GetPropertyValueAsString(propValue);
         propertyList.Add(propertyName, propertyValueAsString);
         Console.WriteLine("Name: " + propertyName + " Value: " + propertyValueAsString);
     }
 }
        /// <summary>
        /// Get properties and add them to a list.
        /// </summary>
        /// <param name="obs"></param>
        /// <returns></returns>
        public static Dictionary <string, string> ExtractProperties(ISpeciesObservation obs)
        {
            Dictionary <string, string> propertyList = new Dictionary <string, string>();
            string propertyValueAsString             = null;
            Type   type = obs.GetType();

            PropertyInfo[] propInfos = type.GetProperties();
            for (int i = 0; i < propInfos.Length; i++)
            {
                PropertyInfo pi        = (PropertyInfo)propInfos.GetValue(i);
                string       propName  = pi.Name;
                object       propValue = pi.GetValue(obs, null);
                if (propValue is ISpeciesObservationConservation || propValue is ISpeciesObservationEvent ||
                    propValue is ISpeciesObservationGeologicalContext ||
                    propValue is ISpeciesObservationIdentification || propValue is ISpeciesObservationLocation ||
                    propValue is ISpeciesObservationMeasurementOrFact ||
                    propValue is ISpeciesObservationOccurrence || propValue is ISpeciesObservationProject ||
                    propValue is ISpeciesObservationResourceRelationship ||
                    propValue is ISpeciesObservationTaxon)
                {
                    PropertyInfo[] subSpeciesPropertyInfos = null;
                    if (propValue is ISpeciesObservationConservation)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationConservation).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationEvent)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationEvent).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationGeologicalContext)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationGeologicalContext).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationIdentification)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationIdentification).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationLocation)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationLocation).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationMeasurementOrFact)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationMeasurementOrFact).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationOccurrence)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationOccurrence).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationProject)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationProject).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationResourceRelationship)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationResourceRelationship).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationTaxon)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationTaxon).GetProperties();
                    }
                    else
                    {
                        subSpeciesPropertyInfos = new PropertyInfo[] { };
                    }

                    //Add property
                    AddClassPropertiesToList(obs, subSpeciesPropertyInfos, propValue, propertyList);
                }
                else
                {
                    propertyValueAsString = GetPropertyValueAsString(propValue);
                    propertyList.Add(propName, propertyValueAsString);
                }
            }
            return(propertyList);
        }
 private static void CopyDataFromSubProperty(
     ISpeciesObservation speciesObservation,
     PropertyInfo[] propertyInfos,
     object value,
     SpeciesObservationViewModel model)
 {
     for (int j = 0; j < propertyInfos.Length; j++)
     {
         PropertyInfo        speciesSubPropertyInfo = propertyInfos[j];
         ISpeciesObservation speciesClass           = speciesObservation;
         object propValue    = null;
         string propertyName = null;
         propertyName = speciesSubPropertyInfo.Name;
         // We must know what tye of class the sub property orginates from to get correct value.
         if (value is ISpeciesObservationConservation)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Conservation, null);
         }
         else if (value is ISpeciesObservationEvent)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Event, null);
         }
         else if (value is ISpeciesObservationGeologicalContext)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.GeologicalContext, null);
         }
         else if (value is ISpeciesObservationIdentification)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Identification, null);
         }
         else if (value is ISpeciesObservationLocation)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Location, null);
         }
         else if (value is ISpeciesObservationMeasurementOrFact)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.MeasurementOrFact, null);
         }
         else if (value is ISpeciesObservationOccurrence)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Occurrence, null);
         }
         else if (value is ISpeciesObservationProject)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Project, null);
         }
         else if (value is ISpeciesObservationResourceRelationship)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.ResourceRelationship, null);
         }
         else if (value is ISpeciesObservationTaxon)
         {
             propValue = speciesSubPropertyInfo.GetValue(speciesClass.Taxon, null);
         }
         string       propertyValue = GetPropertyValueAsString(propValue);
         PropertyInfo modelPi;
         if (ObservationDarwinCoreViewModelPropertiesDictionary.TryGetValue(propertyName, out modelPi))
         {
             modelPi.SetValue(model, propertyValue, null);
         }
     }
 }
        /// <summary>
        /// Copies the data from observation to model.
        /// The observation object contains hierarchical data, while the model is flat.
        /// </summary>
        /// <param name="observation">The observation.</param>
        /// <param name="model">The model.</param>
        /// <param name="speciesObservationFieldDescriptions"></param>
        private void CopyDataFromObservationToModel(
            ISpeciesObservation observation,
            SpeciesObservationViewModel model,
            ISpeciesObservationFieldDescriptionsViewModel speciesObservationFieldDescriptions)
        {
            // Create detalis list out of propery and resource string
            Type type = observation.GetType();

            PropertyInfo[] propInfos = type.GetProperties();
            for (int i = 0; i < propInfos.Length; i++)
            {
                PropertyInfo pi           = (PropertyInfo)propInfos.GetValue(i);
                string       propertyName = pi.Name;
                object       propValue    = pi.GetValue(observation, null);
                if (propValue is ISpeciesObservationConservation || propValue is ISpeciesObservationEvent ||
                    propValue is ISpeciesObservationGeologicalContext ||
                    propValue is ISpeciesObservationIdentification || propValue is ISpeciesObservationLocation ||
                    propValue is ISpeciesObservationMeasurementOrFact ||
                    propValue is ISpeciesObservationOccurrence || propValue is ISpeciesObservationProject ||
                    propValue is ISpeciesObservationResourceRelationship ||
                    propValue is ISpeciesObservationTaxon)
                {
                    PropertyInfo[] subSpeciesPropertyInfos = null;
                    if (propValue is ISpeciesObservationConservation)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationConservation).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationEvent)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationEvent).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationGeologicalContext)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationGeologicalContext).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationIdentification)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationIdentification).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationLocation)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationLocation).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationMeasurementOrFact)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationMeasurementOrFact).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationOccurrence)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationOccurrence).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationProject)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationProject).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationResourceRelationship)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationResourceRelationship).GetProperties();
                    }
                    else if (propValue is ISpeciesObservationTaxon)
                    {
                        subSpeciesPropertyInfos = typeof(ISpeciesObservationTaxon).GetProperties();
                    }

                    CopyDataFromSubProperty(observation, subSpeciesPropertyInfos, propValue, model);
                }
                else
                {
                    //Get string value for property
                    string propertyValue = GetPropertyValueAsString(propValue);

                    PropertyInfo modelPi;
                    if (ObservationDarwinCoreViewModelPropertiesDictionary.TryGetValue(propertyName, out modelPi))
                    {
                        modelPi.SetValue(model, propertyValue, null);
                    }
                }
            }

            CopyProjectParametersToModel(observation, model, speciesObservationFieldDescriptions);
        }
 /// <summary>
 /// Converts a ISpeciesObservations to a ObservationViewModel which is used
 /// to present the observation on screen.
 /// </summary>
 /// <param name="observation">The observation.</param>
 /// <returns></returns>
 public static ObservationViewModel ToObservationViewModel(this ISpeciesObservation observation)
 {
     return(ObservationViewModel.Create(observation));
 }
        public static ObservationViewModel Create(ISpeciesObservation obs)
        {
            var model = new ObservationViewModel();

            model.ObservationId = Convert.ToString(obs.Id);
            model.OrganismGroup = obs.Taxon.OrganismGroup;

            model.ShowOrganismGroup  = true;
            model.ScientificName     = obs.Taxon.ScientificName;
            model.CommonName         = obs.Taxon.VernacularName;
            model.TaxonConceptStatus = obs.Taxon.TaxonConceptStatus;
            model.RedlistCategory    = obs.Conservation.RedlistCategory;
            string startTime = string.Empty;

            if (obs.Event.Start != null)
            {
                startTime = obs.Event.Start.Value.ToShortDateString();
            }
            model.StartDate = startTime;
            string endTime = string.Empty;

            if (obs.Event.End != null)
            {
                endTime = obs.Event.End.Value.ToShortDateString();
            }
            model.EndDate       = endTime;
            model.Locality      = obs.Location.Locality;
            model.Parish        = obs.Location.Parish;
            model.Municipality  = obs.Location.Municipality;
            model.County        = obs.Location.County;
            model.StateProvince = obs.Location.StateProvince;
            string coordinateY = string.Empty;

            if (obs.Location.CoordinateY.IsNotNull())
            {
                coordinateY = Convert.ToString(obs.Location.CoordinateY);
            }
            model.CoordinateNorth = coordinateY;
            string coordinateX = string.Empty;

            if (obs.Location.CoordinateX.IsNotNull())
            {
                coordinateX = Convert.ToString(obs.Location.CoordinateX);
            }
            model.CoordinateEast = coordinateX;
            model.Accurancy      = obs.Location.CoordinateUncertaintyInMeters;
            model.RecordedBy     = obs.Occurrence.RecordedBy;
            model.Owner          = obs.Owner;
            model.Quantity       = obs.Occurrence.Quantity;
            model.QuantityUnit   = obs.Occurrence.QuantityUnit;
            model.LifeStage      = obs.Occurrence.LifeStage;
            model.Behaviour      = obs.Occurrence.Behavior;
            model.Substrate      = ""; //obs.Occurrence.Substrate;

            model.OcurranceRemarks = obs.Occurrence.OccurrenceRemarks;
            model.DetConf          = "";
            model.CollectionCode   = obs.CollectionCode;
            string isNeverFound = string.Empty;

            if (obs.Occurrence.IsNeverFoundObservation.IsNotNull())
            {
                isNeverFound = Convert.ToString(obs.Occurrence.IsNeverFoundObservation);
            }
            model.IsNeverFoundObservarion = isNeverFound;

            string isNotRediscovered = string.Empty;

            if (obs.Occurrence.IsNotRediscoveredObservation.IsNotNull())
            {
                isNotRediscovered = Convert.ToString(obs.Occurrence.IsNotRediscoveredObservation);
            }
            model.IsNotRediscoveredObservation = isNotRediscovered;
            model.DatasetID = obs.DatasetID;
            model.Database  = obs.DatasetName;
            string taxonSortOrder = string.Empty;

            if (obs.Taxon.TaxonSortOrder.IsNotNull())
            {
                taxonSortOrder = Convert.ToString(obs.Taxon.TaxonSortOrder);
            }
            model.TaxonSortOrder = taxonSortOrder;
            model.TaxonId        = obs.Taxon.TaxonID;
            model.GroupSorting   = "";
            string protectionLevel = string.Empty;

            if (obs.Conservation.ProtectionLevel.IsNotNull())
            {
                protectionLevel = Convert.ToString(obs.Conservation.ProtectionLevel);
            }
            model.ProtectionLevel = protectionLevel;

            return(model);
        }