/// <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);
            }
        }
 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);
        }