Subversion Repositories bacoAlunos

Rev

Rev 1310 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.web.controllers.profile;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.DomainObjectFactory;
import pt.estgp.estgweb.domain.User;
import pt.estgp.estgweb.domain.UserSessionImpl;
import pt.estgp.estgweb.domain.views.RecordView;
import pt.estgp.estgweb.domain.views.UserView;
import pt.estgp.estgweb.services.expceptions.AlreadyExistsException;
import pt.estgp.estgweb.web.UserSessionProxy;
import pt.estgp.estgweb.web.controllers.AddRolesController;
import pt.estgp.estgweb.web.exceptions.NoCookiesException;
import pt.estgp.estgweb.web.form.AddRolesForm;
import pt.estgp.estgweb.web.form.profile.ProfileForm;
import pt.estgp.estgweb.web.utils.RequestUtils;
import pt.utl.ist.berserk.logic.filterManager.exceptions.FilterRetrieveException;
import pt.utl.ist.berserk.logic.filterManager.exceptions.IncompatibleFilterException;
import pt.utl.ist.berserk.logic.filterManager.exceptions.InvalidFilterException;
import pt.utl.ist.berserk.logic.filterManager.exceptions.InvalidFilterExpressionException;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ServiceManagerException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author Jorge Machado
 * @date 26/Fev/2008
 * @time 18:01:54
 * @see pt.estgp.estgweb.web
 */

public class ProfileController extends AddRolesController
{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(ProfileController.class);
    private static final 1.5.0/docs/api/java/lang/String.html">String TARGET_ROLES_KEY = AddRolesForm.TARGET_ROLES_KEY;


    public ActionForward editUser(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        ProfileForm profileForm = (ProfileForm) form;
        ActionErrors errors = profileForm.validate(mapping, request);
        if (!errors.isEmpty())
        {
            if(profileForm.getUserView().getId() > 0)
            {
                UserView uV = loadUser(profileForm.getUserView().getId(), request, response);
                profileForm.getUserView().setPasswordLocal(uV.isPasswordLocal());
                UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
                uV.setUserRoles((List<String>) userSession.get(TARGET_ROLES_KEY));
                profileForm.setSelectedRoles(uV.getUserRoles());
            }
            saveMessages(request, errors);
            return mapping.getInputForward();
        }
        UserView userView = profileForm.getUserView();
        try
        {
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            userView.setUserRoles((List<String>) userSession.get(TARGET_ROLES_KEY));
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{userView, profileForm.getTypeClass()};
            try
            {
                userView = (UserView) sm.execute(RequestUtils.getRequester(request, response), "SubmitUser", args, names);
            }
            catch (AlreadyExistsException e)
            {
                if(profileForm.getUserView().getId() > 0)
                {
                    UserView uV = loadUser(profileForm.getUserView().getId(), request, response);
                    profileForm.getUserView().setPasswordLocal(uV.isPasswordLocal());
                }
                addErrorWithKeys(request,"user.exists",e.getMessage());
                return mapping.getInputForward();
            }
            profileForm.setUserView(userView);
            if (userSession.getUser().getId() == userView.getId())
            {
                userSession.setUser(userView.getUser());
                request.setAttribute(Globals.USER_SESSION_KEY, userSession);
                logger.info("Puting new user info in session:" + userView.getId());
            }
            addMessage(request, "profile.edited.sucess");
            return mapping.findForward("success");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward newTeacher(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        return newGenericUser(mapping, form, request, response, DomainObjectFactory.createTeacherImpl());
    }

    public ActionForward newStudent(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        return newGenericUser(mapping, form, request, response, DomainObjectFactory.createStudentImpl());
    }

    public ActionForward newUser(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        return newGenericUser(mapping, form, request, response, DomainObjectFactory.createUserImpl());
    }

    public ActionForward newGenericUser(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response,
                                        User u)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);

            UserView uV = new UserView(u);
            pF.setUserView(uV);
            pF.setTypeClass(u.getClass().getName());
            pF.setSelectedRoles(uV.getUserRoles());
            userSession.put(TARGET_ROLES_KEY, (1.5.0/docs/api/java/io/Serializable.html">Serializable) uV.getUserRoles());
            userSession.serialize(request, response);
            return mapping.findForward("personalData");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward changeProfile(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            1.5.0/docs/api/java/lang/String.html">String id = request.getParameter("id");
            long userToLoad;

            if (id != null)
                userToLoad = 1.5.0/docs/api/java/lang/Long.html">Long.valueOf(id);
            else
                userToLoad = userSession.getUser().getId();
            UserView uV = loadUser(userToLoad, request, response);
            pF.setUserView(uV);
            pF.setSelectedRoles(uV.getUserRoles());
            userSession.put(TARGET_ROLES_KEY, (1.5.0/docs/api/java/io/Serializable.html">Serializable) uV.getUserRoles());
            userSession.serialize(request, response);
            return mapping.findForward("personalData");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward home(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            long userToLoad;
            if (request.getParameter("id")!=null)
                userToLoad = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(request.getParameter("id"));
            else if (pF.getUserView() != null && pF.getUserView().getId() > 0)
                userToLoad = pF.getUserView().getId();
            else
                userToLoad = userSession.getUser().getId();
            UserView uV = loadUserWithRecords(userToLoad, request, response);
            pF.setUserView(uV);
            return mapping.findForward("home");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward grades(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            1.5.0/docs/api/java/lang/String.html">String id = request.getParameter("id");
            long userToLoad;

            if (id != null)
                userToLoad = 1.5.0/docs/api/java/lang/Long.html">Long.valueOf(id);
            else if (pF.getUserView().getId() > 0)
                userToLoad = pF.getUserView().getId();
            else
                userToLoad = userSession.getUser().getId();
            UserView uV = loadUserWithGrades(userToLoad, request, response);
            pF.setUserView(uV);
            return mapping.findForward("grades");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }


    public ActionForward homeCode(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            int userToLoad = pF.getUserView().getCode();
            UserView uV = loadUserWithRecordsCode(userToLoad, request, response);
            pF.setUserView(uV);
            return mapping.findForward("home");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward curriculum(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm pF = (ProfileForm) form;
            UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            long userToLoad = userSession.getUser().getId();
            UserView uV = loadUserWithRecords(userToLoad, request, response);
            pF.setUserView(uV);
            return mapping.findForward("curriculum");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    private UserView loadUser(long id, HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{id};
        UserView uV = (UserView) sm.execute(RequestUtils.getRequester(request, response), "LoadUserById", args, names);
        return uV;
    }

    private UserView loadUserWithRecords(long id, HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{id};
        UserView uV = (UserView) sm.execute(RequestUtils.getRequester(request, response), "LoadUserWithRecordsById", args, names);
        return uV;
    }

    private UserView loadUserWithGrades(long id, HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{id};
        UserView uV = (UserView) sm.execute(RequestUtils.getRequester(request, response), "LoadUserWithGradesById", args, names);
        return uV;
    }

    public UserView  loadUserWithRecordsCode(int code, HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{code};
        UserView uV = (UserView) sm.execute(RequestUtils.getRequester(request, response), "LoadUserWithRecordsByCode", args, names);
        return uV;
    }

    public ActionForward search(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm fromRequest = (ProfileForm) form;

            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{fromRequest.getTextToSearch(), fromRequest.getTypeToSearch()};
            List<UserView> uVs = (List<UserView>) sm.execute(RequestUtils.getRequester(request, response), "SearchUser", args, names);
            fromRequest.setUsersFind(uVs);
            request.setAttribute("ProfileForm", fromRequest);
            if (uVs.size() != 0)
                addMessage(request, "profile.search.user.find", "" + uVs.size());
            else
                addMessage(request, "profile.search.user.not.find");
            return mapping.findForward("search");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {

            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{new 1.5.0/docs/api/java/lang/Long.html">Long(request.getParameter("id"))};
            1.5.0/docs/api/java/lang/Boolean.html">Boolean status = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) sm.execute(RequestUtils.getRequester(request, response), "DeleteUser", args, names);
            if (status)
            {
                addMessage(request, "user.deleted.sucess");
                return mapping.findForward("success");
            }
            else
            {
                addMessage(request, "user.deleted.fail");
                return mapping.findForward("success");
            }
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward createRecord(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm profileForm = (ProfileForm) form;
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};

            5+0%2Fdocs%2Fapi+Object">Object[] args;
            if (profileForm.getUploadFile() != null)
                args = new 5+0%2Fdocs%2Fapi+Object">Object[]{profileForm.getRecordView(), profileForm.getUploadFile().getInputStream(), profileForm.getUploadFile().getFileName(), profileForm.getUploadFile().getContentType(), profileForm.getUploadFile().getFileSize()};
            else
                args = new 5+0%2Fdocs%2Fapi+Object">Object[]{profileForm.getRecordView(), null, null, null, 0};

            boolean newRecord = false;
            if(profileForm.getRecordView().getId() <= 0)
                newRecord = true;
            UserView userView;
            if (profileForm.getRecordView().getId() > 0)
                userView = (UserView) sm.execute(RequestUtils.getRequester(request, response), "EditUserProfileRecord", args, names);
            else
                userView = (UserView) sm.execute(RequestUtils.getRequester(request, response), "CreateUserProfileRecord", args, names);

            if(newRecord)
                addMessage(request, "profile.record.add.sucess", profileForm.getRecordView().getTitle());
            else
                addMessage(request, "profile.record.updated.sucess", profileForm.getRecordView().getTitle());
            profileForm.setUserView(userView);
            return mapping.findForward("home");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward deleteRecord(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm profileForm = (ProfileForm) form;
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args;
            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{profileForm.getRecordView().getId()};

            UserView userView = (UserView) sm.execute(RequestUtils.getRequester(request, response), "DeleteUserProfileRecord", args, names);
            addMessage(request, "profile.record.deleted.sucess");
            profileForm.setUserView(userView);
            return mapping.findForward("home");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward loadEditRecord(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            ProfileForm profileForm = (ProfileForm) form;
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args;
            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{profileForm.getRecordView().getId()};

            RecordView recordView = (RecordView) sm.execute(RequestUtils.getRequester(request, response), "LoadEditUserProfileRecord", args, names);
            UserView uV = recordView.getOwnerUserView();
//            addMessage(request, "profile.record.deleted.sucess");
            profileForm.setUserView(uV);
            profileForm.setRecordView(recordView);
            return mapping.findForward("curriculum");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward addRole(ActionMapping mapping,
                                 ActionForm form ,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        ProfileForm profileForm = (ProfileForm) form;
        UserView uV = loadUser(profileForm.getUserView().getId(), request, response);
        profileForm.getUserView().setPasswordLocal(uV.isPasswordLocal());
        profileForm.setUserView(uV);
        UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
        return super.addRole(mapping,form,request,response);
    }

    public ActionForward removeRole(ActionMapping mapping,
                                 ActionForm form ,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        ProfileForm profileForm = (ProfileForm) form;
        UserView uV = loadUser(profileForm.getUserView().getId(), request, response);
        profileForm.setUserView(uV);
        profileForm.getUserView().setPasswordLocal(uV.isPasswordLocal());
        UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
        return super.removeRole(mapping,form,request,response);
    }

}