protected override void CopyEntityObjectToMetaObject(EntityObject target, Mediachase.Ibn.Data.Meta.MetaObject metaObject) { base.CopyEntityObjectToMetaObject(target, metaObject); if(metaObject.GetMetaType().Name == AddressEntity.GetAssignedMetaClassName()) AddressRequestHandler.UpdateAddressName(metaObject); }
protected override void PreDeleteInsideTransaction(BusinessContext context) { // Call Base method base.PreDeleteInsideTransaction(context); #region Delete Assigned Addresses EntityObject[] addresses = BusinessManager.List(AddressEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("OrganizationId", context.GetTargetPrimaryKeyId()) }); foreach (AddressEntity address in addresses) { DeleteRequest request = new DeleteRequest(address); request.Parameters.Add(AddressRequestParameters.Delete_SkipDefaultAddressCheck, null); BusinessManager.Execute(request); } #endregion #region Process RelatedContactAction // Read RelatedContactAction from request parameters RelatedContactAction relContactAction = context.Request.Parameters.GetValue <RelatedContactAction>(OrganizationRequestParameters.Delete_RelatedContactAction, RelatedContactAction.None); switch (relContactAction) { // Detach all assigned contacts case RelatedContactAction.Detach: EntityObject[] detachedContacts = BusinessManager.List(ContactEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("OrganizationId", context.GetTargetPrimaryKeyId()) }); foreach (ContactEntity contact in detachedContacts) { contact.OrganizationId = null; BusinessManager.Update(contact); } break; // Delete all assigned actions case RelatedContactAction.Delete: EntityObject[] deletedContacts = BusinessManager.List(ContactEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("OrganizationId", context.GetTargetPrimaryKeyId()) }); foreach (ContactEntity contact in deletedContacts) { BusinessManager.Execute(new DeleteRequest(contact)); } break; } #endregion #region Remove references from IBN 4.7 objects SqlHelper.ExecuteNonQuery(SqlContext.Current, System.Data.CommandType.StoredProcedure, "bus_cls_Organization_Delete", SqlHelper.SqlParameter("@OrgUid", SqlDbType.UniqueIdentifier, context.GetTargetPrimaryKeyId().Value)); #endregion }
/// <summary> /// Creates the entity object. /// </summary> /// <param name="metaClassName">Name of the meta class.</param> /// <param name="primaryKeyId">The primary key id.</param> /// <returns></returns> protected override EntityObject CreateEntityObject(string metaClassName, PrimaryKeyId?primaryKeyId) { if (metaClassName == AddressEntity.GetAssignedMetaClassName()) { AddressEntity retVal = new AddressEntity(); retVal.PrimaryKeyId = primaryKeyId; return(retVal); } return(base.CreateEntityObject(metaClassName, primaryKeyId)); }
protected override void PreDelete(BusinessContext context) { base.PreDelete(context); AddressEntity address = (AddressEntity)BusinessManager.Load(AddressEntity.GetAssignedMetaClassName(), context.GetTargetPrimaryKeyId().Value); if (!context.Request.Parameters.Contains(AddressRequestParameters.Delete_SkipDefaultAddressCheck) && (address.IsDefaultOrganizationElement || address.IsDefaultContactElement)) { throw new DeleteDefaultAddressException(); } }
/// <summary> /// Customs the method. /// </summary> /// <param name="context">The context.</param> protected virtual void SetDefaultAddress(BusinessContext context) { // Load Address MetaObject newDefaultAddress = MetaObjectActivator.CreateInstance(AddressEntity.GetAssignedMetaClassName(), context.GetTargetPrimaryKeyId().Value); if (newDefaultAddress["ContactId"] != null) { // Find default addresses MetaObject[] defaultAddressList = MetaObject.List(DataContext.Current.GetMetaClass(AddressEntity.GetAssignedMetaClassName()), FilterElement.EqualElement("ContactId", newDefaultAddress["ContactId"]), FilterElement.EqualElement("IsDefaultContactElement", true)); // Reset default addresses foreach (MetaObject defaultAddress in defaultAddressList) { defaultAddress["IsDefaultContactElement"] = false; defaultAddress.Save(); } // Set default address newDefaultAddress["IsDefaultContactElement"] = true; newDefaultAddress.Save(); } else if (newDefaultAddress["OrganizationId"] != null) { // Find default addresses MetaObject[] defaultAddressList = MetaObject.List(DataContext.Current.GetMetaClass(AddressEntity.GetAssignedMetaClassName()), FilterElement.EqualElement("OrganizationId", newDefaultAddress["OrganizationId"]), FilterElement.EqualElement("IsDefaultOrganizationElement", true)); // Reset default addresses foreach (MetaObject defaultAddress in defaultAddressList) { defaultAddress["IsDefaultOrganizationElement"] = false; defaultAddress.Save(); } // Set default address newDefaultAddress["IsDefaultOrganizationElement"] = true; newDefaultAddress.Save(); } context.SetResponse(new Response()); }
internal static void UpdateAddressName(MetaObject address) { if (address == null) { return; } if (address.GetMetaType().Name != AddressEntity.GetAssignedMetaClassName()) { throw new ArgumentOutOfRangeException("The address object has wrong meta object type. Should be 'Address'."); } address["Name"] = string.Join(";", new string[] { address.Properties["Line2"].GetValue <string>(string.Empty), address.Properties["Line1"].GetValue <string>(string.Empty), address.Properties["City"].GetValue <string>(string.Empty), address.Properties["Region"].GetValue <string>(string.Empty), address.Properties["PostalCode"].GetValue <string>(string.Empty), address.Properties["Country"].GetValue <string>(string.Empty) }); }
/// <summary> /// Pres the delete inside transaction. /// </summary> /// <param name="context">The context.</param> protected override void PreDeleteInsideTransaction(BusinessContext context) { // Call Base method base.PreDeleteInsideTransaction(context); #region Delete Assigned Addresses EntityObject[] addresses = BusinessManager.List(AddressEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("OrganizationId", context.GetTargetPrimaryKeyId()) }); foreach (AddressEntity address in addresses) { DeleteRequest request = new DeleteRequest(address); request.Parameters.Add(AddressRequestParameters.Delete_SkipDefaultAddressCheck, null); BusinessManager.Execute(request); } #endregion #region Remove references from IBN 4.7 objects SqlHelper.ExecuteNonQuery(SqlContext.Current, System.Data.CommandType.StoredProcedure, "bus_cls_Contact_Delete", SqlHelper.SqlParameter("@ContactUid", SqlDbType.UniqueIdentifier, context.GetTargetPrimaryKeyId().Value)); #endregion }
/// <summary> /// Exports the specified contact to VCard format. /// </summary> /// <param name="contactPrimaryKey">The contact primary key.</param> /// <returns></returns> public static string Export(PrimaryKeyId contactPrimaryKey) { StringBuilder sb = new StringBuilder(256); ContactEntity entity = (ContactEntity)BusinessManager.Load(ContactEntity.GetAssignedMetaClassName(), contactPrimaryKey); sb.Append("BEGIN:VCARD\r\n"); sb.Append("VERSION:2.1\r\n"); #region Export Name Info // Export Name Info sb.AppendFormat("N:{0};{1};{2}\r\n", ReturnEmptyIfNull(entity.LastName), ReturnEmptyIfNull(entity.FirstName), ReturnEmptyIfNull(entity.MiddleName)); #endregion #region Export Common Info // Export Common Info if (!string.IsNullOrEmpty(entity.FullName)) { sb.AppendFormat("FN:{0}\r\n", entity.FullName); } if (!string.IsNullOrEmpty(entity.NickName)) { sb.AppendFormat("NICKNAME:{0}\r\n", entity.NickName); } if (entity.BirthDate.HasValue && entity.BirthDate != DateTime.MinValue) { sb.AppendFormat("BDAY:{0}\r\n", entity.BirthDate.Value.ToString("yyyy-MM-dd")); } if (entity.Gender.HasValue) { sb.AppendFormat("X-GENDER:{0}\r\n", GlobalResource.GetString(MetaEnum.GetFriendlyName(DataContext.Current.GetMetaClass(ContactEntity.GetAssignedMetaClassName()).Fields["Gender"].GetMetaType(), entity.Gender.Value))); } if (!string.IsNullOrEmpty(entity.Role)) { sb.AppendFormat("ROLE:{0}\r\n", entity.Role); } if (!string.IsNullOrEmpty(entity.JobTitle)) { sb.AppendFormat("TITLE:{0}\r\n", entity.JobTitle); } if (!string.IsNullOrEmpty(entity.WebSiteUrl)) { sb.AppendFormat("URL:{0}\r\n", entity.WebSiteUrl); } if (!string.IsNullOrEmpty(entity.Description)) { sb.AppendFormat("NOTE:{0}\r\n", entity.Description); } #endregion #region Export Organization Info // Export Organization Info if (entity.OrganizationId.HasValue) { sb.AppendFormat("ORG:{0};{1}\r\n", entity.Organization, entity.OrganizationUnit); } #endregion #region Export EMails // Export EMails foreach (string emailFieldName in GetEmailFieldNames()) { string email = (string)entity.Properties[emailFieldName].Value; if (string.IsNullOrEmpty(email)) { continue; } string emailTypeString = string.Empty; if (emailFieldName == "EMailAddress1") { emailTypeString += ";PREF"; } if (emailFieldName == "EMailAddress1") { emailTypeString += ";WORK"; } else if (emailFieldName == "EMailAddress2") { emailTypeString += ";HOME"; } emailTypeString += ";INTERNET"; sb.AppendFormat("EMAIL{1}:{0}\r\n", email, emailTypeString); } #endregion #region Export Phones //// Export Phones foreach (string phoneFieldName in new string[] { "Fax", "MobilePhone", "Telephone1", "Telephone2", "Telephone3" }) { string phone = (string)entity.Properties[phoneFieldName].Value; if (string.IsNullOrEmpty(phone)) { continue; } string phoneTypeString = string.Empty; if (phoneFieldName == "Telephone1") { phoneTypeString += ";PREF"; } else if (phoneFieldName == "Telephone1") { phoneTypeString += ";WORK"; } else if (phoneFieldName == "Telephone2") { phoneTypeString += ";HOME"; } else if (phoneFieldName == "Fax") { phoneTypeString += ";FAX"; } else if (phoneFieldName == "MobilePhone") { phoneTypeString += ";CELL"; } sb.AppendFormat("TEL{1}:{0}\r\n", phone, phoneTypeString); } #endregion #region Export Addresses // Export Addresses foreach (AddressEntity address in BusinessManager.List(AddressEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("ContactId", contactPrimaryKey) })) { string adrTypeString = string.Empty; if (address.IsDefaultContactElement) { adrTypeString += ";PREF"; } //if (address.AddressType == Add) // adrTypeString += ";WORK"; //else if (vCardAdrr.VCardAddressTypeId == (int)VCardTelephoneType.Home) // adrTypeString += ";HOME"; sb.AppendFormat("ADR{0}:;{1};{2};{3};{4};{5};{6}\r\n", adrTypeString, string.Empty, address.Line1, address.City, address.Region, address.PostalCode, address.Country ); } #endregion sb.Append("END:VCARD\r\n"); return(sb.ToString()); }