Search in sources :

Example 1 with ProfileForm

use of cn.edu.zju.acm.onlinejudge.form.ProfileForm in project zoj by licheng.

the class EditProfileAction method execute.

/**
 * Edit Profile.
 *
 * @param mapping
 *            action mapping
 * @param form
 *            action form
 * @param request
 *            http servlet request
 * @param response
 *            http servlet response
 *
 * @return action forward instance
 *
 * @throws Exception
 *             any errors happened
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    if (!Features.editProfile()) {
        context.getResponse().sendError(404);
        return null;
    }
    if (!this.isLogin(context)) {
        return this.handleSuccess(mapping, context, "login");
    }
    UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
    ProfileForm profileForm = (ProfileForm) form;
    UserProfile profile = context.getUserProfile();
    UserPreference perference = userPersistence.getUserPreference(profile.getId());
    if (profileForm.getHandle() == null) {
        profileForm.populate(profile, perference);
        context.setAttribute("ProfileForm", profileForm);
        return this.handleSuccess(mapping, context, "failure");
    }
    if (userPersistence.login(profileForm.getHandle(), profileForm.getPassword()) == null) {
        return this.handleFailure(mapping, context, "password", "ProfileForm.password.invalid");
    }
    UserProfile newProfile = profileForm.toUserProfile();
    newProfile.setId(profile.getId());
    newProfile.setRegDate(profile.getRegDate());
    if (!profile.getHandle().equals(newProfile.getHandle())) {
        return this.handleFailure(mapping, context, "handle", "ProfileForm.handle.changed");
    }
    if (!profile.getEmail().equals(newProfile.getEmail())) {
        UserProfile temp = userPersistence.getUserProfileByEmail(newProfile.getEmail());
        if (temp != null && temp.getId() != profile.getId()) {
            return this.handleFailure(mapping, context, "email", "ProfileForm.email.used");
        }
    }
    userPersistence.updateUserProfile(newProfile, profile.getId());
    UserPreference newPerference = profileForm.toUserPreference();
    newPerference.setId(profile.getId());
    userPersistence.updateUserPreference(newPerference, profile.getId());
    context.setUserProfile(newProfile);
    context.getRequest().setAttribute("Countries", PersistenceManager.getInstance().getUserPersistence().getAllCountries());
    ActionMessages messages = new ActionMessages();
    messages.add("message", new ActionMessage("onlinejudge.editProfile.success"));
    this.saveErrors(context.getRequest(), messages);
    context.setAttribute("back", "");
    return this.handleSuccess(mapping, context, "success");
}
Also used : UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) ActionMessages(org.apache.struts.action.ActionMessages) ProfileForm(cn.edu.zju.acm.onlinejudge.form.ProfileForm) ActionMessage(org.apache.struts.action.ActionMessage) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) UserPersistence(cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)

Example 2 with ProfileForm

use of cn.edu.zju.acm.onlinejudge.form.ProfileForm in project zoj by licheng.

the class RegisterAction method execute.

/**
 * Register.
 *
 * @param mapping
 *            action mapping
 * @param form
 *            action form
 * @param request
 *            http servlet request
 * @param response
 *            http servlet response
 *
 * @return action forward instance
 *
 * @throws Exception
 *             any errors happened
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    if (!Features.register()) {
        context.getResponse().sendError(404);
        return null;
    }
    UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
    ProfileForm profileForm = (ProfileForm) form;
    if (profileForm.getHandle() == null) {
        return this.handleSuccess(mapping, context, "failure");
    }
    context.getRequest().getSession().invalidate();
    ActionMessages errors = this.validate(userPersistence, profileForm);
    if (errors.size() > 0) {
        return this.handleFailure(mapping, context, errors);
    }
    // create user profile
    UserProfile profile = profileForm.toUserProfile();
    userPersistence.createUserProfile(profile, 0);
    // create user perference
    UserPreference perference = profileForm.toUserPreference();
    perference.setId(profile.getId());
    userPersistence.createUserPreference(perference, 0);
    AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
    authorizationPersistence.addUserRole(profile.getId(), 2);
    context.getRequest().setAttribute("Countries", PersistenceManager.getInstance().getUserPersistence().getAllCountries());
    // get UserSecurity
    UserSecurity security = authorizationPersistence.getUserSecurity(profile.getId());
    context.setUserProfile(profile);
    context.setUserSecurity(security);
    context.setUserPreference(perference);
    ActionMessages messages = new ActionMessages();
    messages.add("message", new ActionMessage("onlinejudge.register.success"));
    this.saveErrors(context.getRequest(), messages);
    context.setAttribute("back", "");
    return this.handleSuccess(mapping, context, "success");
}
Also used : UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) ActionMessages(org.apache.struts.action.ActionMessages) ProfileForm(cn.edu.zju.acm.onlinejudge.form.ProfileForm) ActionMessage(org.apache.struts.action.ActionMessage) AuthorizationPersistence(cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference) UserPersistence(cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)

Aggregations

UserPreference (cn.edu.zju.acm.onlinejudge.bean.UserPreference)2 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)2 ProfileForm (cn.edu.zju.acm.onlinejudge.form.ProfileForm)2 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)2 ActionMessage (org.apache.struts.action.ActionMessage)2 ActionMessages (org.apache.struts.action.ActionMessages)2 AuthorizationPersistence (cn.edu.zju.acm.onlinejudge.persistence.AuthorizationPersistence)1 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)1