コード例 #1
0
ファイル: RedcapNefcy.cs プロジェクト: jeffmun/Research2013
        // get any RC data. Need Token and strReturnCheck (to see if error in data returning). rest are optional
        // strRecordsSelect: any records you want seperated by ','; all records if ""
        // strFields: Particular fields you want, seperated by ','; all fields if ""
        // strEvents: Particular events you want, seperated by ','; all events if ""
        // strForms: Particular forms you want, seperated by ','; all forms if ""
        // boolLabels: false=raw; true=label
        // boolAccessGroups: false=no access group returned; true=access group returned (should be false, see note below)
        // (note: can't import access group column if in your data table; ontology fields at the moment can't be imported too)
        public DataTable GetTableFromAnyRC(string strReturnCheck, string strRecordsSelect, string strFields,
                                           string strEvents, string strForms, bool boolLabels, bool boolAccessGroups)
        {
            Debug.WriteLine("GetTableFromAnyRC()");

            string strRecord;
            CSVDoc csvDoc;
            string strPostParameters = "";
            int    intReturnLength   = strReturnCheck.Trim().Length;

            DataTable dtDataTable = new DataTable();
            DataRow   drRecord;

            strPostParameters = "&content=record&format=csv&type=flat&eventName=unique";

            if (strReturnCheck == "")
            {
                throw new Exception("Error: " + "Must provide first field to check");
            }

            if (strRecordsSelect != "")
            {
                strPostParameters += "&records=" + strRecordsSelect;
            }

            strPostParameters += "&exportSurveyFields=true";


            if (strFields != "")
            {
                strPostParameters += "&fields=" + strFields;
            }

            if (strEvents != "")
            {
                strPostParameters += "&events=" + strEvents;
            }

            if (strForms != "")
            {
                strPostParameters += "&forms=" + strForms;
            }

            if (boolLabels)
            {
                strPostParameters += "&rawOrLabel=label";
            }
            else
            {
                strPostParameters += "&rawOrLabel=raw";
            }

            // probably should take out if you are going to import this exported data
            if (boolAccessGroups)
            {
                strPostParameters += "&exportDataAccessGroups=true";
            }

            byte[] bytePostData = Encoding.UTF8.GetBytes("token=" + strPostToken + strPostParameters);

            string strResponse = responseHTTP(bytePostData);

            //replace newline with commas
            //strResponse = strResponse.Replace('\n', ',');

            // if no records found, there are no fields.  new in RC 6 and greater
            // have to deal with null DataTable in your call to this function
            // if Rc < 6, then it will return field names without any rows of data
            if (strResponse == "\n")
            {
                return(dtDataTable);
            }

            // should return the first field you expect. otherwise it is error
            string strFirstField  = strResponse.Split(',').ToList()[0];
            string strSecondField = strResponse.Split(',').ToList()[1];
            string strThirdField  = strResponse.Split(',').ToList()[2];
            string strFourthField = strResponse.Split(',').ToList()[3];

            List <string> response_vars = strResponse.Split(',').ToList();

            //if (strFirstField != strReturnCheck && strSecondField != strReturnCheck)
            if (!response_vars.Contains(strReturnCheck))
            {
                throw new Exception("RC Error: " + strResponse);
            }

            csvDoc = new CSVDoc(strResponse);

            // first line of .csv is column names
            strRecord = csvDoc.ReadLine();

            // get column headers
            string[] strColumnHeaders = strRecord.Split(',');

            // set up table
            for (int i = 0; i < strColumnHeaders.Length; i++)
            {
                Debug.WriteLine(String.Format("setup table i={0}  adding {1}", i, strColumnHeaders[i]));
                dtDataTable.Columns.Add(strColumnHeaders[i].ToString(), typeof(string));
            }

            // now read all data and assign to data table
            while ((strRecord = csvDoc.ReadLine()) != null)
            {
                CSVLine csvLine = new CSVLine(strRecord);

                drRecord = dtDataTable.NewRow();

                // now get fields
                for (int i = 0; i < strColumnHeaders.Length; i++)
                {
                    drRecord[i] = csvLine.ReadField();
                }

                dtDataTable.Rows.Add(drRecord);
            }

            return(dtDataTable);
        }
コード例 #2
0
        public DataTable GetFormEventMapping()
        {
            Debug.WriteLine("GetFormEventMapping()");

            string strRecord;
            CSVDoc csvDoc;
            string strPostParameters = "";
            //int intReturnLength = strReturnCheck.Trim().Length;

            DataTable dtDataTable = new DataTable();
            DataRow   drRecord;

            strPostParameters = "&content=formEventMapping&format=csv&returnFormat=csv";             // &eventName=unique";

            strPostParameters += "&rawOrLabel=raw";

            string post_string = String.Format("token={0}{1}", strPostToken, strPostParameters);

            byte[] bytePostData = Encoding.UTF8.GetBytes(post_string);
            //byte[] bytePostData = Encoding.UTF8.GetBytes("token=" + strPostToken + strPostParameters);

            string strResponse = responseHTTP(bytePostData);

            if (strResponse.StartsWith("ERROR"))
            {
                return(null);
            }

            //replace newline with commas
            //strResponse = strResponse.Replace('\n', ',');

            // if no records found, there are no fields.  new in RC 6 and greater
            // have to deal with null DataTable in your call to this function
            // if Rc < 6, then it will return field names without any rows of data
            else if (strResponse == "\n")
            {
                return(dtDataTable);
            }

            List <string> response_vars = strResponse.Split(',').ToList();

            csvDoc = new CSVDoc(strResponse);

            // first line of .csv is column names
            strRecord = csvDoc.ReadLine();

            // get column headers
            string[] strColumnHeaders = strRecord.Split(',');

            // set up table
            for (int i = 0; i < strColumnHeaders.Length; i++)
            {
                Debug.WriteLine(String.Format("setup table i={0}  adding {1}", i, strColumnHeaders[i]));
                dtDataTable.Columns.Add(strColumnHeaders[i].ToString(), typeof(string));
            }

            // now read all data and assign to data table
            while ((strRecord = csvDoc.ReadLine()) != null)
            {
                CSVLine csvLine = new CSVLine(strRecord);

                drRecord = dtDataTable.NewRow();

                // now get fields
                for (int i = 0; i < strColumnHeaders.Length; i++)
                {
                    drRecord[i] = csvLine.ReadField();
                }

                dtDataTable.Rows.Add(drRecord);
            }

            return(dtDataTable);
        }