public static bool TryGettingModelID( IBDatabaseServiceInterface _DatabaseService, string _RequestedModelName, out string _ModelID, out BWebServiceResponse _FailureResponse, Action <string> _ErrorMessageAction) { _FailureResponse = BWebResponse.InternalError(""); _ModelID = string.Empty; if (!_DatabaseService.GetItem( UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(_RequestedModelName), UniqueFileFieldsDBEntry.Properties, out JObject ModelIDResponse, _ErrorMessageAction) || !ModelIDResponse.ContainsKey(ModelDBEntry.KEY_NAME_MODEL_ID)) { _FailureResponse = BWebResponse.InternalError("Model ID could not be retrieved from the database."); return(false); } _ModelID = (string)ModelIDResponse[ModelDBEntry.KEY_NAME_MODEL_ID]; return(true); }
private BWebServiceResponse UpdateModelInfo(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject UpdateFieldsModelEntry, false, out ModelDBEntry _, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } //If there is a change in the model name bool bGetClearanceForModelName_New = false; bool bGetClearanceForModelName_Old = false; string ModelUniqueName_New = null; string ModelUniqueName_Old = null; try { if (UpdateFieldsModelEntry.ContainsKey(ModelDBEntry.MODEL_UNIQUE_NAME_PROPERTY) && Model.ModelName != (string)UpdateFieldsModelEntry[ModelDBEntry.MODEL_UNIQUE_NAME_PROPERTY]) { ModelUniqueName_New = (string)UpdateFieldsModelEntry[ModelDBEntry.MODEL_UNIQUE_NAME_PROPERTY]; if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + ModelUniqueName_New, _ErrorMessageAction)) { return(BWebResponse.InternalError("Atomic operation control has failed.")); } bGetClearanceForModelName_New = true; ModelUniqueName_Old = Model.ModelName; if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + ModelUniqueName_Old, _ErrorMessageAction)) { return(BWebResponse.InternalError("Atomic operation control has failed.")); } bGetClearanceForModelName_Old = true; //Put the new one if (!DatabaseService.UpdateItem( UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(ModelUniqueName_New), new JObject() { [ModelDBEntry.KEY_NAME_MODEL_ID] = RequestedModelID }, out JObject _, EBReturnItemBehaviour.DoNotReturn, DatabaseService.BuildAttributeNotExistCondition(UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME), _ErrorMessageAction)) { if (!DatabaseService.GetItem( UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(ModelUniqueName_New), UniqueFileFieldsDBEntry.Properties, out JObject ModelIDResponse, _ErrorMessageAction) || !ModelIDResponse.ContainsKey(ModelDBEntry.KEY_NAME_MODEL_ID)) { return(BWebResponse.InternalError("Model ID could not be retrieved upon conflict.")); } var Result = JObject.Parse(BWebResponse.Error_Conflict_String("Attribute " + UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + " must be globally unique.")); Result[ModelDBEntry.KEY_NAME_MODEL_ID] = (string)ModelIDResponse[ModelDBEntry.KEY_NAME_MODEL_ID]; return(new BWebServiceResponse( BWebResponse.Error_Conflict_Code, new BStringOrStream(Result.ToString()), BWebResponse.Error_Conflict_ContentType)); } //Delete the old one Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(ModelUniqueName_Old)); } var OldModelMetadataSet = Model.ModelMetadata; Model.Merge(UpdateFieldsModelEntry); var NewModelMetadataSet = Model.ModelMetadata; if (OldModelMetadataSet != NewModelMetadataSet) { var MetaLocator = Controller_AttributeTables.MetadataLocator.ItIsModelMetadata(RequestedModelID); //First remove all metadata sets Controller_AttributeTables.Get().AddRemoveMetadataSets_AttributesTables( InnerDeliveryEnsurerUserProcessor, Model.ModelOwnerUserID, MetaLocator, OldModelMetadataSet, Controller_AttributeTables.EAddRemove.Remove, Controller_AttributeTables.EKillProcedureIfGetClearanceFails.No, out BWebServiceResponse _, _ErrorMessageAction); //Then add new metadata sets Controller_AttributeTables.Get().AddRemoveMetadataSets_AttributesTables( InnerDeliveryEnsurerUserProcessor, Model.ModelOwnerUserID, MetaLocator, NewModelMetadataSet, Controller_AttributeTables.EAddRemove.Add, Controller_AttributeTables.EKillProcedureIfGetClearanceFails.No, out BWebServiceResponse _, _ErrorMessageAction); } Model.MRVLastUpdateTime = CommonMethods.GetTimeAsCreationTime(); Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID), JObject.Parse(JsonConvert.SerializeObject(Model))); Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelUpdated ( RequestedModelID, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID, UpdateFieldsModelEntry ), _ErrorMessageAction); return(BWebResponse.StatusOK("Model has been successfully updated.")); } finally { if (bGetClearanceForModelName_New) { Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + ModelUniqueName_New, _ErrorMessageAction); } if (bGetClearanceForModelName_Old) { Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + ModelUniqueName_Old, _ErrorMessageAction); } } }
private BWebServiceResponse CreateModel(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject _, true, out ModelDBEntry NewModelObject, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } NewModelObject.ModelOwnerUserID = AuthorizedUser.UserID; if (!CommonMethods.GenerateNonExistentUniqueID( this, DatabaseService, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, ModelDBEntry.MustHaveProperties, CommonMethods.EGetClearance.Yes, out string ModelID, out BWebServiceResponse _FailureResponse, _ErrorMessageAction)) { return(_FailureResponse); } bool bGetClearanceForModelName = false; try { if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + NewModelObject.ModelName, _ErrorMessageAction)) { return(BWebResponse.InternalError("Atomic operation control has failed.")); } bGetClearanceForModelName = true; if (!DatabaseService.UpdateItem( UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(NewModelObject.ModelName), new JObject() { [ModelDBEntry.KEY_NAME_MODEL_ID] = ModelID }, out JObject _, EBReturnItemBehaviour.DoNotReturn, DatabaseService.BuildAttributeNotExistCondition(UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME), _ErrorMessageAction)) { if (!DatabaseService.GetItem( UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(NewModelObject.ModelName), UniqueFileFieldsDBEntry.Properties, out JObject ModelIDResponse, _ErrorMessageAction) || !ModelIDResponse.ContainsKey(ModelDBEntry.KEY_NAME_MODEL_ID)) { return(BWebResponse.InternalError("Model ID could not be retrieved upon conflict.")); } var Result = JObject.Parse(BWebResponse.Error_Conflict_String("Attribute " + UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + " must be globally unique.")); Result[ModelDBEntry.KEY_NAME_MODEL_ID] = (string)ModelIDResponse[ModelDBEntry.KEY_NAME_MODEL_ID]; return(new BWebServiceResponse( BWebResponse.Error_Conflict_Code, new BStringOrStream(Result.ToString()), BWebResponse.Error_Conflict_ContentType)); } if (!Controller_AttributeTables.Get().AddRemoveMetadataSets_AttributesTables( InnerDeliveryEnsurerUserProcessor, NewModelObject.ModelOwnerUserID, Controller_AttributeTables.MetadataLocator.ItIsModelMetadata(ModelID), NewModelObject.ModelMetadata, Controller_AttributeTables.EAddRemove.Add, Controller_AttributeTables.EKillProcedureIfGetClearanceFails.Yes, out _FailureResponse, _ErrorMessageAction)) { return(_FailureResponse); } NewModelObject.CreationTime = CommonMethods.GetTimeAsCreationTime(); NewModelObject.MRVLastUpdateTime = NewModelObject.CreationTime; Controller_DeliveryEnsurer.Get().DB_PutItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(ModelID), JObject.Parse(JsonConvert.SerializeObject(NewModelObject))); Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelCreated ( ModelID, AuthorizedUser.UserID, new List <string>(), AuthorizedUser.AuthMethodKey ), _ErrorMessageAction); return(BWebResponse.StatusCreated("Model has been created.", new JObject() { [ModelDBEntry.KEY_NAME_MODEL_ID] = ModelID })); } finally { Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelID, _ErrorMessageAction); if (bGetClearanceForModelName) { Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME + ":" + NewModelObject.ModelName, _ErrorMessageAction); } } }