Search in sources :

Example 1 with UserType

use of org.akaza.openclinica.domain.user.UserType in project OpenClinica by OpenClinica.

the class UserProcessor method process.

@Override
public ProcessorEnum process(SubmissionContainer container) throws Exception {
    logger.info("Executing User Processor.");
    Errors errors = container.getErrors();
    String contextStudySubjectOid = container.getSubjectContext().get("studySubjectOID");
    String studySubjectOid = container.getSubject().getOcOid();
    String parentStudyOid = getParentStudy(container.getStudy().getOc_oid()).getOc_oid();
    String userAccountId = container.getSubjectContext().get("userAccountID");
    // Check if UserAccount is specified in subject context
    if (!StringUtils.isEmpty(userAccountId)) {
        UserAccount existingAccount = userAccountDao.findByUserId(Integer.valueOf(userAccountId));
        if (existingAccount == null) {
            logger.info("Could not find existing user account from provided context.  Aborting submission.");
            errors.reject("Could not find existing user account from provided context.  Aborting submission.");
            throw new Exception("Could not find existing user account from provided context.  Aborting submission.");
        }
        container.setUser(existingAccount);
    // if study subject oid is not null, just look up user account
    } else if (contextStudySubjectOid != null) {
        String userName = parentStudyOid + "." + contextStudySubjectOid;
        UserAccount existingAccount = userAccountDao.findByUserName(userName);
        if (existingAccount == null) {
            logger.info("Could not find existing user account.  Aborting submission.");
            errors.reject("Could not find existing user account.  Aborting submission.");
            throw new Exception("Could not find existing user account.  Aborting submission.");
        }
        container.setUser(existingAccount);
    } else {
        String userName = parentStudyOid + "." + studySubjectOid;
        UserAccount existingAccount = userAccountDao.findByUserName(userName);
        if (existingAccount != null) {
            container.setUser(existingAccount);
        } else {
            //Create user account
            UserAccount rootUser = userAccountDao.findByUserId(1);
            UserAccount createdUser = new UserAccount();
            createdUser.setUserName(parentStudyOid + "." + container.getSubject().getOcOid());
            createdUser.setFirstName(INPUT_FIRST_NAME);
            createdUser.setLastName(INPUT_LAST_NAME);
            createdUser.setEmail(INPUT_EMAIL);
            createdUser.setInstitutionalAffiliation(INPUT_INSTITUTION);
            createdUser.setActiveStudy(container.getStudy());
            String passwordHash = UserAccountBean.LDAP_PASSWORD;
            createdUser.setPasswd(passwordHash);
            createdUser.setPasswdTimestamp(null);
            createdUser.setDateLastvisit(null);
            createdUser.setStatus(Status.DELETED);
            createdUser.setPasswdChallengeQuestion("");
            createdUser.setPasswdChallengeAnswer("");
            createdUser.setPhone("");
            createdUser.setUserAccount(rootUser);
            createdUser.setRunWebservices(false);
            createdUser.setActiveStudy(container.getStudy());
            UserType type = userTypeDao.findByUserTypeId(2);
            createdUser.setUserType(type);
            createdUser.setEnabled(true);
            createdUser.setAccountNonLocked(true);
            createdUser.setAccessCode("");
            createdUser.setApiKey("");
            createdUser = userAccountDao.saveOrUpdate(createdUser);
            container.setUser(createdUser);
            //Create study user role
            Date date = new Date();
            StudyUserRoleId studyUserRoleId = new StudyUserRoleId(Role.RESEARCHASSISTANT2.getName(), container.getStudy().getStudyId(), Status.AUTO_DELETED.getCode(), rootUser.getUserId(), date, createdUser.getUserName());
            StudyUserRole studyUserRole = new StudyUserRole(studyUserRoleId);
            studyUserRoleDao.saveOrUpdate(studyUserRole);
        //TODO: StudyUserRole object had to be heavily modified.  May need fixing.  Also roleName specified
        // doesn't exist in role table.  May need to fix that.
        // This table should also foreign key to user_account but doesn't.
        }
    }
    return ProcessorEnum.PROCEED;
}
Also used : Errors(org.springframework.validation.Errors) StudyUserRole(org.akaza.openclinica.domain.datamap.StudyUserRole) UserAccount(org.akaza.openclinica.domain.user.UserAccount) UserType(org.akaza.openclinica.domain.user.UserType) Date(java.util.Date) StudyUserRoleId(org.akaza.openclinica.domain.datamap.StudyUserRoleId)

Aggregations

Date (java.util.Date)1 StudyUserRole (org.akaza.openclinica.domain.datamap.StudyUserRole)1 StudyUserRoleId (org.akaza.openclinica.domain.datamap.StudyUserRoleId)1 UserAccount (org.akaza.openclinica.domain.user.UserAccount)1 UserType (org.akaza.openclinica.domain.user.UserType)1 Errors (org.springframework.validation.Errors)1