public void DeleteEntity(CachedMetadata metadata, RowInfo rowInfo) { var dbSetInfo = rowInfo.dbSetInfo; if (rowInfo.changeType != ChangeType.Deleted) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetInfo.EntityType.Name, rowInfo.changeType)); } var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Delete); if (methodData == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_DELETE_NOT_IMPLEMENTED, dbSetInfo.EntityType.Name, GetType().Name)); } var dbEntity = Activator.CreateInstance(dbSetInfo.EntityType); UpdateEntityFromRowInfo(dbEntity, rowInfo, true); rowInfo.changeState.Entity = dbEntity; rowInfo.changeState.OriginalEntity = dbEntity; var instance = GetMethodOwner(methodData); methodData.methodInfo.Invoke(instance, new[] { dbEntity }); }
public void UpdateEntity(CachedMetadata metadata, RowInfo rowInfo) { var dbSetInfo = rowInfo.dbSetInfo; if (rowInfo.changeType != ChangeType.Updated) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetInfo.EntityType.Name, rowInfo.changeType)); } var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Update); if (methodData == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_UPDATE_NOT_IMPLEMENTED, dbSetInfo.EntityType.Name, GetType().Name)); } var dbEntity = Activator.CreateInstance(dbSetInfo.EntityType); UpdateEntityFromRowInfo(dbEntity, rowInfo, false); var original = GetOriginalEntity(dbEntity, rowInfo); rowInfo.changeState.Entity = dbEntity; rowInfo.changeState.OriginalEntity = original; var instance = GetMethodOwner(methodData); //apply this changes to entity that is in the database (this is done in user domain service method) methodData.methodInfo.Invoke(instance, new[] { dbEntity }); }
public static DbSetPermit GetDbSetPermissions(CachedMetadata metadata, string dbSetName, IAuthorizer authorizer) { MethodInfoData method = null; var permit = new DbSetPermit(); permit.dbSetName = dbSetName; method = metadata.getOperationMethodInfo(dbSetName, MethodType.Insert); permit.canAddRow = method != null && CanAccessMethod(method, authorizer); method = metadata.getOperationMethodInfo(dbSetName, MethodType.Update); permit.canEditRow = method != null && CanAccessMethod(method, authorizer); method = metadata.getOperationMethodInfo(dbSetName, MethodType.Delete); permit.canDeleteRow = method != null && CanAccessMethod(method, authorizer); method = metadata.getOperationMethodInfo(dbSetName, MethodType.Refresh); permit.canRefreshRow = method != null && CanAccessMethod(method, authorizer); return(permit); }
public static MethodInfoData GetCRUDMethodInfo(CachedMetadata metadata, string dbSetName, RowInfo rowInfo) { MethodInfoData method = null; switch (rowInfo.changeType) { case ChangeType.Added: method = metadata.getOperationMethodInfo(dbSetName, MethodType.Insert); break; case ChangeType.Deleted: method = metadata.getOperationMethodInfo(dbSetName, MethodType.Delete); break; case ChangeType.Updated: method = metadata.getOperationMethodInfo(dbSetName, MethodType.Update); break; default: throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetName, rowInfo.changeType)); } return(method); }
public async Task <bool> ValidateEntity(CachedMetadata metadata, RequestContext requestContext) { var validatorsContainer = metadata.ValidatorsContainer; var rowInfo = requestContext.CurrentRowInfo; var dbSetInfo = rowInfo.dbSetInfo; IEnumerable <ValidationErrorInfo> errs1 = null; IEnumerable <ValidationErrorInfo> errs2 = null; var mustBeChecked = new LinkedList <string>(); LinkedList <string> skipCheckList = null; var dataHelper = _services.DataHelper; var validationHelper = _services.ValidationHelper; if (rowInfo.changeType == ChangeType.Added) { skipCheckList = new LinkedList <string>(); foreach (var pn in rowInfo.changeState.ParentRows) { foreach (var frel in pn.association.fieldRels) { skipCheckList.AddLast(frel.childField); } } } foreach (var fieldInfo in dbSetInfo.fieldInfos) { dataHelper.ForEachFieldInfo("", fieldInfo, (fullName, f) => { if (!f.GetIsIncludeInResult()) { return; } if (f.fieldType == FieldType.Object || f.fieldType == FieldType.ServerCalculated) { return; } var value = dataHelper.SerializeField(rowInfo.changeState.Entity, fullName, f); if (rowInfo.changeType == ChangeType.Added) { var isSkip = f.isAutoGenerated || (skipCheckList != null && skipCheckList.Any(n => n == fullName)); if (!isSkip) { validationHelper.CheckValue(f, value); mustBeChecked.AddLast(fullName); } } else if (rowInfo.changeType == ChangeType.Updated) { string newVal; var isChanged = isEntityValueChanged(rowInfo, fullName, f, out newVal); if (isChanged) { validationHelper.CheckValue(f, newVal); } if (isChanged) { mustBeChecked.AddLast(fullName); } } }); } rowInfo.changeState.NamesOfChangedFields = mustBeChecked.ToArray(); var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Validate); if (methodData != null) { var instance = GetMethodOwner(methodData); var invokeRes = methodData.methodInfo.Invoke(instance, new[] { rowInfo.changeState.Entity, rowInfo.changeState.NamesOfChangedFields }); errs1 = (IEnumerable <ValidationErrorInfo>) await GetMethodResult(invokeRes).ConfigureAwait(false); } if (errs1 == null) { errs1 = Enumerable.Empty <ValidationErrorInfo>(); } var validator = validatorsContainer.GetValidator(requestContext, rowInfo.dbSetInfo.EntityType); if (validator != null) { errs2 = await validator.ValidateModelAsync(rowInfo.changeState.Entity, rowInfo.changeState.NamesOfChangedFields); } if (errs2 == null) { errs2 = Enumerable.Empty <ValidationErrorInfo>(); } errs1 = errs1.Concat(errs2); if (errs1 != null && errs1.Count() > 0) { rowInfo.changeState.ValidationErrors = errs1.ToArray(); return(false); } return(true); }