using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using ServerCore; using ServerBase; using USER_GUID = System.String; using RESERVATION_GUID = System.String; namespace ServerCommon { public class TaskReservationAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public RESERVATION_GUID ReservationGuid { get; set; } = string.Empty; [JsonProperty] public TaskReservationActionType ReservationType { get; set; } = TaskReservationActionType.None; [JsonProperty] public string JsonValue { get; set; } = string.Empty; public TaskReservationAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType) : base(owner, entityOfOwnerEntityType) { } public override void onClear() { ReservationGuid = string.Empty; ReservationType = TaskReservationActionType.None; JsonValue = string.Empty; getAttributeState().reset(); } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { var user_base = getOwner().getRootParent() as UserBase; NullReferenceCheckHelper.throwIfNull(user_base, () => $"user_base is null !!!"); return new TaskReservationAttributeTransactor(user_base); } public override EntityAttributeBase onCloned() { var user_base = getOwner().getRootParent() as UserBase; NullReferenceCheckHelper.throwIfNull(user_base, () => $"user_base is null !!!"); var entity_type = getEntityOfOwnerEntityType(); NullReferenceCheckHelper.throwIfNull(entity_type, () => "Entity Type is null"); var cloned = new TaskReservationAttribute(user_base, entity_type); cloned.ReservationGuid = ReservationGuid; cloned.ReservationType = ReservationType; cloned.JsonValue = JsonValue; return cloned; } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var result = new Result(); var owner = getOwner().getRootParent(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var acoount_attribute = owner.getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(acoount_attribute, () => $"acoount_attribute is null !!! - {owner.toBasicString()}"); USER_GUID user_guid = acoount_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as TaskReservationDoc; if (null == try_pending_doc) { var to_copy_doc = new TaskReservationDoc(user_guid, ReservationGuid); 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 take_reservation_attrib = try_pending_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(take_reservation_attrib, () => $"take_reservation_attrib is null !!! - {owner.toBasicString()}"); take_reservation_attrib.ReservationGuid = ReservationGuid; take_reservation_attrib.ReservationType = ReservationType; take_reservation_attrib.JsonValue = JsonValue; 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 root_parent = getOwner().getRootParent(); NullReferenceCheckHelper.throwIfNull(root_parent, () => $"root_parent is null !!!"); var err_msg = string.Empty; var to_cast_string = typeof(TaskReservationDoc).Name; var task_reservation_doc_base = docBase as TaskReservationDoc; if (null == task_reservation_doc_base) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, task_reservation_doc_base is null : {to_cast_string} - {root_parent.toBasicString()}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(task_reservation_doc_base); //===================================================================================== // Doc => Attribute //===================================================================================== var task_reservation_attrib = task_reservation_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(task_reservation_attrib, () => $"task_reservation_attrib is null !!! - {root_parent.toBasicString()}"); ReservationGuid = task_reservation_attrib.ReservationGuid; ReservationType = task_reservation_attrib.ReservationType; JsonValue = task_reservation_attrib.JsonValue; 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 task_reservation_attribute = otherEntityAttribute as TaskReservationAttribute; if (null == task_reservation_attribute) { err_msg = $"Failed to cast TaskReservationAttribute !!!, task_reservation_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } ReservationGuid = task_reservation_attribute.ReservationGuid; ReservationType = task_reservation_attribute.ReservationType; JsonValue = task_reservation_attribute.JsonValue; //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = task_reservation_attribute.getTryPendingDocBase() as TaskReservationDoc; if (null != try_pending_doc) { task_reservation_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var task_reservation_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(task_reservation_attrib, () => $"task_reservation_attrib is null !!! - {owner.toBasicString()}"); task_reservation_attrib.ReservationGuid = ReservationGuid; task_reservation_attrib.ReservationType = ReservationType; task_reservation_attrib.JsonValue = JsonValue; return result; } } public class TaskReservationAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public TaskReservationAttributeTransactor(UserBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(TaskReservationAttribute).Name; var copy_from_attribute = entityAttributeBase as TaskReservationAttribute; if (null == copy_from_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_attribute = getClonedEntityAttribute() as TaskReservationAttribute; if (null == copy_to_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_attribute.ReservationGuid = copy_from_attribute.ReservationGuid; copy_to_attribute.ReservationType = copy_from_attribute.ReservationType; copy_to_attribute.JsonValue = copy_from_attribute.JsonValue; return true; } } }