/// <summary> /// Creates a new resource of type collectionName in the repository. /// </summary> /// <param name="collectionName">The resource type.</param> /// <param name="atomEntry">Information about the resource.</param> /// <returns>A SyndicationItem that describes the newly created resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or atomEntry is null/empty .</exception> SyndicationItem IAtomPubStoreWriter.CreateMember(string collectionName, AtomEntryDocument atomEntry) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (null == atomEntry) { throw new ArgumentNullException("atomEntry"); } AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken(); using (ZentityContext context = CoreHelper.CreateZentityContext()) { if (!authenticatedToken.HasCreatePermission(context)) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } ScholarlyWork resource = CreateScholarlyWork(collectionName); context.AddToResources(resource); ZentityAtomPubStoreWriter.UpdateResourceProperty(context, resource, atomEntry); resource.GrantDefaultPermissions(context, authenticatedToken); context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Updates the Resource.File of the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The resource whose File needs to be updated.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <returns>A SyndicationItem that describes the updated resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or media is null.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> SyndicationItem IAtomPubStoreWriter.UpdateMedia(string collectionName, string memberResourceId, string mimeType, byte[] media) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (null == media) { throw new ArgumentNullException("media"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { Type collectionType = CoreHelper.GetSystemResourceType(collectionName); // Prepare a query to get a resource with specified Id and specified type. string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaResource = query.FirstOrDefault(); if (null == mediaResource) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaResource.Authorize("Update", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } mediaResource.Resources.Load(); ScholarlyWork resource = (ScholarlyWork)mediaResource.Resources.First(); resource.DateModified = DateTime.Now; mediaResource.MimeType = mimeType; mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType); MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); context.UploadFileContent(mediaResource, mediaStream); // Bug Fix : 180811 - Save Changes once mime type and contents are set. context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Creates a new Resource.File for a specified resource of type collectionName in the repository. /// </summary> /// <param name="collectionName">The resource type.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <param name="fileExtension">The media file extension.</param> /// <returns>A SyndicationItem that describes the newly created resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or mimeType is null/empty or media is null.</exception> protected SyndicationItem CreateMedia(string collectionName, string mimeType, byte[] media, string fileExtension) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(mimeType)) { throw new ArgumentNullException("mimeType"); } if (null == media) { throw new ArgumentNullException("media"); } AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken(); using (ZentityContext context = CoreHelper.CreateZentityContext()) { if (!authenticatedToken.HasCreatePermission(context)) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } ScholarlyWork resource = CreateScholarlyWork(collectionName); resource.DateModified = DateTime.Now; Core.File mediaResource = new Core.File(); mediaResource.MimeType = mimeType; mediaResource.FileExtension = string.IsNullOrEmpty(fileExtension) ? AtomPubHelper.GetFileExtension(mimeType) : fileExtension; context.AddToResources(mediaResource); context.SaveChanges(); resource.Files.Add(mediaResource); MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); context.UploadFileContent(mediaResource, mediaStream); mediaStream.Close(); resource.GrantDefaultPermissions(context, authenticatedToken); mediaResource.GrantDefaultPermissions(context, authenticatedToken); context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Updates the navigation properties. /// </summary> /// <param name="zentityContext">The zentity context.</param> /// <param name="resource">The resource.</param> /// <param name="property">The property.</param> /// <param name="values">The values.</param> private static void UpdateNavigationproperties(ZentityContext zentityContext, ScholarlyWork resource, PropertyInfo property, ReadOnlyCollection <string> values) { EntityCollection <Contact> contactRelation = property.GetValue(resource, null) as EntityCollection <Contact>; if (null != contactRelation) { contactRelation.Clear(); var authors = values.Distinct().Where(name => name.Length > 0) .Select(name => new Person { Title = name }); ZentityAtomPubStoreWriter.AddPersons(zentityContext, contactRelation, authors); } }
/// <summary> /// Creates a new Resource.File for a specified resource of type collectionName in the repository. /// </summary> /// <param name="collectionName">The resource type.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <param name="fileExtension">The media file extension.</param> /// <returns>A SyndicationItem that describes the newly created resource.</returns> SyndicationItem IAtomPubStoreWriter.CreateMedia(string collectionName, string mimeType, byte[] media, string fileExtension) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(mimeType)) { throw new ArgumentNullException("mimeType"); } if (null == media) { throw new ArgumentNullException("media"); } if (SwordConstants.ZipContentType != mimeType) { return(base.CreateMedia(collectionName, mimeType, media, fileExtension)); } // Convert byte array to stream. MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); string extractionPath = ExtractZipContent(mediaStream); HttpContext.Current.Items[SwordConstants.ZipExtractedPath] = extractionPath; // Get the path of METS xml file. string metsFilePath = extractionPath + "\\" + SwordConstants.MetsDocumentName; if (!System.IO.File.Exists(metsFilePath)) { //string errorMessage = string.Format(CultureInfo.CurrentCulture, // Properties.Resources.SWORD_MISSING_METS_DOCUMENT, // SwordConstants.MetsDocumentName); //throw new MetsException(errorMessage); return(base.CreateMedia(collectionName, mimeType, media, fileExtension)); } AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken(); using (ZentityContext zentityContext = CoreHelper.CreateZentityContext()) { if (!authenticatedToken.HasCreatePermission(zentityContext)) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } // Generate METS document from given METS xml file. MetsDocument document = new MetsDocument(metsFilePath); // Create resource of specified collection type. ScholarlyWork resource = CreateScholarlyWork(collectionName); resource.DateModified = DateTime.Now; // Upload the zip file contents as media for main resource. // This will be required in AtomPub get requests and further use. Core.File mediaResource = AddFileResource(zentityContext, resource, mediaStream); mediaResource.MimeType = mimeType; mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType); // close the stream mediaStream.Close(); AddChildResources(extractionPath, document, resource, zentityContext); resource.GrantDefaultPermissions(zentityContext, authenticatedToken); mediaResource.GrantDefaultPermissions(zentityContext, authenticatedToken); // Save all changes at the end zentityContext.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(base.BaseUri, resource)); } }