Exemplo n.º 1
0
        public static void StoreListMetadataValue(Folder phoneClient, ClientEntity list, string fieldName, string value)
        {
            if (phoneClient == null)
            {
                return;
            }

            var listsMetadata = GetListMetadataList(phoneClient);
            var listID        = list.ID.ToString();

            if (phoneClient.Items.Any(i =>
                                      i.ParentID == listsMetadata.ID &&
                                      i.FieldValues.Any(fv => fv.FieldName == FieldNames.EntityRef && fv.Value == listID)))
            {
                var metadataItem = phoneClient.Items.Single(i =>
                                                            i.ParentID == listsMetadata.ID &&
                                                            i.FieldValues.Any(fv => fv.FieldName == FieldNames.EntityRef && fv.Value == listID));
                Item copy       = new Item(metadataItem);
                var  fieldValue = metadataItem.GetFieldValue(fieldName, true);
                fieldValue.Value = value;

                // queue up a server request
                if (phoneClient.ID != Guid.Empty)
                {
                    RequestQueue.EnqueueRequestRecord(RequestQueue.SystemQueue, new RequestQueue.RequestRecord()
                    {
                        ReqType = RequestQueue.RequestRecord.RequestType.Update,
                        Body    = new List <Item>()
                        {
                            copy, metadataItem
                        },
                        BodyTypeName    = "Item",
                        ID              = metadataItem.ID,
                        IsDefaultObject = true
                    });
                }
            }
            else
            {
                Guid     id           = Guid.NewGuid();
                DateTime now          = DateTime.UtcNow;
                var      metadataItem = new Item()
                {
                    ID           = id,
                    Name         = list.Name,
                    ItemTypeID   = SystemItemTypes.Reference,
                    FolderID     = phoneClient.ID,
                    ParentID     = listsMetadata.ID,
                    Created      = now,
                    LastModified = now,
                    FieldValues  = new ObservableCollection <FieldValue>()
                    {
                        new FieldValue()
                        {
                            ItemID    = id,
                            FieldName = FieldNames.EntityRef,
                            Value     = list.ID.ToString(),
                        },
                        new FieldValue()
                        {
                            ItemID    = id,
                            FieldName = FieldNames.EntityType,
                            Value     = list.GetType().Name,
                        },
                        new FieldValue()
                        {
                            ItemID    = id,
                            FieldName = fieldName,
                            Value     = value
                        }
                    }
                };
                phoneClient.Items.Add(metadataItem);

                // queue up a server request
                if (phoneClient.ID != Guid.Empty)
                {
                    RequestQueue.EnqueueRequestRecord(RequestQueue.SystemQueue, new RequestQueue.RequestRecord()
                    {
                        ReqType         = RequestQueue.RequestRecord.RequestType.Insert,
                        Body            = metadataItem,
                        ID              = metadataItem.ID,
                        IsDefaultObject = true
                    });
                }
            }

            // store the phone client folder
            StorageHelper.WritePhoneClient(phoneClient);
        }