Пример #1
0
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    ActionErrors errors = new ActionErrors();
    ActionForward forward = new ActionForward();
    // return value
    String username = (String) PropertyUtils.getSimpleProperty(form, "usuario");
    String password = (String) PropertyUtils.getSimpleProperty(form, "password");

    try {
      // obtiene la transacción asociada al administrador de persistencia.
      SessionManager.beginTransaction();

      Cysdreq cysdreq;
      try {
        cysdreq =
            (Cysdreq)
                PersistenceManagers.findObject(
                    SessionManager.getSession(), SessionManager.ROOT_OBJECT_ID);
        System.out.println("Cysdreq obtenido");
      } catch (Throwable t) {
        // Report the error using the appropriate name and ID.
        errors.add("login", new ActionError("errors.obtenerSistema"));
        throw t;
      }

      Usuario usuario = cysdreq.getUsuario(username, password);
      if (usuario == null) {
        errors.add("login", new ActionError("errors.login.inexistente"));
      } else {
        UserBean userBean = new UserBean();
        userBean.setLongUsername(usuario.getNombre());
        userBean.setUsername(usuario.getUsuario());
        userBean.setPassword(usuario.getPassword());

        // Save our logged-in user in the session
        HttpSession session = request.getSession();
        session.setAttribute(USER_KEY, userBean);
      }

      SessionManager.commit();
    } catch (Throwable t) {
      SessionManager.rollback();
      t.printStackTrace();
      // Report the error using the appropriate name and ID.
      errors.add("sistema", new ActionError("errors.errorDesconocido"));
    }

    // If a message is required, save the specified key(s)
    // into the request for use by the <struts:errors> tag.

    if (!errors.isEmpty()) {
      saveErrors(request, errors);

      // Forward control to the appropriate 'failure' URI (change name as desired)
      forward = mapping.findForward("error");

    } else {

      // Forward control to the appropriate 'success' URI (change name as desired)
      forward = mapping.findForward("success");
    }

    // Finish with
    return (forward);
  }