using Newtonsoft.Json; using ServerCore; using ServerBase; using META_ID = System.UInt32; using USER_GUID = System.String; namespace ServerCommon { public class OwnedBuildingAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public META_ID BuildingMetaId { get; set; } = 0; [JsonProperty] public OwnedType OwnedType { get; set; } = OwnedType.None; public OwnedBuildingAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType) : base(owner, entityOfOwnerEntityType) { } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new OwnedBuildingAttributeTransactor(getOwner()); } public override void onClear() { BuildingMetaId = 0; OwnedType = OwnedType.None; getAttributeState().reset(); } public override EntityAttributeBase onCloned() { var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var owner_entity = getEntityOfOwnerEntityType(); NullReferenceCheckHelper.throwIfNull(owner_entity, () => $"owner_entity is null !!! - {owner.toBasicString()}"); var cloned = new OwnedBuildingAttribute(owner, owner_entity); cloned.deepCopyFromBase(this); cloned.BuildingMetaId = BuildingMetaId; cloned.OwnedType = OwnedType; return cloned; } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var result = new Result(); var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => "owner is null"); var user_attribute = owner.getRootParent().getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(user_attribute, () => "user_attribute is null"); USER_GUID user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as OwnedBuildingDoc; if (null == try_pending_doc) { var to_copy_doc = new OwnedBuildingDoc(user_guid, BuildingMetaId); var origin_doc = getOriginDocBase(); if (null != origin_doc) { to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc); } try_pending_doc = to_copy_doc; setTryPendingDocBase(try_pending_doc); } var to_copy_doc_attrib = try_pending_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!!"); to_copy_doc_attrib.BuildingMetaId = BuildingMetaId; to_copy_doc_attrib.OwnedType = OwnedType; if (false == isForQuery) { return (result, try_pending_doc); } //===================================================================================== // Doc QueryType 반영 //===================================================================================== (result, var to_query_doc) = await applyDoc4Query(try_pending_doc); if (result.isFail()) { return (result, null); } return (result, to_query_doc); } public bool copyEntityAttributeFromDoc(DynamoDbDocBase? docBase) { var err_msg = string.Empty; var to_cast_string = typeof(OwnedBuildingDoc).Name; var owned_building_doc_base = docBase as OwnedBuildingDoc; if (null == owned_building_doc_base) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, owned_building_doc_base is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(owned_building_doc_base); //===================================================================================== // Doc => Attribute //===================================================================================== var doc_attrib = owned_building_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"OwnedBuildingAttrib doc_attrib is null !!!"); BuildingMetaId = doc_attrib.BuildingMetaId; OwnedType = doc_attrib.OwnedType; return true; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var result = new Result(); var err_msg = string.Empty; if (null == otherEntityAttribute) { err_msg = $"Invalid Param !!!, otherEntityAttribute is null"; result.setFail(ServerErrorCode.FunctionParamNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } //===================================================================================== // OtherAttribute => Attribute //===================================================================================== var owned_building_attribute = otherEntityAttribute as OwnedBuildingAttribute; if (null == owned_building_attribute) { err_msg = $"Failed to cast OwnedBuildingAttribute !!!, owned_building_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } BuildingMetaId = owned_building_attribute.BuildingMetaId; OwnedType = owned_building_attribute.OwnedType; //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = owned_building_attribute.getTryPendingDocBase() as OwnedBuildingDoc; if (null != try_pending_doc) { owned_building_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var owned_building_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(owned_building_attrib, () => $"owned_building_attrib is null !!! - {owner.toBasicString()}"); owned_building_attrib.BuildingMetaId = BuildingMetaId; owned_building_attrib.OwnedType = OwnedType; return result; } } public class OwnedBuildingAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public OwnedBuildingAttributeTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(OwnedBuildingAttribute).Name; var copy_from_owned_building_attribute = entityAttributeBase as OwnedBuildingAttribute; if (null == copy_from_owned_building_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_from_owned_building_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_owned_building_attribute = getClonedEntityAttribute() as OwnedBuildingAttribute; if (null == copy_to_owned_building_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_owned_building_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_owned_building_attribute.BuildingMetaId = copy_from_owned_building_attribute.BuildingMetaId; copy_to_owned_building_attribute.OwnedType = copy_from_owned_building_attribute.OwnedType; return true; } } }