Ejemplo n.º 1
0
        public virtual EventLog GetEventLogInfo(CallContext outputContext, T savedEntity)
        {
            EventLog result = new EventLog();

            int    currentConsumerID = Constants.INVALID_ID;
            Person currentPerson     = null;

            if (!string.IsNullOrEmpty(outputContext.CurrentConsumerSessionId) && !string.IsNullOrEmpty(outputContext.CurrentConsumerNames))
            {
                result.idUser          = int.Parse(outputContext.CurrentConsumerID);
                result.SessionId       = outputContext.CurrentConsumerSessionId;
                result.PersonName      = outputContext.CurrentConsumerNames;
                result.Page            = outputContext.Page;
                result.EventMessage    = outputContext.SecuritySettingBG;
                result.SecuritySetting = outputContext.SecuritySetting;
            }
            else
            {
                if (Int32.TryParse(outputContext.CurrentConsumerID, out currentConsumerID))
                {
                    currentPerson = new UserBL().GetCurrentPersonByUserId(Int32.Parse(outputContext.CurrentConsumerID));
                }
                else
                {
                    currentConsumerID = Constants.INVALID_ID;
                }

                result.PersonName = currentPerson != null ? currentPerson.FullName : " неопределен ";
                result.idUser     = currentConsumerID;
            }
            try
            {
                result.EventDate = DateTime.Now;
                //  result.EventAction = outputContext.securitySettings.ToString();
                result.EventAction = outputContext.ActionName;
                result.EntityID    = savedEntity.EntityID.ToString();
                result.EntityName  = savedEntity.GetType().FullName;



                if (string.IsNullOrEmpty(result.EventMessage) && outputContext.securitySettings.ToString().Contains("Save"))
                {
                    result.EventMessage = "Запис на данни";
                }
            }
            catch (Exception e)
            {
                BaseHelper.Log("Грешка при създаване на EventLog за entity " + this.savedEntity.GetType().FullName);
                BaseHelper.Log(e.Message);



                BaseHelper.LogToMail("Грешка при създаване на EventLog за entity " + this.savedEntity.GetType().FullName);
                BaseHelper.LogToMail(e.Message);
            }

            return(result);
        }
Ejemplo n.º 2
0
        protected EventLog MakeEventLog(CallContext outputContext, T savedEntity)
        {
            EventLog eventLog = null;

            try
            {
                if (outputContext.ResultCode == ETEMEnums.ResultEnum.Success ||
                    outputContext.ResultCode == ETEMEnums.ResultEnum.Warning)
                {
                    eventLog = GetEventLogInfo(outputContext, savedEntity);
                    this.dbContext.AddToEventLogs(eventLog);
                    this.dbContext.SaveChanges();
                }
            }
            catch (ETEMModelException e)
            {
                BaseHelper.Log("Грешка при създаване на EventLog на entity " + savedEntity.GetType().FullName);
                BaseHelper.Log(e.Message);
            }


            return(eventLog);
        }
Ejemplo n.º 3
0
        private void MakeFullDbLog(List <Tuple <EventLog, T> > itemsToLog)
        {
            //to make full db log the Entity needs to be ILoggable
            try
            {
                using (dbContextLog = new UMS_LOGEntities())
                {
                    for (int i = 0; i < itemsToLog.Count; i++)
                    {
                        EventFullLog eventFullLog = new EventFullLog();
                        SetEventLogInfoFull(eventFullLog, itemsToLog[i].Item1);


                        XmlDocument doc = XMLLogHelper.CreateNewXMLDoc(eventFullLog.EntityActionName);
                        SetAllProperties(doc, itemsToLog[i].Item2);
                        eventFullLog.FullLogPropertiesXML = XMLLogHelper.SringValueOf(doc);


                        //make async log in a different database by the simple log and the entity that is being saved
                        if (typeof(ICustomLoggable).IsAssignableFrom(typeof(T)))
                        {
                            ((ICustomLoggable)itemsToLog[i].Item2).CreateMeaningfulEntityLogXML(eventFullLog);
                        }


                        dbContextLog.AddToEventFullLogs(eventFullLog);
                        dbContextLog.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                BaseHelper.Log("Грешка при детайлно създаване на EventLog на entity " + savedEntity.GetType().FullName);
                BaseHelper.Log(e.Message);
            }
        }
Ejemplo n.º 4
0
        public virtual CallContext EntityDelete <E>(List <T> entities, CallContext inputContext)
        {
            CallContext outputContext = new CallContext();

            outputContext.CurrentConsumerID = inputContext.CurrentConsumerID;
            outputContext.ResultCode        = ETEMEnums.ResultEnum.Success;
            // outputContext.securitySettings = inputContext.securitySettings;
            outputContext.ActionName = Constants.ACTION_DELETE;

            try
            {
                SetCallContextDataFromSession(outputContext);

                //outputContext.CurrentConsumerNames = props.PersonTwoNamePlusTitle;
                //outputContext.CurrentConsumerSessionId = props.SessionID;
                //outputContext.CurrentConsumerID = props.IdUser;
                //
            }
            catch (Exception ex)
            {
                outputContext.CurrentConsumerID = inputContext.CurrentConsumerID;
                BaseHelper.Log("Грешка създаване на обекта сесия в модела на приложението!");
                BaseHelper.Log(ex.Message);
            }

            List <Tuple <EventLog, T> > itemsToLog = new List <Tuple <EventLog, T> >();

            if (entities.Count > 0)
            {
                for (int i = 0; i < entities.Count; i++)
                {
                    var entity = entities[i];
                    if (!HasUserActionPermission(entity, outputContext, inputContext))
                    {
                        return(outputContext);
                    }

                    if (entity != null && entity.EntityID != Constants.INVALID_ID_ZERO && entity.EntityID != Constants.INVALID_ID)
                    {
                        savedEntity = GetEntityById(entity.EntityID);

                        if (savedEntity != null)
                        {
                            outputContext.EntityID = savedEntity.EntityID.ToString();

                            outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_not_deleted");
                            outputContext.ResultCode = ETEMEnums.ResultEnum.Error;


                            dbContext.DeleteObject(savedEntity);

                            dbContext.SaveChanges();

                            outputContext.listKvEntityID.Add(new KeyValuePair <string, int>(EntitySetName, entity.EntityID));
                            outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_deleted_successful");
                            outputContext.ResultCode = ETEMEnums.ResultEnum.Success;

                            EventLog simpleLog = MakeEventLog(outputContext, savedEntity);
                            itemsToLog.Add(new Tuple <EventLog, T>(simpleLog, entity));
                        }
                        else
                        {
                            outputContext.ResultCode = ETEMEnums.ResultEnum.Warning;
                            outputContext.Message    = String.Format(BaseHelper.GetCaptionString("Entity_Not_Found_By_ID"), Constants.INVALID_ID);
                        }
                    }
                    else
                    {
                        outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_not_created");
                        outputContext.ResultCode = ETEMEnums.ResultEnum.Error;
                        outputContext.EntityID   = Constants.INVALID_ID_STRING;
                    }
                }


                if (itemsToLog.Count > 0)
                {
                    Thread t = new Thread(() => MakeFullDbLog(itemsToLog));
                    t.Start();
                }

                return(outputContext);
            }
            else
            {
                outputContext.ResultCode = ETEMEnums.ResultEnum.Error;
                outputContext.Message    = BaseHelper.GetCaptionString("The_list_of_entities_was_empty");
                return(outputContext);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Saves list of Entities
        /// </summary>
        /// <typeparam name="E"></typeparam>
        /// <param name="entities"></param>
        /// <param name="inputContext"></param>
        /// <param name="setNull"> this param is flag for refrection save, and it allows to set null or string empry as value of some prop</param>
        /// <param name="propertiesToChange">if this is != null, it means that only selected protoperties of the Entity will be saved</param>
        /// <returns></returns>

        public virtual CallContext EntitySave <E>(List <T> entities, CallContext inputContext, List <string> propertiesToChange = null, bool?allowNullAsPropvalue = null) where E : Identifiable
        {
            CallContext outputContext = new CallContext();

            outputContext.CurrentConsumerID = inputContext.CurrentConsumerID;

            try
            {
                SetCallContextDataFromSession(outputContext);

                //outputContext.CurrentConsumerNames = props.PersonTwoNamePlusTitle;
                //outputContext.CurrentConsumerSessionId = props.SessionID;
                //outputContext.CurrentConsumerID = props.IdUser;
                //
            }
            catch (Exception ex)
            {
                outputContext.CurrentConsumerID = inputContext.CurrentConsumerID;
                BaseHelper.Log("Грешка създаване на обекта сесия в модела на приложението!");
                BaseHelper.Log(ex.Message);
            }



            outputContext.PersonType       = inputContext.PersonType;
            outputContext.ResultCode       = ETEMEnums.ResultEnum.Success;
            outputContext.securitySettings = inputContext.securitySettings;
            outputContext.CurrentYear      = inputContext.CurrentYear;
            outputContext.CurrentPeriod    = inputContext.CurrentPeriod;


            List <Tuple <EventLog, T> > itemsToLog = new List <Tuple <EventLog, T> >();

            for (int i = 0; i < entities.Count; i++)
            {
                var entity = entities[i];
                if (!HasUserActionPermission(entity, outputContext, inputContext))
                {
                    return(outputContext);
                }

                if (entity.ValidateEntity(outputContext).Count > 0)
                {
                    return(outputContext);
                }

                if (entity != null && entity.EntityID != Constants.INVALID_ID_ZERO && entity.EntityID != Constants.INVALID_ID)
                {
                    savedEntity = GetEntityById(entity.EntityID);

                    if (savedEntity != null)
                    {
                        outputContext.ActionName = Constants.ACTION_UPDATE;
                        if (propertiesToChange == null)
                        {
                            EntityToEntity(entity, savedEntity);
                        }
                        else
                        {
                            BaseGenericHelper <T> .EntityToEntityByReflection(entity, savedEntity, propertiesToChange, allowNullAsPropvalue);
                        }

                        if (savedEntity is IModifiable)
                        {
                            ((IModifiable)savedEntity).SetModificationData(outputContext);
                        }

                        outputContext.EntityID = savedEntity.EntityID.ToString();

                        outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_not_update");
                        outputContext.ResultCode = ETEMEnums.ResultEnum.Error;

                        try
                        {
                            dbContext.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            BaseHelper.Log("Грешка в BaseClassBL.EntitySave() - Update");
                            BaseHelper.Log(ex.Message);
                            BaseHelper.Log(ex.StackTrace);
                        }

                        outputContext.listKvEntityID.Add(new KeyValuePair <string, int>(EntitySetName, entity.EntityID));
                        outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_update_successful");
                        outputContext.ResultCode = ETEMEnums.ResultEnum.Success;

                        EventLog simpleLog = MakeEventLog(outputContext, savedEntity);
                        itemsToLog.Add(new Tuple <EventLog, T>(simpleLog, savedEntity));
                    }
                    else
                    {
                        outputContext.ResultCode = ETEMEnums.ResultEnum.Warning;
                        outputContext.Message    = String.Format(BaseHelper.GetCaptionString("Entity_Not_Found_By_ID"), Constants.INVALID_ID);
                    }
                }
                else
                {
                    outputContext.Message    = BaseHelper.GetCaptionString("Entity_is_not_created");
                    outputContext.ResultCode = ETEMEnums.ResultEnum.Error;
                    outputContext.EntityID   = Constants.INVALID_ID_STRING;

                    if (string.IsNullOrEmpty(this.EntitySetName))
                    {
                        outputContext.Message = BaseHelper.GetCaptionString("EntitySetName_is_not_set");
                    }

                    if (entity is IModifiable)
                    {
                        ((IModifiable)entity).SetCreationData(outputContext);
                    }

                    dbContext.AddObject(this.EntitySetName, entity);

                    try
                    {
                        dbContext.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        BaseHelper.Log("Грешка в BaseClassBL.EntitySave() - Create");
                        BaseHelper.Log(ex.Message);
                        BaseHelper.Log(ex.StackTrace);
                    }

                    outputContext.ActionName = Constants.ACTION_INSERT;
                    outputContext.ResultCode = ETEMEnums.ResultEnum.Success;
                    outputContext.EntityID   = entity.EntityID.ToString();
                    outputContext.listKvEntityID.Add(new KeyValuePair <string, int>(EntitySetName, entity.EntityID));
                    outputContext.Message = BaseHelper.GetCaptionString("Entity_is_created_successful");

                    EventLog simpleLog = MakeEventLog(outputContext, entity);

                    itemsToLog.Add(new Tuple <EventLog, T>(simpleLog, entity));
                }
            }


            var     application = System.Web.HttpContext.Current.Application;
            Setting setting     = new Setting();

            Dictionary <string, Setting> dictionarySetting = new Dictionary <string, Setting>();

            if (application != null && application[Constants.APPLICATION_SETTING_LIST] != null)
            {
                dictionarySetting = application[Constants.APPLICATION_SETTING_LIST] as Dictionary <string, Setting>;

                if (dictionarySetting == null)
                {
                    setting = new Setting()
                    {
                        SettingName         = "Създаване на подробен лог в базата данни(YES|NO)",
                        SettingDescription  = "Създаване на подробен лог в базата данни(YES|NO)",
                        SettingIntCode      = ETEMEnums.AppSettings.MakeLogInDB.ToString(),
                        SettingValue        = "YES",
                        SettingDefaultValue = "YES",
                        SettingClass        = ETEMEnums.AppSettingsClass.String.ToString()
                    };

                    dictionarySetting.Add(ETEMEnums.AppSettings.MakeLogInDB.ToString(), setting);
                }
            }
            else
            {
                setting = new Setting()
                {
                    SettingName         = "Създаване на подробен лог в базата данни(YES|NO)",
                    SettingDescription  = "Създаване на подробен лог в базата данни(YES|NO)",
                    SettingIntCode      = ETEMEnums.AppSettings.MakeLogInDB.ToString(),
                    SettingValue        = "YES",
                    SettingDefaultValue = "YES",
                    SettingClass        = ETEMEnums.AppSettingsClass.String.ToString()
                };
                dictionarySetting.Add(ETEMEnums.AppSettings.MakeLogInDB.ToString(), setting);
            }

            setting = dictionarySetting.Where(k => k.Key == ETEMEnums.AppSettings.MakeLogInDB.ToString()).FirstOrDefault().Value;

            if (setting.SettingValue == "YES")
            {
                if (itemsToLog.Count > 0)
                {
                    Thread t = new Thread(() => MakeFullDbLog(itemsToLog));
                    t.Start();
                }
            }

            return(outputContext);
        }