public PutProgramToPatientResponse PutPatientToProgram(PutProgramToPatientRequest request) { try { PutProgramToPatientResponse response = new PutProgramToPatientResponse(); response.Outcome = new Phytel.API.DataDomain.Program.DTO.Outcome(); #region validation calls if (!IsValidPatientId(request)) { return(FormatExceptionResponse(response, "Patient does not exist or has an invalid id.", "500")); } if (!IsValidContractProgramId(request)) { return(FormatExceptionResponse(response, "ContractProgram does not exist or has an invalid identifier.", "500")); } if (!IsContractProgramAssignable(request)) { return(FormatExceptionResponse(response, "ContractProgram is not currently active.", "500")); } #endregion /**********************************/ List <MEPatientProgram> pp = DTOUtility.FindExistingpatientProgram(request); if (!DTOUtility.CanInsertPatientProgram(pp)) { response.Outcome.Result = 0; response.Outcome.Reason = pp[0].Name + " is already assigned or reassignment is not allowed"; } else { MEProgram cp = DTOUtility.GetProgramForDeepCopy(request); var stepIdList = new List <ObjectId>(); List <MEResponse> responseList = DTOUtility.GetProgramResponseslist(stepIdList, cp, request); DTOUtils.HydrateResponsesInProgram(cp, responseList, request.UserId); MEPatientProgram nmePp = DTOUtility.CreateInitialMEPatientProgram(request, cp, stepIdList); DTOUtility.InitializePatientProgramAssignment(request, nmePp); List <MEPatientProgramResponse> pprs = DTOUtils.ExtractMEPatientProgramResponses(nmePp, request.ContractNumber, request.UserId); ProgramInfo pi = DTOUtility.SaveNewPatientProgram(request, nmePp); if (pi != null) { response.program = pi; DTOUtility.SavePatientProgramResponses(pprs, request); DTOUtility.InitializeProgramAttributes(request, response); } response.Outcome.Result = 1; response.Outcome.Reason = "Successfully assigned this program for the individual"; } return(response); } catch (Exception ex) { throw new Exception("DD:DataProgramManager:PutPatientToProgram()::" + ex.Message, ex.InnerException); } }