protected override void HandleChooser()
		{
			using (var dlg = new GenDateChooserDlg(m_mediator.HelpTopicProvider))
			{
				dlg.Text = string.Format(DetailControlsStrings.ksFieldChooserDlgTitle, m_fieldName);
				GenDate x = (m_cache.DomainDataByFlid as ISilDataAccessManaged).get_GenDateProp(m_obj.Hvo, m_flid);
				// If we don't yet have a value, make today the default.
				if (x.IsEmpty)
				{
					DateTime now = DateTime.Now;
					x = new GenDate(GenDate.PrecisionType.Exact, now.Month, now.Day, now.Year, true);
				}
				dlg.GenericDate = x;
				if (dlg.ShowDialog(m_mediator.PropertyTable.GetValue("window") as IWin32Window) == DialogResult.OK)
				{
					var genDate = dlg.GenericDate;
					UndoableUnitOfWorkHelper.Do(string.Format(DetailControlsStrings.ksUndoSet, m_fieldName),
						string.Format(DetailControlsStrings.ksRedoSet, m_fieldName), m_obj, () =>
					{
						(m_cache.DomainDataByFlid as ISilDataAccessManaged).SetGenDate(m_obj.Hvo, m_flid, genDate);
					});
					m_genDateTextBox.Text = genDate.ToLongString();
				}
			}
		}
		public void kcptGenDateTests()
		{
			var firstPerson = (ICmPerson)Cache.LanguageProject.PeopleOA.PossibilitiesOS[0];
			var startValue = firstPerson.DateOfBirth;
			var newValue = new GenDate(GenDate.PrecisionType.Before, 1, 1, 3000, true);
			using (var undoHelper = new UndoableUnitOfWorkHelper(m_actionHandler, "set DOB"))
			{
				firstPerson.DateOfBirth = newValue;
				undoHelper.RollBack = false;
			}

			Assert.AreEqual(UndoResult.kuresSuccess, m_actionHandler.Undo());
			Assert.AreEqual(startValue, firstPerson.DateOfBirth, "Wrong starting DOB");
			Assert.AreEqual(UndoResult.kuresSuccess, m_actionHandler.Redo());
			Assert.AreEqual(newValue, firstPerson.DateOfBirth, "Wrong undone DOB");
		}
		public void set_GenDatePropTest()
		{
			var person2 = Cache.LanguageProject.PeopleOA.PossibilitiesOS[1] as ICmPerson;
			var genDate = new GenDate(GenDate.PrecisionType.Exact, 0, 0, 300, false);
			m_sda.SetGenDate(person2.Hvo, CmPersonTags.kflidDateOfBirth, genDate);
			Assert.AreEqual(genDate, person2.DateOfBirth);
		}
		/// <summary>
		/// Set the generic date property.
		/// </summary>
		/// <param name="hvo">The hvo.</param>
		/// <param name="tag">The tag.</param>
		/// <param name="genDate">The generic date.</param>
		public virtual void SetGenDate(int hvo, int tag, GenDate genDate)
		{
			throw new NotImplementedException();
		}
		/// <summary>
		/// Set the generic date property.
		/// </summary>
		/// <param name="hvo">The hvo.</param>
		/// <param name="tag">The tag.</param>
		/// <param name="genDate">The generic date.</param>
		public override void SetGenDate(int hvo, int tag, GenDate genDate)
		{
			m_domainDataByFlid.SetGenDate(hvo, tag, genDate);
		}
		private bool TryParseGenDate(string sData, List<string> rgsFmt, out GenDate gdt)
		{
			GenDate.PrecisionType prec = GenDate.PrecisionType.Exact;
			if (sData[0] == '~')
			{
				prec = GenDate.PrecisionType.Approximate;
				sData = sData.Substring(1).Trim();
			}
			else if (sData[0] == '<')
			{
				prec = GenDate.PrecisionType.Before;
				sData = sData.Substring(1).Trim();
			}
			else if (sData[0] == '>')
			{
				prec = GenDate.PrecisionType.After;
				sData = sData.Substring(1).Trim();
			}
			if (sData.Length == 0)
			{
				gdt = new GenDate();
				return false;
			}
			int year = 0;
			bool fAD = true;
			DateTime dt;
			if (DateTime.TryParseExact(sData, rgsFmt.ToArray(), null, DateTimeStyles.None, out dt))
			{
				if (dt.Year > 0)
				{
					year = dt.Year;
				}
				else
				{
					year = -dt.Year;
					fAD = false;
				}
				gdt = new GenDate(prec, dt.Month, dt.Day, year, fAD);
				return true;
			}
			foreach (string sFmt in rgsFmt)
			{
				GenDateInfo gdi;
				string sResidue = ParseFormattedDate(sData, sFmt, out gdi);
				if (!gdi.error)
				{
					year = gdi.year;
					if (prec == GenDate.PrecisionType.Exact)
					{
						if (sResidue.Trim().StartsWith("?"))
							prec = GenDate.PrecisionType.Approximate;
						else
							prec = gdi.prec;
					}
					if (year < 0)
					{
						year = -year;
						fAD = false;
					}
					gdt = new GenDate(prec, gdi.ymon, gdi.mday, year, fAD);
					return true;
				}
			}
			gdt = new GenDate();
			return false;
		}
Exemple #7
0
		public static string GetGenDateAttribute(GenDate dataProperty)
		{
			var genDateStr = "0";
			if (!dataProperty.IsEmpty)
			{
				genDateStr = String.Format("{0}{1:0000}{2:00}{3:00}{4}", dataProperty.IsAD ? "" : "-", dataProperty.Year,
					dataProperty.Month, dataProperty.Day, (int)dataProperty.Precision);
			}
			return genDateStr;
		}
		private bool GenDateIsInRange(GenDate gen)
		{
			return GenDateIsAfterDate(gen, m_start, IsStartAD) &&
				GenDateIsBeforeDate(gen, m_end, IsEndAD);
		}
		private static string GetDefaultValueForPropertyType(CellarPropertyType propertyType)
		{
			switch (propertyType)
			{
				case CellarPropertyType.Boolean:
					return "False";
				case CellarPropertyType.GenDate:
					var genDate = new GenDate();
					return string.Format("{0}{1:0000}{2:00}{3:00}{4}",
						genDate.IsAD ? "" : "-",
						genDate.Year,
						genDate.Month,
						genDate.Day,
						(int)genDate.Precision);
				case CellarPropertyType.Guid:
					return Guid.Empty.ToString();
				case CellarPropertyType.Float:
					throw new NotSupportedException("The 'Float' data type is not supported in the FW data model yet (as of 23 March 2013).");
				case CellarPropertyType.Integer:
					return "0";
				case CellarPropertyType.Time:
					var datetime = new DateTime();
					datetime = datetime.ToUniversalTime(); // Store it as UTC.
					return String.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}",
						datetime.Year,
						datetime.Month,
						datetime.Day,
						datetime.Hour,
						datetime.Minute,
						datetime.Second,
						datetime.Millisecond);
				case CellarPropertyType.Numeric:
					throw new NotSupportedException("The 'Numeric' data type is not supported in the FW data model yet (as of 23 March 2013).");
				default:
					throw new InvalidOperationException("The given 'propertyType' is not a basic data type.");
			}
		}
		private bool GenDateMightBeAfterDate(GenDate gen, DateTime date, bool fAD)
		{
			if (gen.IsAD && !fAD)		// AD > BC
				return true;
			if (!gen.IsAD && fAD)		// BC < AD
				return gen.Precision == GenDate.PrecisionType.After;
			if (!gen.IsAD && !fAD)		// Both BC
			{
				if (gen.Year > date.Year)
					return gen.Precision == GenDate.PrecisionType.After;
				else if (gen.Year < date.Year)
					return true;
			}
			if (gen.IsAD && fAD)		// Both AD
			{
				if (gen.Year < date.Year)
					return gen.Precision == GenDate.PrecisionType.After;
				else if (gen.Year > date.Year)
					return true;
			}
			if (gen.Month < date.Month)
			{
				return gen.Month == GenDate.UnknownMonth &&
					gen.Precision != GenDate.PrecisionType.Before;
			}
			else if (gen.Month > date.Month)
			{
				return true;
			}
			if (gen.Day == GenDate.UnknownDay)
				return gen.Precision != GenDate.PrecisionType.Before;
			else if (gen.Day == date.Day)
				return gen.Precision != GenDate.PrecisionType.Before;
			else
				return gen.Day > date.Day || gen.Precision == GenDate.PrecisionType.After;
		}
		private bool GenDateIsAfterDate(GenDate gen, DateTime date, bool fAD)
		{
			if (UnspecificMatching)
				return GenDateMightBeAfterDate(gen, date, fAD);

			if (gen.IsAD && !fAD)		// AD > BC
				return gen.Precision != GenDate.PrecisionType.Before;
			if (!gen.IsAD && fAD)		// BC < AD
				return false;
			if (!gen.IsAD && !fAD)		// Both BC
			{
				if (gen.Year > date.Year)
					return false;
				else if (gen.Year < date.Year)
					return gen.Precision != GenDate.PrecisionType.Before;
			}
			if (gen.IsAD && fAD)		// Both AD
			{
				if (gen.Year < date.Year)
					return false;
				else if (gen.Year > date.Year)
					return gen.Precision != GenDate.PrecisionType.Before;
			}
			if (gen.Month < date.Month)
			{
				return gen.Month == GenDate.UnknownMonth &&
					gen.Precision == GenDate.PrecisionType.After;
			}
			else if (gen.Month > date.Month)
			{
				return gen.Precision != GenDate.PrecisionType.Before;
			}
			if (gen.Day == GenDate.UnknownDay)
				return gen.Precision == GenDate.PrecisionType.After;
			return gen.Day >= date.Day && gen.Precision != GenDate.PrecisionType.Before;
		}
		/// <summary>
		/// Set the generic date property.
		/// </summary>
		/// <param name="hvo">The hvo.</param>
		/// <param name="tag">The tag.</param>
		/// <param name="genDate">The generic date.</param>
		public void SetGenDate(int hvo, int tag, GenDate genDate)
		{
			GetInternalInterfaceForObject(hvo).SetProperty(tag, genDate, true);
		}
		private static void VerifyGenDate(CustomFieldData fieldData, GenDate genDate)
		{
			//"<trait name=\"CustomFldEntry GenDate\" value=\"201105232\"/>",
			//   '-'(BC and ''AD) 2011 05(May) 11(Day) 2(GenDate.PrecisionType (Before, Exact, Approximate, After)
			var sValue = fieldData.GenDateLiftFormat;
			Assert.IsNotNull(sValue);
			var liftGenDate = LiftExporter.GetGenDateFromInt(Convert.ToInt32(sValue));
			Assert.AreEqual(liftGenDate.Precision, genDate.Precision);
			Assert.AreEqual(liftGenDate.IsAD, genDate.IsAD);
			Assert.AreEqual(liftGenDate.Year, genDate.Year);
			Assert.AreEqual(liftGenDate.Month, genDate.Month);
			Assert.AreEqual(liftGenDate.Day, genDate.Day);
		}
		public void kcptGenDateTests()
		{
			var firstPerson = (ICmPerson)Cache.LanguageProject.PeopleOA.PossibilitiesOS[0];
			var startValue = firstPerson.DateOfBirth;
			var newValue = new GenDate(GenDate.PrecisionType.Before, 1, 1, 3000, true);
			firstPerson.DateOfBirth = newValue;
			Assert.AreEqual(newValue, firstPerson.DateOfBirth);
			// Reset it to original value.
			firstPerson.DateOfBirth = startValue;
			Assert.AreEqual(startValue, firstPerson.DateOfBirth);
		}