public static Dictionary<string, List<DataTuple>> GetSplitDatatuples(List<DataTuple> incomingDatatuples, List<long> primaryKeys, DatasetVersion workingCopy, ref List<long> datatuplesFromDatabaseIds)
{
Dictionary<string, List<DataTuple>> data = new Dictionary<string, List<DataTuple>>();
Dictionary<string, DataTuple> newDtList = new Dictionary<string, DataTuple>();
Dictionary<string, DataTuple> editDtList = new Dictionary<string, DataTuple>();
List<DataTuple> deleteDtList = new List<DataTuple>();
DatasetManager datasetManager = new DatasetManager();
DataTupleIterator tupleIterator = new DataTupleIterator(datatuplesFromDatabaseIds, datasetManager, false);
// Keep the DB loop outer to reduce the number of DB queries
foreach (var existingTuple in tupleIterator)
{
if (existingTuple == null || existingTuple.Id < 0) // it is unlikely to happen, but just to reinforce it.
continue;
// iterating over the in-memory newDataTuples is faster
for (int counter = 0; counter < incomingDatatuples.Count(); counter++)
//foreach (var incomingTuple in newDatatuples)
{
DataTuple incomingTuple = incomingDatatuples[counter];
if (!IsEmpty(incomingTuple))
{
// we first assume that any incoming tuple is new, and then try to check if it is not.
// this reduces the iterations of the inner loop.
// because the search takes the DB tuple and lloks for it in the coming tuples, it is posssible for an incoming tuple to be added more than once.
// So they are added to a dictionary to avoid duplicates
string keysValueNewDataTuple = getPrimaryKeysAsString(incomingTuple, primaryKeys);
if (!newDtList.ContainsKey(keysValueNewDataTuple))
{
newDtList.Add(keysValueNewDataTuple, incomingTuple); // by default, assume that the incoming tuple is new (not in the DB)
}
string keysValueSourceDatatuple = getPrimaryKeysAsStringFromXml(existingTuple, primaryKeys);
if (keysValueNewDataTuple.Equals(keysValueSourceDatatuple)) // the incoming tuple exists in the DB
{
if (!Equal(incomingTuple, existingTuple)) // the incoming tuple is a changed version of an existing one
{
// the incoming tuple is found in the DB and brings some changes, therefore not NEW!
newDtList.Remove(keysValueNewDataTuple);
if (!editDtList.ContainsKey(keysValueNewDataTuple))
{
// apply the changes to the exisiting one and register is an edited tuple
editDtList.Add(keysValueNewDataTuple, Merge(incomingTuple, (DataTuple)existingTuple));
// remove the current incoming item to shorten the list for the next round
incomingDatatuples.RemoveAt(counter);
}
// the decision is made, hence break the inner loop
break;
}
else // the incoming tuple is found in the BD, but introduces no change., hence no action is needed.
{
// remove the incoming tuple from the list and from the new ones.
newDtList.Remove(keysValueNewDataTuple);
incomingDatatuples.RemoveAt(counter);
}
}
else // the incoming tuple does not match the PK, so it should be a new tuple, which is already added to the list.
{ // DO NOTHING
}
}
}
}
data.Add("new", newDtList.Values.ToList());
data.Add("edit", editDtList.Values.ToList());
return data;
}