protected bool HasUserActionPermission(T entity, CallContext outputContext, CallContext inputContext) { bool res = false; ///TODO: Проверка за правата if (res) { outputContext.ResultCode = ETEMEnums.ResultEnum.Error; outputContext.Message = BaseHelper.GetCaptionString("Permission_Action_Denied"); outputContext.EntityID = entity.EntityID.ToString(); } return(true); }
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); } }
/// <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); }