示例#1
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="queryBuilder"></param>
		/// <param name="record"> </param>
		/// <param name="modifiedProperties"></param>
		protected override void DoToUpdateStatement(IMansionContext context, ModificationQueryBuilder queryBuilder, Record record, IPropertyBag modifiedProperties)
		{
			// check if the property is not modified
			int newOrder;
			if (!modifiedProperties.TryGet(context, PropertyName, out newOrder))
				return;

			// check if the record contains a pointer
			NodePointer pointer;
			if (!record.TryGet(context, "pointer", out pointer))
				throw new InvalidOperationException("Could not update this record because it did not contain a pointer");

			// don't update order for root  nodes
			if (pointer.Depth == 1)
				return;

			// do not allow values smaller than 1
			if (newOrder < 1)
				throw new InvalidOperationException("Can not set orders smaller than 1");

			// assemble parameter
			var newOrderParameterName = queryBuilder.AddParameter("newOrder", newOrder, DbType.Int32);
			var oldOrderParameterName = queryBuilder.AddParameter("oldOrder", record.Get<int>(context, PropertyName), DbType.Int32);
			var parentIdParameterName = queryBuilder.AddParameter("parentId", pointer.Parent.Id, DbType.Int32);

			// update the orders before updating the order of the current node
			queryBuilder.PrependQuery(string.Format("UPDATE [Nodes] SET [order] = [order] + 1 WHERE (parentId = {0}) AND ([order] < {1} AND [order] >= {2})", parentIdParameterName, oldOrderParameterName, newOrderParameterName));
			queryBuilder.PrependQuery(string.Format("UPDATE [Nodes] SET [order] = [order] - 1 WHERE (parentId = {0}) AND ([order] > {1} AND [order] <= {2})", parentIdParameterName, oldOrderParameterName, newOrderParameterName));

			// update the column
			queryBuilder.AddColumnValue(PropertyName, newOrderParameterName);
		}
示例#2
0
        /// <summary>
        /// This method is called just before a node is updated by the repository.
        /// </summary>
        /// <param name="context">The <see cref="IMansionContext"/>.</param>
        /// <param name="record"> </param>
        /// <param name="properties">The updated properties of the node.</param>
        protected override void DoBeforeUpdate(IMansionContext context, Record record, IPropertyBag properties)
        {
            // if the name has not changed we are not interested
            string newName;
            if (!properties.TryGet(context, "name", out newName))
                return;

            // if the name has not changed after normalization we are not interested
            newName = TagUtilities.Normalize(newName);
            if (record.Get(context, "name", string.Empty).Equals(newName))
            {
                properties.Remove("name");
                return;
            }
            properties.Set("name", newName);

            // if the tag is renamed to another already existing tag, move all content to that existing tag and delete this one
            Node existingTag;
            var tagIndexNode = TagUtilities.RetrieveTagIndexNode(context);
            if (TagUtilities.TryRetrieveTagNode(context, tagIndexNode, newName, out existingTag))
            {
                // TODO: move all content to the existing tag

                // TODO: delete this tag
            }
        }
		/// <summary>
		/// This method is called just before a node is updated by the repository.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="record"> </param>
		/// <param name="properties">The updated properties of the node.</param>
		protected override void DoBeforeUpdate(IMansionContext context, Record record, IPropertyBag properties)
		{
			base.DoBeforeUpdate(context, record, properties);

			// check if the identifier was updated
			string identifier;
			if (!properties.TryGet(context, "identifier", out identifier))
				return;
			identifier = identifier.Trim().ToLower();
			properties.Set("identifier", identifier);

			// only update the name with the identifier if the previous name was also derived from the identifier
			if (record.Get(context, "name", string.Empty).Equals(record.Get(context, "identifier", string.Empty)))
			{
				var name = properties.Get(context, "name", identifier).Trim().ToLower();
				properties.Set("name", name);
			}
		}
		/// <summary>
		/// This method is called just after a node is created by the repository.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="record"> </param>
		/// <param name="properties">The properties from which the <paramref name="record"/> was constructed.</param>
		protected override void DoAfterCreate(IMansionContext context, Record record, IPropertyBag properties)
		{
			base.DoAfterCreate(context, record, properties);

			// check if a preferred term was specified
			Guid newPreferredTermGuid;
			if (!record.TryGet(context, "preferredTermGuid", out newPreferredTermGuid) || newPreferredTermGuid == Guid.Empty)
				return;

			// retrieve the preferredTermNode
			var newPreferredTermNode = context.Repository.RetrieveSingleNode(context, newPreferredTermGuid);
			if (newPreferredTermNode == null)
				return;

			// store the guid in the synonymGuids field
			context.Repository.UpdateNode(context, newPreferredTermNode, new PropertyBag {
				{"synonymGuids", newPreferredTermNode.Get(context, "synonymGuids", string.Empty).AppendNeedle(record.Get<string>(context, "guid"))}
			});
		}
		/// <summary>
		/// This method is called just before a node is updated by the repository.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="record"> </param>
		/// <param name="properties">The updated properties of the node.</param>
		protected override void DoBeforeUpdate(IMansionContext context, Record record, IPropertyBag properties)
		{
			base.DoBeforeUpdate(context, record, properties);

			// check if the preferred term has changed
			Guid newPreferredTermGuid;
			if (!properties.TryGet(context, "preferredTermGuid", out newPreferredTermGuid))
				return;

			// delete the old link if it exists
			var recordGuidString = record.Get<string>(context, "guid");
			Guid currentPreferredTermGuid;
			if (record.TryGet(context, "preferredTermGuid", out currentPreferredTermGuid) && currentPreferredTermGuid != Guid.Empty)
			{
				// retrieve the old record
				var currentPreferredTermNode = context.Repository.RetrieveSingleNode(context, currentPreferredTermGuid);

				// remove the link, if the target was found
				if (currentPreferredTermNode != null)
				{
					// store the guid in the synonymGuids field
					context.Repository.UpdateNode(context, currentPreferredTermNode, new PropertyBag {
						{"synonymGuids", currentPreferredTermNode.Get(context, "synonymGuids", string.Empty).RemoveNeedle(recordGuidString)}
					});
				}
			}

			// check if there is no new synonym
			if (newPreferredTermGuid == Guid.Empty)
				return;

			// retrieve the preferredTermNode
			var newPreferredTermNode = context.Repository.RetrieveSingleNode(context, newPreferredTermGuid);
			if (newPreferredTermNode == null)
				return;

			// store the guid in the synonymGuids field
			context.Repository.UpdateNode(context, newPreferredTermNode, new PropertyBag {
				{"synonymGuids", newPreferredTermNode.Get(context, "synonymGuids", string.Empty).AppendNeedle(recordGuidString)}
			});
		}
		/// <summary>
		/// Generates an sync statements of this colum.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="command"></param>
		/// <param name="record"></param>
		/// <param name="columnText"></param>
		/// <param name="valueText"></param>
		protected override void DoToSyncStatement(IMansionContext context, SqlCommand command, Record record, StringBuilder columnText, StringBuilder valueText)
		{
			// determine the value
			var value = GetValue(context, record.Get<object>(context, PropertyName));

			// write the SQL statement
			columnText.AppendFormat("[{0}], ", ColumnName);
			valueText.AppendFormat("@{0}, ", command.AddParameter(value));
		}
		/// <summary>
		/// Get the trigger key based on the task and job.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="jobRecord"></param>
		/// <returns></returns>
		private static TriggerKey GetTriggerKey(IMansionContext context, Record jobRecord)
		{
			var jobName = jobRecord.Get<string>(context, "name");
			var taskName = String.Format("{0} {1}", jobRecord.Id, jobName);
			return new TriggerKey(taskName, jobName);
		}
		/// <summary>
		/// Creates an SimpleSchedule for the job depending on the last run datetime.
		/// If the last run is known, it schedules the next run accordingly, else it will start immediately.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="jobRecord"></param>
		/// <param name="triggerTimeSpan"></param>
		/// <returns>Quartz ITrigger</returns>
		private static ITrigger GetJobTrigger(IMansionContext context, Record jobRecord, TimeSpan triggerTimeSpan)
		{
			var simpleSchedule = SimpleScheduleBuilder.Create()
				.WithInterval(triggerTimeSpan)
				.RepeatForever();

			var dateTimeOffset = new DateTimeOffset();
			var lastRun = jobRecord.Get(context, "lastRun", DateTime.MinValue);
			if (lastRun != DateTime.MinValue)
				dateTimeOffset = new DateTimeOffset(lastRun).Add(triggerTimeSpan);

			if (lastRun != DateTime.MinValue && dateTimeOffset > DateTime.Now)
			{
				return TriggerBuilder.Create()
					.WithIdentity(GetTriggerKey(context, jobRecord))
					.StartAt(dateTimeOffset)
					.WithSchedule(simpleSchedule)
					.Build();
			}
			else
			{
				return TriggerBuilder.Create()
					.WithIdentity(GetTriggerKey(context, jobRecord))
					.StartNow()
					.WithSchedule(simpleSchedule)
					.Build();
			}
		}