Search in sources :

Example 1 with ResponseSuccessStudyDTO

use of org.akaza.openclinica.bean.login.ResponseSuccessStudyDTO in project OpenClinica by OpenClinica.

the class StudyController method createNewStudy.

/**
	 * @api {post} /pages/auth/api/v1/studies/ Create a study
	 * @apiName createNewStudy
	 * @apiPermission Authenticate using api-key. admin
	 * @apiVersion 3.8.0
	 * @apiParam {String} uniqueProtococlId Study unique protocol ID.
	 * @apiParam {String} briefTitle Brief Title .
	 * @apiParam {String} principalInvestigator Principal Investigator Name.
	 * @apiParam {Integer} expectedTotalEnrollment Expected Total Enrollment number
	 * @apiParam {String} sponsor Sponsor name.
	 * @apiParam {String} protocolType 'Interventional' or ' Observational'
	 * @apiParam {String} status 'Available' or 'Design'
	 * @apiParam {String} briefSummary Study Summary
	 * @apiParam {Date} startDate Start date
	 * @apiParam {Array} assignUserRoles Assign Users to Roles for this Study.
	 * @apiGroup Study
	 * @apiHeader {String} api_key Users unique access-key.
	 * @apiDescription This API is to create a New Study in OC.
	 *                 All the fields are required fields and can't be left blank.
	 *                 You need to provide your Api-key to be connected.
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "briefTitle": "Study Protocol ID Name",
	 *                  "principalInvestigator": "Principal Investigator Name",
	 *                  "expectedTotalEnrollment": "10",
	 *                  "sponsor": "Sponsor Name",
	 *                  "protocolType": "Interventional",
	 *                  "status": "available",
	 *                  "assignUserRoles": [
	 *                  { "username": "usera", "role": "Data Manager" },
	 *                  { "username": "userb", "role": "Study Director" },
	 *                  { "username": "userc", "role": "Data Specialist" },
	 *                  { "username": "userd", "role": "Monitor" },
	 *                  { "username": "usere", "role": "Data Entry Person" }
	 *                  ],
	 *                  "uniqueProtocolID": "Study Protocol ID",
	 *                  "briefSummary": "Study Summary",
	 *                  "startDate": "2011-11-11"
	 *                  }
	 * @apiErrorExample {json} Error-Response:
	 *                  HTTP/1.1 400 Bad Request
	 *                  {
	 *                  "message": "VALIDATION FAILED",
	 *                  "status": "available",
	 *                  "principalInvestigator": "Principal Investigator Name",
	 *                  "expectedTotalEnrollment": "10",
	 *                  "sponsor": "Sponsor Name",
	 *                  "protocolType": "Interventional",
	 *                  "errors": [
	 *                  {"field": "UniqueProtocolId","resource": "Study Object","code": "Unique Protocol Id exist in the System"}
	 *                  ],
	 *                  "startDate": "2011-11-11",
	 *                  "assignUserRoles": [
	 *                  {"username": "usera","role": "Data Manager"},
	 *                  {"username": "userb","role": "Study Director"},
	 *                  {"username": "userc","role": "Data Specialist"}
	 *                  ],
	 *                  "uniqueProtocolID": "Study Protocol ID",
	 *                  "briefTitle": "Study Protocol ID",
	 *                  "briefSummary": "Study Summary",
	 *                  "studyOid": null
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    HTTP/1.1 200 OK
	 *                    {
	 *                    "message": "SUCCESS",
	 *                    "uniqueProtocolID": "Study Protocol ID",
	 *                    "studyOid": "S_STUDYPRO",
	 *                    }
	 */
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<Object> createNewStudy(HttpServletRequest request, @RequestBody HashMap<String, Object> map) throws Exception {
    ArrayList<ErrorObject> errorObjects = new ArrayList();
    StudyBean studyBean = null;
    System.out.println("I'm in Create Study");
    ResponseEntity<Object> response = null;
    String validation_failed_message = "VALIDATION FAILED";
    String validation_passed_message = "SUCCESS";
    String uniqueProtocolID = (String) map.get("uniqueProtocolID");
    String name = (String) map.get("briefTitle");
    String principalInvestigator = (String) map.get("principalInvestigator");
    String briefSummary = (String) map.get("briefSummary");
    String sponsor = (String) map.get("sponsor");
    String protocolType = (String) map.get("protocolType");
    String startDate = (String) map.get("startDate");
    String expectedTotalEnrollment = (String) map.get("expectedTotalEnrollment");
    String status = (String) map.get("status");
    ArrayList<UserRole> assignUserRoles = (ArrayList<UserRole>) map.get("assignUserRoles");
    ArrayList<UserRole> userList = new ArrayList<>();
    if (assignUserRoles != null) {
        for (Object userRole : assignUserRoles) {
            UserRole uRole = new UserRole();
            uRole.setUsername((String) ((HashMap<String, Object>) userRole).get("username"));
            uRole.setRole((String) ((HashMap<String, Object>) userRole).get("role"));
            udao = new UserAccountDAO(dataSource);
            UserAccountBean assignedUserBean = (UserAccountBean) udao.findByUserName(uRole.getUsername());
            if (assignedUserBean == null || !assignedUserBean.isActive()) {
                ErrorObject errorOBject = createErrorObject("Study Object", "The Assigned Username " + uRole.getUsername() + " is not a Valid User", "Assigned User");
                errorObjects.add(errorOBject);
            }
            ResourceBundle resterm = org.akaza.openclinica.i18n.util.ResourceBundleProvider.getTermsBundle();
            if (getStudyRole(uRole.getRole(), resterm) == null) {
                ErrorObject errorOBject = createErrorObject("Study Object", "Assigned Role for " + uRole.getUsername() + " is not a Valid Study Role", "Assigned Role");
                errorObjects.add(errorOBject);
            }
            userList.add(uRole);
        }
    }
    StudyDTO studyDTO = buildStudyDTO(uniqueProtocolID, name, briefSummary, principalInvestigator, sponsor, expectedTotalEnrollment, protocolType, status, startDate, userList);
    if (uniqueProtocolID == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "UniqueProtocolID");
        errorObjects.add(errorOBject);
    } else {
        uniqueProtocolID = uniqueProtocolID.trim();
    }
    if (name == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "BriefTitle");
        errorObjects.add(errorOBject);
    } else {
        name = name.trim();
    }
    if (principalInvestigator == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "PrincipalInvestigator");
        errorObjects.add(errorOBject);
    } else {
        principalInvestigator = principalInvestigator.trim();
    }
    if (briefSummary == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "BriefSummary");
        errorObjects.add(errorOBject);
    } else {
        briefSummary = briefSummary.trim();
    }
    if (sponsor == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "Sponsor");
        errorObjects.add(errorOBject);
    } else {
        sponsor = sponsor.trim();
    }
    if (protocolType == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "ProtocolType");
        errorObjects.add(errorOBject);
    } else {
        protocolType = protocolType.trim();
    }
    if (startDate == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "StartDate");
        errorObjects.add(errorOBject);
    } else {
        startDate = startDate.trim();
    }
    if (expectedTotalEnrollment == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "ExpectedTotalEnrollment");
        errorObjects.add(errorOBject);
    } else {
        expectedTotalEnrollment = expectedTotalEnrollment.trim();
    }
    if (status == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "Status");
        errorObjects.add(errorOBject);
    } else {
        status = status.trim();
    }
    if (assignUserRoles == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Missing Field", "AssignUserRoles");
        errorObjects.add(errorOBject);
    }
    if (status != null && !status.equalsIgnoreCase("available") && !status.equalsIgnoreCase("design") && !status.equals("")) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Status Field Should have 'Available' or 'Design' Status only, If left empty , will default to 'Design' Mode", "Status");
        errorObjects.add(errorOBject);
    }
    request.setAttribute("uniqueProId", uniqueProtocolID);
    // Brief Title
    request.setAttribute("name", name);
    request.setAttribute("prinInvestigator", principalInvestigator);
    request.setAttribute("description", briefSummary);
    request.setAttribute("sponsor", sponsor);
    request.setAttribute("startDate", startDate);
    request.setAttribute("expectedTotalEnrollment", expectedTotalEnrollment);
    request.setAttribute("status", status);
    String format = "yyyy-MM-dd";
    SimpleDateFormat formatter = null;
    Date formattedDate = null;
    if (startDate != "" && startDate != null) {
        try {
            formatter = new SimpleDateFormat(format);
            formattedDate = formatter.parse(startDate);
        } catch (ParseException e) {
            ErrorObject errorOBject = createErrorObject("Study Object", "The StartDate format is not a valid 'yyyy-MM-dd' format", "StartDate");
            errorObjects.add(errorOBject);
        }
        if (formattedDate != null) {
            if (!startDate.equals(formatter.format(formattedDate))) {
                ErrorObject errorOBject = createErrorObject("Study Object", "The StartDate format is not a valid 'yyyy-MM-dd' format", "StartDate");
                errorObjects.add(errorOBject);
            }
        }
    }
    UserAccountBean ownerUserAccount = getStudyOwnerAccount(request);
    if (ownerUserAccount == null) {
        ErrorObject errorOBject = createErrorObject("Study Object", "The Owner User Account is not Valid Account or Does not have Admin user type", "Owner Account");
        errorObjects.add(errorOBject);
    }
    Validator v0 = new Validator(request);
    v0.addValidation("name", Validator.NO_BLANKS);
    HashMap vError0 = v0.validate();
    if (!vError0.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "BriefTitle");
        errorObjects.add(errorOBject);
    }
    Validator v1 = new Validator(request);
    v1.addValidation("uniqueProId", Validator.NO_BLANKS);
    HashMap vError1 = v1.validate();
    if (!vError1.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "UniqueProtocolId");
        errorObjects.add(errorOBject);
    }
    Validator v2 = new Validator(request);
    v2.addValidation("description", Validator.NO_BLANKS);
    HashMap vError2 = v2.validate();
    if (!vError2.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "BriefSummary");
        errorObjects.add(errorOBject);
    }
    Validator v3 = new Validator(request);
    v3.addValidation("prinInvestigator", Validator.NO_BLANKS);
    HashMap vError3 = v3.validate();
    if (!vError3.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "PrincipleInvestigator");
        errorObjects.add(errorOBject);
    }
    Validator v4 = new Validator(request);
    v4.addValidation("sponsor", Validator.NO_BLANKS);
    HashMap vError4 = v4.validate();
    if (!vError4.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "Sponsor");
        errorObjects.add(errorOBject);
    }
    Validator v5 = new Validator(request);
    v5.addValidation("startDate", Validator.NO_BLANKS);
    HashMap vError5 = v5.validate();
    if (!vError5.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "StartDate");
        errorObjects.add(errorOBject);
    }
    Validator v6 = new Validator(request);
    HashMap vError6 = v6.validate();
    if (uniqueProtocolID != null)
        validateUniqueProId(request, vError6);
    if (!vError6.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Unique Protocol Id exist in the System", "UniqueProtocolId");
        errorObjects.add(errorOBject);
    }
    Validator v7 = new Validator(request);
    v7.addValidation("expectedTotalEnrollment", Validator.NO_BLANKS);
    HashMap vError7 = v7.validate();
    if (!vError7.isEmpty()) {
        ErrorObject errorOBject = createErrorObject("Study Object", "This field cannot be blank.", "ExpectedTotalEnrollment");
        errorObjects.add(errorOBject);
    }
    if (protocolType != null && !verifyProtocolTypeExist(protocolType)) {
        ErrorObject errorOBject = createErrorObject("Study Object", "Protocol Type is not Valid", "ProtocolType");
        errorObjects.add(errorOBject);
    }
    studyDTO.setErrors(errorObjects);
    if (errorObjects != null && errorObjects.size() != 0) {
        studyDTO.setMessage(validation_failed_message);
        response = new ResponseEntity(studyDTO, org.springframework.http.HttpStatus.BAD_REQUEST);
    } else {
        studyBean = buildStudyBean(uniqueProtocolID, name, briefSummary, principalInvestigator, sponsor, Integer.valueOf(expectedTotalEnrollment), protocolType, status, formattedDate, ownerUserAccount);
        StudyBean sBean = createStudy(studyBean, ownerUserAccount);
        studyDTO.setStudyOid(sBean.getOid());
        studyDTO.setMessage(validation_passed_message);
        StudyUserRoleBean sub = new StudyUserRoleBean();
        sub.setRole(Role.COORDINATOR);
        sub.setStudyId(sBean.getId());
        sub.setStatus(Status.AVAILABLE);
        sub.setOwner(ownerUserAccount);
        StudyUserRoleBean surb = createRole(ownerUserAccount, sub);
        ResourceBundle resterm = org.akaza.openclinica.i18n.util.ResourceBundleProvider.getTermsBundle();
        for (UserRole userRole : userList) {
            sub = new StudyUserRoleBean();
            sub.setRole(getStudyRole(userRole.getRole(), resterm));
            sub.setStudyId(sBean.getId());
            sub.setStatus(Status.AVAILABLE);
            sub.setOwner(ownerUserAccount);
            udao = new UserAccountDAO(dataSource);
            UserAccountBean assignedUserBean = (UserAccountBean) udao.findByUserName(userRole.getUsername());
            surb = createRole(assignedUserBean, sub);
        }
        ResponseSuccessStudyDTO responseSuccess = new ResponseSuccessStudyDTO();
        responseSuccess.setMessage(studyDTO.getMessage());
        responseSuccess.setStudyOid(studyDTO.getStudyOid());
        responseSuccess.setUniqueProtocolID(studyDTO.getUniqueProtocolID());
        response = new ResponseEntity(responseSuccess, org.springframework.http.HttpStatus.OK);
    }
    return response;
}
Also used : ResponseSuccessStudyDTO(org.akaza.openclinica.bean.login.ResponseSuccessStudyDTO) StudyDTO(org.akaza.openclinica.bean.login.StudyDTO) HashMap(java.util.HashMap) ResponseSuccessStudyDTO(org.akaza.openclinica.bean.login.ResponseSuccessStudyDTO) ErrorObject(org.akaza.openclinica.bean.login.ErrorObject) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) StudyUserRoleBean(org.akaza.openclinica.bean.login.StudyUserRoleBean) ArrayList(java.util.ArrayList) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) Date(java.util.Date) ResponseEntity(org.springframework.http.ResponseEntity) UserRole(org.akaza.openclinica.bean.login.UserRole) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) ErrorObject(org.akaza.openclinica.bean.login.ErrorObject) ResourceBundle(java.util.ResourceBundle) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Validator(org.akaza.openclinica.control.form.Validator)

Aggregations

ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ResourceBundle (java.util.ResourceBundle)1 ErrorObject (org.akaza.openclinica.bean.login.ErrorObject)1 ResponseSuccessStudyDTO (org.akaza.openclinica.bean.login.ResponseSuccessStudyDTO)1 StudyDTO (org.akaza.openclinica.bean.login.StudyDTO)1 StudyUserRoleBean (org.akaza.openclinica.bean.login.StudyUserRoleBean)1 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)1 UserRole (org.akaza.openclinica.bean.login.UserRole)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1 Validator (org.akaza.openclinica.control.form.Validator)1 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)1 ResponseEntity (org.springframework.http.ResponseEntity)1