Beispiel #1
0
		public static Identity GetIdentity(object item, DataDestination destination)
		{
            IdentityConfiguration[] keys = destination.GetIdentityKeys();
			Identity identity = new Identity(item);
			foreach(IdentityConfiguration ic in keys)
			{
                string key = ic.Property;
				PropertyInfo pi = item.GetType().GetProperty(key);
				if( pi != null )
				{
					try
					{
						identity[key] = pi.GetValue( item, new object[0] );
					}
					catch(Exception ex)
					{
						throw new FluorineException(__Res.GetString(__Res.Identity_Failed, key), ex);
					}
				}
				else
				{				
					try
					{
						FieldInfo fi = item.GetType().GetField(key, BindingFlags.Public | BindingFlags.Instance);
						if( fi != null )
						{
							identity[key] = fi.GetValue( item );
						}
					}
					catch(Exception ex)
					{
						throw new FluorineException(__Res.GetString(__Res.Identity_Failed, key), ex);
					}
				}
			}
			return identity;
		}
		private UpdateCollectionMessage CreateUpdateCollectionMessage(DataMessage dataMessage, Sequence sequence, Identity identity, int position, int updateMode)
		{
			UpdateCollectionMessage updateCollectionMessage = new UpdateCollectionMessage();
			// The unique identifier for the collection that was updated. For a collection filled with the 
			// DataService.fill() method this contains an Array of the parameters specified.
			updateCollectionMessage.collectionId = sequence.Parameters;
			updateCollectionMessage.destination = dataMessage.destination;
			updateCollectionMessage.replace = false;
			updateCollectionMessage.updateMode = updateMode;
			updateCollectionMessage.messageId = "srv:" + Guid.NewGuid().ToString("D") + ":0";
			updateCollectionMessage.correlationId = dataMessage.correlationId;

			UpdateCollectionRange updateCollectionRange = new UpdateCollectionRange();
			// An Array of identity objects that represent which items were either deleted or inserted in the 
			// associated collection starting at the position indicated by the position property
			updateCollectionRange.identities = new object[1];
			//(updateCollectionRange.identities as IList).Add( identity );
			(updateCollectionRange.identities as object[])[0] = identity;
			updateCollectionRange.updateType = UpdateCollectionRange.InsertIntoCollection;
			updateCollectionRange.position = position;
					
			//ArrayList body = new ArrayList();
			//body.Add(updateCollectionRange);
			object[] body = new object[1]; body[0] = updateCollectionRange;
			updateCollectionMessage.body = body;
			return updateCollectionMessage;
		}
		public SequencedMessage GetPageItems(DataMessage dataMessage)
		{
			int sequenceId = (int)dataMessage.headers[DataMessage.SequenceIdHeader];
			Sequence sequence = GetSequence(sequenceId);
            if (sequence != null)
            {
                IList DSids = dataMessage.headers["DSids"] as IList;
                //ArrayList items = new ArrayList(DSids.Count);
                SequencedMessage sequencedMessage = new SequencedMessage();
                object[] items = new object[DSids.Count];
                lock (_objLock)
                {
                    for (int i = 0; i < DSids.Count; i++)
                    {
                        Identity identity = new Identity(DSids[i] as IDictionary);
                        ItemWrapper itemWrapper = GetItem(identity);
                        //items.Add(item);
                        items[i] = itemWrapper.Instance;
                    }

                    sequencedMessage.destination = dataMessage.destination;
                    sequencedMessage.sequenceId = sequence.Id;
                    sequencedMessage.sequenceSize = sequence.Size;
                    sequencedMessage.sequenceProxies = null;

                    sequencedMessage.body = items;
                }
                return sequencedMessage;
            }
            else
            {
                DataServiceException dse = new DataServiceException(string.Format("Sequence {0} in destination {1} was not found", sequenceId, dataMessage.destination));
                throw dse;
            }
		}
		void ApplyUpdateCollectionMessage(Sequence sequence, UpdateCollectionMessage updateCollectionMessage)
		{
			IList updateCollectionRanges = updateCollectionMessage.body as IList;
			for(int k = 0; k < updateCollectionRanges.Count; k++)
			{
				UpdateCollectionRange updateCollectionRange = updateCollectionRanges[k] as UpdateCollectionRange;
				int insertCount = 0;
				for(int l = 0; l < updateCollectionRange.identities.Length; l++)
				{
					Identity identity = updateCollectionRange.identities[l] as Identity;
					if( identity == null )
					{
                        identity = new Identity(updateCollectionRange.identities[l] as IDictionary);
					}
					if( updateCollectionRange.updateType == UpdateCollectionRange.InsertIntoCollection )
					{
						this.AddIdentityToSequence(sequence, updateCollectionRange.position + insertCount, identity, null);
						insertCount++;
					}
					if( updateCollectionRange.updateType == UpdateCollectionRange.DeleteFromCollection )
						this.RemoveIdentityFromSequence(sequence, identity, updateCollectionRange.position, null);
				}
			}
		}
		public Sequence CreateSequence(string clientId, IList result, IList parameters, DataServiceTransaction dataServiceTransaction)
		{
			Sequence sequence = null;
			Identity[] identities = new Identity[result.Count];

			lock(_objLock)
			{
				for(int i = 0; i < identities.Length; i++)
				{
					if( result[i] != null )
					{
						Identity identity = Identity.GetIdentity(result[i], _dataDestination);
						identities[i] = identity;
						if( ! _itemIdToItemHash.ContainsKey(identity) )
							_itemIdToItemHash.Add(identity, new ItemWrapper(result[i]));
						else
						{
							ItemWrapper itemWrapper = _itemIdToItemHash[identity] as ItemWrapper;
							itemWrapper.Instance = result[i];
						}
					}
				}
				//Lookup existing sequence
                if (parameters != null)
                {
                    if (_parametersToSequenceIdHash.Contains(parameters))
                        sequence = _parametersToSequenceIdHash[parameters] as Sequence;
                }
                else
                {
                    IDictionary sequenceIdMap = _itemIdToSequenceIdMapHash[identities[0]] as IDictionary;
                    if (sequenceIdMap != null)
                    {
                        foreach (Sequence sequenceTmp in sequenceIdMap.Values)
                        {
                            if (sequenceTmp.Parameters == null)
                            {
                                sequence = sequenceTmp;
                                break;
                            }
                        }
                    }
                }
                //if (parameters == null)
                //    parameters = new ArrayList();
				
				if( sequence == null )
				{
					sequence = new Sequence();
					sequence.Id = sequence.GetHashCode();

                    object[] parametersArray = null;
                    if (parameters != null)
                    {
                        parametersArray = new object[parameters.Count];
                        parameters.CopyTo(parametersArray, 0);
                        sequence.Parameters = parametersArray;
                        _parametersToSequenceIdHash[parameters] = sequence;
                    }

					for(int i = 0; i < identities.Length; i++)
					{
						Identity identity = identities[i];
						AddIdentityToSequence(sequence, identity, dataServiceTransaction);
					}

					_sequenceIdToSequenceHash[sequence.Id] = sequence;

					if( log.IsDebugEnabled )
						log.Debug(__Res.GetString(__Res.SequenceManager_CreateSeq, sequence.Id, clientId));

				}
				else
				{
					for(int i = 0; i < identities.Length; i++)
					{
						Identity identity = identities[i];
						Identity existingIdentity = null;
						if( i < sequence.Count )
							existingIdentity = sequence[i];
						if( !identity.Equals(existingIdentity) )
						{
							//Identity not found in sequence
							if( !sequence.Contains(identity) )
							{
								int position = AddIdentityToSequence(sequence, identity, dataServiceTransaction);
							}
						}
					}
				}
				sequence.AddSubscriber(clientId);
				ArrayList sequences;
				if( _clientIdToSequenceHash.Contains(clientId) )
					sequences = _clientIdToSequenceHash[clientId] as ArrayList;
				else
				{
					sequences = new ArrayList();
					_clientIdToSequenceHash[clientId] = sequences;
				}
				if( !sequences.Contains(sequence) )
					sequences.Add(sequence);
			}
			return sequence;
		}
		private ItemWrapper GetItem(Identity identity)
		{
			lock(_objLock)
			{
				return _itemIdToItemHash[identity] as ItemWrapper;
			}
		}
		public void RemoveIdentityFromSequence(Sequence sequence, Identity identity, int position, DataServiceTransaction dataServiceTransaction)
		{
			if( position == -1 )
				return;
			lock(_objLock)
			{
                IDictionary sequenceIdMap = _itemIdToSequenceIdMapHash[identity] as IDictionary;
                if (sequenceIdMap != null)
                {
                    sequenceIdMap.Remove(sequence.Id);
                    //Release the item if it does'n occur in any sequence
                    if (sequenceIdMap.Count == 0)
                    {
                        _itemIdToItemHash.Remove(identity);
                        _itemIdToSequenceIdMapHash.Remove(identity);
                    }
                    if (sequence[position].Equals(identity))
                        sequence.RemoveAt(position);
                    else
                        sequence.Remove(identity);

                    if (dataServiceTransaction != null)
                        dataServiceTransaction.GenerateUpdateCollectionMessage(UpdateCollectionRange.DeleteFromCollection, _dataDestination, sequence, position, identity);
                }
                else
                {
                    _itemIdToItemHash.Remove(identity);
                    sequence.Remove(identity);
                }
			}
		}
Beispiel #8
0
        internal void GenerateUpdateCollectionMessage(int updateType, DataDestination dataDestination, Sequence sequence, int position, Identity identity)
        {
            UpdateCollectionMessage updateCollectionMessage = CreateUpdateCollectionMessage(dataDestination, sequence);

            updateCollectionMessage.AddItemIdentityChange(updateType, position, identity);
            if (updateCollectionMessage.collectionId != null)
            {
                _updateCollectionMessages[updateCollectionMessage.collectionId] = updateCollectionMessage;
            }
            else
            {
                //without fill parameters
                _updateCollectionMessages[new object[0]] = updateCollectionMessage;
            }
        }
		public int AddIdentityToSequence(Sequence sequence, Identity identity, DataServiceTransaction dataServiceTransaction)
		{
			return AddIdentityToSequence(sequence, -1, identity, dataServiceTransaction);
		}
		public int AddIdentityToSequence(Sequence sequence, int position, Identity identity, DataServiceTransaction dataServiceTransaction)
		{
			lock(_objLock)
			{
				if(position == -1 || position > sequence.Size)
					position = sequence.Add(identity);
				else
					sequence.Insert(position, identity);

                IDictionary sequenceIdMap = _itemIdToSequenceIdMapHash[identity] as IDictionary;
				if( sequenceIdMap == null )
				{
					sequenceIdMap = new Hashtable();
					_itemIdToSequenceIdMapHash[identity] = sequenceIdMap;
				}
				sequenceIdMap[sequence.Id] = sequence;

				if(dataServiceTransaction != null)
					dataServiceTransaction.GenerateUpdateCollectionMessage(UpdateCollectionRange.InsertIntoCollection, _dataDestination, sequence, position, identity);

				return position;
			}
		}
Beispiel #11
0
		public int IndexOf(Identity identity)
		{
			return this.InnerList.IndexOf(identity);
		}
Beispiel #12
0
		public void Remove(Identity identity)
		{
			this.InnerList.Remove(identity);
		}
Beispiel #13
0
		public bool Contains(Identity identity)
		{
			return this.InnerList.Contains(identity);
		}
Beispiel #14
0
		public void Insert(int index, Identity identity)
		{
			this.InnerList.Insert(index, identity);
		}
Beispiel #15
0
		public int Add(Identity identity)
		{
			return this.InnerList.Add(identity);
		}
Beispiel #16
0
		private IMessage UpdateCollection(UpdateCollectionMessage updateCollectionMessage, IList messages)
		{
			IList updateCollectionRanges = updateCollectionMessage.body as IList;
			for(int i = 0; i < updateCollectionRanges.Count; i++)
			{
				UpdateCollectionRange updateCollectionRange = updateCollectionRanges[i] as UpdateCollectionRange;
				for(int j = 0; j < updateCollectionRange.identities.Length; j++)
				{
					string messageId = updateCollectionRange.identities[j] as string;
					Identity identity = null;
					if( messageId != null )
					{
						//Search for previous Create or CreateAndSequence
						//This was a "pending update collection" sent from the client and it must be replaced by the actual Identity
						IMessage refMessage = GetMessage(messages, messageId);
						DataMessage dataMessage = refMessage as DataMessage;
						if( dataMessage != null )
						{
							DataDestination dataDestination = this.Destination as DataDestination;
							identity = Identity.GetIdentity(dataMessage.body, dataDestination);
						}
						//replace with the actual identity
						updateCollectionRange.identities[j] = identity;
					}
					else
					{
                        IDictionary identityMap = updateCollectionRange.identities[j] as IDictionary;
						if( identityMap != null )
							identity = new Identity(identityMap);
					}

					IList fillParameters = updateCollectionMessage.collectionId as IList;

					IAssembler assembler = GetAssembler();
					if( assembler != null )
					{
						if( updateCollectionRange.updateType == UpdateCollectionRange.InsertIntoCollection )
							assembler.AddItemToFill(fillParameters, updateCollectionRange.position + j ,identity);
						if( updateCollectionRange.updateType == UpdateCollectionRange.DeleteFromCollection )
							assembler.RemoveItemFromFill(fillParameters, updateCollectionRange.position ,identity);
					}
				}
			}
			return updateCollectionMessage;
		}
		internal void GenerateUpdateCollectionMessage(int updateType, DataDestination dataDestination, Sequence sequence, int position, Identity identity)
		{
			UpdateCollectionMessage updateCollectionMessage = CreateUpdateCollectionMessage(dataDestination, sequence);
			updateCollectionMessage.AddItemIdentityChange(updateType, position, identity);
            if (updateCollectionMessage.collectionId != null)
                _updateCollectionMessages[updateCollectionMessage.collectionId] = updateCollectionMessage;
            else
            {
                //without fill parameters
                _updateCollectionMessages[new object[0]] = updateCollectionMessage;
            }
		}
		public void RemoveIdentityFromSequence(Sequence sequence, Identity identity, DataServiceTransaction dataServiceTransaction)
		{
			RemoveIdentityFromSequence(sequence, identity, sequence.IndexOf(identity), dataServiceTransaction);
		}