예제 #1
0
        private object deserialize(XElement element, IMoBiProject project, int version, Type type = null, SerializationContext parentSerializationContext = null)
        {
            object deserializedObject;
            bool   conversionHappened = false;

            using (var serializationContext = _serializationContextFactory.Create(parentSerializationContext))
            {
                conversionHappened = convertXml(element, version, project);

                IXmlSerializer <SerializationContext> serializer;
                Type deserializeType;

                if (type == null)
                {
                    serializer      = _repository.SerializerFor(element);
                    deserializeType = serializer.ObjectType;
                }
                else
                {
                    serializer      = serializeFor(type);
                    deserializeType = type;
                }

                var formulaCacheElement = getFormulaCacheElementFor(element, deserializeType);
                conversionHappened = convertXml(formulaCacheElement, version, project) || conversionHappened;
                deserializeFormula(formulaCacheElement, version, project, serializationContext);

                deserializedObject = serializer.Deserialize(element, serializationContext);
            }

            //Performs the conversion to the latest project version
            conversionHappened = convert(deserializedObject, version, project) || conversionHappened;

            //Once the project was converted, update all formula references
            _deserializedReferenceResolver.ResolveFormulaAndTemplateReferences(deserializedObject, project);

            if (conversionHappened)
            {
                _eventPublisher.PublishEvent(new ObjectConvertedEvent(deserializedObject, ProjectVersions.FindBy(version)));
            }

            return(deserializedObject);
        }
        public IMoBiProject MapFrom(ProjectMetaData projectMetaData)
        {
            try
            {
                // deserialization is a three stage process. First we need to load the observed data
                // then all other entities and finish with the project charts
                using (var serializationContext = _serializationContextFactory.Create())
                {
                    _project             = deserializeContent <IMoBiProject>(projectMetaData.Content, serializationContext);
                    _project.Name        = projectMetaData.Name;
                    _project.Description = projectMetaData.Description;

                    //add observed data to global
                    addObservedDataToSerializationContext(serializationContext);

                    //load simulation first
                    foreach (var simulationMetaData in projectMetaData.Simulations)
                    {
                        addSimulationToProject(simulationMetaData, serializationContext);
                    }

                    //then load the rest of the entities
                    foreach (var entityMetaData in projectMetaData.Children)
                    {
                        addEntityToProject(entityMetaData, serializationContext);
                    }

                    _deserializedReferenceResolver.ResolveFormulaAndTemplateReferences(_project, _project);
                    return(_project);
                }
            }
            finally
            {
                _project = null;
            }
        }