Exemplo n.º 1
0
        public void SaveData(EAV.Model.IModelRootInstance instance)
        {
            HttpResponseMessage response;

            if (instance.ObjectState == EAV.Model.ObjectState.New)
            {
                response = client.PostAsJsonAsync <EAV.Store.IStoreInstance>(String.Format("api/data/subjects/{0}/instances?container={1}", instance.SubjectID.GetValueOrDefault(), instance.ContainerID), instance).Result;
                if (response.IsSuccessStatusCode)
                {
                    EAVModelLibrary.ModelInstance newInstance = response.Content.ReadAsAsync <EAVModelLibrary.ModelRootInstance>().Result;

                    instance.InstanceID = newInstance.InstanceID;

                    instance.MarkUnmodified();
                }
                else
                {
                    throw (new ApplicationException("Attempt to create data failed."));
                }
            }
            else if (instance.ObjectState == EAV.Model.ObjectState.Modified)
            {
                response = client.PatchAsJsonAsync <EAV.Store.IStoreInstance>("api/data/instances", instance).Result;
                if (response.IsSuccessStatusCode)
                {
                    instance.MarkUnmodified();
                }
                else
                {
                    throw (new ApplicationException("Attempt to update data failed."));
                }
            }

            foreach (EAVModelLibrary.ModelValue value in instance.Values)
            {
                SaveValue(value);
            }

            foreach (EAVModelLibrary.ModelChildInstance childInstance in instance.ChildInstances)
            {
                SaveChildInstance(childInstance);
            }

            if (instance.ObjectState == EAV.Model.ObjectState.Deleted)
            {
                response = client.DeleteAsync(String.Format("api/data/instances/{0}", instance.InstanceID.GetValueOrDefault())).Result;
                if (response.IsSuccessStatusCode)
                {
                }
                else
                {
                    throw (new ApplicationException("Attempt to delete data failed."));
                }
            }
        }
Exemplo n.º 2
0
        public void LoadData(EAV.Model.IModelRootInstance instance)
        {
            bool instanceWasUnmodified = instance.ObjectState == EAV.Model.ObjectState.Unmodified;

            HttpResponseMessage response = client.GetAsync(String.Format("api/data/instances/{0}/instances", instance.InstanceID.GetValueOrDefault())).Result;

            if (response.IsSuccessStatusCode)
            {
                instance.ChildInstances.Clear();

                var childInstances = response.Content.ReadAsAsync <IEnumerable <EAVModelLibrary.ModelChildInstance> >().Result;
                foreach (EAVModelLibrary.ModelChildInstance childInstance in childInstances)
                {
                    childInstance.MarkUnmodified();

                    EAV.Model.IModelContainer container = instance.Container.ChildContainers.Single(it => it.ContainerID == childInstance.ContainerID);

                    childInstance.Container = container;

                    if (container.Attributes.Any())
                    {
                        LoadValues(childInstance, container.Attributes);
                    }

                    if (container.ChildContainers.Any())
                    {
                        LoadChildInstances(childInstance, container.ChildContainers);
                    }

                    instance.ChildInstances.Add(childInstance);

                    childInstance.MarkUnmodified();
                }

                if (instanceWasUnmodified)
                {
                    instance.MarkUnmodified();
                }
            }
            else
            {
                throw (new ApplicationException("Attempt to load data failed."));
            }
        }