PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/1.31/src/Platform/Services/OrganizationService.cs

#
C# | 247 lines | 163 code | 33 blank | 51 comment | 53 complexity | ef137a51d6306b64db92293847c179cd MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. /****************************************************************************************************
  2. Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
  3. Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ****************************************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.Collections.Specialized;
  20. using System.Configuration;
  21. using System.Data;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using System.ServiceModel;
  26. using System.ServiceModel.Activation;
  27. using System.ServiceModel.Web;
  28. using System.Web;
  29. using System.Web.Security;
  30. using System.Web.SessionState;
  31. using System.Web.UI;
  32. using System.Web.UI.HtmlControls;
  33. using System.Web.UI.WebControls;
  34. using System.Web.UI.WebControls.WebParts;
  35. using System.Xml.Linq;
  36. using BaoJianSoft.Common;
  37. using BaoJianSoft.Common.Caching;
  38. using BaoJianSoft.Common.Data;
  39. using BaoJianSoft.Common.Web;
  40. using BaoJianSoft.Platform.Initialization;
  41. using BaoJianSoft.Platform.Linq;
  42. namespace BaoJianSoft.Platform.Services
  43. {
  44. /// <summary>
  45. /// The service implementation to search accessible organizations for current authenticated user.
  46. /// </summary>
  47. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  48. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  49. public class OrganizationService : IOrganizationService, IRequiresSessionState
  50. {
  51. private static IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
  52. private static IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
  53. private static IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
  54. private static IPermissionApi permissionApi = SpringContext.Current.GetObject<IPermissionApi>();
  55. private static IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  56. /// <summary>
  57. /// Search organizations by a collection of criterias for the authenticated user of request.
  58. /// </summary>
  59. /// <param name="domain">Which domain of the searching organizations.</param>
  60. /// <param name="orgTypeId">Which organization type the searching organizations should belong to.</param>
  61. /// <param name="q">Keywords for searching.</param>
  62. /// <param name="sortDirection">Sorting field name, the default sorting field is LastUpdatedDate.</param>
  63. /// <param name="sortOrder">Sorting order, DESC or ASC, the default sorting order is DESC.</param>
  64. /// <param name="start">The start organization index of hit to return.</param>
  65. /// <param name="limit">The limit of returned organizations.</param>
  66. /// <returns>The query results object includes total hit count, returned records, start and limit.</returns>
  67. public QueryResultObject SearchJson(string domain, string orgTypeId, string q, string sortDirection, string sortOrder, int start, int limit)
  68. {
  69. #region Arguments Validation
  70. if (!authenticationContext.Identity.IsAuthenticated)
  71. throw new BadRequestException("The access is not authenticated.");
  72. Guid userId = authenticationContext.User.UserId;
  73. if (string.IsNullOrEmpty(domain))
  74. throw new BadRequestException("The parameter \"domain\" is not specified.");
  75. if (!platformConfiguration.Domains.Select(d => d.Value).Contains(domain))
  76. throw new BadRequestException("The value of parameter \"domain\" is invalid.");
  77. string sortOrderValue = "DESC";
  78. if (!Kit.IsEmpty(sortOrder))
  79. {
  80. sortOrderValue = sortOrder.ToUpperInvariant();
  81. if (sortOrderValue != "ASC" && sortOrderValue != "DESC")
  82. throw new BadRequestException("The value of parameter \"sortOrder\" is invalid. The candidate value are ASC and DESC.");
  83. }
  84. string sortFieldValue = "LastUpdatedDate";
  85. if (!Kit.IsEmpty(sortDirection))
  86. sortFieldValue = sortDirection;
  87. string orderby = sortFieldValue + " " + sortOrderValue;
  88. int pageIndex = start / limit;
  89. int pageSize = limit;
  90. Guid orgTypeIdValue = Guid.Empty;
  91. try
  92. {
  93. orgTypeIdValue = new Guid(orgTypeId);
  94. }
  95. catch
  96. {
  97. }
  98. #endregion
  99. QueryResultObject queryResultObject = new QueryResultObject { TotalRecordCount = 0, Start = start, Limit = limit, Records = new Collection<object>() };
  100. int recordCount;
  101. Expression<Func<Organization, bool>> predicate;
  102. IEnumerable<OrganizationObject> organizations;
  103. if (Kit.IsEmpty(q))
  104. {
  105. predicate = organization => organization.OrganizationType.Domain == domain
  106. && organization.Status == OrganizationStatus.Enabled;
  107. if (orgTypeIdValue != Guid.Empty)
  108. predicate = predicate.And(org => org.OrganizationTypeId == orgTypeIdValue);
  109. organizations = organizationApi.FindOrganizations(predicate, null, orderby, pageIndex, pageSize, out recordCount, null);
  110. if (recordCount > 0)
  111. {
  112. queryResultObject.TotalRecordCount = recordCount;
  113. queryResultObject.Records = organizations.ToList();
  114. }
  115. }
  116. else
  117. {
  118. predicate = organization => organization.Status == OrganizationStatus.Enabled
  119. && (organization.OrganizationCode.StartsWith(q) || organization.OrganizationName.Contains(q))
  120. && organization.OrganizationType.Domain == domain;
  121. if (orgTypeIdValue != Guid.Empty)
  122. predicate = predicate.And(org => org.OrganizationTypeId == orgTypeIdValue);
  123. organizations = organizationApi.FindOrganizations(predicate, null, orderby, pageIndex, pageSize, out recordCount);
  124. if (recordCount > 0)
  125. {
  126. queryResultObject.TotalRecordCount = recordCount;
  127. queryResultObject.Records = organizations.ToList();
  128. }
  129. }
  130. return queryResultObject;
  131. }
  132. /// <summary>
  133. /// Get first organization matching the specified query.
  134. /// The matching algorithm is to try to search organizations in following order. Once an organization is found, it's returned as the result.
  135. /// 1) completely match organization code;
  136. /// 2) completely match organization name;
  137. /// 3) match whether there has organizations with code starts with specified query;
  138. /// 4) match whether there has organizations with name starts with specified query;
  139. /// </summary>
  140. /// <param name="domain"></param>
  141. /// <param name="q"></param>
  142. /// <returns></returns>
  143. public OrganizationObject GetJson(string domain, string q)
  144. {
  145. if (!authenticationContext.Identity.IsAuthenticated)
  146. throw new BadRequestException("The access is not authenticated.");
  147. Guid userId = authenticationContext.User.UserId;
  148. if (string.IsNullOrEmpty(domain))
  149. throw new BadRequestException("The parameter \"domain\" is not specified.");
  150. if (!platformConfiguration.Domains.Select(d => d.Value).Contains(domain))
  151. throw new BadRequestException("The value of parameter \"domain\" is invalid.");
  152. int recordCount;
  153. IEnumerable<OrganizationTypeObject> organizationTypes = organizationApi.FindOrganizationTypes(new[] { domain });
  154. IEnumerable<Guid> organizationTypeIds = organizationTypes.Select(ct => ct.OrganizationTypeId);
  155. Expression<Func<Organization, bool>> predicate = org => org.Status == OrganizationStatus.Enabled
  156. && org.OrganizationType.Domain == domain
  157. && org.OrganizationCode == q;
  158. IEnumerable<OrganizationObject> organizations = organizationApi.FindOrganizations(predicate, null, "OrganizationCode", 0, 1, out recordCount);
  159. if (recordCount > 0)
  160. return organizations.FirstOrDefault();
  161. predicate = org => org.Status == OrganizationStatus.Enabled
  162. && org.OrganizationType.Domain == domain
  163. && org.OrganizationName == q;
  164. organizations = organizationApi.FindOrganizations(predicate, null, "OrganizationName", 0, 1, out recordCount);
  165. if (recordCount > 0)
  166. return organizations.FirstOrDefault();
  167. predicate = org => org.Status == OrganizationStatus.Enabled
  168. && org.OrganizationType.Domain == domain
  169. && org.OrganizationCode.StartsWith(q);
  170. organizations = organizationApi.FindOrganizations(predicate, null, "OrganizationCode", 0, 1, out recordCount);
  171. if (recordCount > 0)
  172. return organizations.FirstOrDefault();
  173. predicate = org => org.Status == OrganizationStatus.Enabled
  174. && org.OrganizationType.Domain == domain
  175. && org.OrganizationName.Contains(q);
  176. organizations = organizationApi.FindOrganizations(predicate, null, "OrganizationName", 0, 1, out recordCount);
  177. if (recordCount > 0)
  178. return organizations.FirstOrDefault();
  179. return null;
  180. }
  181. /// <summary>
  182. /// Find organization types in specified domain.
  183. /// </summary>
  184. /// <param name="domain"></param>
  185. /// <returns></returns>
  186. public QueryResultObject FindOrganizationTypesJson(string domain)
  187. {
  188. if (!authenticationContext.Identity.IsAuthenticated)
  189. throw new BadRequestException("The access is not authenticated.");
  190. if (string.IsNullOrEmpty(domain))
  191. throw new BadRequestException("The \"domain\" is not specified in accessing URL.");
  192. IEnumerable<OrganizationTypeObject> organizationTypes = organizationApi.FindOrganizationTypes(new[] { domain });
  193. return new QueryResultObject
  194. {
  195. Limit = int.MaxValue,
  196. Start = 0,
  197. TotalRecordCount = organizationTypes.Count(),
  198. Records = organizationTypes
  199. };
  200. }
  201. /// <summary>
  202. /// Lists all available organization domains.
  203. /// </summary>
  204. /// <returns></returns>
  205. public Collection<OrganizationDomainObject> ListDomainsJson()
  206. {
  207. IEnumerable<OrganizationDomainObject> results = platformConfiguration.Domains.Select(d => new OrganizationDomainObject { Text = d.Text, Value = d.Value });
  208. return new Collection<OrganizationDomainObject>(results.ToList());
  209. }
  210. }
  211. }