SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Pages/Elements/IStandardFieldDate.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Globalization;
18 using System.Xml;
19 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
20 using erminas.SmartAPI.Utils;
21 
22 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
23 {
24  public interface IStandardFieldDate : IStandardField<DateTime>
25  {
26  }
27 
31  [PageElementType(ElementType.StandardFieldDate)]
32  internal class StandardFieldDate : StandardField<DateTime>, IStandardFieldDate
33  {
34  private readonly DateTime BASE_DATE = new DateTime(1899, 12, 30);
35 
36  internal StandardFieldDate(IProject project, XmlElement xmlElement) : base(project, xmlElement)
37  {
38  }
39 
40  public StandardFieldDate(IProject project, Guid guid, ILanguageVariant languageVariant)
41  : base(project, guid, languageVariant)
42  {
43  }
44 
45  public override void Commit()
46  {
47  using (new LanguageContext(LanguageVariant))
48  {
49  //TODO testen gegen _value == null und ob das ergebnis mit htmlencode richtig ist
50  var value = _value.Date == default(DateTime)
51  ? RQL.SESSIONKEY_PLACEHOLDER
52  : _value.Date.Subtract(BASE_DATE).Days.ToString(CultureInfo.InvariantCulture);
53  Project.ExecuteRQL(string.Format(SAVE_VALUE, Guid.ToRQLString(), value,
54  (int) ElementType));
55  }
56  //TODO check guid
57  //xml
58  }
59 
60  protected override DateTime FromString(string value)
61  {
62  try
63  {
64  return DateTime.Parse(value, CultureInfo.InvariantCulture);
65  } catch (FormatException e)
66  {
67  throw new ArgumentException(string.Format("Invalid date value: {0}", value), e);
68  }
69  }
70 
71  protected override DateTime FromXmlNodeValue(string value)
72  {
73  return BASE_DATE.AddDays(int.Parse(value));
74  }
75 
76  protected override string GetXmlNodeValue()
77  {
78  return Value == default(DateTime)
79  ? ""
80  : Value.Subtract(BASE_DATE).Days.ToString(CultureInfo.InvariantCulture);
81  }
82 
83  protected override void LoadWholeStandardField()
84  {
85  }
86  }
87 }