Customize the download behavior of the record mosaic

In the mosaic view of the Asset Studio, the download icon enables users to download the latest version of the master file. In most cases, this is the desired behavior, but what if you want to allow the user to select what he or she wants to download?

In this article, we'll explain how you can provide the user with more download options. In the provided sample code, we enable the user to e.g. download the latest version of all files, a resized and web-safe version of an image or a video preview. We have taken this even a step further: the attached sample code project contains a framework that allows you to extend all of this to add your own ways of downloading a record.

Getting started

To customize the download behavior of a mosaic view item in the asset studio, you can inherit from the built-in record mosaic view item:

Adam.Web.Core.UI.WebControls.RecordMosaicViewItem

It pretty easy to modify the download behavior: just override the OnDowloadIconClicked method and implement your own download behavior.

C#
1
2
3
4
5
protected override void OnDowloadIconClicked(object sender, 
                                             CommandEventArgs e)
{
    // Add your code here ...
}

We are going to show the user with a dialog that enables him to select what he wants to download. This can be achieved by using the DialogPanel control provided by Adam.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
// Create a dialog panel control.
DialogPanel oDialogPanel = new DialogPanel();
oDialogPanel.EnableTranslation = false;
oDialogPanel.Title = "Select download type";
oDialogPanel.Width = new Unit(300, UnitType.Pixel);
oDialogPanel.ID = "dlg";
oDialogPanel.DefaultButtons = DefaultDialogButtons.Cancel;

// Add the control to the server-side document hierarchy.
this.Controls.Add(oDialogPanel);

// Make sure the dialog panel is visible.
oDialogPanel.Show();

We can now add controls to the dialog panel that show the user the available download options.

Download methods framework

An asset in Adam can hold multiple files and there files can be of any file type. Having this said, it is pretty obvious that a specific download method (e.g. resize) can't be provided for every type of asset (e.g. a Word document can't be resized).

To solve this issue, we have created a small framework that can be used to test what download method is available for a given record.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public abstract class DownloadMethod
{
    /// 
    /// Tests whether the DownloadMethod.
    /// supports downloading the given record.
    /// 
    protected internal abstract Boolean IsSupported(Record record);

    /// 
    /// Gets the file system path of the file to 
    /// download.
    /// 
    protected internal abstract String CreateDownload(Record record);

    /// 
    /// Gets the title of the download method.
    /// 
    protected internal abstract String Title { get; }

    /// 
    /// Gets the description of the download method.
    /// 
    protected internal abstract String Description { get; }
}

The method IsSupported checks if a given record is supported for a given record. When this method returns true, the download method can be presented to the user. To present user-readable information to the user about the download metho, the properties Title and Description should be used.

The method CreateDownload shoudl create a downloadable file and returns the path of the generated file to download.

Example of a download method

The following example shows you how you can create a download method that allows a user to download all files of record.

In the IsSupported method, a test is done to check whether the record actually contains more than one file. If not, there is no point in providing the user with this download method.

The generation of the file to download is achieved by create a standard download order that adds the latest versions of all files in the record.

And that's it!

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class AllFilesDownload : DownloadMethod
{
    protected internal override bool IsSupported(Record record)
    {
        // The download method is only 
        // available when the record 
        // contains more than one file.
        if (record.Files.Count <= 1)
            return false;
        return true;
    }

    protected internal override string CreateDownload(Record record)
    {
        // Create a download order
        DownloadOrder oOrder = new DownloadOrder(record.App);
        oOrder.AddNew();
        
        // Add the latest version of all files.
        oOrder.AddRecord(record, RecordAssetType.LatestVersionOfAllFiles, 
                                 RecordTargetType.Document);

        // Execute the order
        oOrder.Save();
        // Which gives the file to download.
        return oOrder.DeliveredFiles[0].Path;
    }
    
    protected internal override string Title
    {
        get { return "All files"; }
    }

    protected internal override string Description
    {
        get { return "Download all the files in the record."; }
    }
 }

Take a look in the sample code project to see other (and more complex) examples.

Putting it all together

Now the download method framework has been explained, the next thing to do is putting this all together.

First of all, our custom record mosaic view item must be aware of all possible download methods. In the sample code project, a static list of download methods is hard-coded. If you wish to fine-tune the code, you could add code to load this list from a setting or configuration file.

C#
1
2
3
4
5
6
7
8
9
10
11
private static List<DownloadMethods.DownloadMethod> _downloadMethods;

static RecordMosaicViewItem()
{
    _downloadMethods = new List<DownloadMethods.DownloadMethod>();
    _downloadMethods.Add(new DownloadMethods.PrintResolutionDownload());
    _downloadMethods.Add(new DownloadMethods.WebResolutionDownload());
    _downloadMethods.Add(new DownloadMethods.VideoPreviewDownload());
    _downloadMethods.Add(new DownloadMethods.OrginalDocumentDownload());
    _downloadMethods.Add(new DownloadMethods.AllFilesDownload());
}

At this point, the curstom record mosaic view item knows all possible download methods and we can update the CreateDialogPanel method to add controls for all supported download methods for the current Record.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
private void CreateDialogPanel()
{
    // Get the record that needs to be downloaded.
    Record oRecord = this.DataItem as Record;
    if (oRecord == null)
        throw new Exception("The dataitem is not a Record.");

    // Create the dialog panel control.
    DialogPanel oDialogPanel = new DialogPanel();
    oDialogPanel.EnableTranslation = false;
    oDialogPanel.Title = "Select download type";
    oDialogPanel.Width = new Unit(300, UnitType.Pixel);
    oDialogPanel.ID = "dlg";
    oDialogPanel.DefaultButtons = DefaultDialogButtons.Cancel;

    this.Controls.Add(oDialogPanel);

    // Show a list of available download methods to the user.
    int iCount = _downloadMethods.Count;
    for (int i = 0; i < iCount; i++)
    {
        DownloadMethods.DownloadMethod oMethod = _downloadMethods[i];

        // If the download method supports the record, add layout for 
        // this method.
        if (oMethod.IsSupported(oRecord) == true)
        {
            // Generate layout for the download method.
            HtmlGenericControl oMethodControl = 
                    new HtmlGenericControl("div");
            oMethodControl.Attributes.Add("class", "downloadmethod");

            HyperLink oLink = new HyperLink();
            oLink.CssClass = "downloadmethodtitle";
            // Trigger a post back to the record mosaic view item 
            // when the donwload method is clicked.
            oLink.NavigateUrl = this.Page.ClientScript.       ~
                       GetPostBackClientHyperlink(this, i.ToString());
            oMethodControl.Controls.Add(oLink);

            HtmlGenericControl oDescription = 
                    new HtmlGenericControl("div");
            oDescription.Attributes.Add("class", 
                                        "downloadmethoddescription");
            oMethodControl.Controls.Add(oDescription);

            // Set the labels for the download method.
            oLink.Text = oMethod.Title;
            oDescription.InnerText = oMethod.Description;
            
            oDialogPanel.Controls.Add(oMethodControl);
        }
    }

    oDialogPanel.Show();
}

Note that when a user clicks an the download mehtod, a post-back is issued. The parameter passed is the index of the download method in the static list. Using this information, we can easily retrieve the requested download method and perform the actuall download, as shown in the next code snippet.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void RaisePostBackEvent(String eventArgument)
{
    // Inform the page that an action has occurd.
    this.RaiseBubbleEvent(this, 
                new CommandEventArgs("DownloadMethodSelected", null));

    // The index of the downoad method to use is indicated in the 
    // event argument.
    int iDownloadMethodIndex = int.Parse(eventArgument);
    DownloadMethods.DownloadMethod oMethod 
                = _downloadMethods[iDownloadMethodIndex];

    // Create the order.
    String sPath = oMethod.CreateDownload(this.DataItem as Record);

    // Download the order.
    AdamDownloader.DownloadFile(this.Page, sPath, true);
}

Installing the sample code

Obviously, you shoudl first build the sample code project. Once this is done, registerr the built assembly in Adam, using the Adam.Core.CommandLine RegisterAssembly tool.

Next go to the ConfigSutiod, log in with an administrator account. Go the settings management page and search for the setting "Record View Definition" (without the quotes). In this setting, update the tag adam:mosaicItemType to the following:

XML
1
<adam:mosaicItemType>CustomDownload.RecordMosaicViewItem, CustomDownload</adam:mosaicItemType>

If you now go to the asset studio, this new behavior of the download icon is activated.

Wrap up

The sample code itself contains some more bits and pieces to get a smoother user experience, but we hope that the basics of this approach have been covered in the article. Let this article inspire you to serve your customer needs!

Sample Code

The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed