summaryrefslogtreecommitdiff
path: root/BloggerToDasBlog/trunk/src/app/BloggerToDasBlog.Console/DasBlogWriter.cs
blob: 51435cc72cd0a0081a96514c568f24fd513ca12b (plain)
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
57
58
59
60
61
62
63
using System;
using newtelligence.DasBlog.Runtime;

namespace BloggerToDasBlog.Console {
	public class DasBlogWriter {
		#region Constructors

		public DasBlogWriter( )
			: this( BlogDataServiceFactory.GetService( AppDomain.CurrentDomain.BaseDirectory, null ), "Journal" ) {}

		public DasBlogWriter( IBlogDataService service, String category ) {
			_service = service;
			_category = category;
		}

		#endregion

		#region Public Methods

		public void Write( IBloggerEntry bloggerEntry ) {
			Entry entry = new Entry( );
			entry.CreatedLocalTime = bloggerEntry.Date;
			entry.ModifiedLocalTime = bloggerEntry.Date;
			entry.Title =
				( bloggerEntry.Title.Length > 0
				  	? bloggerEntry.Title
				  	: bloggerEntry.Body.Substring( 0, Math.Min( 20, bloggerEntry.Body.Length ) ) );
			entry.Content = bloggerEntry.Body.Replace( Environment.NewLine, "<br />" );
			entry.EntryId = Guid.NewGuid( ).ToString( );
			entry.Categories = _category;
			entry.Author = bloggerEntry.Author;
			_service.SaveEntry( entry );
			if ( bloggerEntry.Comments.Count > 0 ) {
				foreach ( IBloggerComment bloggerComment in bloggerEntry.Comments ) {
					WriteComments( bloggerComment, entry.EntryId );
				}
			}
		}

		#endregion

		#region Private Methods

		private void WriteComments( IBloggerComment bloggerComment, String targetEntryId ) {
			Comment comment = new Comment( );
			comment.CreatedLocalTime = bloggerComment.Date;
			comment.ModifiedLocalTime = bloggerComment.Date;
			comment.TargetEntryId = targetEntryId;
			comment.Author = bloggerComment.Author;
			comment.Content = bloggerComment.Body;
			_service.AddComment( comment );
		}

		#endregion

		#region Private Fields

		private readonly IBlogDataService _service;
		private readonly string _category;

		#endregion
	}
}