/// <summary>
        /// Main entry point for he business logic that the plug-in is to execute.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }


            try
            {
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService    service = localContext.OrganizationService;
                Common common = new Common();

                if (context.InputParameters["Target"] is Entity)
                {
                    Entity           target = (Entity)context.InputParameters["Target"];
                    Common.OUT_PARAM ret    = new Common.OUT_PARAM();

                    //#region 이벤트에 따른 기능

                    if (target.Contains("new_p_event"))
                    {
                        OptionSetValue eventstate = (OptionSetValue)target["new_p_event"];



                        switch (eventstate.Value)
                        {
                        //이동
                        case (int)Event.이동:
                            ret = common.LocationCheck(target);

                            if (ret.RESULT)
                            {
                                target["new_l_clocation"] = target["new_l_change_location"];
                            }
                            else
                            {
                                throw new InvalidPluginExecutionException(ret.MSG);
                            }

                            break;

                        //불출
                        case (int)Event.불출:
                            ret = common.AmountCheck(target);

                            if (ret.RESULT)
                            {
                                target["new_i_cur_number"] -= Convert.ToInt32(target["new_issuaranceamount"])
                            }
                            else
                            {
                                throw new InvalidPluginExecutionException(ret.MSG);
                            }

                            break;

                        //폐기
                        case (int)Event.폐기:
                            target["new_i_cur_number"] = 0;
                            break;

                        //리뷰
                        case (int)Event.리뷰:
                            target["statuscode"] = new OptionSetValue(2);
                            target["statecode"]  = new OptionSetValue(1);
                            break;
                        }
                    }

                    //#endregion


                    #region ID 인증 관련

                    //LDAP Check.


                    Common.IN_AD_LOGIN loginMetaData = new Common.IN_AD_LOGIN();


                    loginMetaData.ID = target["new_txt_id"].ToString();
                    loginMetaData.PW = target["new_txt_pw"].ToString();


                    ret = common.GET_AD_LOGIN(loginMetaData);

                    if (ret.RESULT == false)
                    {
                        throw new InvalidPluginExecutionException(ret.MSG);
                    }
                    #endregion

                    #region 입력 필드 초기화

                    //target["new_p_event"] = null;
                    //target["new_l_change_location"] = null;
                    //target["new_issuaranceamount"] = null;
                    //target["new_ntxt_comment"] = null;
                    //target["new_txt_id"] = null;
                    //target["new_txt_pw"] = null;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("<span style=' font-family: Segoe UI Light, Segoe UI, Tahoma, Arial !important; font-weight: lighter !important;font-size: 27px !important;' >" + ex.Message + "</span>" + "\n");
            }
        }
        /// <summary>
        /// Main entry point for he business logic that the plug-in is to execute.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }


            try
            {
                lock (SyncLock)
                {
                    IPluginExecutionContext context = localContext.PluginExecutionContext;
                    IOrganizationService    service = localContext.OrganizationService;
                    Common common = new Common();

                    if (context.InputParameters["Target"] is Entity)
                    {
                        if (context.Depth < 2)
                        {
                            Entity target = (Entity)context.InputParameters["Target"];

                            Entity my = service.Retrieve("new_reservesampledetail", target.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_l_reserve",
                                                                                                                                     "new_txt_qty",
                                                                                                                                     "new_i_cur_number",
                                                                                                                                     "new_i_volume",
                                                                                                                                     "new_txt_qty",
                                                                                                                                     "new_chk_dispose",
                                                                                                                                     "statuscode",
                                                                                                                                     "statecode"
                                                                                                                                     ));

                            // 임시코드 재 migration 후 삭제 To Do
                            if (!my.Contains("new_chk_dispose"))
                            {
                                my["new_chk_dispose"] = false;
                            }
                            //

                            Common.OUT_PARAM ret = new Common.OUT_PARAM();
                            ret.RESULT = true;
                            ret.MSG    = "";
                            OptionSetValue eventstate = new OptionSetValue();


                            // 비활성화 -> 활성화
                            if (((OptionSetValue)my["statuscode"]).Value == 2 &&
                                ((OptionSetValue)my["statecode"]).Value == 1
                                )
                            {
                                //권한을 어떻게 줘야할지 논의 필요 To Do
                                //throw new InvalidPluginExecutionException("Permission Denied. Please contact the administrator");
                            }
                            //활성화 상태
                            else
                            {
                                #region 이벤트에 따른 기능
                                if (target.Contains("new_p_event"))
                                {
                                    eventstate = (OptionSetValue)target["new_p_event"];

                                    switch (eventstate.Value)
                                    {
                                    //이동
                                    case (int)Event.이동:
                                        ret = common.LocationCheck(target, my);
                                        if (ret.RESULT)
                                        {
                                            target["new_l_clocation"] = (EntityReference)target["new_l_change_location"];
                                        }

                                        break;

                                    //불출
                                    case (int)Event.불출:

                                        ret = common.AmountCheck(target, my);

                                        if (ret.RESULT)
                                        {
                                            target["new_i_cur_number"] = (Convert.ToInt32(my["new_i_cur_number"])) - (Convert.ToInt32(target["new_i_issuaranceamount"]));

                                            if (Convert.ToInt32(target["new_i_cur_number"]) == 0)
                                            {
                                                target["new_chk_dispose"] = true;
                                            }
                                        }
                                        break;

                                    //폐기
                                    case (int)Event.폐기:
                                        ret = common.DisposalCheck(target, my);

                                        if (ret.RESULT)
                                        {
                                            target["new_i_cur_number"] = 0;
                                            target["new_chk_dispose"]  = true;
                                        }

                                        break;

                                    //리뷰
                                    case (int)Event.리뷰:
                                        ret = common.ReviewCheck(target, my);

                                        if (ret.RESULT)
                                        {
                                            target["statuscode"] = new OptionSetValue(2);
                                            target["statecode"]  = new OptionSetValue(1);
                                        }
                                        target["new_l_reviewer"] = new EntityReference("systemuser", context.UserId);
                                        target["new_dt_review"]  = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));

                                        break;

                                    //수정
                                    case (int)Event.수정:
                                        if ((EntityReference)target["new_l_change_location"] != null)
                                        {
                                            target["new_l_clocation"] = (EntityReference)target["new_l_change_location"];
                                        }
                                        if (target["new_i_issuaranceamount"] != null)
                                        {
                                            target["new_i_cur_number"] = Convert.ToInt32(target["new_i_issuaranceamount"]);
                                        }
                                        break;

                                    default:
                                        break;
                                    }

                                    if (!ret.RESULT)
                                    {
                                        throw new InvalidPluginExecutionException(ret.MSG);
                                    }
                                }

                                #endregion

                                #region ID 인증 관련
                                //ret = common.GET_AD_LOGIN(target, context, service);


                                //if (!ret.RESULT) throw new InvalidPluginExecutionException(ret.MSG);


                                #endregion

                                #region 자동 입력 값
                                //Quantity Setting. ( Number * Amount * Unit )
                                if (eventstate.Value == (int)Event.불출 ||
                                    eventstate.Value == (int)Event.폐기 ||
                                    eventstate.Value == (int)Event.수정)
                                {
                                    Entity mother = service.Retrieve("new_reservesample", ((EntityReference)my["new_l_reserve"]).Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_p_unit", "new_p_type"));
                                    target["new_txt_qty"] = common.QuantitySetUp(target, my, mother);
                                }

                                //history setting.
                                //target["new_txt_inputhistory"] = common.InputHistory(target, service);
                                #endregion

                                #region 입력 필드 초기화


                                target["new_txt_id"] = null;
                                target["new_txt_pw"] = null;
                                #endregion
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("<span style=' font-family: Segoe UI Light, Segoe UI, Tahoma, Arial !important; font-weight: lighter !important;font-size: 27px !important;' >" + ex.Message + "</span>" + "\n");
            }
        }