/// <summary> /// <para> /// This method requests the server to remove elements from the list. The elements to be removed are those that /// have been tagged for removal by the IXiValue PrepForRemove() method. The PrepForRemove() is called individually /// on each list element to be removed, and followed by the CommitRemoveableElements(). /// </para> /// <para> /// The CommitRemoveableElements() method loops through the list to find the elements that have been prepared /// for removal and makes a single call to the server to have them removed from the server's list. /// </para> /// </summary> /// <returns> The list of elements that could not be removed from the server list or null is call to server failed.</returns> protected IEnumerable <TXiDataAndDataJournalListItemBase>?CommitRemoveItemsInternal() { if (Disposed) { throw new ObjectDisposedException("Cannot access a disposed XiDataAndDataJournalListBase."); } // find all values in the client's list that have been prep'd for removal // and add them to the serverAliasesToRemove list and to the // clientValuesToRemove list var serverAliasesToRemove = new List <uint>(); var listItemsToRemove = new List <TXiDataAndDataJournalListItemBase>(); foreach (TXiDataAndDataJournalListItemBase listItem in ListItemsManager) { if (listItem.IsInServerList && listItem.PreparedForRemove) { listItemsToRemove.Add(listItem); serverAliasesToRemove.Add(listItem.ServerAlias); } listItem.PreparedForRemove = false; } var erroredXiValuesToReturn = new List <TXiDataAndDataJournalListItemBase>(); if (listItemsToRemove.Count > 0) { try { // Remove the items from the list in the server List <AliasResult>?aliasResultList = null; // a null list means all were successfully removed or are no longer defined in the server if (Context.ResourceManagement is not null) // if still connected to the server { aliasResultList = Context.RemoveDataObjectsFromList(ServerListId, serverAliasesToRemove); } // Remove each value from the client list unless there was an error and it could not be removed // if there were errors and the value could not be removed from the server if (aliasResultList is not null && aliasResultList.Count > 0) { foreach (TXiDataAndDataJournalListItemBase removedListItem in listItemsToRemove) { // look for the server alias since if the server did not find it, it will not return a client alias AliasResult?aliasResult = aliasResultList.Find(ar => ar.ServerAlias == removedListItem.ServerAlias); if (aliasResult is not null) { if (aliasResult.Result == XiFaultCodes.E_ALIASNOTFOUND) { // server doesn't have the item if result code is E_ALIASNOTFOUND, so ok to take it out here ListItemsManager.Remove(removedListItem.ClientAlias); removedListItem.IsInServerList = false; removedListItem.ClientAlias = 0; //IsInClientList = false; removedListItem.Dispose(); } else // otherwise the value was not deleted from the server, so add it to the list to return { removedListItem.ResultCode = aliasResult.Result; erroredXiValuesToReturn.Add(removedListItem); } } else // no error for this one, so remove it { ListItemsManager.Remove(removedListItem.ClientAlias); removedListItem.IsInServerList = false; removedListItem.ClientAlias = 0; //IsInClientList = false; removedListItem.Dispose(); } } } else // Otherwise, no errors, so remove them all from the client list { foreach (TXiDataAndDataJournalListItemBase listItem in listItemsToRemove) { ListItemsManager.Remove(listItem.ClientAlias); listItem.IsInServerList = false; listItem.ClientAlias = 0; //IsInClientList = false; listItem.Dispose(); } } }
/* * public void Func() * { * if (Disposed) throw new ObjectDisposedException("Cannot access a disposed XiDataListBase."); * }*/ #region protected functions /// <summary> /// This method requests the server to add elements to the list that have been added to the local ClientBase copy /// of the list. For example, after using the AddNewDataObjectToList() method add a set of data objects to the local /// ClientBase copy of the list, this method is called to add them to the server's copy of the list in a single call. /// </summary> /// <returns> The list of elements that were not added to the server or null is call to server failed.</returns> protected IEnumerable <TXiDataAndDataJournalListItemBase>?CommitAddItemsInternal() { var listInstanceIdsCollection = new List <ListInstanceId>(); foreach (TXiDataAndDataJournalListItemBase listItem in ListItemsManager) { if (!listItem.IsInServerList && listItem.PreparedForAdd) { var listInstanceId = new ListInstanceId { ObjectElementId = listItem.InstanceId, ClientAlias = listItem.ClientAlias, }; listInstanceIdsCollection.Add(listInstanceId); } listItem.PreparedForAdd = false; } var resultItems = new List <TXiDataAndDataJournalListItemBase>(); if (listInstanceIdsCollection.Count > 0) { try { List <AddDataObjectResult>?result = Context.AddDataObjectsToList(ServerListId, listInstanceIdsCollection); if (result is not null) { foreach (AddDataObjectResult r in result) { TXiDataAndDataJournalListItemBase?listItem = null; if (ListItemsManager.TryGetValue(r.ClientAlias, out listItem)) { listItem.ServerAlias = r.ServerAlias; listItem.ResultCode = r.Result; listItem.ValueTypeId = r.DataTypeId; listItem.IsReadable = true; listItem.IsWritable = true; if (listItem.ResultCode == XiFaultCodes.S_OK || listItem.ResultCode == XiFaultCodes.S_FALSE) { listItem.IsInServerList = true; listItem.Enabled = false; } else { ListItemsManager.Remove(listItem.ClientAlias); // remove values that the server failed to add listItem.ClientAlias = 0; //IsInClientList = false; resultItems.Add(listItem); } } } } } catch (Exception) { foreach (ListInstanceId ar in listInstanceIdsCollection) { ListItemsManager.Remove(ar.ClientAlias); // remove values that the server failed to add ar.ClientAlias = 0; //IsInClientList = false; } return(null); } GetListAttributes(); } return(resultItems); }
/// <summary> /// This method requests the server to add elements to the list that have been added to the local ClientBase copy /// of the list. For example, after using the AddNewDataObjectToList() method add a set of data objects to the local /// ClientBase copy of the list, this method is called to add them to the server's copy of the list in a single call. /// </summary> /// <returns> The list of elements that were not added to the server or null is call to server failed.</returns> protected IEnumerable <TClientElementListItemBase>?CommitAddItemsInternal() { var listInstanceIdsCollection = new List <ListItemInfo>(); foreach (TClientElementListItemBase listItem in ListItemsManager) { if (!listItem.IsInServerList && listItem.PreparedForAdd) { var listInstanceId = new ListItemInfo { ElementId = listItem.ElementId, ClientAlias = listItem.ClientAlias, }; listInstanceIdsCollection.Add(listInstanceId); } listItem.PreparedForAdd = false; } var resultItems = new List <TClientElementListItemBase>(); if (listInstanceIdsCollection.Count > 0) { try { List <AddItemToListResult> result = Context.AddItemsToList(ListServerAlias, listInstanceIdsCollection); foreach (AddItemToListResult r in result) { TClientElementListItemBase?listItem = null; if (ListItemsManager.TryGetValue(r.AliasResult.ClientAlias, out listItem)) { listItem.ServerAlias = r.AliasResult.ServerAlias; listItem.StatusCode = (StatusCode)r.AliasResult.StatusCode; listItem.ValueTypeId = r.DataTypeId; listItem.IsReadable = r.IsReadable; listItem.IsWritable = r.IsWritable; if (listItem.StatusCode == StatusCode.OK) { listItem.IsInServerList = true; } else { ListItemsManager.Remove(listItem.ClientAlias); // remove values that the server failed to add listItem.ClientAlias = 0; listItem.IsInClientList = false; resultItems.Add(listItem); } } } } catch (Exception) { foreach (ListItemInfo ar in listInstanceIdsCollection) { ListItemsManager.Remove(ar.ClientAlias); // remove values that the server failed to add } return(null); } } return(resultItems); }