using Newtonsoft.Json; using ServerCore; using ServerBase; using META_ID = System.UInt32; using USER_GUID = System.String; namespace ServerCommon { public class OwnedLandAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public META_ID LandMetaId { get; set; } = 0; [JsonProperty] public OwnedType OwnedType { get; set; } = OwnedType.None; public OwnedLandAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType) : base(owner, entityOfOwnerEntityType) { } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new OwnedLandAttributeTransactor(getOwner()); } public override void onClear() { LandMetaId = 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 OwnedLandAttribute(owner, owner_entity); cloned.deepCopyFromBase(this); cloned.LandMetaId = LandMetaId; 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, () => "owner is null"); USER_GUID user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as OwnedLandDoc; if (null == try_pending_doc) { var to_copy_doc = new OwnedLandDoc(user_guid, LandMetaId); 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.LandMetaId = LandMetaId; 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(OwnedLandDoc).Name; var owned_land_doc_base = docBase as OwnedLandDoc; if (null == owned_land_doc_base) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, owned_land_doc_base is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(owned_land_doc_base); //===================================================================================== // Doc => Attribute //===================================================================================== var owned_land_attrib = owned_land_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(owned_land_attrib, () => $"owned_land_attrib is null !!!"); LandMetaId = owned_land_attrib.LandMetaId; OwnedType = owned_land_attrib.OwnedType; return true; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { 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_land_attribute = otherEntityAttribute as OwnedLandAttribute; if (null == owned_land_attribute) { err_msg = $"Failed to cast OwnedLandAttribute !!!, owned_land_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } LandMetaId = owned_land_attribute.LandMetaId; OwnedType = owned_land_attribute.OwnedType; //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = owned_land_attribute.getTryPendingDocBase() as OwnedLandDoc; if (null != try_pending_doc) { owned_land_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var owned_land_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(owned_land_attrib, () => $"owned_land_attrib is null !!! - {owner.toBasicString()}"); owned_land_attrib.LandMetaId = LandMetaId; owned_land_attrib.OwnedType = OwnedType; return result; } } public class OwnedLandAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public OwnedLandAttributeTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(OwnedLandAttribute).Name; var copy_from_owned_land_attribute = entityAttributeBase as OwnedLandAttribute; if (null == copy_from_owned_land_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_from_owned_land_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_owned_land_attribute = getClonedEntityAttribute() as OwnedLandAttribute; if (null == copy_to_owned_land_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_owned_land_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_owned_land_attribute.LandMetaId = copy_from_owned_land_attribute.LandMetaId; copy_to_owned_land_attribute.OwnedType = copy_from_owned_land_attribute.OwnedType; return true; } } }