示例#1
0
        private void LoadOUMap()
        {
            foreach (JObject ouMap in ouMapping)
            {
                JObject ouMeta = (JObject)ouMap.GetValue("meta");
                OUMap   oum    = new OUMap();
                oum.SourceID      = ouMeta["source"].ToString();
                oum.DestinationID = ouMeta["destination"].ToString();

                JArray mapped = (JArray)ouMap.GetValue("mapped");

                foreach (JObject ouM in mapped)
                {
                    OUMapData oumd = new OUMapData();
                    oumd.SourceID      = ouM["sourceID"].ToString();
                    oumd.DestinationID = ouM["destID"].ToString();

                    oum.Mapped.Add(oumd);
                }

                OUMapList.Add(oum);
            }
        }
示例#2
0
        private async Task ProcessJobs(bool dryRun, bool runExport)
        {
            foreach (JObject job in jobs)
            {
                //JArray xferExports = new JArray();

                JObject jobMeta = (JObject)job.GetValue("meta");
                JArray  jobList = (JArray)job.GetValue("jobs");

                JObject sourceServer      = FindServer(jobMeta.GetValue("source").ToString());
                JObject destinationServer = FindServer(jobMeta.GetValue("destination").ToString());

                string baseUrl = sourceServer.GetValue("url").ToString();

                if (baseUrl.Substring(baseUrl.Length - 1) != "/")
                {
                    baseUrl += "/";
                }

                //Load the current OU Mapping for this job
                OUMap OUcurrent = OUMapList.Where(w => w.SourceID == sourceServer.GetValue("ID").ToString() && w.DestinationID == destinationServer.GetValue("ID").ToString()).FirstOrDefault();
                int   index     = 0;

                foreach (JObject j in jobList)
                {
                    index += 1;

                    JObject xferObject = new JObject();
                    JArray  xferList   = new JArray();
                    JObject xferMeta   = new JObject();

                    xferMeta["index"]      = index;
                    xferMeta["total"]      = jobList.Count;
                    xferMeta["dimension"]  = j["srcName"];
                    xferMeta["period"]     = $"{meta["periodStart"]} - {meta["periodEnd"]}";
                    xferMeta["periodType"] = jobMeta["periodType"];
                    xferMeta["level"]      = jobMeta["level"];

                    List <string> periods = GetPeriods(int.Parse(jobMeta.GetValue("periodType").ToString()), meta.GetValue("periodStart").ToString(), meta.GetValue("periodEnd").ToString());

                    string period = "";
                    //Concatenate all the periods together to pull all data at once
                    foreach (string p in periods)
                    {
                        period += p + ";";
                    }

                    string url        = baseUrl + "api/analytics.json?skipMeta=true&paging=false&dimension=ou:LEVEL-" + jobMeta.GetValue("level").ToString() + "&dimension=pe:" + period + "&dimension=dx:" + j.GetValue("sourceID");
                    bool   retry      = true;
                    int    retryCount = 0;

                    JObject data   = new JObject();
                    string  result = "";

                    //If the data that comes back does not parse, log it and retry. This is to prevent a possible network issue
                    while (retry)
                    {
                        result = await HTTPFactory.GET(url, sourceServer.GetValue("user").ToString(), sourceServer.GetValue("password").ToString());

                        try
                        {
                            data  = JObject.Parse(result);
                            retry = false;
                        }
                        catch (Exception ex)
                        {
                            //Catch the error, log it and retry
                            retryCount += 1;

                            JObject log = new JObject();
                            log["type"]    = "error";
                            log["message"] = ex.Message;
                            log["detail"]  = result;

                            if (SessionID != Guid.Empty)
                            {
                                log["sessionID"] = SessionID;
                            }

                            if (LogEvent != null)
                            {
                                LogEvent(log.ToString());
                            }
                        }

                        //After 5 failed attempts, throw and error.
                        if (retryCount == 5)
                        {
                            throw new Exception("Network error occured");
                        }
                    }

                    try
                    {
                        //Don't process if there is no result
                        if (result != "")
                        {
                            JArray rows = (JArray)data.GetValue("rows");

                            foreach (JArray r in rows)
                            {
                                string ouID        = r[1].ToString();
                                var    value       = r[3];
                                string periodValue = r[2].ToString();

                                //Check if there is a matching OrgUnit
                                //string destOU = MatchOU(sourceServer.GetValue("ID").ToString(), destinationServer.GetValue("ID").ToString(), ouID);
                                string destOU = OUcurrent.Mapped.Where(w => w.SourceID == ouID).Select(s => s.DestinationID).FirstOrDefault();

                                //Match was found
                                if (destOU != "")
                                {
                                    JObject xfer = new JObject();
                                    xfer["dataElement"] = j.GetValue("destID").ToString();
                                    xfer["orgUnit"]     = destOU;
                                    xfer["period"]      = periodValue;

                                    //Only add COC if there is one
                                    if (j.GetValue("destCOC").ToString() != "-1" && j.GetValue("destCOC").ToString() != "")
                                    {
                                        xfer["categoryOptionCombo"] = j.GetValue("destCOC").ToString();
                                    }

                                    //xfer["attributeOptionCombo"] = "";
                                    xfer["value"]    = value;
                                    xfer["storedBy"] = destinationServer.GetValue("user").ToString(); //Since its optional to use the username, we can skip an api hit to credentials
                                    xfer["created"]  = DateTime.Now.ToString("yyyy-MM-dd");
                                    xfer["updated"]  = DateTime.Now.ToString("yyyy-MM-dd");
                                    xfer["comment"]  = "Imported";

                                    xferList.Add(xfer);
                                }
                            }
                        }

                        //If true will continue the process to export the data to the destination server
                        if (runExport)
                        {
                            string baseUrlDest = destinationServer.GetValue("url").ToString();
                            //foreach (JObject x in xferExports)
                            //{
                            JObject xfer = new JObject();
                            xfer["dataValues"] = xferList;
                            string exportData = xfer.ToString();

                            //string data = "{\"dataValues\":[" + xfer.ToString() + "]}";
                            string postUrl  = baseUrlDest + "api/dataValueSets?importStrategy=CREATE_AND_UPDATE&format=json&preheatCache=false&skipExistingCheck=false&skipAudit=false&dryRun=" + dryRun;
                            string response = await HTTPFactory.POST(postUrl, sourceServer.GetValue("user").ToString(), sourceServer.GetValue("password").ToString(), exportData);


                            try
                            {
                                JObject r   = JObject.Parse(response);
                                JObject log = new JObject();

                                if (SessionID != Guid.Empty)
                                {
                                    log["sessionID"] = SessionID;
                                }

                                log["type"]     = "log";
                                log["meta"]     = xferMeta;
                                log["response"] = r;

                                if (LogEvent != null)
                                {
                                    LogEvent(log.ToString());
                                }

                                Log.Add(log);
                            }
                            catch (Exception ex)
                            {
                                JObject logError = new JObject();
                                logError["type"]    = "error";
                                logError["message"] = ex.Message;
                                logError["detail"]  = response;

                                if (SessionID != Guid.Empty)
                                {
                                    logError["sessionID"] = SessionID;
                                }

                                if (LogEvent != null)
                                {
                                    LogEvent(logError.ToString());
                                }

                                //Give the log event time to write to the file
                                Thread.Sleep(5000);
                                throw new Exception("An error occured processing a reponse.");
                            }
                        }
                        else
                        {
                            manualImports.Add(xferList);
                        }
                    }
                    catch (Exception ex)
                    {
                        JObject log = new JObject();
                        log["type"]    = "error";
                        log["message"] = ex.Message;
                        log["detail"]  = result;

                        if (SessionID != Guid.Empty)
                        {
                            log["sessionID"] = SessionID;
                        }

                        if (LogEvent != null)
                        {
                            LogEvent(log.ToString());
                        }

                        //Give the log event time to write to the file
                        Thread.Sleep(5000);
                        throw new Exception("An Error Occuried.");
                    }
                }
            }
        }