PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/CompositeC1/Composite/Plugins/Elements/ElementProviders/UserControlFunctionElementProvider/UserControlFunctionElementProvider.cs

#
C# | 245 lines | 188 code | 37 blank | 20 comment | 9 complexity | 0e8b16b64377268ada2240e783be1b87 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * The contents of this web application are subject to the Mozilla Public License Version
  3. * 1.1 (the "License"); you may not use this web application except in compliance with
  4. * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/.
  5. *
  6. * Software distributed under the License is distributed on an "AS IS" basis,
  7. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. * for the specific language governing rights and limitations under the License.
  9. *
  10. * The Original Code is owned by and the Initial Developer of the Original Code is
  11. * Composite A/S (Danish business reg.no. 21744409). All Rights Reserved
  12. *
  13. * Section 11 of the License is EXPRESSLY amended to include a provision stating
  14. * that any dispute, including but not limited to disputes related to the enforcement
  15. * of the License, to which Composite A/S as owner of the Original Code, as Initial
  16. * Developer or in any other role, becomes a part to shall be governed by Danish law
  17. * and be initiated before the Copenhagen City Court ("K�benhavns Byret")
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Configuration;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Linq;
  24. using Composite.AspNet.Security;
  25. using Composite.C1Console.Elements;
  26. using Composite.C1Console.Elements.Plugins.ElementProvider;
  27. using Composite.C1Console.Security;
  28. using Composite.C1Console.Workflow;
  29. using Composite.Core.ResourceSystem;
  30. using Composite.Functions;
  31. using Composite.Plugins.Elements.ElementProviders.BaseFunctionProviderElementProvider;
  32. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
  33. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
  34. using Microsoft.Practices.ObjectBuilder;
  35. using Texts = Composite.Core.ResourceSystem.LocalizationFiles.Composite_Plugins_UserControlFunction;
  36. namespace Composite.Plugins.Elements.ElementProviders.UserControlFunctionElementProvider
  37. {
  38. [ConfigurationElementType(typeof(UserControlFunctionProviderElementProviderData))]
  39. internal class UserControlFunctionElementProvider : BaseFunctionProviderElementProvider.BaseFunctionProviderElementProvider
  40. {
  41. private static readonly ActionGroup PrimaryActionGroup = new ActionGroup(ActionGroupPriority.PrimaryHigh);
  42. protected static ResourceHandle AddFunctionIcon { get { return GetIconHandle("usercontrol-function-add"); } }
  43. protected static ResourceHandle EditFunctionIcon { get { return GetIconHandle("usercontrol-function-edit"); } }
  44. protected static ResourceHandle DeleteFunctionIcon { get { return GetIconHandle("usercontrol-function-delete"); } }
  45. private readonly string _functionProviderName;
  46. private readonly string _rootLabel;
  47. public UserControlFunctionElementProvider(string functionProvider, string rootLabel)
  48. {
  49. _functionProviderName = functionProvider;
  50. _rootLabel = rootLabel;
  51. }
  52. public override string FunctionProviderName
  53. {
  54. get { return _functionProviderName; }
  55. }
  56. protected override IEnumerable<IFunctionTreeBuilderLeafInfo> OnGetFunctionInfos(SearchToken searchToken)
  57. {
  58. var functions = FunctionFacade.GetFunctionsByProvider(_functionProviderName);
  59. if (searchToken != null && !String.IsNullOrEmpty(searchToken.Keyword))
  60. {
  61. string keyword = searchToken.Keyword.ToLowerInvariant();
  62. functions = functions.Where(f => f.Namespace.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) > 0
  63. || f.Name.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) > 0);
  64. }
  65. return functions.Select(f => new UserControlFunctionTreeBuilderLeafInfo(f));
  66. }
  67. protected override IEnumerable<Type> OnGetEntityTokenTypes()
  68. {
  69. return new[] { typeof(FileBasedFunctionEntityToken) };
  70. }
  71. protected override IFunctionTreeBuilderLeafInfo OnIsEntityOwner(EntityToken entityToken)
  72. {
  73. if (entityToken is FileBasedFunctionEntityToken && entityToken.Source == _functionProviderName)
  74. {
  75. string functionFullName = entityToken.Id;
  76. IFunction function = FunctionFacade.GetFunctionsByProvider(_functionProviderName)
  77. .FirstOrDefault(func => func.Namespace + "." + func.Name == functionFullName);
  78. return function == null ? null : new UserControlFunctionTreeBuilderLeafInfo(function);
  79. }
  80. return null;
  81. }
  82. private sealed class UserControlFunctionTreeBuilderLeafInfo : IFunctionTreeBuilderLeafInfo
  83. {
  84. private readonly IFunction _function;
  85. public UserControlFunctionTreeBuilderLeafInfo(IFunction function)
  86. {
  87. _function = function;
  88. }
  89. public string Name
  90. {
  91. get { return _function.Name; }
  92. }
  93. public string Namespace
  94. {
  95. get { return _function.Namespace; }
  96. }
  97. public EntityToken EntityToken
  98. {
  99. get { return _function.EntityToken; }
  100. }
  101. }
  102. /// <exclude />
  103. protected override IEnumerable<ElementAction> OnGetFolderActions()
  104. {
  105. Type workflow = WorkflowFacade.GetWorkflowType("Composite.Plugins.Elements.ElementProviders.UserControlFunctionProviderElementProvider.AddNewUserControlFunctionWorkflow");
  106. return new [] { new ElementAction(new ActionHandle(new WorkflowActionToken(workflow, new [] { PermissionType.Add }))) {
  107. VisualData = new ActionVisualizedData {
  108. Label = Texts.AddNewUserControlFunction_Label,
  109. ToolTip = Texts.AddNewUserControlFunction_ToolTip,
  110. Icon = AddFunctionIcon,
  111. Disabled = false,
  112. ActionLocation = new ActionLocation {
  113. ActionType = ActionType.Edit,
  114. IsInFolder = false,
  115. IsInToolbar = true,
  116. ActionGroup = PrimaryActionGroup
  117. }
  118. }
  119. }
  120. };
  121. }
  122. /// <exclude />
  123. protected override IEnumerable<ElementAction> OnGetFunctionActions(IFunctionTreeBuilderLeafInfo function)
  124. {
  125. var editWorkflow = WorkflowFacade.GetWorkflowType("Composite.Plugins.Elements.ElementProviders.UserControlFunctionProviderElementProvider.EditUserControlFunctionWorkflow");
  126. var deleteWorkflow = WorkflowFacade.GetWorkflowType("Composite.Plugins.Elements.ElementProviders.UserControlFunctionProviderElementProvider.DeleteUserControlFunctionWorkflow");
  127. return new ElementAction[]
  128. {
  129. new ElementAction(new ActionHandle(
  130. new WorkflowActionToken(
  131. editWorkflow, new [] { PermissionType.Edit }
  132. ))) {
  133. VisualData = new ActionVisualizedData {
  134. Label = Texts.EditUserControlFunction_Label,
  135. ToolTip = Texts.EditUserControlFunction_ToolTip,
  136. Icon = EditFunctionIcon,
  137. Disabled = false,
  138. ActionLocation = new ActionLocation {
  139. ActionType = ActionType.Edit,
  140. IsInFolder = false,
  141. IsInToolbar = true,
  142. ActionGroup = PrimaryActionGroup
  143. }
  144. }
  145. },
  146. new ElementAction(new ActionHandle(
  147. new WorkflowActionToken(
  148. deleteWorkflow, new [] { PermissionType.Delete }
  149. ){Payload = GetContext().ProviderName})) {
  150. VisualData = new ActionVisualizedData {
  151. Label = Texts.DeleteUserControlFunction_Label,
  152. ToolTip = Texts.DeleteUserControlFunction_ToolTip,
  153. Icon = DeleteFunctionIcon,
  154. Disabled = false,
  155. ActionLocation = new ActionLocation {
  156. ActionType = ActionType.Delete,
  157. IsInFolder = false,
  158. IsInToolbar = true,
  159. ActionGroup = PrimaryActionGroup
  160. }
  161. }
  162. }
  163. };
  164. }
  165. #region Configuration
  166. internal sealed class UserControlFunctionElementProviderAssembler : IAssembler<IHooklessElementProvider, HooklessElementProviderData>
  167. {
  168. [SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
  169. public IHooklessElementProvider Assemble(IBuilderContext context, HooklessElementProviderData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
  170. {
  171. var data = (UserControlFunctionProviderElementProviderData)objectConfiguration;
  172. return new UserControlFunctionElementProvider(data.UserControlFunctionProviderName, data.Label);
  173. }
  174. }
  175. [Assembler(typeof(UserControlFunctionElementProviderAssembler))]
  176. internal sealed class UserControlFunctionProviderElementProviderData : HooklessElementProviderData
  177. {
  178. private const string _UserControlFunctionProviderNameProperty = "userControlFunctionProviderName";
  179. [ConfigurationProperty(_UserControlFunctionProviderNameProperty, IsRequired = true)]
  180. public string UserControlFunctionProviderName
  181. {
  182. get { return (string)base[_UserControlFunctionProviderNameProperty]; }
  183. set { base[_UserControlFunctionProviderNameProperty] = value; }
  184. }
  185. private const string _labelProperty = "label";
  186. [ConfigurationProperty(_labelProperty, DefaultValue = null)]
  187. public string Label
  188. {
  189. get { return (string)base[_labelProperty]; }
  190. set { base[_labelProperty] = value; }
  191. }
  192. }
  193. #endregion Configuration
  194. protected override string RootFolderLabel
  195. {
  196. get
  197. {
  198. return !string.IsNullOrEmpty(_rootLabel)
  199. ? StringResourceSystemFacade.ParseString(_rootLabel)
  200. : Texts.RootElement_Label;
  201. }
  202. }
  203. protected override string RootFolderToolTip
  204. {
  205. get { return Texts.RootElement_ToolTip; }
  206. }
  207. }
  208. }