19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
22 namespace erminas.SmartAPI.CMS.Project.Keywords
40 void DeleteForcibly();
42 void Rename(
string newKeywordName);
45 public static class KeywordFactory
47 public static IKeyword CreateFromGuid(
IProject project, Guid guid)
49 return new Keyword(project, guid);
53 internal class Keyword : PartialRedDotProjectObject, IKeyword
55 private ICategory _category;
57 internal Keyword(IProject project, XmlElement xmlElement) : base(project, xmlElement)
62 internal Keyword(IProject project, Guid guid) : base(project, guid)
66 public ICategory Category
68 get {
return LazyLoad(ref _category); }
69 internal set { _category = value; }
74 const string SAVE_KEYWORD =
@"<PROJECT><KEYWORD action=""save"" guid=""{0}"" value=""{1}""/></PROJECT>";
76 string htmlEncodedName = HttpUtility.HtmlEncode(Name);
77 var xmlDoc = Project.ExecuteRQL(SAVE_KEYWORD.RQLFormat(
this, htmlEncodedName));
79 const string KEYWORD_XPATH_TEMPLATE =
"/IODATA/KEYWORD[@value='{0}' and @guid='{1}']";
80 string keywordXPath = KEYWORD_XPATH_TEMPLATE.RQLFormat(htmlEncodedName,
this);
81 var keyword = xmlDoc.SelectSingleNode(keywordXPath);
85 string.Format(
"Could not rename keyword to '{0}'", Name));
91 const string DELETE_KEYWORD =
@"<KEYWORD action=""delete"" guid=""{0}"" force=""0""/>";
93 var xmlDoc = Project.ExecuteRQL(DELETE_KEYWORD.RQLFormat(
this),
RqlType.SessionKeyInProject);
94 var keyword = (XmlElement) xmlDoc.SelectSingleNode(
"/IODATA/KEYWORD");
98 throw new SmartAPIException(Session.ServerLogin,
string.Format(
"Could not delete keyword {0}",
this));
101 if (IsKeywordStillUsed(keyword))
105 "Could not delete keyword {0}, because it is still used for page connections",
109 Category.Keywords.InvalidateCache();
112 public void DeleteForcibly()
114 const string DELETE_KEYWORD =
@"<KEYWORD action=""delete"" guid=""{0}"" force=""1"" password=""{1}"" />";
117 Project.ExecuteRQL(DELETE_KEYWORD.RQLFormat(
this, Project.Session.ServerLogin.AuthData.Password),
119 var keyword = (XmlElement) xmlDoc.SelectSingleNode(
"/IODATA/KEYWORD");
123 throw new SmartAPIException(Session.ServerLogin,
string.Format(
"Could not delete keyword {0}",
this));
126 Category.Keywords.InvalidateCache();
129 public void Rename(
string newKeywordName)
132 Name = newKeywordName;
143 protected override void LoadWholeObject()
148 protected override XmlElement RetrieveWholeObject()
150 const string LOAD_KEYWORD =
151 @"<PROJECT><CATEGORY><KEYWORD action=""load"" guid=""{0}""/></CATEGORY></PROJECT>";
152 return (XmlElement) Project.ExecuteRQL(LOAD_KEYWORD.RQLFormat(
this)).GetElementsByTagName(
"KEYWORD")[0];
155 private static bool IsKeywordStillUsed(XmlElement keyword)
157 return keyword.GetIntAttributeValue(
"countkeywordonpag").GetValueOrDefault() > 0 ||
158 keyword.GetIntAttributeValue(
"countkeywordonpge") > 0 ||
159 keyword.GetIntAttributeValue(
"countkeywordonpgeandpag") > 0;
162 private void LoadXml()
164 Name = _xmlElement.GetAttributeValue(
"value");
165 InitIfPresent(ref _category,
"categoryguid", x =>
new Category(Project, Guid.Parse(x)));
166 if (_category == null)
168 var keywordsNode = XmlElement.ParentNode as XmlElement;
169 if (keywordsNode == null)
173 var categoryNode = keywordsNode.ParentNode as XmlElement;
174 if (categoryNode == null || categoryNode.Name !=
"CATEGORY")
179 _category =
new Category(Project, categoryNode);