Skip to content

Commit

Permalink
Resolves #65 #64
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdurow committed Aug 18, 2015
1 parent 4cf2b93 commit ffdaeca
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ private static void InitLocalisedContent()
QueryParser queryParser = new QueryParser(new string[] {"connection"});
queryParser.GetView("connection", defaultView);
queryParser.QueryMetadata();
EntityQuery connectionViews = queryParser.EntityLookup["connection"];
string viewName = connectionViews.Views.Keys[0];
FetchQuerySettings view = connectionViews.Views[viewName];

// Get the columsn for the view
EntityQuery connectionViews = queryParser.EntityLookup["connection"];
FetchQuerySettings view = connectionViews.Views[connectionViews.Views.Keys[0]];
string fetchXml = queryParser.GetFetchXmlParentFilter(view, "record1id");
vm = new ConnectionsViewModel(parent, entities.Split(","), pageSize, fetchXml);
vm = new ConnectionsViewModel(parent, entities.Split(","), pageSize, view);

// Bind Connections grid
GridDataViewBinder connectionsGridDataBinder = new GridDataViewBinder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,32 @@ public class ConnectionsViewModel : ViewModelBase
public DependentObservable<bool> AllowAddNew;
public Observable<EntityReference> ParentRecordId = Knockout.Observable<EntityReference>();
private string _viewFetchXml;
private SortCol _defaultSortCol;
#endregion

#region Constructors
public ConnectionsViewModel(EntityReference parentRecordId, string[] connectToTypes, int pageSize, string viewFetchXml)
public ConnectionsViewModel(EntityReference parentRecordId, string[] connectToTypes, int pageSize, FetchQuerySettings view)
{
Connections = new EntityDataViewModel(pageSize, typeof(Connection), true);
if (view != null)
{
_viewFetchXml = QueryParser.GetFetchXmlParentFilter(view, "record1id");
// Set initial sort
_defaultSortCol=new SortCol(view.OrderByAttribute, !view.OrderByDesending);
}

ParentRecordId.SetValue(parentRecordId);
_viewFetchXml = viewFetchXml;

ObservableConnection connection = new ObservableConnection(connectToTypes);
connection.Record2Id.SetValue(parentRecordId);
ConnectionEdit = (Observable<ObservableConnection>)ValidatedObservableFactory.ValidatedObservable(connection);

Connections.OnDataLoaded.Subscribe(Connections_OnDataLoaded);
ConnectionEdit.GetValue().OnSaveComplete += ConnectionsViewModel_OnSaveComplete;
ObservableConnection.RegisterValidation(Connections.ValidationBinder);
AllowAddNew = Knockout.DependentObservable<bool>(AllowAddNewComputed);
}

#endregion

#region Event Handlers
Expand Down Expand Up @@ -76,7 +85,15 @@ private void connection_PropertyChanged(object sender, Xrm.ComponentModel.Proper
switch (e.PropertyName)
{
case "record2roleid":
// Check if the record1id is loaded - if not load it now so we can work out the opposite role
if (updated.Record1Id == null)
{
Connection connection = (Connection) OrganizationServiceProxy.Retrieve(Connection.LogicalName,updated.ConnectionID.Value,new string[] {"record1id"});
updated.Record1Id = connection.Record1Id;
}
connectionToUpdate.Record2RoleId = updated.Record2RoleId;
connectionToUpdate.Record1RoleId = ObservableConnection.GetOppositeRole(updated.Record2RoleId, updated.Record1Id);

updateRequired = true;
break;
case "description":
Expand Down Expand Up @@ -145,13 +162,14 @@ public void Search()
{3}
</entity>
</fetch>";
Connections.Refresh();
}
else
{
Connections.FetchXml = _viewFetchXml.Replace(QueryParser.ParentRecordPlaceholder, parentRecordId);

Connections.SortBy(_defaultSortCol);
}
Connections.Refresh();

}

[PreserveCase]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ public static void RoleSearch(string term, Action<EntityCollection> callback,str
if (typeName != null)
{
// find the entity type code from the type name
int? etc = (int?)Script.Literal("Mscrm.EntityPropUtil.EntityTypeName2CodeMap[{0}]", typeName);
int? etc = GetEntityTypeCodeFromName(typeName);
// Filter by the currently select role
recordTypeFilter = String.Format(@"
<filter>
<filter type='or'>
<condition attribute='associatedobjecttypecode' operator='eq' value='{0}' />
<condition attribute='associatedobjecttypecode' operator='eq' value='0' />
</filter>", etc);
}
string fetchXml = @"
Expand All @@ -151,7 +152,7 @@ public static void RoleSearch(string term, Action<EntityCollection> callback,str
<link-entity name='connectionroleobjecttypecode' from='connectionroleid' to='connectionroleid' >
{1}
</link-entity>
<filter>
<filter type='and'>
<condition attribute='name' operator='like' value='%{0}%' />
</filter>
</entity>
Expand Down Expand Up @@ -184,7 +185,10 @@ public void SaveCommand()
connection.Record1RoleId = Record1RoleId.GetValue();
connection.Record2RoleId = Record2RoleId.GetValue();

OrganizationServiceProxy.BeginCreate(connection,delegate(object state)
EntityReference oppositeRole = GetOppositeRole(connection.Record1RoleId, connection.Record2Id);
connection.Record2RoleId = oppositeRole;

OrganizationServiceProxy.BeginCreate(connection, delegate(object state)
{
try
{
Expand All @@ -193,8 +197,8 @@ public void SaveCommand()
Record1Id.SetValue(null);
Record1RoleId.SetValue(null);
((IValidatedObservable)(object)this).Errors.ShowAllMessages(false);


}
catch (Exception ex)
{
Expand All @@ -205,8 +209,57 @@ public void SaveCommand()
{
this.IsBusy.SetValue(false);
}

});

}

private static int? GetEntityTypeCodeFromName(string typeName)
{
int? etc = (int?)Script.Literal("Mscrm.EntityPropUtil.EntityTypeName2CodeMap[{0}]", typeName);
return etc;
}

public static EntityReference GetOppositeRole(EntityReference role, EntityReference record)
{
EntityReference oppositeRole = null;
int? etc = GetEntityTypeCodeFromName(record.LogicalName);

// Add the opposite connection role
string getOppositeRole = String.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' count='1'>
<entity name='connectionrole'>
<attribute name='category' />
<attribute name='name' />
<attribute name='connectionroleid' />
<attribute name='statecode' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='connectionroleassociation' from='connectionroleid' to='connectionroleid' intersect='true'>
<link-entity name='connectionrole' from='connectionroleid' to='associatedconnectionroleid' alias='ad'>
<filter type='and'>
<condition attribute='connectionroleid' operator='eq' value='{0}' />
</filter>
</link-entity>
<link-entity name='connectionroleobjecttypecode' from='connectionroleid' to='connectionroleid' intersect='true' >
<filter type='or' >
<condition attribute='associatedobjecttypecode' operator='eq' value='{1}' />
<condition attribute='associatedobjecttypecode' operator='eq' value='0' /> <!-- All types-->
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>", role.Id.ToString(), etc);



EntityCollection results = (EntityCollection)OrganizationServiceProxy.RetrieveMultiple(getOppositeRole);

if (results.Entities.Count > 0)
{
oppositeRole = results.Entities[0].ToEntityReference();
}
return oppositeRole;
}

[PreserveCase]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public string GetFetchXmlForQuery(string entityLogicalName, string queryName, st
fetchXml = fetchXml.Replace("#Query#", XmlHelper.Encode(searchTerm));
return fetchXml;
}
public string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAttribute)
public static string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAttribute)
{

jQueryObject fetchElement = query.FetchXml.Find("fetch");
Expand All @@ -387,6 +387,10 @@ public string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAtt
fetchElement.Attribute("distinct", "true");
fetchElement.Attribute("no-lock", "true");
jQueryObject orderByElement = fetchElement.Find("order");
// Get the default order by field - currently only supports a single sort by column
query.OrderByAttribute = orderByElement.GetAttribute("attribute");
query.OrderByDesending = orderByElement.GetAttribute("descending") == "true";

orderByElement.Remove();

// Get the root filter (if there is one)
Expand Down Expand Up @@ -431,7 +435,8 @@ public class FetchQuerySettings
public jQueryObject FetchXml;
public EntityQuery RootEntity;
public Observable<string> RecordCount;

public string OrderByAttribute;
public bool OrderByDesending;

}

Expand Down
Loading

0 comments on commit ffdaeca

Please sign in to comment.