Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> GetSpreadsheet([FromBody] RequestModel request)
        {
            Guid testOutput;

            if (string.IsNullOrWhiteSpace(request.guid) || !Guid.TryParse(request.guid.TrimStart('t'), out testOutput))
            {
                throw new System.Exception("Invalid GUID");
            }

            // authenticate with Forge
            dynamic oauth = await Get2LeggedTokenAsync(new Scope[] { Scope.DataRead });

            // get object on the bucket, should be just 1
            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            dynamic objectsInBucket = await objects.GetObjectsAsync(request.guid);

            string objectId  = string.Empty;
            string objectKey = string.Empty;

            foreach (KeyValuePair <string, dynamic> objInfo in new DynamicDictionaryItems(objectsInBucket.items))
            {
                objectId  = objInfo.Value.objectId;
                objectKey = objInfo.Value.objectKey;
            }

            string xlsFileName = objectKey.Replace(".rvt", ".xls");
            var    xlsPath     = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), request.guid, xlsFileName);

            if (File.Exists(xlsPath))
            {
                return(SendFile(xlsPath));// if the Excel file was already generated
            }
            DerivativesApi derivative = new DerivativesApi();

            derivative.Configuration.AccessToken = oauth.access_token;

            // get the derivative metadata
            dynamic metadata = await derivative.GetMetadataAsync(objectId.Base64Encode());

            foreach (KeyValuePair <string, dynamic> metadataItem in new DynamicDictionaryItems(metadata.data.metadata))
            {
                dynamic hierarchy = await derivative.GetModelviewMetadataAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                dynamic properties = await derivative.GetModelviewPropertiesAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                Workbook xls = new Workbook();
                foreach (KeyValuePair <string, dynamic> categoryOfElements in new DynamicDictionaryItems(hierarchy.data.objects[0].objects))
                {
                    string    name  = categoryOfElements.Value.name;
                    Worksheet sheet = new Worksheet(name);
                    for (int i = 0; i < 100; i++)
                    {
                        sheet.Cells[i, 0] = new Cell("");                 // unless we have at least 100 cells filled, Excel understand this file as corrupted
                    }
                    List <long> ids = GetAllElements(categoryOfElements.Value.objects);
                    int         row = 1;
                    foreach (long id in ids)
                    {
                        Dictionary <string, object> props = GetProperties(id, properties);
                        int collumn = 0;
                        foreach (KeyValuePair <string, object> prop in props)
                        {
                            sheet.Cells[0, collumn]   = new Cell(prop.Key.ToString());
                            sheet.Cells[row, collumn] = new Cell(prop.Value.ToString());
                            collumn++;
                        }

                        row++;
                    }

                    xls.Worksheets.Add(sheet);
                }
                xls.Save(xlsPath);
            }
            return(SendFile(xlsPath));
        }
 private async Task <dynamic> GetModelDetailGuid(DerivativesApi api, string urn)
 {
     return(await api.GetMetadataAsync(urn));
 }