private BWebServiceResponse DeleteRevision(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!CommonMethods.DoesRevisionExist( Model, RequestedRevisionIndex, out Revision RevisionObject, out int ModelRevisionListIx)) { return(BWebResponse.NotFound("Revision does not exist.")); } Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryDeleteAll ( RequestedModelID, RequestedRevisionIndex, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID, JObject.Parse(JsonConvert.SerializeObject(RevisionObject.FileEntry)) ), _ErrorMessageAction); Model.ModelRevisions.RemoveAt(ModelRevisionListIx); 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_ModelRevisionDeleted ( RequestedModelID, RequestedRevisionIndex, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusOK("Revision has been deleted.")); }
private BWebServiceResponse ProcessRequestLocked(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { var RequestedModelName = WebUtility.UrlDecode(RestfulUrlParameters[RestfulUrlParameter_ModelsKey]); if (!CommonMethods.TryGettingModelID( DatabaseService, RequestedModelName, out RequestedModelID, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } RequestedUserID = RestfulUrlParameters[RestfulUrlParameter_UserIDKey]; if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } var OldSharedList = new List <string>(Model.ModelSharedWithUserIDs); if (!Model.ModelSharedWithUserIDs.Remove(RequestedUserID)) { return(BWebResponse.NotFound("Given userId is not among sharees of the model.")); } 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_ModelSharedWithUserIdsChanged ( RequestedModelID, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, OldSharedList, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusOK("Operation has been completed.")); }
private BWebServiceResponse ProcessRequestLocked(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { var RequestedModelName = WebUtility.UrlDecode(RestfulUrlParameters[RestfulUrlParameter_ModelsKey]); if (!CommonMethods.TryGettingModelID( DatabaseService, RequestedModelName, out RequestedModelID, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } string RequestPayload = null; JObject ParsedBody; using (var InputStream = _Context.Request.InputStream) { using var ResponseReader = new StreamReader(InputStream); try { RequestPayload = ResponseReader.ReadToEnd(); ParsedBody = JObject.Parse(RequestPayload); } catch (Exception e) { _ErrorMessageAction?.Invoke("Model_ChangeSharingWithUsers-> Malformed request body. Body content: " + RequestPayload + ", Exception: " + e.Message + ", Trace: " + e.StackTrace); return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form.")); } } var bShareWithAllExists = ParsedBody.ContainsKey("shareWithAll"); var bEmailsExist = ParsedBody.ContainsKey("emails"); var bUserIdsExist = ParsedBody.ContainsKey("userIds"); var FinalUserIds = new List <string>(); bool bShareWithAll = false; if (bShareWithAllExists /*Exist*/) { if (ParsedBody["shareWithAll"].Type != JTokenType.Boolean) { return(BWebResponse.BadRequest("Request is invalid.")); } bShareWithAll = (bool)ParsedBody["shareWithAll"]; if (bShareWithAll) { if (bEmailsExist || bUserIdsExist) { return(BWebResponse.BadRequest("Request has shareWithAll field; therefore cannot have emails or userIds.")); } FinalUserIds.Add("*"); } } if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!bShareWithAll) { var EmailAddresses = new List <string>(); if (bEmailsExist) { if (ParsedBody["emails"].Type != JTokenType.Array) { return(BWebResponse.BadRequest("Request is invalid.")); } var AsJArray = (JArray)ParsedBody["emails"]; foreach (var Token in AsJArray) { if (Token.Type != JTokenType.String) { return(BWebResponse.BadRequest("Request is invalid.")); } var Lowered = ((string)Token).ToLower(); if (!EmailAddresses.Contains(Lowered)) { EmailAddresses.Add(Lowered); } } } if (bUserIdsExist) { if (ParsedBody["userIds"].Type != JTokenType.Array) { return(BWebResponse.BadRequest("Request is invalid.")); } var AsJArray = (JArray)ParsedBody["userIds"]; foreach (var Token in AsJArray) { if (Token.Type != JTokenType.String) { return(BWebResponse.BadRequest("Request is invalid.")); } var Lowered = ((string)Token).ToLower(); if (!FinalUserIds.Contains(Lowered)) { FinalUserIds.Add(Lowered); } } } if (EmailAddresses.Count > 0) { GetTracingService()?.On_FromServiceToService_Sent(_Context, _ErrorMessageAction); var EmailsJArray = new JArray(); foreach (var CurEmail in EmailAddresses) { EmailsJArray.Add(CurEmail); } var RequestObject = new JObject() { ["emailAddresses"] = EmailsJArray }; var Result = BWebServiceExtraUtilities.InterServicesRequest(new BWebServiceExtraUtilities.InterServicesRequestRequest() { DestinationServiceUrl = AuthServiceEndpoint + "/auth/internal/fetch_user_ids_from_emails?secret=" + InternalCallPrivateKey, RequestMethod = "POST", bWithAuthToken = true, UseContextHeaders = _Context, ContentType = "application/json", Content = new BStringOrStream(RequestObject.ToString()), ExcludeHeaderKeysForRequest = null }, false, _ErrorMessageAction); GetTracingService()?.On_FromServiceToService_Received(_Context, _ErrorMessageAction); if (!Result.bSuccess || Result.ResponseCode >= 400) { return(new BWebServiceResponse(Result.ResponseCode, Result.Content, Result.ContentType)); } string InterServicesRequestStringResponse = null; JObject Map; try { InterServicesRequestStringResponse = Result.Content.String; var Json = JObject.Parse(InterServicesRequestStringResponse); Map = (JObject)Json["map"]; } catch (Exception e) { _ErrorMessageAction?.Invoke("Model_ChangeSharingWithUsers-> Malformed request body has been returned from auth service. Body content: " + InterServicesRequestStringResponse + ", Exception: " + e.Message + ", Trace: " + e.StackTrace); return(BWebResponse.InternalError("Malformed request body has been returned from auth service.")); } foreach (var UserIdToken in Map.Values()) { if (UserIdToken.Type == JTokenType.String) { var UserId = (string)UserIdToken; if (!FinalUserIds.Contains(UserId)) { FinalUserIds.Add(UserId); } } } } } if (Model.ModelSharedWithUserIDs.OrderBy(t => t).SequenceEqual(FinalUserIds.OrderBy(t => t))) { return(BWebResponse.StatusOK("No change has been done.")); } var OldSharedList = new List <string>(Model.ModelSharedWithUserIDs); Model.ModelSharedWithUserIDs = FinalUserIds; Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID), JObject.Parse(JsonConvert.SerializeObject(Model))); var bOldHasStar = OldSharedList.Contains("*"); var bNewHasStar = FinalUserIds.Contains("*"); if (bOldHasStar && !bNewHasStar) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, GloballySharedModelIDsDBEntry.DBSERVICE_GLOBALLY_SHARED_MODEL_IDS_TABLE(), GloballySharedModelIDsDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID)); } else if (!bOldHasStar && bNewHasStar) { Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, GloballySharedModelIDsDBEntry.DBSERVICE_GLOBALLY_SHARED_MODEL_IDS_TABLE(), GloballySharedModelIDsDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID), JObject.Parse(JsonConvert.SerializeObject(new GloballySharedModelIDsDBEntry()))); } Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelSharedWithUserIdsChanged ( RequestedModelID, Model.ModelOwnerUserID, FinalUserIds, OldSharedList, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusOK("Operation has been completed.")); }
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 UpdateRevisionInfo(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject UpdatedRevisionJson, true, out Revision UpdatedRevision, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!CommonMethods.DoesRevisionExist( Model, RequestedRevisionIndex, out Revision RevisionObject, out int _)) { return(BWebResponse.NotFound("Revision does not exist.")); } if (UpdatedRevision.RevisionName != null && UpdatedRevision.RevisionName.Length > 0 && RevisionObject.RevisionName != UpdatedRevision.RevisionName) { //There is a change in the revision name foreach (var CurrentRev in Model.ModelRevisions) { if (CurrentRev.RevisionName.ToUpper() == UpdatedRevision.RevisionName) { return(BWebResponse.Conflict("A revision with same " + Revision.REVISION_NAME_PROPERTY + " already exists.")); } } } RevisionObject.Merge(UpdatedRevisionJson); 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_ModelRevisionUpdated ( RequestedModelID, RequestedRevisionIndex, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID, UpdatedRevisionJson ), _ErrorMessageAction); return(BWebResponse.StatusOK("Revision has been updated.")); }
private BWebServiceResponse DeleteModel(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!Controller_AttributeTables.Get().AddRemoveMetadataSets_AttributesTables( InnerDeliveryEnsurerUserProcessor, Model.ModelOwnerUserID, Controller_AttributeTables.MetadataLocator.ItIsModelMetadata(RequestedModelID), Model.ModelMetadata, Controller_AttributeTables.EAddRemove.Remove, Controller_AttributeTables.EKillProcedureIfGetClearanceFails.Yes, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } foreach (var Rev in Model.ModelRevisions) { Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryDeleteAll ( RequestedModelID, Rev.RevisionIndex, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID, JObject.Parse(JsonConvert.SerializeObject(Rev.FileEntry)) ), _ErrorMessageAction); } Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueFileFieldsDBEntry.DBSERVICE_UNIQUEFILEFIELDS_TABLE(), UniqueFileFieldsDBEntry.KEY_NAME_MODEL_UNIQUE_NAME, new BPrimitiveType(Model.ModelName)); var RequestedModelIDPrimitive = new BPrimitiveType(RequestedModelID); Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, RequestedModelIDPrimitive); if (Model.ModelSharedWithUserIDs.Contains("*")) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, GloballySharedModelIDsDBEntry.DBSERVICE_GLOBALLY_SHARED_MODEL_IDS_TABLE(), GloballySharedModelIDsDBEntry.KEY_NAME_MODEL_ID, RequestedModelIDPrimitive); } Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelDeleted ( RequestedModelID, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusOK("Model has been deleted.")); }
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); } } }
private BWebServiceResponse DeleteRawFile(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryGettingAllInfo( DatabaseService, RequestedModelID, RequestedRevisionIndex, out ModelDBEntry ModelObject, out Revision RevisionObject, out int ModelRevisionListIx, out BWebServiceResponse _FailureResponse, _ErrorMessageAction)) { return(_FailureResponse); } var PreviousProcessStage = RevisionObject.FileEntry.FileProcessStage; if (PreviousProcessStage == (int)Constants.EProcessStage.NotUploaded) { return(BWebResponse.NotFound("Raw files have not been uploaded.")); } //if (RevisionObject.FileEntry.FileProcessStage == (int)Constants.EProcessStage.Uploaded_Processing) //{ // var RequestObject = new JObject() // { // ["bucketName"] = CadFileStorageBucketName, // ["rawFileRelativeUrl"] = RevisionObject.FileEntry.RawFileRelativeUrl // }; // GetTracingService()?.On_FromServiceToService_Sent(_Context, _ErrorMessageAction); // var Result = BWebUtilities_GC_CloudRun.InterServicesRequest(new BWebUtilities_GC_CloudRun.InterServicesRequestRequest() // { // DestinationServiceUrl = CadProcessServiceEndpoint + "/3d/process/stop", // RequestMethod = "POST", // ContentType = "application/json", // Content = new BStringOrStream(RequestObject.ToString()), // bWithAuthToken = false, //Kubernetes Service // UseContextHeaders = _Context, // ExcludeHeaderKeysForRequest = null // }, // false, // _ErrorMessageAction); // GetTracingService()?.On_FromServiceToService_Received(_Context, _ErrorMessageAction); // if (!Result.bSuccess || Result.ResponseCode >= 400) // { // return new BWebServiceResponse(Result.ResponseCode, Result.Content, Result.ContentType); // } //} Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryDeleteAll ( RequestedModelID, RequestedRevisionIndex, ModelObject.ModelOwnerUserID, ModelObject.ModelSharedWithUserIDs, AuthorizedUser.UserID, JObject.Parse(JsonConvert.SerializeObject(RevisionObject.FileEntry)) ), _ErrorMessageAction); var FileType = RevisionObject.FileEntry.FileEntryFileType; RevisionObject.FileEntry = new FileEntry() { FileEntryFileType = FileType }; RevisionObject.FileEntry.SetRelativeUrls_GetCommonUrlPart_FileEntryFileTypePreSet(RequestedModelID, RequestedRevisionIndex); ModelObject.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(ModelObject))); Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryDeleted ( RequestedModelID, RequestedRevisionIndex, ModelObject.ModelOwnerUserID, ModelObject.ModelSharedWithUserIDs, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusOK("Raw " + (PreviousProcessStage == (int)Constants.EProcessStage.Uploaded_Processed ? "and processed models have " : "model has ") + "been deleted.")); }
private BWebServiceResponse UpdateRawFile(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject UpdatedFileEntryJson, true, out FileEntry CheckForGenerateUploadUrl, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } bool bGenerateUploadUrl = CheckForGenerateUploadUrl.bGenerateUploadUrl; if (bGenerateUploadUrl) { UpdatedFileEntryJson[FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY] = CheckForGenerateUploadUrl.FileEntryFileType.TrimStart('.').ToLower(); } else { if (UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_NAME_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.ZIP_MAIN_ASSEMBLY_FILE_NAME_IF_ANY_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.DATA_SOURCE_PROPERTY)) { return(BWebResponse.BadRequest("Name|type related fields can only be set when " + FileEntry.GENERATE_UPLOAD_URL_PROPERTY + " option is true.")); } } if (!CommonMethods.TryGettingAllInfo( DatabaseService, RequestedModelID, RequestedRevisionIndex, out ModelDBEntry ModelObject, out Revision RevisionObject, out int _, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (RevisionObject.FileEntry.FileEntryName != null && RevisionObject.FileEntry.FileEntryName.Length > 0) { if (UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_NAME_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.ZIP_MAIN_ASSEMBLY_FILE_NAME_IF_ANY_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.DATA_SOURCE_PROPERTY)) { return(BWebResponse.BadRequest("File entry (raw) must be deleted before updating.")); } } RevisionObject.FileEntry.Merge(UpdatedFileEntryJson); RevisionObject.FileEntry.FileEntryFileType = RevisionObject.FileEntry.FileEntryFileType.ToLower().TrimStart('.'); RevisionObject.FileEntry.SetRelativeUrls_GetCommonUrlPart_FileEntryFileTypePreSet(RequestedModelID, RequestedRevisionIndex); RevisionObject.FileEntry.FileEntryCreationTime = CommonMethods.GetTimeAsCreationTime(); ModelObject.MRVLastUpdateTime = RevisionObject.FileEntry.FileEntryCreationTime; string UploadUrl_IfRequested = null; if (bGenerateUploadUrl && !FileService.CreateSignedURLForUpload( out UploadUrl_IfRequested, CadFileStorageBucketName, RevisionObject.FileEntry.RawFileRelativeUrl, FileEntry.RAW_FILE_UPLOAD_CONTENT_TYPE, FileEntry.EXPIRY_MINUTES, _ErrorMessageAction)) { return(BWebResponse.InternalError("Signed url generation has failed.")); } Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID), JObject.Parse(JsonConvert.SerializeObject(ModelObject))); Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryUpdated ( RequestedModelID, RequestedRevisionIndex, ModelObject.ModelOwnerUserID, ModelObject.ModelSharedWithUserIDs, AuthorizedUser.UserID, UpdatedFileEntryJson ), _ErrorMessageAction); var ResultObject = new JObject(); if (bGenerateUploadUrl) { ResultObject[FileEntry.FILE_UPLOAD_URL_PROPERTY] = UploadUrl_IfRequested; ResultObject[FileEntry.FILE_UPLOAD_CONTENT_TYPE_PROPERTY] = FileEntry.RAW_FILE_UPLOAD_CONTENT_TYPE; ResultObject[FileEntry.FILE_DOWNLOAD_UPLOAD_EXPIRY_MINUTES_PROPERTY] = FileEntry.EXPIRY_MINUTES; } return(BWebResponse.StatusAccepted("Update raw file request has been accepted.", ResultObject)); }
private BWebServiceResponse AddRevision(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject _, true, out Revision NewRevisionObject, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (!CommonMethods.TryGettingModelInfo( DatabaseService, RequestedModelID, out JObject _, true, out ModelDBEntry Model, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } var UpperCaseNewRevisionName = NewRevisionObject.RevisionName.ToUpper(); int BiggestExistingIndex = -1; foreach (var CurrentRev in Model.ModelRevisions) { if (CurrentRev.RevisionName.ToUpper() == UpperCaseNewRevisionName) { return(BWebResponse.Conflict("A revision with same " + Revision.REVISION_NAME_PROPERTY + " already exists.")); } BiggestExistingIndex = BiggestExistingIndex < CurrentRev.RevisionIndex ? CurrentRev.RevisionIndex : BiggestExistingIndex; } int NewRevisionIndex = BiggestExistingIndex + 1; NewRevisionObject.RevisionIndex = NewRevisionIndex; NewRevisionObject.CreationTime = CommonMethods.GetTimeAsCreationTime(); Model.MRVLastUpdateTime = NewRevisionObject.CreationTime; Model.ModelRevisions.Add(NewRevisionObject); 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_ModelRevisionCreated ( RequestedModelID, NewRevisionObject.RevisionIndex, Model.ModelOwnerUserID, Model.ModelSharedWithUserIDs, AuthorizedUser.UserID ), _ErrorMessageAction); return(BWebResponse.StatusCreated("Revision has been created.", new JObject() { [Revision.REVISION_INDEX_PROPERTY] = NewRevisionIndex })); }