Navigatie

Contact

Send mail to the author(s) E-mail

View Richard Soeteman's profile on LinkedIn

RSS 2.0 | Atom 1.0 | CDF

Archief

Categorieën

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Zoeken

# Friday, 16 April 2010
Friday, 16 April 2010 14:09:04 (GMT Daylight Time, UTC+01:00) ( CMSImport | Umbraco )

This Blog is not active anymore, further posts will be available on my company website. Read new posts here

When I released CMSImport 1.0 two months ago, I briefly described the FieldAdapters feature that  I was going to build for version 1.1. Today I want to go more into detail about the FieldAdapters feature for CMSImport.

The problem

The reason I started to think about fieldadapters was because I wanted to solve a very common problem. As you might know Umbraco only accepts three kinds of  data; string, integer and datetime. What happens when you try to map a boolean value to  a yes/no Umbraco datatype? An exception is thrown.  The following screenshot contains several products that we want to import into Umbraco.

products

When we’ve mapped the InStore column to a yes/no datatype the import will fail because it can’t map the boolean value to a 0 or 1 and we will see the following ugly screen

errors 

And a FieldAdapter can fix that?

Yes a fieldadapter inspects the value and tries to parse it, in this case it will try to pares a true/false value to 1/0, best to explain by exploring the source code. The whole feature is based on the new IFieldAdapter interface.

using System;

namespace CMSImportLibrary.Interfaces
{
    /// <summary>
    /// Implement the IFieldAdapter interface to convert an mallformed field to a correct type.
    /// </summary>
    public interface IFieldAdapter
    {
        /// <summary>
        /// Contains the GUID of the datatype we want to parse using this FieldAdapter
        /// </summary>
        Guid DataTypeId { get; }

        /// <summary>
        /// Parse the data 
        /// </summary>
        /// <param name="value">The value to parse</param>
        /// <returns></returns>
        object Parse(object value);
    }
}

As you can see in the code snippet the interface contains the property DataTypeId. The value needs to correspondent to the Id (GUID) of the datatype you are creating the fieldadapter for. During the import a factory inspects the underlying datatype of the document property, if a fieldadapter is found it will execute  the Parse method. This will work on all Umbraco datatypes (also custom datatypes or datatypes from third party packages) as long as you know the Id of the datatype. You can find the id by opening the datatype in Umbraco, there you see the RenderControl .

truefalsedatatype

When opening the source code of the rendercontrol you’ll see the Id of the datatype (a GUID). Don’t worry if you can’t open the source code of the datatype i’ll add the Id’s of all known datatypes to the documentation of CMSImport and I’ll make a free tool that shows the Id of any datatype.

Implementation of the BooleanFieldAdapter

Now enough with all the boring stuff, let’s see the fieldadapter in action. Below you’ll see the implementation of the BooleanFieldAdapter. I’ve mapped the id of the True/false datatype to the DataTypeId property so CMSImport knows that it needs to call the Parse method during the mapping of a yes/no document property. In the Parse method I simply check if the value is already in the correct format , if not it will try to convert the value to 1 or 0 and return that , otherwise it will just return the original value.

using System;
using CMSImportLibrary.Interfaces;

namespace CMSImportLibrary.FieldAdapters.DefaultFieldAdapters
{
    public class BooleanFieldAdapter : IFieldAdapter
    {
        #region IFieldAdapter Members

        public Guid DataTypeId
        {
            get { return new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a"); }
        }

        public object Parse(object value)
        {
            if (!(value.Equals("0") || value.Equals("1")))
            {
                bool boolValue = false;
                if (bool.TryParse(value.ToString(), out boolValue))
                {
                    return boolValue ? 1 : 0;
                }
            }
            return value;
        }

        #endregion
    }
}

Now when we run the import  again it will just import the data without any errors, just by adding a few lines of code, isn’t that powerful?

Beyond fixing problems

Now that we have this mechanism we can also use it to modify data during the import. Let’s say we import a piece of content from an old site that contains an image tag. This will just run fine but when you have a reference to an image on your old site and  you delete that old site all the references to the images are dead. FieldAdapters can help solve this issue by inspecting the text, extract the image tags, import these images into the media library and update the image tag with a reference to the media item. This will really help keep your site consistent. The same applies for Upload fields.

Will all of this be included in the free version of CMSImport also?

All FieldAdapters that fix errors (Like the BooleanFieldAdapter) will be included into the free version of CMSImport. FieldAdapters that helps you updating the content will only be included in the PRO version of CMSImport. And again you can also create your own FieldAdapters, free for both versions.

This will be the major feature of the V1.1 release of CMSImport. Please let me know what you think about this feature.

Comments [0] | | # 
# Sunday, 04 April 2010
Sunday, 04 April 2010 19:22:10 (GMT Daylight Time, UTC+01:00) ( Package | Umbraco )

This Blog is not active anymore, further posts will be available on my company website. Read new posts here

With Taskscheduler you can schedule url's/pages to be executed on a certain date and time. It’s a simplified version of the Windows task scheduler, build on top of the Umbraco scheduler functionality. If you’ve used  CMSImport PRO, the package should look familiar because TaskScheduler is based on the scheduled import functionality of CMSImport PRO.  When you installed the package and browse to the developer section you’ll notice the new Scheduled Tasks tree. From here you can create new Scheduled tasks, or browse to existing ones.

Schedule a task

When you create a scheduled task, you’ll see the following screen. In this screen you can configure the scheduled task.

TaskScheduler

The scheduled task name contains the name of the scheduled task. This will also be shown in the tree and you can use this in the notification email template.

The Schedule Url contains the url that you want to execute. You can insert a full url (http://mydemosite.com/scheduledpage.aspx), an absolute path(/scheduledpage.aspx) or a relative path(~/scheduledpage.aspx). The last option will come handy when you start working with Umbraco 4.1 where virtual folders are supported. When you want to retrieve the HTTP output of the page and use that in your notify email you can set the url output to true.

If you want to retrieve a notification email that tells you the scheduled task has executed you can fill in your emailaddress in the notify emailaddress field.

Basically you’ll have three options

  • Execute every week on certain days and a certain time.
  • Execute every day on a certain time
  • Execute every hour

Settings

Not a lot of settings to configure. Only the settings for the notification email. You can configure the from address, the subject  and the email template. In the email template you can use two tags.

  • [#Taskname]. Will be replaced with the name of the configured task
  • [#Output] . Will be replaced with the html that got returned from the page if you had checked “Use Url output in url”.

taskschedulersettings

System Requirements

TaskScheduler is tested on Umbraco 4.0.3.1, is compatible with .NET Framework 2.0  and is compatible with SQL Server(Express) 2005, 2008.

Download

You can download the package from our.umbraco.org

As always I hope that you like the package and it’s useful for you.

Comments [1] | | #