Ejemplo n.º 1
0
        public static void SetKey(string projectId, string keyId, string fileNameOrValueOrGeometry)
        {
            CheckForInitAndLoginSDK();

            Project project = GetProjectById(projectId);

            CellInfo key = project.DataTable.Cells.FirstOrDefault(c => c.CellId == keyId);

            if (key == null)
            {
                throw new Exception(string.Format("Key by id: '{0}' not found.", keyId));
            }

            string       valueForSet;
            SourceOfData sourceOfData = SourceOfData.Primitive;
            string       name         = string.Empty;

            //checking parameter as filename
            string fileName = string.Empty;

            if (fileNameOrValueOrGeometry.StartsWith("<") && fileNameOrValueOrGeometry.Length > 1)
            {
                //delete first char
                fileName = fileNameOrValueOrGeometry.Substring(1);
                if (!FileHelper.IsFileExists(ref fileName))
                {
                    Console.WriteLine("File name '{0}' is not exists", fileName);
                }

                valueForSet  = File.ReadAllText(fileName);
                sourceOfData = SourceOfData.File;
            }
            else
            {
                name = fileNameOrValueOrGeometry.ToUpper();

                //check parameter, if name is inside Geometric Primitives
                if (!GeometricPrimitives.TryGetValue(name, out valueForSet))
                {
                    //check parameter, if number is inside Geometric Primitives
                    int numberPrimitive;
                    if (int.TryParse(name, out numberPrimitive) && numberPrimitive <= GeometricPrimitives.Count)
                    {
                        valueForSet = GeometricPrimitives.Values.ElementAt(numberPrimitive - 1);
                        name        = GeometricPrimitives.Keys.ElementAt(numberPrimitive - 1);
                    }
                    else
                    {
                        valueForSet  = fileNameOrValueOrGeometry;
                        sourceOfData = SourceOfData.Value;
                    }
                }
            }

            var      serializedValueStr = DataSerializer.Serialize(valueForSet);
            Stream   stream             = StreamUtils.GenerateStreamFromString(serializedValueStr);
            CellInfo updatingKey        = project.DataTable.SetCell(key.CellId, stream, key.ClientMetadata);

            switch (sourceOfData)
            {
            case SourceOfData.File:
                Console.WriteLine("Key '{0}' was updated from file: '{1}'", updatingKey.ClientMetadata.Label, fileName);
                break;

            case SourceOfData.Primitive:
                Console.WriteLine("Key '{0}' was updated by predefined geometric primitive '{1}': ", updatingKey.ClientMetadata.Label, name);
                Console.WriteLine(@"Please vivsit https://flux.io, project - '{0}', key - '{1}' to review this changes.", project.Name, key.ClientMetadata.Label);
                break;

            case SourceOfData.Value:
                Console.WriteLine("Key '{0}' was updated with value: '{1}'", updatingKey.ClientMetadata.Label, valueForSet);
                break;
            }
        }