Welcome to the last post of our Image Management System, this certainly has been a long post. I hope that by now you see some value in taking some time and designing your system instead of coding first and now some code:). If you have not created any of the wire frame projects now would be a good time to look at Adding Image Management to your Commerce Site Part (IV).
I am going to include the code as part of this post see attachment at the bottom. Also we are going to walk through one of the operations instead of having the complete end to end development. I want to minimize the length of this post :). So by walking through one operation you should have a good understanding of how to fill the rest of the operations.
We need to define the operations for Image Management this is defined for us based on the Catalog UI forms and Controls we created to display images.
We are going to add some code to get the Image management System up and running. for this sample we are only going to work with the Get Images operation. The following references needs to be added to our ImageManagementSystem project:
Microsoft.CommerceServer.RuntimeImageModuleMicrosoft.Practices.EnterpriseLibrary.DataMicrosoft.Practices.EnterpriseLibrary.Common
The following Web References needs to be added to our project:
localhost
Now lets add some methods, properties and members.
Your completed diagram should look like the image Bellow:
Your Image Management System project code should look like the following:
Now lets talk code for a while.
ImageServiceAgent and ImageSiteAgent works similar to CatalogSiteAgent and CatalogServiceAgent I have only created a few methods to show you how you can create your own Agents based on your requirements you can add\remove what you need.
In our AbstractImage class we code our method so that it calls our Implementor's method to retrieving images.
ImageWebServiceAccess
In the ImageWebServiceAccess's constructor we need to set the Image Web Proxy's URL and the credentials for accessing the Web Service.
The RetrieveImageRecords method we call our Web Service Method to get our Images (we will code the Web Service later on).
ImageDatabaseAccess
In the ImgeDatabaseAccess's constructor we need to get the Product Catalog connection string and use it to create Database object using the EnterpriseLib Data Block. Now that we have a database object we can query our Image SQL objects.
So in our RetrieveImageRecords method we call img_GetImageProduct Stored Procedure and return the results back to our caller.
Note: You may want to sign EnterpriseLibrary projects if you are going to add your assemblies into the GAC.
Add the following references to your Image Module Project:
Microsoft.CommerceServer.RuntimeImageManagementSystem
Now lets add some methods, properties and members. Create a UML folder under the Image Module Project. Under the UML folder add a class diagram object. In the Diagram designer windows add a new class and name it WebImageModule.cs.
Add the following private Fields
Add the following methods
Add the following properties
Your class Diagram should look like the following:
Your Image Module project code should look like the following:
The ImageContext property code explained:
We use the Singleton pattern to get an ImageContext using site Agent. The site name is retrieved from the CommerceContext. We should have a dependency on CommerceApplicationModule, the reason for this is that we depend on this object to get our sitename.
Modify the web.config so we have the CommerceApplicationModule loaded and add a configuration section to hold our site name. I have outlined the section for you to modify.
web.config walk through, You need to add a section handler under the sectiongroup tag so our CommerceAppication can read the site name. Next we add application tag for Commerce Server's siteName and finally we add two modules the CommerceApplicationModule and our ImageModule.
Code walk through, in the WebService constructor set the field to current ImageContext. So in each method all you have to do is call the ImageContext's methods.
Now lets tie it all together but first compile your solution to make sure that we have no errors.
case CatalogManagerItem.ViewProductImages:objectToCreate = new ProductCatalogImages();break;We added the enum so that we our factory can create the image tab based on the code we added in the switch statement of itemToCreate.add code to CatalogItemEdit_Load method of CatalogItemEdit under the Dialog folder.pages.Add(CatalogManagerItem.ProductCatalogImages, "Images"); Once our factory knows how to create an image tab we need to add it to our form.
By now we have walked through one operation and you should be familiar enough with the projects to create the rest on your own, good luck.
There are many ways to upload images to a file server you can use FtpWebRequest Class , HttpWebRequest (using the PUT verb) and Web Service. Web Service code sample:
You would need a method to read the image from the file system
FileStream fs = new FileStream(textBox1.Text, FileMode.Open);Byte[] image = new Byte[fs.Length];fs.Read(image, 0, (int)fs.Length);fs.Close();localhost.ImageUpload ws = new localhost.ImageUpload();ws.Credentials = System.Net.CredentialCache.DefaultCredentials;ws.UplaodImage(image);
From the Web Service you would accept a byte array of our image and save it to the file system
[WebMethod]public void UplaodImage(byte[] image){FileStream fs = new FileStream("C:\\Inetpub\\wwwroot\\CatalogWebService\\test.jpg", FileMode.CreateNew, FileAccess.Write);fs.Write(image, 0, image.GetLength(0));fs.Close();}
In order for the Web Service to write the image you will need to give the AppPool write permissions.
Catalog Manager has a caching class you can use this to cache your image DataSets.
Use the Enterprise Library for exception management and logging for both the Server side and client side.
Extend the Authorization Manager to have Image Management roles as described in our requirement.
You will need to use the two methods under all the Web Service to get the authorization policy and perform your own access checks. This means that you have to create your own Authorization Context. The AzMan has an interop layer that you can use.The Authorization Methods exposed to all Web Services (for more details see my post Extending Commerce Server 2007 Web Services):
For more information on Authorization Manager see this MSDN article Use Role-Based Security in Your Middle Tier .NET Apps with Authorization Manager.
Not everyone does this but you should start thinking about it. This should be done during the design of your application to identify risks to your application. You can find out more about Threat Modeling on MSDN.
So now you have all the tools and knowledge to complete the image projects and add multiple images to a product. When you upload your image to your Web Service you can use the Commerce Staging Service to move your files to your production site.