public EntityPart UpdateEntityPartData(string partName, string eTag, string newData)
        {
            //TODO: Check e-tag

            var entityPart = GetEntityPartPackagePart(partName);

            //Copy the data to the entity part
            using (var fileStream = new StringStream(newData))
            {
                fileStream.CopyTo(entityPart.GetStream());
            }

            //Update the metadata part.
            var entityPartMetadata = GetEntityPartMetadata(partName);

            entityPartMetadata.Modified   = DateTime.Now;
            entityPartMetadata.ModifiedBy = User.GetCurrentUser().LoginName;
            entityPartMetadata.Etag.IncrementBy(1);

            var entityPartMetadataPart = GetEntityPartMetadataPackagePart(partName);
            var metadata = JsonConvert.SerializeObject(entityPartMetadata, new EtagJsonConverter());

            //Copy the metadata to the entity part metadata part.
            using (var fileStream = new StringStream(metadata))
            {
                fileStream.CopyTo(entityPartMetadataPart.GetStream());
            }

            return(GetEntityPart(partName, true));
        }
        private EntityPart CreateEntityPart(string partName, EntityPartMetadata entityPartMetadata, string data)
        {
            if (partName.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("partName");
            }

            if (entityPartMetadata == null)
            {
                throw new ArgumentNullException("entityPartMetadata");
            }

            var entityPartUri = GetEntityPartUriFromEntityPartName(partName);

            if (m_package.PartExists(entityPartUri))
            {
                throw new InvalidOperationException("An entity part with the specified name already exists within the entity.");
            }

            var entityPart =
                m_package.CreatePart(entityPartUri, Data.Constants.EntityPartContentType);

            //Copy the data to the entity part
            using (var fileStream = new StringStream(data))
            {
                fileStream.CopyTo(entityPart.GetStream());
            }

            //Create a relationship between the default entity part and the entity part.
            var defaultEntityPart = GetDefaultEntityPartPackagePart();

            defaultEntityPart.CreateRelationship(entityPartUri, TargetMode.Internal, Data.Constants.EntityPartRelationship);

            //Create the entity part metadata object.
            var entityPartMetadataUri  = GetEntityPartMetadataUriFromEntityPartName(partName);
            var entityPartMetadataPart = m_package.CreatePart(entityPartMetadataUri, Data.Constants.EntityPartContentType);

            var metadata = JsonConvert.SerializeObject(entityPartMetadata, new EtagJsonConverter());

            //Copy the metadata to the entity part metadata part.
            using (var fileStream = new StringStream(metadata))
            {
                fileStream.CopyTo(entityPartMetadataPart.GetStream());
            }

            //Finally, create the relationship between the entity part and its metadata -- not that we couldn't have found it by convention.
            entityPart.CreateRelationship(entityPartMetadataUri, TargetMode.Internal, Data.Constants.EntityPartMetadataRelationship);

            return(MapEntityPartFromPackagePart(entityPart));
        }
Ejemplo n.º 3
0
        private static void InitializeEntityPackage(Package package, Guid?entityId, string @namespace, string title, string data)
        {
            if (@namespace.IsNullOrWhiteSpace())
            {
                throw new ArgumentException(@"An entity namespace must be specified.", "namespace");
            }


            if (entityId.HasValue == false)
            {
                entityId = Guid.NewGuid();
            }

            if (title.IsNullOrWhiteSpace())
            {
                title = entityId.ToString();
            }

            // Add the metadata part to the Package.
            package.PackageProperties.Identifier     = entityId.Value.ToString();
            package.PackageProperties.ContentType    = Constants.EntityPackageContentType;
            package.PackageProperties.Subject        = @namespace;
            package.PackageProperties.Title          = title;
            package.PackageProperties.Created        = DateTime.Now;
            package.PackageProperties.Creator        = User.GetCurrentUser().LoginName;
            package.PackageProperties.Modified       = DateTime.Now;
            package.PackageProperties.LastModifiedBy = User.GetCurrentUser().LoginName;

            // Add the default entity part to the Package.
            var defaultEntityPart =
                package.CreatePart(DefaultEntityPartUri, Constants.EntityPartContentType);

            //Copy the data to the default entity part
            if (data.IsNullOrWhiteSpace())
            {
                return;
            }

            using (var fileStream = new StringStream(data))
            {
                fileStream.CopyTo(defaultEntityPart.GetStream());
            }
        }
Ejemplo n.º 4
0
        public Entity UpdateDefaultEntityPartData(string etag, string data)
        {
            //TODO: Check to see if the eTag matches.

            var defaultEntityPartPackagePart = GetDefaultEntityPartPackagePart();

            //Copy the data to the default entity part
            using (var fileStream = new StringStream(data))
            {
                fileStream.CopyTo(defaultEntityPartPackagePart.GetStream());
            }

            //Bump the modified by and modified date.
            m_package.PackageProperties.Modified       = DateTime.Now;
            m_package.PackageProperties.LastModifiedBy = User.GetCurrentUser().LoginName;

            //TODO: Bump the etag...

            return(MapEntityFromPackage(true));
        }
        private Attachment CreateAttachment(string fileName, AttachmentMetadata attachment, Stream attachmentData)
        {
            if (fileName.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("fileName");
            }

            if (attachment == null)
            {
                throw new ArgumentNullException("attachment");
            }

            var defaultEntityPart = GetDefaultEntityPartPackagePart();

            var attachmentRelationships = defaultEntityPart.GetRelationshipsByType(Data.Constants.AttachmentRelationship);
            var attachmentUri           = GetAttachmentUriFromAttachmentFileName(fileName);
            var attachmentMetadataUri   = GetAttachmentMetadataUriFromAttachmentFileName(fileName);

            var existingAttachmentRelationship = attachmentRelationships.FirstOrDefault(ar => ar.TargetUri == attachmentUri);

            if (existingAttachmentRelationship != null)
            {
                throw new InvalidOperationException("An attachment with the specified name already exists in the package: " + fileName);
            }

            var attachmentPackagePart =
                m_package.CreatePart(attachmentUri, StringHelper.GetMimeTypeFromFileName(fileName));

            if (attachmentData != null)
            {
                attachmentData.CopyTo(attachmentPackagePart.GetStream());
            }

            //Create a relationship between the default entity part and the attachment part.
            defaultEntityPart.CreateRelationship(attachmentUri, TargetMode.Internal, Data.Constants.EntityPartRelationship);

            //Create the attachment metadata object.
            var attachmentMetadataPackagePart = m_package.CreatePart(attachmentMetadataUri, Data.Constants.EntityPartContentType);

            //Throw the metadata in the file.
            var attachmentMetadata = new AttachmentMetadata
            {
                Category         = attachment.Category,
                Created          = attachment.Created,
                CreatedBy        = attachment.CreatedBy,
                Etag             = attachment.Etag.IncrementBy(1),
                FileName         = fileName,
                Modified         = attachment.Modified,
                ModifiedBy       = attachment.ModifiedBy,
                Path             = attachment.Path,
                Properties       = attachment.Properties,
                Size             = attachment.Size,
                TimeLastModified = attachment.TimeLastModified,
                Url = attachment.Url
            };

            var metadata = JsonConvert.SerializeObject(attachmentMetadata, new EtagJsonConverter());

            //Copy the metadata to the entity part metadata part.
            using (var fileStream = new StringStream(metadata))
            {
                fileStream.CopyTo(attachmentMetadataPackagePart.GetStream());
            }

            return(MapAttachmentFromPackagePart(attachmentPackagePart));
        }