/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils;

import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.web.*;
import fr.gouv.finances.dgfip.xemelios.web.utils.FilAriane;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 * @author CBO
 */
public class StdDispatchAction extends DispatchAction {

  protected FilAriane getFilAriane(HttpServletRequest request) {
    return AttributeHelper.getFilAriane(request.getSession());
  }

  protected String getMessage(HttpServletRequest request, String key) {
    return getResources(request).getMessage(getLocale(request), key);
  }

  protected String getMessage(HttpServletRequest request, String key, Object arg) {
    return getResources(request).getMessage(getLocale(request), key, arg);
  }

  protected String getMessage(HttpServletRequest request, String key, Object arg1, Object arg2) {
    return getResources(request).getMessage(getLocale(request), key, arg1, arg1);
  }

  protected String getMessage(HttpServletRequest request, String key, Object args[]) {
    return getResources(request).getMessage(getLocale(request), key, args);
  }
  
  protected  boolean validate(ActionMapping mapping, ActionForm form, HttpServletRequest request) {
    ActionErrors errors = form.validate( mapping, request );

    if ( errors != null && !errors.isEmpty() ) {
        request.getSession().setAttribute(Globals.ERROR_KEY, errors);
        return false;
    }
    else return true;
  }

  protected boolean hasRole(HttpSession session, String role) {
    XemeliosUser user = AttributeHelper.getUser(session);
    return user != null && user.hasRole(role);
  }

  /**
   * Vérifie que l'utilisateur dispose des bons droits, si ce n'est pas le cas,
   * une exception est levée
   * @param session
   * @param role role à tester
   * @return
   */
  protected void checkRole(HttpSession session, String role) throws UnauthorizedAccessException {
    if (!hasRole(session, role))
      throw new UnauthorizedAccessException();
  }
}
