Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Client;
- using Microsoft.Xrm.Sdk.Query;
- using Sb.Common;
- using Sb.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GlobalActions
- {
- public class SearchPlots : IPlugin
- {
- private IOrganizationService _service;
- private ITracingService tracingService;
- public void Execute(IServiceProvider serviceProvider)
- {
- try
- {
- var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
- var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
- tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
- _service = factory.CreateOrganizationService(context.UserId);
- //tracingService.Trace("SearchPlots action started at: {0}", DateTime.Now);
- var target = (EntityReference)context.InputParameters["Target"];
- //tracingService.Trace("TargetId: {0}", target.Id);
- var plotlist = GetPlots(target.Id);
- //tracingService.Trace("Plots count: {0}", plotlist.Count());
- var searchTemplateId = CreateSearchTemplate(target.Id);
- AssociatePlots(searchTemplateId, plotlist);
- //tracingService.Trace("SearchTemplateId: {0}", searchTemplateId);
- context.OutputParameters["SearchTemplateId"] = searchTemplateId;
- tracingService.Trace("SearchPlots action finished at: {0}", DateTime.Now);
- }
- catch (InvalidPluginExecutionException ex)
- {
- throw ex;
- }
- }
- private Guid CreateSearchTemplate(Guid targetId) =>
- CreateSearchTemplate(new EntityReference(Helper.Lead.LogicalName, targetId));
- private Guid CreateSearchTemplate(EntityReference targetRef)
- {
- var initializeFormRequest = new InitializeFromRequest
- {
- EntityMoniker = targetRef,
- TargetEntityName = Helper.SearchTemplate.LogicalName
- };
- var initializedEntity = ((InitializeFromResponse)_service.Execute(initializeFormRequest)).Entity;
- return _service.Create(initializedEntity);
- }
- private void AssociatePlots(Guid searchTemplateId, IEnumerable<Guid> plotList)
- {
- if (plotList.Count() == 0)
- {
- return;
- }
- var entityReferenceCollection = new EntityReferenceCollection();
- foreach (var id in plotList)
- {
- entityReferenceCollection.Add(new EntityReference(Helper.Plot.LogicalName, id));
- }
- _service.Associate(Helper.SearchTemplate.LogicalName, searchTemplateId,
- new Relationship(Helper.SearchTemplate.Relationships.SearchTemplatePlots), entityReferenceCollection);
- }
- private IEnumerable<Guid> GetPlots(Guid leadId)
- {
- var targetLead = _service.Retrieve(Helper.Lead.LogicalName, leadId,
- new ColumnSet(
- Helper.Lead.Fields.City,
- Helper.Lead.Fields.NumberOfBedRooms,
- Helper.Lead.Fields.FloorFrom,
- Helper.Lead.Fields.FloorTill,
- Helper.Lead.Fields.AreaFrom,
- Helper.Lead.Fields.AreaTill,
- Helper.Lead.Fields.BudgetAmount,
- Helper.Lead.Fields.RealtyType
- ));
- var houseModels = RetrieveHouseModels(leadId);
- var projects = RetrieveProjects(leadId);
- var plotsQuery = new QueryExpression(Helper.Plot.LogicalName)
- {
- Criteria = new FilterExpression(LogicalOperator.And)
- };
- if (houseModels.Any())
- plotsQuery.Criteria.AddCondition(Helper.Plot.Fields.HouseModels,
- ConditionOperator.In, houseModels.Cast<object>().ToArray());
- if (projects.Any())
- plotsQuery.Criteria.AddCondition(Helper.Plot.Fields.ProjectId,
- ConditionOperator.In, projects.Cast<object>().ToArray());
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.City,
- Helper.Plot.Fields.City, ConditionOperator.Equal);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.NumberOfBedRooms,
- Helper.Plot.Fields.NumberOfBedrooms, ConditionOperator.Equal);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.FloorFrom,
- Helper.Plot.Fields.FloorNumber, ConditionOperator.GreaterEqual);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.FloorTill,
- Helper.Plot.Fields.FloorNumber, ConditionOperator.LessEqual);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.AreaFrom,
- Helper.Plot.Fields.ProjectArea, ConditionOperator.GreaterEqual);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.AreaTill,
- Helper.Plot.Fields.ProjectArea, ConditionOperator.LessEqual);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.BudgetAmount,
- Helper.Plot.Fields.BasePrice, ConditionOperator.LessEqual);
- ApplyConditionIfContains(targetLead, plotsQuery, Helper.Lead.Fields.RealtyType,
- Helper.Plot.Fields.HouseType, ConditionOperator.Equal);
- var plots = _service.RetrieveMultipleByQuery(plotsQuery).Select(item => item.Id);
- return plots;
- }
- private static void ApplyConditionIfContains(Entity entity, QueryExpression query,
- string sourceAttribute, string targetAttribute, ConditionOperator @operator)
- {
- if (!entity.Contains(sourceAttribute)) return;
- var value = entity[sourceAttribute];
- if (value is EntityReference entityReference)
- query.Criteria.AddCondition(targetAttribute, @operator, entityReference.Id);
- else
- if (value is Money money)
- query.Criteria.AddCondition(targetAttribute, @operator, money.Value);
- else
- if (value is OptionSetValue optionSet)
- query.Criteria.AddCondition(targetAttribute, @operator, optionSet.Value);
- else
- query.Criteria.AddCondition(targetAttribute, @operator, value);
- }
- private List<Guid> RetrieveHouseModels(Guid leadId)
- {
- var houseModelsQuery = new QueryExpression(Helper.HouseModel.LogicalName);
- var leadRelationshipLink = houseModelsQuery.AddLink(Helper.Lead.Relationships.LeadHouseModel,
- Helper.HouseModel.Fields.Id, Helper.HouseModel.Fields.Id);
- leadRelationshipLink.LinkCriteria.AddCondition(Helper.Lead.Id, ConditionOperator.Equal, leadId);
- return _service.RetrieveMultipleByQuery(houseModelsQuery).Select(item => item.Id).Distinct().ToList();
- }
- private List<Guid> RetrieveProjects(Guid leadId)
- {
- var projectsQuery = new QueryExpression(Helper.Project.LogicalName);
- var leadRelationshipLink = projectsQuery.AddLink(Helper.Lead.Relationships.LeadProject,
- Helper.Project.Fields.Id, Helper.Project.Fields.Id);
- leadRelationshipLink.LinkCriteria.AddCondition(Helper.Lead.Id, ConditionOperator.Equal, leadId);
- return _service.RetrieveMultipleByQuery(projectsQuery).Select(item => item.Id).Distinct().ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement