private static bool InsertFeatures(fulcrumrecords fulcrumRecords, IFeatureClass featureClass)
        {
            try
            {
                    // Create an insert cursor.
                    IFeatureCursor insertCursor = featureClass.Insert(false);
                    //comReleaser.ManageLifetime(insertCursor);

                    foreach (fulcrumrecord record in fulcrumRecords.records)
                    {
                        // Create a feature buffer.
                        IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
                        //comReleaser.ManageLifetime(featureBuffer);

                        IPoint pt = new Point();
                        pt.PutCoords(record.longitude, record.latitude);
                        featureBuffer.Shape = pt as IGeometry;
                        //handle photos separately
                        if (record.photos.Count >= 1)
                        {
                            try
                            {
                                //only link to the first photo for now
                                //ToDo: how to put the actual image in the photos field (the actual image is held in the photos property of the record)
                                //like so...
                                //featureBuffer.set_Value(featureClass.FindField("photos"), record.photos[0]);
                                featureBuffer.set_Value(featureClass.FindField(photoFieldName), record.photolinks[0]);
                                //ToDo: enhancement: multiple photo support; could create a separate table to store the photo links and join it back to resulting feature class via record id
                            }
                            catch (Exception e)
                            {
                                string photoInsertError = e.Message;
                            }
                        }
                        foreach (fulcrumattribute attr in record.attributes)
                        {
                            if (attr.fieldName != "photos")
                            {
                                //ToDo: why are some (first?) field names empty strings?
                                if (attr.fieldName != "" && attr.fieldName != null)
                                {
                                    try
                                    {
                                        featureBuffer.set_Value(featureClass.FindField(attr.fieldName), attr.fieldValue);
                                    }
                                    catch(Exception e)
                                    {
                                        string ouch = e.Message;
                                    }
                                }
                            }
                        }
                        try
                        {
                            insertCursor.InsertFeature(featureBuffer);
                            featureBuffer = null;
                            GC.Collect();
                        }
                        catch (Exception e)
                        {
                            string insertFailed = e.Message;
                        }
                    }
                    // Flush the buffer to the geodatabase.
                    insertCursor.Flush();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 public static bool DoArcStuff(fulcrumform form, fulcrumrecords records)
 {
     try
     {
         IFeatureClass newFeatureClass = CreateFeatureClass(form, Properties.Settings.Default.pathToGeoDB);
         InsertFeatures(records, newFeatureClass);
         IFeatureLayer layer = CreateFeatureLayer(newFeatureClass, form.name);
         IMap map = ArcMap.Document.FocusMap;
         map.AddLayer((ILayer)layer);
         ArcMap.Document.ActiveView.Refresh();
         return true;
     }
     catch (Exception e)
     {
         string bomb = e.Message;
         return false;
     }
 }
        public static bool ParseRecords(fulcrumform form, fulcrumrecords fulcrumRecords, System.Windows.Controls.ProgressBar progBar)
        {
            try
            {
                progBar.Maximum = fulcrumRecords.records.Count;
                //WPF Prog Bar, WTF?!
                //System.Windows.Media.Duration duration = new Duration(TimeSpan.FromSeconds(10));
                //System.Windows.Media.Animation.DoubleAnimation doubleanimation = new System.Windows.Media.Animation.DoubleAnimation(100.0, duration);
                //progBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, doubleanimation);
                int count = 1;
                foreach (fulcrumrecord record in fulcrumRecords.records)
                {
                    progBar.Value = count;
                    string recordId = record.id;

                    foreach (string key in record.form_values.Keys)
                    {
                        fulcrumattribute recordAttribute = new fulcrumattribute();
                        recordAttribute.recordID = recordId;
                        string fieldname;
                        string fieldType;
                        if (GetFieldNameAndTypeFromFormDefinition(form, key, out fieldname, out fieldType))
                        {
                            //if the value is a set of sub objects, we'll get another dictionary
                            //if the value is a javascript array, we'll get an arraylist
                            //otherwise we'll get the string value
                            object o = record.form_values[key];

                            if (o is Dictionary<string, object>)
                            {
                                //I think!!! the only time this will happen is when the value comes from a choice list
                                //so grab the value from the choice_values item

                                Dictionary<string, object> d = ((Dictionary<string, object>)o);
                                //ToDo: if this is a value\label pair we're always going to ge the coded value
                                //is there a way to determine if it is a coded value???
                                //e.g. coded value = 002, label = Krazy Kool
                                ArrayList a = (ArrayList)d["choice_values"];
                                recordAttribute.fieldName = fieldname;
                                recordAttribute.fieldValue = a[0].ToString();
                            }
                            else if (o is ArrayList)
                            {
                                foreach (object child in ((ArrayList)o))
                                {
                                    if (child is string)
                                    {
                                        recordAttribute.fieldName = fieldname;
                                        recordAttribute.fieldValue = child.ToString();
                                        //Console.WriteLine(fieldname + ": " + child.ToString());
                                    }
                                    else if (child is Dictionary<string, object>)
                                    {
                                        Dictionary<string, object> childDict = (Dictionary<string, object>)child;
                                        foreach (string childKey in childDict.Keys)
                                        {
                                            //ToDo: we are writing the link to the photo as well as
                                            //grabbing the photo thumb as an image and adding it to the records' photo collection
                                            //and saving it to disk
                                            //you will likely want to choose from these options, downloading is expensive
                                            if (fieldType == "PhotoField")
                                            {
                                                string photoId = childDict["photo_id"].ToString();
                                                string thumbUrl = childDict["thumbnail"].ToString();
                                                string url = childDict["url"].ToString();
                                                Image img = GetFulcrumImage(thumbUrl);
                                                record.photos.Add(img);
                                                string pathToPhoto = Properties.Settings.Default.pathToPhotoFolder + "\\" + photoId + ".jpg";
                                                string linkToThumb = "http://web.fulcrumapp.com/photos/" + photoId + "/thumbnail";
                                                string linkToPhoto = " http://web.fulcrumapp.com/photos/" + photoId;
                                                img.Save(pathToPhoto, System.Drawing.Imaging.ImageFormat.Jpeg);
                                                record.photolinks.Add(linkToPhoto);
                                            }
                                            else
                                            {
                                                //assuming all values are now strings
                                                recordAttribute.fieldName = childKey.ToString();
                                                recordAttribute.fieldValue = childDict[childKey].ToString();
                                                //Console.WriteLine(childKey.ToString() + ": " + childDict[childKey].ToString());
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                recordAttribute.fieldName = fieldname;
                                recordAttribute.fieldValue = o.ToString();
                                //Console.WriteLine(fieldname + ": " + o.ToString());
                            }
                            record.attributes.Add(recordAttribute);
                        }
                        else
                        {
                            //couldnt get the field name or type
                        }
                    }
                    count++;
                }
                return true;
            }
            catch (Exception e)
            {
                string ouch = e.Message;
                return false;
            }
        }