Esempio n. 1
0
        public async Task <IActionResult> CreateAndUploadData(int instanceOwnerId, Guid instanceGuid, string elementType)
        {
            string instanceId = $"{instanceOwnerId}/{instanceGuid}";

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

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

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

            // check metadata
            Application appInfo = GetApplication(instance.AppId, instance.Org, out ActionResult appErrorMessage);

            if (appInfo == null)
            {
                return(appErrorMessage);
            }

            if (!appInfo.ElementTypes.Exists(e => e.Id == elementType))
            {
                return(BadRequest("Requested element type is not declared in application metadata"));
            }

            DataElement newData = GetDataElementFromRequest(Request, elementType, instance, out Stream theStream);

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

            if (instance.Data == null)
            {
                instance.Data = new List <DataElement>();
            }

            instance.Data.Add(newData);

            try
            {
                // store file as blob
                newData.FileSize = await _dataRepository.WriteDataToStorage(theStream, newData.StorageUrl);

                // update instance
                Instance result = await _instanceRepository.Update(instance);

                InstancesController.AddSelfLinks(Request, result);

                return(Ok(result));
            }
            catch (Exception e)
            {
                return(StatusCode(500, $"Unable to create instance data in storage: {e}"));
            }
        }
Esempio n. 2
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"));
        }
Esempio n. 3
0
 private void AddSelfLinks(Instance instance, DataElement dataElement)
 {
     InstancesController.AddDataSelfLinks(
         InstancesController.ComputeInstanceSelfLink(Request, instance),
         dataElement);
 }