Beispiel #1
0
        /// <summary>
        /// Execute
        /// </summary>
        /// <returns></returns>
        protected override bool Execute()
        {
            DTO.CcnUsername = SamAccountName;
            if (!IsUserAuthenticated())
            {
                return(true);
            }
            JSONRPC_Output response = new JSONRPC_Output();
            //The cache key is created to be unique to a given CcnUsername.
            MemCachedD_Value cache = MemCacheD.Get_BSO <dynamic>("PxStat.Security", "Account_API", "ReadCurrentAccesss", DTO.CcnUsername);

            if (cache.hasData)
            {
                Response.data = cache.data;
                return(true);
            }


            Account_BSO      bso    = new Account_BSO();
            ADO_readerOutput output = bso.ReadCurrentAccess(Ado, DTO.CcnUsername);

            if (!output.hasData)
            {
                Log.Instance.Debug("No Account data found");
                return(false);
            }
            Response.data = output.data;
            return(true);
        }
Beispiel #2
0
        internal List <PxApiItem> ReadSubjectsAsObjectList(JSONRPC_Output result)
        {
            List <PxApiItem> pList = new List <PxApiItem>();

            foreach (var item in result.data)
            {
                pList.Add(new PxApiItem()
                {
                    id = item.SbjCode.ToString(), text = item.SbjValue, type = Utility.GetCustomConfig("APP_PXAPI_LIST")
                });
            }

            return(pList);
        }
Beispiel #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="request"></param>
        /// <param name="validator"></param>
        protected BaseTemplate(JSONRPC_API request, IValidator <T> validator)
        {
            Ado = new ADO("defaultConnection");

            if (ActiveDirectory.IsAuthenticated(request.userPrincipal))
            {
                SamAccountName = request.userPrincipal.SamAccountName.ToString();
            }

            Request   = request;
            Response  = new JSONRPC_Output();
            Validator = validator;
            Trace_BSO_Create.Execute(Ado, request);
        }
Beispiel #4
0
        internal List <PxApiItem> ReadCollectionAsObjectList(JSONRPC_Output result)
        {
            List <PxApiItem> pList = new List <PxApiItem>();

            foreach (var item in result.data)
            {
                pList.Add(new PxApiItem()
                {
                    id = item.MtrCode, text = item.MtrTitle, type = Utility.GetCustomConfig("APP_PXAPI_TABLE")
                });
            }

            return(pList);
        }
Beispiel #5
0
        internal JSONRPC_Output WorkflowSignoffCreate(ADO Ado, WorkflowSignoff_DTO DTO, string SamAccountName)
        {
            JSONRPC_Output   response   = new JSONRPC_Output();
            ADO_readerOutput moderators = new ADO_readerOutput();
            ADO_readerOutput powerUsers = new ADO_readerOutput();

            var adoWorkflowRequest  = new WorkflowRequest_ADO();
            var adoWorkflowResponse = new WorkflowResponse_ADO();

            Release_DTO dtoWip = null;


            if (!adoWorkflowResponse.IsInUse(Ado, DTO)) // is current workflow -- this should be the response!!
            {
                //No Live workflow found so we can't proceed
                Log.Instance.Debug("No Current workflow response found for this Release Code");
                response.error = Label.Get("error.create");
                return(response);
            }

            //Is this awaiting signoff?
            var adoWorkflow = new Workflow_ADO();
            ADO_readerOutput resultStatus = adoWorkflow.ReadAwaitingSignoff(Ado, SamAccountName, DTO.RlsCode, Configuration_BSO.GetCustomConfig(ConfigType.global, "language.iso.code"));

            if (!resultStatus.hasData)
            {
                //Release not awaiting signoff so we can't proceed
                Log.Instance.Debug("Release not in status Awaiting Signoff");
                response.error = Label.Get("error.update");
                return(response);
            }

            Security.ActiveDirectory_DTO signoffUser = new Security.ActiveDirectory_DTO()
            {
                CcnUsername = SamAccountName
            };
            Security.ActiveDirectory_ADO accAdo = new Security.ActiveDirectory_ADO();
            Security.Account_DTO_Read    accDto = new Security.Account_DTO_Read()
            {
                CcnUsername = signoffUser.CcnUsername
            };

            DTO.SignoffAccount = accAdo.GetUser(Ado, accDto);


            var adoSignoff = new WorkflowSignoff_ADO();

            //Create a comment
            var adoComment  = new Comment_ADO();
            int commentCode = adoComment.Create(Ado, DTO, SamAccountName);

            if (commentCode == 0)
            {
                // Can't create a comment so we can't proceed
                Log.Instance.Debug("Can't create a comment ");
                response.error = Label.Get("error.create");
                return(response);
            }

            DTO.CmmCode = commentCode;

            //We must read the Request and in order to see how we are going to proceed
            WorkflowRequest_ADO        adoWrq     = new WorkflowRequest_ADO();
            List <WorkflowRequest_DTO> dtoWrqList = adoWrq.Read(Ado, DTO.RlsCode, true);

            if (dtoWrqList.Count > 1)
            {
                //Multiple requests found for this release
                Log.Instance.Debug("More than one request found for this release ");
                response.error = Label.Get("error.create");
                return(response);
            }

            //there must be exactly one live Workflow request at this point
            WorkflowRequest_DTO dtoWrq = dtoWrqList.Find(x => x.RqsCode != null);

            //Get the current Release
            Release_ADO adoRelease = new Release_ADO(Ado);
            Release_DTO dtoRelease = Release_ADO.GetReleaseDTO(adoRelease.Read(DTO.RlsCode, SamAccountName));

            if (dtoRelease == null)
            {
                Log.Instance.Debug("Release not found");
                response.error = Label.Get("error.create");
                return(response);
            }

            Account_BSO aBso = new Account_BSO(Ado);

            moderators = aBso.getReleaseUsers(DTO.RlsCode, null);
            powerUsers = aBso.getUsersOfPrivilege(Constants.C_SECURITY_PRIVILEGE_POWER_USER);

            //Is this a Reject?
            if (DTO.SgnCode.Equals(Constants.C_WORKFLOW_STATUS_REJECT))
            {
                int res = adoSignoff.Create(Ado, DTO, SamAccountName);

                if (res == 0)
                {
                    //Can't create a Workflow Signoff so we can't proceed
                    Log.Instance.Debug("Can't create a Workflow Signoff ");
                    response.error = Label.Get("error.create");
                    return(response);
                }


                WorkflowRequest_DTO_Update dtoReq = new WorkflowRequest_DTO_Update(DTO.RlsCode);
                dtoReq.WrqCurrentFlag = false;

                //update the request
                int reqUpdate = adoWorkflowRequest.Update(Ado, dtoReq, SamAccountName);
                if (reqUpdate == 0)
                {
                    //Can't save the Request so we can't proceed
                    Log.Instance.Debug("Can't save the Workflow Request");
                    response.error = Label.Get("error.update");
                    return(response);
                }

                DTO.MtrCode = dtoRelease.MtrCode; // we need this to see which cache we must flush

                response.data = JSONRPC.success;

                Email_BSO_NotifyWorkflow notifyReject = new Email_BSO_NotifyWorkflow();

                try
                {
                    notifyReject.EmailSignoff(dtoWrq, DTO, dtoRelease, moderators, powerUsers);
                }
                catch { }

                return(response);
            }

            //Not a Reject so we proceed...
            switch (dtoWrq.RqsCode)
            {
            case Constants.C_WORKFLOW_REQUEST_PUBLISH:

                if (String.IsNullOrEmpty(dtoRelease.PrcCode))
                {
                    //There must be a valid product for this release
                    Log.Instance.Debug("No product found for the release ");
                    response.error = Label.Get("error.publish");
                    return(response);
                }

                //Update the current release LiveDatetimeTo to the request Date time
                dtoRelease.RlsLiveDatetimeFrom = dtoWrq.WrqDatetime;

                //set the release live flag
                //update the release version and set the current revision to 0
                DateTime switchDate;
                switchDate = DateTime.Now > dtoWrq.WrqDatetime ? DateTime.Now : dtoWrq.WrqDatetime;
                dtoRelease.RlsVersion++;
                dtoRelease.RlsRevision         = 0;
                dtoRelease.RlsLiveFlag         = true;
                dtoRelease.RlsExceptionalFlag  = dtoWrq.WrqExceptionalFlag != null ? dtoWrq.WrqExceptionalFlag.Value : false;
                dtoRelease.RlsReservationFlag  = dtoWrq.WrqReservationFlag != null ? dtoWrq.WrqReservationFlag.Value : false;
                dtoRelease.RlsArchiveFlag      = dtoWrq.WrqArchiveFlag != null ? dtoWrq.WrqArchiveFlag.Value : false;
                dtoRelease.RlsExperimentalFlag = dtoWrq.WrqExperimentalFlag != null ? dtoWrq.WrqExperimentalFlag.Value : false;
                dtoRelease.RlsLiveDatetimeFrom = switchDate;



                //get the current live release

                Release_DTO releaseDTONow = Release_ADO.GetReleaseDTO(adoRelease.ReadLiveNow(DTO.RlsCode));

                //Save the changes for the release we're changing
                int update = adoRelease.Update(dtoRelease, SamAccountName);
                if (update == 0)
                {
                    Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoRelease.RlsCode);
                    response.error = Label.Get("error.update");
                    return(response);
                }

                if (releaseDTONow != null)
                {
                    //...if there is a previous release
                    if (releaseDTONow.RlsCode != 0)
                    {
                        //Update the  Live LiveDatetimeTo to the request Datetime
                        releaseDTONow.RlsLiveDatetimeTo = switchDate;
                        //Save the changes for the previous release
                        adoRelease.Update(releaseDTONow, SamAccountName);
                    }
                }


                break;

            case Constants.C_WORKFLOW_REQUEST_PROPERTY:
                //update release to transfer all flag values from the request to the release
                dtoRelease.RlsReservationFlag  = dtoWrq.WrqReservationFlag != null ? dtoWrq.WrqReservationFlag.Value : false;
                dtoRelease.RlsArchiveFlag      = dtoWrq.WrqArchiveFlag != null ? dtoWrq.WrqArchiveFlag.Value : false;
                dtoRelease.RlsExperimentalFlag = dtoWrq.WrqExperimentalFlag != null ? dtoWrq.WrqExperimentalFlag.Value : false;

                //Save the release
                int updateCount = adoRelease.Update(dtoRelease, SamAccountName);
                if (updateCount == 0)
                {
                    //Update of Release failed
                    Log.Instance.Debug("Can't update the Release, RlsCode:" + DTO.RlsCode);
                    response.error = Label.Get("error.update");
                    return(response);
                }



                //if there is a WIP or a pending live associated with this matrix then we need to update the WIP/Pending Live as well:
                Release_BSO rBso       = new Release_BSO(Ado);
                dynamic     wipForLive = rBso.GetWipForLive(dtoRelease.RlsCode, SamAccountName);
                if (wipForLive == null)
                {
                    wipForLive = rBso.GetPendingLiveForLive(dtoRelease.RlsCode, SamAccountName);
                }
                if (wipForLive != null)
                {
                    //if a workflow exists for wipForLive, then we must update the flags on that workflow as well
                    var wfForLive = adoWorkflowRequest.Read(Ado, wipForLive.RlsCode, true);
                    if (wfForLive != null)
                    {
                        if (wfForLive.Count > 0)
                        {
                            adoWorkflowRequest.Update(Ado, new WorkflowRequest_DTO_Update()
                            {
                                RlsCode             = wipForLive.RlsCode,
                                WrqArchiveFlag      = dtoWrq.WrqArchiveFlag,
                                WrqCurrentFlag      = true,
                                WrqExperimentalFlag = dtoWrq.WrqExperimentalFlag,
                                WrqReservationFlag  = dtoWrq.WrqReservationFlag
                            }, SamAccountName);
                        }
                    }

                    dtoWip = Release_ADO.GetReleaseDTO(adoRelease.Read(wipForLive.RlsCode, SamAccountName));
                    dtoWip.RlsReservationFlag  = dtoRelease.RlsReservationFlag;
                    dtoWip.RlsArchiveFlag      = dtoRelease.RlsArchiveFlag;
                    dtoWip.RlsExperimentalFlag = dtoRelease.RlsExperimentalFlag;

                    if (adoRelease.Update(dtoWip, SamAccountName) == 0)
                    {
                        Log.Instance.Debug("Failed to update associated WIP " + dtoWip.MtrCode + " " + dtoWip.RlsVersion + '.' + dtoWip.RlsRevision);
                    }

                    //if this wip has a workflow request, then the workflow request details must also be updated

                    List <WorkflowRequest_DTO> wfList = adoWrq.Read(Ado, dtoWip.RlsCode, true);
                    if (wfList.Count > 0)
                    {
                        foreach (var wf in wfList)
                        {
                            wf.WrqReservationFlag  = dtoWrq.WrqReservationFlag;
                            wf.WrqArchiveFlag      = dtoWrq.WrqArchiveFlag;
                            wf.WrqExperimentalFlag = dtoWrq.WrqExperimentalFlag;
                            adoWrq.Update(Ado, new WorkflowRequest_DTO_Update()
                            {
                                RlsCode             = wf.RlsCode,
                                WrqCurrentFlag      = dtoWrq.WrqCurrentFlag,
                                WrqArchiveFlag      = dtoWrq.WrqArchiveFlag,
                                WrqExperimentalFlag = dtoWrq.WrqExperimentalFlag,
                                WrqReservationFlag  = dtoWrq.WrqReservationFlag
                            }, SamAccountName);
                        }
                    }
                }

                break;

            case Constants.C_WORKFLOW_REQUEST_DELETE:
                //We can't soft delete the release just yet. We need it to be live until the Request is updated.

                break;

            case Constants.C_WORKFLOW_REQUEST_ROLLBACK:

                //Delete the future release if it exists and set the current to_date to null
                //Otherwise delete the current release and make the previous release current by setting its to_date to null



                if (adoRelease.IsLiveNext(dtoRelease.RlsCode))    //this is a future release so get the previous release to roll back to (even if that previous is now historical)
                {
                    Compare_ADO cAdo          = new Compare_ADO(Ado);
                    Release_DTO dtoNowRelease = Release_ADO.GetReleaseDTO(adoRelease.Read(cAdo.ReadPreviousRelease(DTO.RlsCode), SamAccountName));


                    dtoNowRelease.RlsLiveDatetimeTo = default(DateTime);
                    int rows = adoRelease.Update(dtoNowRelease, SamAccountName);
                    if (rows == 0)
                    {
                        Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoNowRelease.RlsCode);
                        response.error = Label.Get("error.update");
                        return(response);
                    }

                    //As things stand, dtoRelease is the requested Release (which is a Live Next). This will be deleted in the Delete section below
                }
                else
                {
                    //This isn't a future release - it had better be a Live Now (with a previous)
                    if (!adoRelease.IsLiveNow(dtoRelease.RlsCode))
                    {
                        //If the request is neither a Live Now release then there's a problem
                        Log.Instance.Debug("Can't delete the Release, RlsCode:" + dtoRelease.RlsCode + ". Release is not current live");
                        response.error = Label.Get("error.delete");
                        return(response);
                    }

                    //Find the release that we're trying to rollback to
                    Release_DTO dtoPrevious = Release_ADO.GetReleaseDTO(adoRelease.ReadLivePrevious(dtoRelease.RlsCode));
                    if (dtoPrevious.RlsCode == 0)
                    {
                        //Previous release not found
                        //You can't roll back unless there's something to roll back to, so...
                        Log.Instance.Debug("Can't delete the Release, RlsCode:" + dtoRelease.RlsCode + ". Release is not current live");
                        response.error = Label.Get("error.delete");
                        return(response);
                    }


                    //We set the DatetimeTo to null in the previous release
                    dtoPrevious.RlsLiveDatetimeTo = default(DateTime);
                    int rows = adoRelease.Update(dtoPrevious, SamAccountName);
                    if (rows == 0)
                    {
                        Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoPrevious.RlsCode);
                        response.error = Label.Get("error.update");
                        return(response);
                    }


                    //Do the rollback of the current release
                    dtoRelease.RlsVersion          = dtoPrevious.RlsVersion;
                    dtoRelease.RlsLiveDatetimeFrom = default(DateTime);

                    rows = adoRelease.Update(dtoRelease, SamAccountName);
                    if (rows == 0)
                    {
                        Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoRelease.RlsCode);
                        response.error = Label.Get("error.update");
                        return(response);
                    }

                    adoRelease.IncrementRevision(dtoRelease.RlsCode, SamAccountName);
                }


                break;

            default:
                response.error = Label.Get("error.update");
                return(response);
            }

            int signoffID = adoSignoff.Create(Ado, DTO, SamAccountName);

            if (signoffID == 0)
            {
                //Can't create a Workflow Signoff so we can't proceed
                Log.Instance.Debug("Can't create a Workflow Signoff ");
                response.error = Label.Get("error.create");
                return(response);
            }

            //In all cases, if we have reached this stage, we must update the request to make it non-current
            WorkflowRequest_DTO_Update dtoRequest = new WorkflowRequest_DTO_Update(DTO.RlsCode);

            dtoRequest.WrqCurrentFlag = false;

            //save the request
            int updated = adoWorkflowRequest.Update(Ado, dtoRequest, SamAccountName);

            if (updated == 0)
            {
                //Can't save the Request so we can't proceed
                Log.Instance.Debug("Can't save the Workflow Signoff");
                response.error = Label.Get("error.update");
                return(response);
            }

            // We may now proceed with the soft delete
            Release_BSO_Delete bsoDelete = new Release_BSO_Delete();

            System.Navigation.Keyword_Release_ADO krbAdo = new System.Navigation.Keyword_Release_ADO();

            switch (dtoWrq.RqsCode)
            {
            case Constants.C_WORKFLOW_REQUEST_DELETE:
                //Soft delete the Release. We had to hold this over to last because the Request updates wouldn't work without a live Release
                dtoRelease.RlsCode = DTO.RlsCode;
                Request_ADO adoRequest = new Request_ADO();

                if (adoRelease.IsLiveNow(dtoRelease.RlsCode))
                {
                    Release_DTO dtoNowRelease = Release_ADO.GetReleaseDTO(adoRelease.ReadLiveNow(dtoRequest.RlsCode));

                    //Set the toDate to now, thus setting the release to historical
                    if (dtoNowRelease != null)
                    {
                        dtoNowRelease.RlsLiveDatetimeTo = DateTime.Now;
                        int updateCount = adoRelease.Update(dtoNowRelease, SamAccountName);
                        if (updateCount == 0)
                        {
                            Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoNowRelease.RlsCode);
                            response.error = Label.Get("error.update");
                            return(response);
                        }
                    }

                    //Delete the search keywords for this release
                    krbAdo.Delete(Ado, DTO.RlsCode, null, true);
                }
                else if (adoRelease.IsLiveNext(dtoRelease.RlsCode) || adoRelease.IsWip(dtoRelease.RlsCode))
                {
                    //Find the previous list if it exists
                    Compare_ADO cAdo = new Compare_ADO(Ado);
                    Release_DTO dtoPreviousRelease = Release_ADO.GetReleaseDTO(adoRelease.Read(cAdo.ReadPreviousRelease(DTO.RlsCode), SamAccountName));

                    //if there is a previous live set it to historical, but not if we're deleting a WIP
                    if (dtoPreviousRelease != null && !adoRelease.IsWip(dtoRelease.RlsCode))
                    {
                        //Delete the search keywords for the previous release
                        krbAdo.Delete(Ado, dtoPreviousRelease.RlsCode, null, true);

                        dtoPreviousRelease.RlsLiveDatetimeTo = DateTime.Now;
                        int updateCount = adoRelease.Update(dtoPreviousRelease, SamAccountName);
                        if (updateCount == 0)
                        {
                            Log.Instance.Debug("Can't update the Release, RlsCode:" + dtoPreviousRelease.RlsCode);
                            response.error = Label.Get("error.update");
                            return(response);
                        }
                    }


                    //Delete the search keywords for this release
                    krbAdo.Delete(Ado, DTO.RlsCode, null, true);

                    // We may now proceed with the soft delete
                    if (bsoDelete.Delete(Ado, DTO.RlsCode, SamAccountName, true) == 0)
                    {
                        Log.Instance.Debug("Can't delete the Release, RlsCode:" + DTO.RlsCode);
                        response.error = Label.Get("error.delete");
                        return(response);
                    }
                }
                else
                {
                    //Only LiveNow, LiveNext and WIP releases can be deleted. Anything else means there's a problem.
                    Log.Instance.Debug("Can't delete the Release - invalid release status, RlsCode:" + DTO.RlsCode);
                    response.error = Label.Get("error.delete");
                    return(response);
                }

                break;

            case Constants.C_WORKFLOW_REQUEST_ROLLBACK:

                //First, if there is a WIP ahead of this live release then that WIP must be deleted
                Release_ADO releaseAdo     = new Release_ADO(Ado);
                var         releaseDTORead = new Release_DTO_Read()
                {
                    MtrCode = dtoRelease.MtrCode
                };
                var latestRelease = releaseAdo.ReadLatest(releaseDTORead);
                if (latestRelease != null)
                {
                    if (dtoRelease.RlsCode != latestRelease.RlsCode)
                    {
                        if (bsoDelete.Delete(Ado, latestRelease.RlsCode, SamAccountName, true) == 0)
                        {
                            Log.Instance.Debug("Can't delete the Release, RlsCode:" + latestRelease.RlsCode);
                            response.error = Label.Get("error.delete");
                            return(response);
                        }
                    }
                }

                // Only Live Next gets soft deleted, while Live Now is turned historical above
                if (adoRelease.IsLiveNext(dtoRelease.RlsCode))
                {
                    if (bsoDelete.Delete(Ado, DTO.RlsCode, SamAccountName, true) == 0)
                    {
                        Log.Instance.Debug("Can't delete the Release, RlsCode:" + DTO.RlsCode);
                        response.error = Label.Get("error.delete");
                        return(response);
                    }

                    //Delete the search keywords for this release
                    krbAdo.Delete(Ado, DTO.RlsCode, null, true);
                }

                // Delete the requested release (it may have been live but may also have been demoted to WIP by now)
                if (adoRelease.IsWip(dtoRelease.RlsCode))
                {
                    if (bsoDelete.Delete(Ado, dtoRelease.RlsCode, SamAccountName, true) == 0)
                    {
                        Log.Instance.Debug("Can't delete the Release, RlsCode:" + dtoRelease.RlsCode);
                        response.error = Label.Get("error.delete");
                        return(response);
                    }

                    //Delete the search keywords for this release
                    krbAdo.Delete(Ado, DTO.RlsCode, null, true);
                }
                break;
            }


            DTO.MtrCode = dtoRelease.MtrCode; // we need this to see which cache we must flush

            response.data = JSONRPC.success;
            Email_BSO_NotifyWorkflow notify = new Email_BSO_NotifyWorkflow();

            var sendMailThread = new Thread(() =>
            {
                //If an email error occurs, just ignore it and continue as before
                try
                {
                    notify.EmailSignoff(dtoWrq, DTO, dtoRelease, moderators, powerUsers);
                }
                catch { }
            });

            sendMailThread.Start();

            // Clean up caching
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_ADDITION + DTO.RlsCode);
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_DELETION + DTO.RlsCode);
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_AMENDMENT + DTO.RlsCode);

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_DATASET + DTO.RlsCode);
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_METADATA + DTO.RlsCode);

            if (dtoWip != null)
            {
                MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_ADDITION + dtoWip.RlsCode);
                MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_DELETION + dtoWip.RlsCode);
                MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_COMPARE_READ_AMENDMENT + dtoWip.RlsCode);

                MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_DATASET + dtoWip.RlsCode);
                MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_METADATA + dtoWip.RlsCode);
            }

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_NAVIGATION_SEARCH);
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_NAVIGATION_READ);
            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_COLLECTION);

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + DTO.MtrCode);

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + DTO.MtrCode);

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_METADATA + DTO.MtrCode);

            MemCacheD.CasRepositoryFlush(Resources.Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI);



            return(response);
        }
Beispiel #6
0
        internal JSONRPC_Output WorkflowRequestCreate(WorkflowRequest_DTO DTO, ADO Ado, string SamAccountName)
        {
            JSONRPC_Output response = new JSONRPC_Output();
            //Validation of parameters and user have been successful. We may now proceed to read from the database
            var adoWorkflowRequest = new WorkflowRequest_ADO();

            //check if this release already has a Current WorkflowRequest
            if (adoWorkflowRequest.IsCurrent(Ado, DTO.RlsCode))
            {
                //Can't create a comment so we can't proceed
                Log.Instance.Debug("Release already has a live Workflow, can't create");
                response.error = Label.Get("error.duplicate");
                return(response);
            }
            //Get Release
            Release_ADO releaseAdo = new Release_ADO(Ado);
            Release_DTO releaseDTO = Release_ADO.GetReleaseDTO(releaseAdo.Read(DTO.RlsCode, SamAccountName));

            if (releaseDTO == null)
            {
                Log.Instance.Debug("Release Code not found");
                response.error = Label.Get("error.duplicate");
                return(response);
            }
            Request_ADO adoRequest = new Request_ADO();
            Request_DTO dtoRequest = new Request_DTO
            {
                RlsCode = DTO.RlsCode
            };

            Release_ADO adoRelease = new Release_ADO(Ado);

            //We must validate the request, depending on the RequestCode and the current state of the Release
            switch (DTO.RqsCode)
            {
            case Constants.C_WORKFLOW_REQUEST_PUBLISH:

                if (!adoRelease.IsWip(dtoRequest.RlsCode))
                {
                    Log.Instance.Debug("The requested Release Code is not the WIP Release");
                    response.error = Label.Get("error.create");
                    return(response);
                }

                break;

            case Constants.C_WORKFLOW_REQUEST_PROPERTY:
                //Release must be CURRENT LIVE - RlsLiveFlag is live only between from and to dates
                if (!adoRelease.IsLiveNow(dtoRequest.RlsCode) && !adoRelease.IsLiveNext(dtoRequest.RlsCode))
                {
                    Log.Instance.Debug("Cannot create a Flag Request. The Release must be either current Live or Next Live");
                    response.error = Label.Get("error.create");
                    return(response);
                }
                break;

            case Constants.C_WORKFLOW_REQUEST_DELETE:
                //What about WIP? - not a workflow request but it is part of Release. It will have its own API.
                //Release must be LIVE NEXT, CURRENT LIVE or WIP
                if (!adoRelease.IsLiveNow(dtoRequest.RlsCode) && !adoRelease.IsLiveNext(dtoRequest.RlsCode) && !adoRelease.IsWip(dtoRequest.RlsCode))
                {
                    Log.Instance.Debug("Can't create a DELETE Request on a historical Release");
                    response.error = Label.Get("error.create");
                    return(response);
                }

                break;

            case Constants.C_WORKFLOW_REQUEST_ROLLBACK:

                //Release must be CURRENT LIVE OR NEXT LIVE and PREVIOUS must exist
                if ((!adoRelease.IsLiveNow(dtoRequest.RlsCode) && !adoRelease.IsLiveNext(dtoRequest.RlsCode)) || !adoRelease.HasPrevious(dtoRequest.RlsCode))
                {
                    Log.Instance.Debug("Can't create a ROLLBACK Request because (a) the Request is neither live nor pending-live or (b) there is no valid Release to roll back to.");
                    response.error = Label.Get("error.create");
                    return(response);
                }



                break;

            default:
                Log.Instance.Debug("Invalid Request Code");
                response.error = Label.Get("error.validation");
                return(response);
            }
            var adoComment = new Comment_ADO();

            int commentCode = adoComment.Create(Ado, DTO, SamAccountName);

            if (commentCode == 0)
            {
                //Can't create a comment so we can't proceed
                Log.Instance.Debug("Can't create a comment - WorkflowRequest create request refused");
                response.error = Label.Get("error.create");
                return(response);
            }

            DTO.CmmCode = commentCode;

            //Create the WorkflowRequest - and retrieve the newly created Id
            int newId = adoWorkflowRequest.Create(Ado, DTO, SamAccountName);

            if (newId == 0)
            {
                response.error = Label.Get("error.create");
                return(response);
            }



            response.data = JSONRPC.success;

            List <WorkflowRequest_DTO> dtoWrqList = adoWorkflowRequest.Read(Ado, DTO.RlsCode, true);

            if (dtoWrqList.Count == 1)
            {
                Email_BSO_NotifyWorkflow notify = new Email_BSO_NotifyWorkflow();
                try
                {
                    notify.EmailRequest(dtoWrqList[0], releaseDTO, Ado);
                }
                catch { }
            }
            else
            {
                Log.Instance.Error("Email failed");
            }

            return(response);
        }
Beispiel #7
0
        internal JSONRPC_Output WorkflowResponseCreate(WorkflowResponse_DTO DTO, ADO Ado, string SamAccountName)
        {
            JSONRPC_Output response = new JSONRPC_Output();
            //We need to get the Request for notification purposes
            WorkflowRequest_ADO        adoWrq     = new WorkflowRequest_ADO();
            List <WorkflowRequest_DTO> dtoWrqList = adoWrq.Read(Ado, DTO.RlsCode, true);

            if (dtoWrqList.Count > 1)
            {
                //Multiple requests found for this release
                Log.Instance.Debug("More than one request found for this release ");
                response.error = Label.Get("error.create");
                return(response);
            }

            if (dtoWrqList.Count == 0)
            {
                //No request found for this release
                Log.Instance.Debug("No request found for this release ");
                response.error = Label.Get("error.create");
                return(response);
            }


            //Validation of parameters and user have been successful. We may now proceed to read from the database
            var adoWorkflowRequest = new WorkflowRequest_ADO();

            if (!adoWorkflowRequest.IsCurrent(Ado, DTO.RlsCode))
            {
                //No workflow found
                Log.Instance.Debug("No Live workflow found for this Release Code");
                response.error = Label.Get("error.create");
                return(response);
            }

            var adoWorkflowResponse = new WorkflowResponse_ADO();

            //If this is a Moderator, we need to check if the user is in the same group as the release and has approve rights

            Account_BSO acBso = new Account_BSO();

            if (acBso.IsModerator(Ado, SamAccountName))
            {
                var approveRlsList = adoWorkflowResponse.GetApproverAccess(Ado, SamAccountName, true, DTO.RlsCode);
                if (approveRlsList.data.Count == 0)
                {
                    Log.Instance.Debug("Insufficient access for a Moderator");
                    response.error = Label.Get("error.authentication");
                    return(response);
                }
            }

            //Check that there isn't already a WorkflowResponse for this Release

            if (adoWorkflowResponse.IsInUse(Ado, DTO))
            {
                //Duplicate Workflow exists
                Log.Instance.Debug("Workflow Response exists already, can't create");
                response.error = Label.Get("error.duplicate");
                return(response);
            }


            var adoComment = new Comment_ADO();

            int commentCode = adoComment.Create(Ado, DTO, SamAccountName);

            if (commentCode == 0)
            {
                //Can't create a comment so we can't proceed
                Log.Instance.Debug("Can't create comment - create WorkflowResponse refused");
                response.error = Label.Get("error.create");
                return(response);
            }

            DTO.CmmCode = commentCode;



            int createResponse = adoWorkflowResponse.Create(Ado, DTO, SamAccountName);

            if (createResponse == 0)
            {
                response.error = Label.Get("error.create");
                return(response);
            }

            //If this is a Reject then we must reset the workflow to stop it being current
            if (DTO.RspCode.Equals(Resources.Constants.C_WORKFLOW_STATUS_REJECT))
            {
                WorkflowRequest_DTO_Update dtoRequest = new WorkflowRequest_DTO_Update(DTO.RlsCode)
                {
                    WrqCurrentFlag = false
                };

                adoWorkflowRequest.Update(Ado, dtoRequest, SamAccountName);
            }

            response.data = JSONRPC.success;

            //Get Release
            Release_ADO releaseAdo = new Release_ADO(Ado);
            Release_DTO releaseDTO = Release_ADO.GetReleaseDTO(releaseAdo.Read(DTO.RlsCode, SamAccountName));

            Security.ActiveDirectory_DTO responseUser = new Security.ActiveDirectory_DTO()
            {
                CcnUsername = SamAccountName
            };
            Security.ActiveDirectory_ADO accAdo = new Security.ActiveDirectory_ADO();
            Security.Account_DTO_Read    accDto = new Security.Account_DTO_Read()
            {
                CcnUsername = responseUser.CcnUsername
            };

            DTO.ResponseAccount = accAdo.GetUser(Ado, accDto);

            Email_BSO_NotifyWorkflow notify = new Email_BSO_NotifyWorkflow();

            try
            {
                notify.EmailResponse(dtoWrqList[0], DTO, releaseDTO);
            }
            catch
            { }
            return(response);
        }
        /// <summary>
        /// Execute
        /// </summary>
        /// <param name="theAdo"></param>
        /// <param name="theCubeDTO"></param>
        /// <param name="theReleaseDto"></param>
        /// <param name="theResponse"></param>
        /// <returns></returns>
        static internal bool ExecuteReadMetadata(ADO theAdo, Cube_DTO_Read theCubeDTO, Release_DTO theReleaseDto, JSONRPC_Output theResponse, bool isLive = true)
        {
            var ado = new Cube_ADO(theAdo);

            // The matrix constructor will load all the metadata from the db when instances specification
            var theMatrix = new Matrix(theAdo, theReleaseDto, theCubeDTO.language).ApplySearchCriteria(theCubeDTO);

            theMatrix.FormatType    = theCubeDTO.Format.FrmType;
            theMatrix.FormatVersion = theCubeDTO.Format.FrmVersion;
            if (theMatrix == null)
            {
                theResponse.data = null;
                return(true);
            }

            var jsonStat = theMatrix.GetJsonStatObject();

            theResponse.data = new JRaw(Serialize.ToJson(jsonStat));

            if (isLive)
            {
                if (theReleaseDto.RlsLiveDatetimeTo != default)
                {
                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadMetadata", theCubeDTO, theResponse.data, theReleaseDto.RlsLiveDatetimeTo, Resources.Constants.C_CAS_DATA_CUBE_READ_METADATA + theMatrix.Code);
                }
                else
                {
                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadMetadata", theCubeDTO, theResponse.data, new DateTime(), Resources.Constants.C_CAS_DATA_CUBE_READ_METADATA + theMatrix.Code);
                }
            }
            else
            {
                if (theReleaseDto.RlsLiveDatetimeFrom > DateTime.Now)
                {
                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadPreMetadata", theCubeDTO, theResponse.data, theReleaseDto.RlsLiveDatetimeFrom, Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_METADATA + theReleaseDto.RlsCode);
                }
                else
                {
                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadPreMetadata", theCubeDTO, theResponse.data, default(DateTime), Resources.Constants.C_CAS_DATA_CUBE_READ_PRE_METADATA + theReleaseDto.RlsCode);
                }
            }
            return(true);
        }
Beispiel #9
0
        internal static bool ExecuteReadDataset(ADO theAdo, CubeQuery_DTO theDto, Release_DTO releaseDto, JSONRPC_Output theResponse, string requestLanguage, string culture = null)
        {
            var theMatrix = new Matrix(theAdo, releaseDto, theDto.jStatQueryExtension.extension.Language.Code).ApplySearchCriteria(theDto);

            if (theMatrix == null)
            {
                theResponse.data = null;
                return(true);
            }

            var matrix = new Cube_ADO(theAdo).ReadCubeData(theMatrix);

            if (matrix == null)
            {
                theResponse.data = null;
                return(true);
            }

            CultureInfo readCulture;

            if (culture == null)
            {
                readCulture = CultureInfo.CurrentCulture;
            }
            else
            {
                readCulture = CultureInfo.CreateSpecificCulture(culture);
            };


            switch (theDto.jStatQueryExtension.extension.Format.Type)
            {
            case DatasetFormat.JsonStat:
                matrix.FormatType    = theDto.jStatQueryExtension.extension.Format.Type;
                matrix.FormatVersion = theDto.jStatQueryExtension.extension.Format.Version;

                if (theDto.jStatQueryExtension.extension.Format.Version == "1.0")
                {
                    var jsonStat = matrix.GetJsonStatV1Object(true, null);
                    theResponse.data = new JRaw(SerializeJsonStatV1.ToJson(jsonStat));
                }
                else
                {
                    var jsonStat = matrix.GetJsonStatObject(false, true, null, readCulture);
                    theResponse.data = new JRaw(Serialize.ToJson(jsonStat));
                }
                break;

            case DatasetFormat.Csv:
                theResponse.data = matrix.GetCsvObject(requestLanguage, false, readCulture);
                break;

            case DatasetFormat.Px:
                theResponse.data = matrix.GetPxObject().ToString();
                break;

            case DatasetFormat.Xlsx:
                theResponse.data = matrix.GetXlsxObject(requestLanguage, CultureInfo.CurrentCulture);
                break;

            case DatasetFormat.Sdmx:
                theResponse.data = matrix.GetSdmx(theDto.jStatQueryExtension.extension.Language.Code);
                break;
            }

            if (releaseDto.RlsLiveDatetimeTo != default(DateTime))
            {
                MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadDataset", theDto, theResponse.data, releaseDto.RlsLiveDatetimeTo, Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + matrix.Code);
            }
            else
            {
                MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadDataset", theDto, theResponse.data, new DateTime(), Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + matrix.Code);
            }
            return(true);
        }
Beispiel #10
0
        internal static bool ExecuteReadDataset(ADO theAdo, CubeQuery_DTO theDto, Release_DTO releaseDto, JSONRPC_Output theResponse, string requestLanguage, string culture = null, bool defaultPivot = false)
        {
            var theMatrix = new Matrix(theAdo, releaseDto, theDto.jStatQueryExtension.extension.Language.Code).ApplySearchCriteria(theDto);

            if (theMatrix == null)
            {
                theResponse.data = null;
                return(true);
            }

            if (theDto.jStatQueryExtension.extension.Format.Type.Equals(Resources.Constants.C_SYSTEM_XLSX_NAME))
            {
                int dataSize = theMatrix.MainSpec.GetDataSize();
                int maxSize  = Configuration_BSO.GetCustomConfig(ConfigType.global, "dataset.download.threshold.xlsx");
                if (dataSize > maxSize)
                {
                    theResponse.error = String.Format(Label.Get("error.dataset.limit", requestLanguage), dataSize, Resources.Constants.C_SYSTEM_XLSX_NAME, maxSize);
                    return(false);
                }
            }
            else if (theDto.jStatQueryExtension.extension.Format.Type.Equals(Resources.Constants.C_SYSTEM_CSV_NAME))
            {
                int dataSize = theMatrix.MainSpec.GetDataSize();
                int maxSize  = Configuration_BSO.GetCustomConfig(ConfigType.global, "dataset.download.threshold.csv");
                if (dataSize > maxSize)
                {
                    theResponse.error = String.Format(Label.Get("error.dataset.limit", requestLanguage), dataSize, Resources.Constants.C_SYSTEM_CSV_NAME, maxSize);
                    return(false);
                }
            }


            var matrix = new Cube_ADO(theAdo).ReadCubeData(theMatrix);

            if (matrix == null)
            {
                theResponse.data = null;
                return(true);
            }


            if (!string.IsNullOrEmpty(theDto.jStatQueryExtension.extension.Pivot))
            {
                if (theMatrix.MainSpec.Classification.Where(x => x.Code == theDto.jStatQueryExtension.extension.Pivot).Count() == 0 &&
                    Utility.GetCustomConfig("APP_CSV_STATISTIC") != theDto.jStatQueryExtension.extension.Pivot &&
                    theMatrix.MainSpec.Frequency.Code != theDto.jStatQueryExtension.extension.Pivot)
                {
                    theResponse.error = Label.Get("error.validation", theDto.jStatQueryExtension.extension.Language.Code);
                    return(false);
                }
            }
            else
            {
                theDto.jStatQueryExtension.extension.Pivot = null;
            }

            CultureInfo readCulture;

            if (culture == null)
            {
                readCulture = CultureInfo.CurrentCulture;
            }
            else
            {
                readCulture = CultureInfo.CreateSpecificCulture(culture);
            };

            if (theDto.jStatQueryExtension.extension.Pivot == null && defaultPivot && theDto.jStatQueryExtension.extension.Format.Type == DatasetFormat.Csv)
            {
                theDto.jStatQueryExtension.extension.Pivot = matrix.MainSpec.Frequency.Code;
            }


            switch (theDto.jStatQueryExtension.extension.Format.Type)
            {
            case DatasetFormat.JsonStat:
                matrix.FormatType    = theDto.jStatQueryExtension.extension.Format.Type;
                matrix.FormatVersion = theDto.jStatQueryExtension.extension.Format.Version;

                if (theDto.jStatQueryExtension.extension.Format.Version == "1.0")
                {
                    var jsonStat = matrix.GetJsonStatV1Object(true, null);
                    theResponse.data = new JRaw(SerializeJsonStatV1.ToJson(jsonStat));
                }
                else if (theDto.jStatQueryExtension.extension.Format.Version == "1.1")
                {
                    var jsonStat = matrix.GetJsonStatV1_1Object(false, true, null, readCulture);
                    theResponse.data = new JRaw(SerializeJsonStatV1_1.ToJson(jsonStat));
                }
                else
                {
                    var jsonStat = matrix.GetJsonStatObject(false, true, null, readCulture);
                    theResponse.data = new JRaw(Serialize.ToJson(jsonStat));
                }
                break;

            case DatasetFormat.Csv:
                theResponse.data = matrix.GetCsvObject(requestLanguage, false, readCulture, theDto.jStatQueryExtension.extension.Pivot, theDto.jStatQueryExtension.extension.Codes == null ? true : (bool)theDto.jStatQueryExtension.extension.Codes);
                break;

            case DatasetFormat.Px:
                theResponse.data = matrix.GetPxObject();
                break;

            case DatasetFormat.Xlsx:
                theResponse.data = matrix.GetXlsxObject(requestLanguage, CultureInfo.CurrentCulture, theDto.jStatQueryExtension.extension.Pivot, theDto.jStatQueryExtension.extension.Codes == null ? true : (bool)theDto.jStatQueryExtension.extension.Codes);
                break;

            case DatasetFormat.Sdmx:
                theResponse.data = matrix.GetSdmx(theDto.jStatQueryExtension.extension.Language.Code);
                break;
            }

            if (releaseDto.RlsLiveDatetimeTo != default(DateTime))
            {
                MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadDataset", theDto, theResponse.data, releaseDto.RlsLiveDatetimeTo, Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + matrix.Code);
            }
            else
            {
                MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadDataset", theDto, theResponse.data, new DateTime(), Resources.Constants.C_CAS_DATA_CUBE_READ_DATASET + matrix.Code);
            }
            return(true);
        }
Beispiel #11
0
        public static dynamic PxAPIv1(RESTful_API restfulRequest)
        {
            try
            {
                var         parameters = ((List <string>)restfulRequest.parameters).Where(x => !string.IsNullOrWhiteSpace(x));
                int         pcount     = parameters.Count() - 1;
                JSONRPC_API rpc        = Map.RESTful2JSONRPC_API(restfulRequest);

                Cube_MAP      map       = new Cube_MAP();
                JObject       rpcParams = new JObject();
                PxApiMetadata meta      = new PxApiMetadata();

                var context = HttpContext.Current.Request;

                switch (pcount)
                {
                case Constants.C_DATA_PXAPIV1_SUBJECT_QUERY:
                    //Get a list of all subjects

                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));
                    rpc.parameters = rpcParams;

                    var rspSbj = new Subject_BSO_Read(rpc).Read().Response;

                    if (rspSbj.data == null)
                    {
                        return new RESTful_Output()
                               {
                                   statusCode = HttpStatusCode.NotFound
                               }
                    }
                    ;

                    rspSbj.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadSubjectsAsObjectList(rspSbj));



                    var responseSbj = Map.JSONRPC2RESTful_Output(rspSbj, null, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError);

                    return(responseSbj);

                case Constants.C_DATA_PXAPIV1_PRODUCT_QUERY:
                    //Get a list of products for a subject
                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                    rpcParams.Add("SbjCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY));
                    rpc.parameters = rpcParams;

                    var rspPrd = new Product_BSO_Read(rpc).Read().Response;

                    if (rspPrd.data == null)
                    {
                        return new RESTful_Output()
                               {
                                   statusCode = HttpStatusCode.NotFound
                               }
                    }
                    ;

                    meta = new PxApiMetadata();

                    if (rspPrd.data.Count == 0)
                    {
                        rspPrd = new JSONRPC_Output();

                        rspPrd.data = null;
                    }
                    else
                    {
                        rspPrd.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadProductsAsObjectList(rspPrd));
                    }

                    var responsePrd = Map.JSONRPC2RESTful_Output(rspPrd, null, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError);

                    return(responsePrd);

                case Constants.C_DATA_PXAPIV1_COLLECTION_QUERY:
                    //Get a list of live tables for a product
                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));
                    rpcParams.Add("product", parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));

                    if (!Int32.TryParse(parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), out int sbjInt))
                    {
                        rpcParams.Add("SbjCode", "0");
                    }
                    else
                    {
                        rpcParams.Add("SbjCode", sbjInt.ToString());
                    }


                    rpc.parameters = rpcParams;

                    List <dynamic> collection = new List <dynamic>();

                    RESTful_Output output = new RESTful_Output();
                    DateTime       nextReleaseDateForProduct = default;

                    using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                    {
                        nextReleaseDateForProduct = cBso.GetNextReleaseDate(parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                    }

                    //See if this request has cached data
                    MemCachedD_Value cache = MemCacheD.Get_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams);

                    if (cache.hasData && (nextReleaseDateForProduct == default || nextReleaseDateForProduct > DateTime.Now))
                    {
                        output.response   = cache.data;
                        output.statusCode = HttpStatusCode.OK;
                    }

                    else
                    {
                        using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                        {
                            collection = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                        }

                        meta = new PxApiMetadata();

                        if (collection != null)
                        {
                            if (collection.Count > 0)
                            {
                                output.response = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadCollectionAsObjectList(new JSONRPC_Output()
                                {
                                    data = collection
                                }));
                                output.statusCode = HttpStatusCode.OK;
                            }
                            else
                            {
                                output.statusCode = HttpStatusCode.NotFound;
                            }
                        }
                        else
                        {
                            output.statusCode = HttpStatusCode.NotFound;
                        }
                    }


                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams, output.response, nextReleaseDateForProduct, Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI);

                    return(output);

                default:

                    if (pcount < Constants.C_DATA_PXAPIV1_METADATA_QUERY)
                    {
                        throw new Exception(Label.Get("error.validation"));
                    }

                    //Either get the metadata for the table or, if a query has been supplied, get the table data based on the query
                    string request = string.IsNullOrWhiteSpace(restfulRequest.httpPOST) ? restfulRequest.httpGET[Constants.C_JSON_STAT_QUERY_CLASS] : restfulRequest.httpPOST;

                    //For conventional RESTful patterns, don't return metadata unless the requested MtrCode is contained in the collection
                    //for the subject and product parameters
                    List <dynamic> collectionPrdSbjmeta = new List <dynamic>();
                    using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                    {
                        collectionPrdSbjmeta = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                    }

                    if (collectionPrdSbjmeta == null)
                    {
                        var empty = new RESTful_Output();
                        empty.statusCode = HttpStatusCode.NotFound;
                        return(empty);
                    }

                    if (collectionPrdSbjmeta.Where(x => x.MtrCode == parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)).ToList().Count == 0)
                    {
                        var empty = new RESTful_Output();
                        empty.statusCode = HttpStatusCode.NotFound;
                        return(empty);
                    }

                    if (request == null)
                    {
                        rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                        rpcParams.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));

                        rpcParams.Add("format", JToken.FromObject(new { type = Constants.C_SYSTEM_JSON_STAT_NAME, version = Constants.C_SYSTEM_JSON_STAT_2X_VERSION }));

                        Matrix theMatrix;
                        using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                        {
                            theMatrix = cBso.GetMetadataMatrix(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));
                        }
                        var rspMeta = new RESTful_Output();    // JSONRPC_Output();

                        if (theMatrix != null)
                        {
                            var json = theMatrix.GetApiMetadata(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                            rpc.parameters = rpcParams;

                            rspMeta.response   = Utility.JsonSerialize_IgnoreLoopingReference(json);
                            rspMeta.statusCode = HttpStatusCode.OK;
                        }
                        else
                        {
                            rspMeta.statusCode = HttpStatusCode.NotFound;
                        }


                        return(rspMeta);
                    }
                    else
                    {
                        //This is a request for data - we need to use the query

                        //For conventional RESTful patterns, don't return metadata unless the requested MtrCode is contained in the collection
                        //for the subject and product parameters
                        List <dynamic> collectionPrdSbjdata = new List <dynamic>();
                        using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                        {
                            collectionPrdSbjdata = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                        }

                        if (collectionPrdSbjmeta == null)
                        {
                            var empty = new RESTful_Output();
                            empty.statusCode = HttpStatusCode.NotFound;
                            return(empty);
                        }

                        if (collectionPrdSbjdata.Where(x => x.MtrCode == parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY)).ToList().Count == 0)
                        {
                            var empty = new RESTful_Output();
                            empty.statusCode = HttpStatusCode.NotFound;
                            return(empty);
                        }


                        string queryString = string.IsNullOrWhiteSpace(restfulRequest.httpPOST) ? restfulRequest.httpGET[Constants.C_JSON_STAT_QUERY_CLASS] : restfulRequest.httpPOST;

                        PxApiV1Query pxapiQuery = Utility.JsonDeserialize_IgnoreLoopingReference <PxApiV1Query>(queryString);


                        Format_DTO_Read format = new Format_DTO_Read(pxapiQuery.Response.Format);
                        string          pivot  = pxapiQuery.Response.Pivot;


                        JsonStatQuery jsQuery = new JsonStatQuery();
                        jsQuery.Class      = Constants.C_JSON_STAT_QUERY_CLASS;
                        jsQuery.Id         = new List <string>();
                        jsQuery.Dimensions = new Dictionary <string, JsonQuery.Dimension>();
                        foreach (var q in pxapiQuery.Query)
                        {
                            jsQuery.Id.Add(q.Code);
                            jsQuery.Dimensions.Add(q.Code, new JsonQuery.Dimension()
                            {
                                Category = new JsonQuery.Category()
                                {
                                    Index = q.Selection.Values.ToList <string>()
                                }
                            });
                        }
                        jsQuery.Extension = new Dictionary <string, object>();

                        jsQuery.Extension.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));
                        jsQuery.Extension.Add("language", new Language()
                        {
                            Code = parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), Culture = null
                        });
                        jsQuery.Extension.Add("codes", false);
                        jsQuery.Extension.Add("format", new JsonQuery.Format()
                        {
                            Type = format.FrmType, Version = format.FrmVersion
                        });
                        if (pivot != null)
                        {
                            jsQuery.Extension.Add("pivot", pivot);
                        }
                        jsQuery.Version = Constants.C_JSON_STAT_QUERY_VERSION;


                        JSONRPC_API jsonRpcRequest = Map.RESTful2JSONRPC_API(restfulRequest);

                        jsonRpcRequest.parameters = Utility.JsonSerialize_IgnoreLoopingReference(jsQuery);

                        //Run the request as a Json Rpc call
                        JSONRPC_Output rsp = new Cube_BSO_ReadDataset(jsonRpcRequest, true).Read().Response;

                        if (rsp.error != null)
                        {
                            return(Map.JSONRPC2RESTful_Output(rsp, null, HttpStatusCode.Forbidden));
                        }
                        // Convert the JsonRpc output to RESTful output
                        var response = Map.JSONRPC2RESTful_Output(rsp, format.FrmMimetype, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError);

                        string suffix;
                        using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection")))
                        {
                            suffix = bso.GetFileSuffixForFormat(format);
                        };


                        response.fileName = parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY) + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + suffix;
                        return(response);
                    }
                }
            }
            catch (Exception ex)

            {
                RESTful_Output error = new RESTful_Output
                {
                    statusCode = HttpStatusCode.InternalServerError
                };

                Log.Instance.Debug(ex.Message);
                return(error);
            }
        }
Beispiel #12
0
        public static dynamic PxAPIv1(RESTful_API restfulRequest)
        {
            try
            {
                var         parameters = ((List <string>)restfulRequest.parameters).Where(x => !string.IsNullOrWhiteSpace(x));
                int         pcount     = parameters.Count() - 1;
                JSONRPC_API rpc        = Map.RESTful2JSONRPC_API(restfulRequest);

                Cube_MAP      map       = new Cube_MAP();
                JObject       rpcParams = new JObject();
                PxApiMetadata meta      = new PxApiMetadata();

                var context = HttpContext.Current.Request;

                switch (pcount)
                {
                case Constants.C_DATA_PXAPIV1_SUBJECT_QUERY:
                    //Get a list of all subjects

                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));
                    rpc.parameters = rpcParams;

                    var rspSbj = new Subject_BSO_Read(rpc).Read().Response;

                    if (rspSbj.data == null)
                    {
                        return new RESTful_Output()
                               {
                                   statusCode = HttpStatusCode.NotFound
                               }
                    }
                    ;

                    rspSbj.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadSubjectsAsObjectList(rspSbj));



                    var responseSbj = Map.JSONRPC2RESTful_Output(rspSbj, null, rspSbj.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK);

                    return(responseSbj);

                case Constants.C_DATA_PXAPIV1_PRODUCT_QUERY:
                    //Get a list of products for a subject
                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                    rpcParams.Add("SbjCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_PRODUCT_QUERY));
                    rpc.parameters = rpcParams;

                    var rspPrd = new Product_BSO_Read(rpc).Read().Response;

                    if (rspPrd.data == null)
                    {
                        return new RESTful_Output()
                               {
                                   statusCode = HttpStatusCode.NotFound
                               }
                    }
                    ;

                    meta = new PxApiMetadata();

                    if (rspPrd.data.Count == 0)
                    {
                        rspPrd = new JSONRPC_Output();

                        rspPrd.data = null;
                    }
                    else
                    {
                        rspPrd.data = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadProductsAsObjectList(rspPrd));
                    }

                    var responsePrd = Map.JSONRPC2RESTful_Output(rspPrd, null, rspPrd.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK);

                    return(responsePrd);

                case Constants.C_DATA_PXAPIV1_COLLECTION_QUERY:
                    //Get a list of live tables for a product
                    rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                    rpcParams.Add("product", parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                    rpc.parameters = rpcParams;

                    List <dynamic> collection = new List <dynamic>();

                    RESTful_Output output = new RESTful_Output();
                    DateTime       nextReleaseDateForProduct = default;

                    using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                    {
                        nextReleaseDateForProduct = cBso.GetNextReleaseDate(parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                    }

                    //See if this request has cached data
                    MemCachedD_Value cache = MemCacheD.Get_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams);

                    if (cache.hasData && (nextReleaseDateForProduct == default || nextReleaseDateForProduct > DateTime.Now))
                    {
                        output.response   = cache.data;
                        output.statusCode = HttpStatusCode.OK;
                    }

                    else
                    {
                        using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                        {
                            collection = cBso.ReadCollection(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                        }

                        meta = new PxApiMetadata();

                        if (collection.Count > 0)
                        {
                            output.response = Utility.JsonSerialize_IgnoreLoopingReference(meta.ReadCollectionAsObjectList(new JSONRPC_Output()
                            {
                                data = collection
                            }));
                            output.statusCode = HttpStatusCode.OK;
                        }
                        else
                        {
                            output.statusCode = HttpStatusCode.NotFound;
                        }
                    }



                    MemCacheD.Store_BSO <dynamic>("PxStat.Data", "Cube_API", "ReadCollectionPxApi", rpcParams, output.response, nextReleaseDateForProduct, Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI + parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));

                    //MemCacheD.CasRepositoryFlush(Constants.C_CAS_DATA_CUBE_READ_COLLECTION_PXAPI + parameters.ElementAt(Constants.C_DATA_PXAPIV1_COLLECTION_QUERY));
                    return(output);

                case Constants.C_DATA_PXAPIV1_METADATA_QUERY:
                    //Either get the metadata for the table or, if a query has been supplied, get the table data based on the query
                    string request = string.IsNullOrWhiteSpace(Utility.HttpPOST()) ? Utility.HttpGET()["query"] : Utility.HttpPOST();

                    if (request == null)
                    {
                        rpcParams.Add("LngIsoCode", parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                        rpcParams.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));

                        rpcParams.Add("format", JToken.FromObject(new { type = "JSON-stat", version = "2.0" }));

                        Matrix theMatrix;
                        using (Cube_BSO cBso = new Cube_BSO(new ADO("defaultConnection")))
                        {
                            theMatrix = cBso.GetMetadataMatrix(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY), parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));
                        }
                        var rspMeta = new RESTful_Output();    // JSONRPC_Output();

                        if (theMatrix != null)
                        {
                            var json = theMatrix.GetApiMetadata(parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY));

                            rpc.parameters = rpcParams;

                            rspMeta.response   = Utility.JsonSerialize_IgnoreLoopingReference(json);
                            rspMeta.statusCode = HttpStatusCode.OK;
                        }
                        else
                        {
                            rspMeta.statusCode = HttpStatusCode.NotFound;
                        }


                        return(rspMeta);
                    }
                    else
                    {
                        //This is a request for data - we need to use the query



                        string queryString = string.IsNullOrWhiteSpace(Utility.HttpPOST()) ? Utility.HttpGET()["query"] : Utility.HttpPOST();

                        PxApiV1Query pxapiQuery = Utility.JsonDeserialize_IgnoreLoopingReference <PxApiV1Query>(queryString);

                        Format_DTO_Read format = new Format_DTO_Read(pxapiQuery.Response.Format);

                        Analytic_BSO_Create.Create(HttpContext.Current.Request, "PxStat.Data.PxAPIv1", HttpContext.Current.Request.UserAgent,
                                                   restfulRequest.ipAddress, parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY), false, format);

                        JsonStatQuery jsQuery = new JsonStatQuery();
                        jsQuery.Class      = "query";
                        jsQuery.Id         = new List <string>();
                        jsQuery.Dimensions = new Dictionary <string, JsonQuery.Dimension>();
                        foreach (var q in pxapiQuery.Query)
                        {
                            jsQuery.Id.Add(q.Code);
                            jsQuery.Dimensions.Add(q.Code, new JsonQuery.Dimension()
                            {
                                Category = new JsonQuery.Category()
                                {
                                    Index = q.Selection.Values.ToList <string>()
                                }
                            });
                        }
                        jsQuery.Extension = new Dictionary <string, object>();
                        jsQuery.Extension.Add("matrix", parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY));
                        jsQuery.Extension.Add("language", new Language()
                        {
                            Code = parameters.ElementAt(Constants.C_DATA_PXAPIV1_SUBJECT_QUERY)
                        });
                        jsQuery.Extension.Add("format", new JsonQuery.Format()
                        {
                            Type = format.FrmType, Version = format.FrmVersion
                        });
                        jsQuery.Version = Utility.GetCustomConfig("APP_JSON_STAT_QUERY_VERSION");

                        JSONRPC_API jsonRpcRequest = Map.RESTful2JSONRPC_API(restfulRequest);

                        jsonRpcRequest.parameters = Utility.JsonSerialize_IgnoreLoopingReference(jsQuery);
                        //Run the request as a Json Rpc call
                        JSONRPC_Output rsp = new Cube_BSO_ReadDataset(jsonRpcRequest).Read().Response;
                        // Convert the JsonRpc output to RESTful output
                        var response = Map.JSONRPC2RESTful_Output(rsp, format.FrmMimetype, rsp.data == null ? HttpStatusCode.NotFound : HttpStatusCode.OK);

                        string suffix;
                        using (Format_BSO bso = new Format_BSO(new ADO("defaultConnection")))
                        {
                            suffix = bso.GetFileSuffixForFormat(format);
                        };


                        response.fileName = parameters.ElementAt(Constants.C_DATA_PXAPIV1_METADATA_QUERY) + suffix;
                        return(response);
                    }


                default:
                    throw new Exception(Label.Get("error.validation"));
                }
            }
            catch (Exception ex)

            {
                RESTful_Output error = new RESTful_Output
                {
                    statusCode = HttpStatusCode.InternalServerError
                };

                Log.Instance.Error(ex.Message);
                return(error);
            }
        }