OSDN Git Service

Ver.1.0.0.185 Task187 Interface based design
[simplebackup/SBTGitRepository.git] / Main / SimpleBackup.Models / ModelFactory.cs
1 //-----------------------------------------------------------------------
2 // <copyright file="ModelFactory.cs" company="Takayoshi Matsuyama">
3 //     Copyright (c) Takayoshi Matsuyama. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6
7 // This file is part of Simple Backup.
8 //
9 // Simple Backup is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // Simple Backup is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Simple Backup.  If not, see <http://www.gnu.org/licenses/>.
21
22 namespace SimpleBackup.Models
23 {
24     using System;
25     using System.Collections.Generic;
26     using System.Linq;
27     using System.Xml.Linq;
28     using SimpleBackup.Core.Models;
29
30     /// <summary>
31     /// Creates model objectrs.
32     /// </summary>
33     internal class ModelFactory : IModelFactory
34     {
35         /// <summary>
36         /// Creates the backup setting.
37         /// </summary>
38         /// <returns>
39         /// The backup setting.
40         /// </returns>
41         public IBackupSetting CreateBackupSetting()
42         {
43             return new BackupSetting();
44         }
45
46         /// <summary>
47         /// Creates the backup setting.
48         /// </summary>
49         /// <param name="settingFileName">Name of the setting file.</param>
50         /// <param name="backupInfoCollection">The backup information collection.</param>
51         /// <returns>
52         /// The backup setting.
53         /// </returns>
54         public IBackupSetting CreateBackupSetting(
55             string settingFileName,
56             IEnumerable<IBackupInfo> backupInfoCollection)
57         {
58             return new BackupSetting(settingFileName, backupInfoCollection);
59         }
60
61         /// <summary>
62         /// Creates the backup information.
63         /// </summary>
64         /// <returns>
65         /// The backup inforamtion.
66         /// </returns>
67         public IBackupInfo CreateBackupInfo()
68         {
69             return new BackupInfo();
70         }
71
72         /// <summary>
73         /// Creates the backup information.
74         /// </summary>
75         /// <param name="sourceBackupInfo">The source backup information.</param>
76         /// <returns>
77         /// The backup inforamtion.
78         /// </returns>
79         public IBackupInfo CreateBackupInfo(IBackupInfo sourceBackupInfo)
80         {
81             return new BackupInfo(sourceBackupInfo);
82         }
83
84         /// <summary>
85         /// Creates the backup information.
86         /// </summary>
87         /// <param name="label">The label.</param>
88         /// <param name="backupSourceType">Type of the backup source.</param>
89         /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
90         /// <param name="isZipArchive">if set to <c>true</c> [is zip archive].</param>
91         /// <param name="sourcePath">The source path.</param>
92         /// <param name="destinationFolderPath">The destination folder path.</param>
93         /// <returns>
94         /// The backup inforamtion.
95         /// </returns>
96         public IBackupInfo CreateBackupInfo(
97             string label,
98             BackupSourceType backupSourceType,
99             bool isEnabled,
100             bool isZipArchive,
101             string sourcePath,
102             string destinationFolderPath)
103         {
104             return new BackupInfo(
105                 label,
106                 backupSourceType,
107                 isEnabled,
108                 isZipArchive,
109                 sourcePath,
110                 destinationFolderPath);
111         }
112
113         /// <summary>
114         /// Creates the backup information.
115         /// </summary>
116         /// <param name="id">The identifier.</param>
117         /// <param name="label">The label.</param>
118         /// <param name="backupSourceType">Type of the backup source.</param>
119         /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
120         /// <param name="isZipArchive">if set to <c>true</c> [is zip archive].</param>
121         /// <param name="sourcePath">The source path.</param>
122         /// <param name="destinationFolderPath">The destination folder path.</param>
123         /// <returns>
124         /// The backup inforamtion.
125         /// </returns>
126         public IBackupInfo CreateBackupInfo(
127             Guid id,
128             string label,
129             BackupSourceType backupSourceType,
130             bool isEnabled,
131             bool isZipArchive,
132             string sourcePath,
133             string destinationFolderPath)
134         {
135             return new BackupInfo(
136                 id,
137                 label,
138                 backupSourceType,
139                 isEnabled,
140                 isZipArchive,
141                 sourcePath,
142                 destinationFolderPath);
143         }
144
145         /// <summary>
146         /// Creates the backup information from binary.
147         /// </summary>
148         /// <param name="binaryData">The binary data.</param>
149         /// <param name="copySuffixNeeded">if set to <c>true</c> copy suffix is needed.</param>
150         /// <returns>
151         /// The backup inforamtion.
152         /// </returns>
153         public IBackupInfo CreateBackupInfoFromBinary(byte[] binaryData, bool copySuffixNeeded)
154         {
155             return BackupInfo.FromBinary(binaryData, copySuffixNeeded);
156         }
157
158         /// <summary>
159         /// Creates the result information.
160         /// </summary>
161         /// <param name="category">The category.</param>
162         /// <param name="description">The description.</param>
163         /// <returns>
164         /// The result inforamtion.
165         /// </returns>
166         public IResultInfo CreateResultInfo(BackupState category, string description)
167         {
168             return new ResultInfo(category, description);
169         }
170
171         /// <summary>
172         /// Creates the destination path information.
173         /// </summary>
174         /// <param name="backupSourceType">Type of the backup source.</param>
175         /// <param name="path">The path.</param>
176         /// <returns>
177         /// The destination path information.
178         /// </returns>
179         public IDestinationPathInfo CreateDestinationPathInfo(BackupSourceType backupSourceType, string path)
180         {
181             return new DestinationPathInfo(backupSourceType, path);
182         }
183
184         /// <summary>
185         /// Restores the backup setting from XML.
186         /// </summary>
187         /// <param name="xml">The XML.</param>
188         /// <param name="fileName">Name of the file.</param>
189         /// <returns>The restored backup setting.</returns>
190         public IBackupSetting RestoreBackupSettingFromXml(XElement xml, string fileName)
191         {
192             if (xml.Name != "BackupSetting")
193             {
194                 return null;
195             }
196
197             XElement itemsElement = xml.Elements("Items").FirstOrDefault();
198             if (itemsElement == null)
199             {
200                 return null;
201             }
202
203             var backupInfoList = new List<IBackupInfo>();
204             foreach (XElement element in itemsElement.Elements())
205             {
206                 IBackupInfo backupInfo = RestoreBackupInfoFromXml(element);
207                 backupInfoList.Add(backupInfo);
208             }
209
210             return new BackupSetting(fileName, backupInfoList);
211         }
212
213         /// <summary>
214         /// Restores the backup information from XML.
215         /// </summary>
216         /// <param name="xml">The XML.</param>
217         /// <returns>The restored backup information.</returns>
218         private static IBackupInfo RestoreBackupInfoFromXml(XElement xml)
219         {
220             if (xml.Name != "BackupInfo")
221             {
222                 return null;
223             }
224
225             XElement idElement = xml.Elements("Id").FirstOrDefault();
226             XElement labelElement = xml.Elements("Label").FirstOrDefault();
227             XElement backupSourceTypeElement = xml.Elements("BackupSourceType").FirstOrDefault();
228             XElement enabledElement = xml.Elements("IsEnabled").FirstOrDefault();
229             XElement zipArchiveElement = xml.Elements("IsZipArchive").FirstOrDefault();
230             XElement sourcePathElement = xml.Elements("SourcePath").FirstOrDefault();
231             XElement destinationFolderPathElement = xml.Elements("DestinationFolderPath").FirstOrDefault();
232
233             if ((idElement == null) ||
234                 (labelElement == null) ||
235                 (backupSourceTypeElement == null) ||
236                 (enabledElement == null) ||
237                 (zipArchiveElement == null) ||
238                 (sourcePathElement == null) ||
239                 (destinationFolderPathElement == null))
240             {
241                 return null;
242             }
243
244             Guid id;
245             if (Guid.TryParse(idElement.Value, out id) == false)
246             {
247                 return null;
248             }
249
250             BackupSourceType backupSourceType;
251             if (Enum.TryParse(backupSourceTypeElement.Value, out backupSourceType) == false)
252             {
253                 return null;
254             }
255
256             bool isEnabled;
257             if (bool.TryParse(enabledElement.Value, out isEnabled) == false)
258             {
259                 return null;
260             }
261
262             bool isZipArchive;
263             if (bool.TryParse(zipArchiveElement.Value, out isZipArchive) == false)
264             {
265                 return null;
266             }
267
268             return BackupInfo.CreateBackupInfo(
269                 id,
270                 labelElement.Value,
271                 backupSourceType,
272                 isEnabled,
273                 isZipArchive,
274                 sourcePathElement.Value,
275                 destinationFolderPathElement.Value);
276         }
277     }
278 }