Exemplo n.º 1
0
        public async Task <ActionResult <DataElement> > Delete(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid)
        {
            _logger.LogInformation("//DataController // Delete // Starting method");

            (Instance instance, ActionResult instanceError) = await GetInstanceAsync(instanceGuid, instanceOwnerPartyId);

            if (instance == null)
            {
                return(instanceError);
            }

            (DataElement dataElement, ActionResult dataElementError) = await GetDataElementAsync(instanceGuid, dataGuid);

            if (dataElement == null)
            {
                return(dataElementError);
            }

            string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataGuid.ToString());

            await _dataRepository.DeleteDataInStorage(instance.Org, storageFileName);

            await _dataRepository.Delete(dataElement);

            await DispatchEvent(InstanceEventType.Deleted.ToString(), instance, dataElement);

            return(Ok(dataElement));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Delete(Guid instanceGuid, Guid dataId, int instanceOwnerId)
        {
            _logger.LogInformation($"//DataController // Delete // Starting method");

            string instanceId = $"{instanceOwnerId}/{instanceGuid}";

            // check if instance id exist and user is allowed to change the instance data
            Instance instance = await _instanceRepository.GetOne(instanceId, instanceOwnerId);

            if (instance == null)
            {
                return(NotFound("Provided instanceId is unknown to storage service"));
            }

            string dataIdString = dataId.ToString();

            if (instance.Data.Exists(m => m.Id == dataIdString))
            {
                string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataId.ToString());
                bool   result          = await _dataRepository.DeleteDataInStorage(storageFileName);

                if (result)
                {
                    // Update instance record
                    DataElement data = instance.Data.Find(m => m.Id == dataIdString);
                    instance.Data.Remove(data);
                    Instance storedInstance = await _instanceRepository.Update(instance);

                    return(Ok(storedInstance));
                }
            }

            return(BadRequest());
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Get(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid)
        {
            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            if (instanceOwnerPartyId == 0)
            {
                return(BadRequest("Missing parameter value: instanceOwnerPartyId can not be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            (Instance instance, ActionResult errorResult) = await GetInstanceAsync(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(errorResult);
            }

            string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataGuid.ToString());

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataGuid);

            if (dataElement != null)
            {
                string orgFromClaim = User.GetOrg();

                if (!string.IsNullOrEmpty(orgFromClaim))
                {
                    _logger.LogInformation($"App owner download of {instance.Id}/data/{dataGuid}, {instance.AppId} for {orgFromClaim}");

                    // update downloaded structure on data element
                    dataElement.AppOwner ??= new ApplicationOwnerDataState();
                    dataElement.AppOwner.Downloaded ??= new List <DateTime>();
                    dataElement.AppOwner.Downloaded.Add(DateTime.UtcNow);

                    await _dataRepository.Update(dataElement);
                }

                if (string.Equals(dataElement.BlobStoragePath, storageFileName))
                {
                    try
                    {
                        Stream dataStream = await _dataRepository.ReadDataFromStorage(instance.Org, storageFileName);

                        if (dataStream == null)
                        {
                            return(NotFound($"Unable to read data element from blob storage for {dataGuid}"));
                        }

                        return(File(dataStream, dataElement.ContentType, dataElement.Filename));
                    }
                    catch (Exception e)
                    {
                        return(StatusCode(500, $"Unable to access blob storage for dataelement {e}"));
                    }
                }
            }

            return(NotFound("Unable to find requested data item"));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Get(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid)
        {
            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            if (instanceOwnerPartyId == 0)
            {
                return(BadRequest("Missing parameter value: instanceOwnerPartyId can not be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            (Instance instance, ActionResult errorResult) = await GetInstanceAsync(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(errorResult);
            }

            string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataGuid.ToString());

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataGuid);

            if (dataElement != null && string.Equals(dataElement.BlobStoragePath, storageFileName))
            {
                if (!dataElement.IsRead && User.GetOrg() != instance.Org)
                {
                    dataElement.IsRead = true;
                    await _dataRepository.Update(dataElement);
                }

                try
                {
                    Stream dataStream = await _dataRepository.ReadDataFromStorage(instance.Org, storageFileName);

                    if (dataStream == null)
                    {
                        return(NotFound($"Unable to read data element from blob storage for {dataGuid}"));
                    }

                    return(File(dataStream, dataElement.ContentType, dataElement.Filename));
                }
                catch (Exception e)
                {
                    return(StatusCode(500, $"Unable to access blob storage for dataelement {e}"));
                }
            }

            return(NotFound("Unable to find requested data item"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Get(int instanceOwnerId, Guid instanceGuid, Guid dataId)
        {
            string instanceId = $"{instanceOwnerId}/{instanceGuid}";

            if (instanceOwnerId == 0)
            {
                return(BadRequest("Missing parameter value: instanceOwnerId can not be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            Instance instance = GetInstance(instanceId, instanceOwnerId, out ActionResult errorResult);

            if (instance == null)
            {
                return(errorResult);
            }

            string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataId.ToString());
            string dataIdString    = dataId.ToString();

            // check if dataId exists in instance
            if (instance.Data.Exists(element => element.Id == dataIdString))
            {
                DataElement data = instance.Data.Find(element => element.Id == dataIdString);

                if (string.Equals(data.StorageUrl, storageFileName))
                {
                    try
                    {
                        Stream dataStream = await _dataRepository.ReadDataFromStorage(storageFileName);

                        if (dataStream == null)
                        {
                            return(NotFound("Unable to read data storage for " + dataIdString));
                        }

                        return(File(dataStream, data.ContentType, data.FileName));
                    }
                    catch (Exception e)
                    {
                        return(StatusCode(500, $"Unable to access blob storage for dataelement {e}"));
                    }
                }
            }

            return(NotFound("Unable to find requested data item"));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> Get(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid)
        {
            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            if (instanceOwnerPartyId == 0)
            {
                return(BadRequest("Missing parameter value: instanceOwnerPartyId can not be empty"));
            }

            (Instance instance, ActionResult instanceError) = await GetInstanceAsync(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(instanceError);
            }

            (DataElement dataElement, ActionResult dataElementError) = await GetDataElementAsync(instanceGuid, dataGuid);

            if (dataElement == null)
            {
                return(dataElementError);
            }

            if (!dataElement.IsRead && User.GetOrg() != instance.Org)
            {
                dataElement.IsRead = true;
                await _dataRepository.Update(dataElement);
            }

            string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataGuid.ToString());

            if (string.Equals(dataElement.BlobStoragePath, storageFileName))
            {
                Stream dataStream = await _dataRepository.ReadDataFromStorage(instance.Org, storageFileName);

                if (dataStream == null)
                {
                    return(NotFound($"Unable to read data element from blob storage for {dataGuid}"));
                }

                return(File(dataStream, dataElement.ContentType, dataElement.Filename));
            }

            return(NotFound("Unable to find requested data item"));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Delete(Guid instanceGuid, Guid dataId, int instanceOwnerPartyId)
        {
            _logger.LogInformation($"//DataController // Delete // Starting method");

            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            // check if instance id exist and user is allowed to change the instance data
            Instance instance = await _instanceRepository.GetOne(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(NotFound("Provided instanceId is unknown to storage service"));
            }

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataId);

            if (dataElement == null)
            {
                return(NotFound("Provided dataId is unknown to storage service"));
            }

            // delete blob stored file and data element object
            try
            {
                string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataId.ToString());
                bool   result          = await _dataRepository.DeleteDataInStorage(storageFileName);

                await _dataRepository.Delete(dataElement);

                await DispatchEvent(InstanceEventType.Deleted.ToString(), instance, dataElement);

                return(Ok(true));
            }
            catch (Exception deleteException)
            {
                _logger.LogError($"Unable to delete data element {dataId} due to {deleteException}");

                return(StatusCode(500, $"Unable to delete data element {dataId} due to {deleteException.Message}"));
            }
        }
Exemplo n.º 8
0
        public async Task <ActionResult <DataElement> > Delete(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid)
        {
            _logger.LogInformation($"//DataController // Delete // Starting method");

            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            Instance instance = await _instanceRepository.GetOne(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(NotFound("Provided instanceId is unknown to storage service"));
            }

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataGuid);

            if (dataElement == null)
            {
                return(NotFound("Provided dataGuid is unknown to storage service"));
            }

            try
            {
                string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataGuid.ToString());

                await _dataRepository.DeleteDataInStorage(instance.Org, storageFileName);

                await _dataRepository.Delete(dataElement);

                await DispatchEvent(InstanceEventType.Deleted.ToString(), instance, dataElement);

                return(Ok(dataElement));
            }
            catch (Exception deleteException)
            {
                _logger.LogError($"Unable to delete data element {dataGuid} due to {deleteException}");

                return(StatusCode(500, $"Unable to delete data element {dataGuid} due to {deleteException.Message}"));
            }
        }
Exemplo n.º 9
0
        public async Task <ActionResult <DataElement> > OverwriteData(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid, [FromQuery(Name = "refs")] List <Guid> refs = null)
        {
            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            if (instanceOwnerPartyId == 0 || Request.Body == null)
            {
                return(BadRequest("Missing parameter values: instanceId, datafile or attached file content cannot be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            (Instance instance, ActionResult errorMessage) = await GetInstanceAsync(instanceId, instanceOwnerPartyId);

            if (instance == null)
            {
                return(errorMessage);
            }

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataGuid);

            if (dataElement == null)
            {
                return(NotFound($"Data guid {dataGuid} is not registered in storage"));
            }

            if (dataElement.Locked)
            {
                return(Conflict($"Data element {dataGuid} is locked and cannot be updated"));
            }

            string blobStoragePathName = DataElementHelper.DataFileName(
                instance.AppId,
                instanceGuid.ToString(),
                dataGuid.ToString());

            if (string.Equals(dataElement.BlobStoragePath, blobStoragePathName))
            {
                var streamAndDataElement = await ReadRequestAndCreateDataElementAsync(Request, dataElement.DataType, refs, instance);

                Stream      theStream   = streamAndDataElement.Item1;
                DataElement updatedData = streamAndDataElement.Item2;

                if (theStream == null)
                {
                    return(BadRequest("No data found in request body"));
                }

                DateTime changedTime = DateTime.UtcNow;

                dataElement.ContentType   = updatedData.ContentType;
                dataElement.Filename      = HttpUtility.UrlDecode(updatedData.Filename);
                dataElement.LastChangedBy = User.GetUserOrOrgId();
                dataElement.LastChanged   = changedTime;
                dataElement.Refs          = updatedData.Refs;

                dataElement.Size = await _dataRepository.WriteDataToStorage(instance.Org, theStream, blobStoragePathName);

                if (dataElement.Size > 0)
                {
                    DataElement updatedElement = await _dataRepository.Update(dataElement);

                    updatedElement.SetPlatformSelfLinks(_storageBaseAndHost, instanceOwnerPartyId);

                    await DispatchEvent(InstanceEventType.Saved.ToString(), instance, updatedElement);

                    return(Ok(updatedElement));
                }

                return(UnprocessableEntity($"Could not process attached file"));
            }

            return(StatusCode(500, $"Storage url does not match with instance metadata"));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> OverwriteData(int instanceOwnerId, Guid instanceGuid, Guid dataId)
        {
            string instanceId = $"{instanceOwnerId}/{instanceGuid}";

            if (instanceOwnerId == 0 || Request.Body == null)
            {
                return(BadRequest("Missing parameter values: instanceId, datafile or attached file content cannot be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            Instance instance = GetInstance(instanceId, instanceOwnerId, out ActionResult errorMessage);

            if (instance == null)
            {
                return(errorMessage);
            }

            string dataIdString = dataId.ToString();

            // check that data element exists, if not return not found
            if (instance.Data != null && instance.Data.Exists(m => m.Id == dataIdString))
            {
                DataElement data = instance.Data.Find(m => m.Id == dataIdString);

                if (data == null)
                {
                    return(NotFound("Dataid is not registered in instance"));
                }

                string storageFileName = DataElementHelper.DataFileName(instance.AppId, instanceGuid.ToString(), dataIdString);

                if (string.Equals(data.StorageUrl, storageFileName))
                {
                    DateTime updateTime = DateTime.UtcNow;

                    DataElement updatedData = GetDataElementFromRequest(Request, data.ElementType, instance, out Stream theStream);

                    if (theStream == null)
                    {
                        return(BadRequest("No data attachements found"));
                    }

                    DateTime changedTime = DateTime.UtcNow;

                    // update data record
                    data.ContentType         = updatedData.ContentType;
                    data.FileName            = updatedData.FileName;
                    data.LastChangedBy       = User.Identity.Name;
                    data.LastChangedDateTime = changedTime;

                    instance.LastChangedDateTime = changedTime;
                    instance.LastChangedBy       = User.Identity.Name;

                    // store file as blob
                    data.FileSize = _dataRepository.WriteDataToStorage(theStream, storageFileName).Result;

                    if (data.FileSize > 0)
                    {
                        // update instance
                        Instance result = await _instanceRepository.Update(instance);

                        InstancesController.AddSelfLinks(Request, instance);

                        return(Ok(result));
                    }

                    return(UnprocessableEntity($"Could not process attached file"));
                }

                return(StatusCode(500, $"Storage url does not match with instance metadata"));
            }

            return(BadRequest("Cannot update data element that is not registered"));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> OverwriteData(int instanceOwnerPartyId, Guid instanceGuid, Guid dataGuid, [FromQuery(Name = "refs")] List <Guid> refs = null)
        {
            string instanceId = $"{instanceOwnerPartyId}/{instanceGuid}";

            if (instanceOwnerPartyId == 0 || Request.Body == null)
            {
                return(BadRequest("Missing parameter values: instanceId, datafile or attached file content cannot be empty"));
            }

            // check if instance id exist and user is allowed to change the instance data
            Instance instance = GetInstance(instanceId, instanceOwnerPartyId, out ActionResult errorMessage);

            if (instance == null)
            {
                return(errorMessage);
            }

            DataElement dataElement = await _dataRepository.Read(instanceGuid, dataGuid);

            if (dataElement == null)
            {
                return(NotFound($"Dataid {dataGuid} is not registered in storage"));
            }

            string blobStoragePathName = DataElementHelper.DataFileName(
                instance.AppId,
                instanceGuid.ToString(),
                dataGuid.ToString());

            if (string.Equals(dataElement.BlobStoragePath, blobStoragePathName))
            {
                DataElement updatedData = ReadRequestAndCreateDataElement(Request, dataElement.DataType, refs, instance, out Stream theStream);

                if (theStream == null)
                {
                    return(BadRequest("No data found in request body"));
                }

                DateTime changedTime = DateTime.UtcNow;

                // update data record
                dataElement.ContentType   = updatedData.ContentType;
                dataElement.Filename      = updatedData.Filename;
                dataElement.LastChangedBy = GetUserId();
                dataElement.LastChanged   = changedTime;
                dataElement.Refs          = updatedData.Refs;

                instance.LastChanged = changedTime;

                instance.LastChangedBy = GetUserId();

                // store file as blob
                dataElement.Size = _dataRepository.WriteDataToStorage(theStream, blobStoragePathName).Result;

                if (dataElement.Size > 0)
                {
                    // update data element
                    // Question: Do we need to update instance?
                    DataElement updatedElement = await _dataRepository.Update(dataElement);

                    AddSelfLinks(instance, dataElement);

                    await DispatchEvent(InstanceEventType.Deleted.ToString(), instance, dataElement);

                    return(Ok(dataElement));
                }

                return(UnprocessableEntity($"Could not process attached file"));
            }

            return(StatusCode(500, $"Storage url does not match with instance metadata"));
        }