Ejemplo n.º 1
0
        private void btnFill_Click(object sender, EventArgs e)
        {
            if (currentDoc == null || currentDoc.TypesFields == null || currentDoc.TypesFields.Count == 0)
            {
                MessageBox.Show("File not analyzed yet!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string fn      = txtFilename.Text;
            string outPath = Path.GetDirectoryName(fn);

            outPath = Path.Combine(outPath, $"{Path.GetFileNameWithoutExtension(fn)}_csvs");
            CSVDocumentCollection docCollection = new CSVDocumentCollection(outPath);

            if (!Directory.Exists(outPath))
            {
                Directory.CreateDirectory(outPath);
            }

            foreach (var type in currentDoc.TypesFields)
            {
                docCollection.AddDocument(type.Key, type.Value);
            }

            using (StreamReader file = File.OpenText(fn))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JToken o = JToken.ReadFrom(reader);
                    if (o is JArray)
                    {
                        int i = 1;
                        foreach (JObject jo in ((JArray)o))
                        {
                            string instanceId = jo["instanceID"].Value <string>();
                            ProcessObject(currentDoc.TypesFields, docCollection, jo, "", instanceId, i++, false);
                        }
                        docCollection.Close();
                    }
                    else
                    {
                        throw new Exception("Not valid ODK exportion");
                    }
                }
        }
Ejemplo n.º 2
0
        private void ProcessObject(Dictionary <string, Dictionary <string, ODKObjectType> > typesFields,
                                   CSVDocumentCollection docCollection,
                                   JObject jo,
                                   string currentType,
                                   string instanceId,
                                   int index,
                                   bool isSub
                                   )
        {
            List <object> record = new List <object>();

            if (isSub)
            {
                record.Add(instanceId);
            }
            record.Add(index);
            foreach (var prop in jo.Properties())
            {
                var t = typesFields[currentType][prop.Name];
                if (prop.Value.Type == JTokenType.Null)
                {
                    record.Add("");
                    continue;
                }

                switch (t)
                {
                case ODKObjectType.TextualValue:
                    record.Add(prop.Value.Value <string>());
                    break;

                case ODKObjectType.MultiValue:
                    record.Add(JoinMultiValue(prop.Value as JArray));
                    break;

                case ODKObjectType.File:
                    record.Add(ProcessFile(prop.Value));
                    break;

                case ODKObjectType.Repeat:
                    if (prop.Value.Type != JTokenType.Null)
                    {
                        int i = 1;
                        foreach (var rec in (prop.Value as JArray))
                        {
                            ProcessObject(typesFields, docCollection, rec as JObject, ODK.ODKUtils.ExtractTypeName(prop.Name), instanceId, i++, true);
                        }
                    }
                    break;

                case ODKObjectType.Unknown:
                    record.Add("");
                    break;

                default:
                    break;
                }
            }
            var doc = docCollection.GetDocument(currentType);

            doc.WriteRecord(record);
        }