/*
 * $Id: SystemParametersAction.java,v 1.15 2005/08/24 13:40:21 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 *$Log: SystemParametersAction.java,v $
 *Revision 1.15  2005/08/24 13:40:21  marbobo
 *+ Versteckte Parameter nicht anzeigen
 *
 *Revision 1.14  2005/02/14 14:01:46  bigmac
 *Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 *Revision 1.13  2004/11/18 10:57:09  koppy
 *Bugfix: Falls Größe der Logdatei gleich Null, wird sie gelöscht.
 *
 *Revision 1.12  2004/09/16 13:10:54  marbobo
 *+ Auf VKCRequestProcessor umgestellt
 *
 *Revision 1.11  2004/07/20 08:05:35  jonyp
 *added copyright info + cvs log
 *
 *Revision 1.10  2004/07/15 12:34:56  jonyp
 *added functionality to update local cache of sysParameters when making parameter persistent into DB
 *
 *Revision 1.9  2004/05/05 19:04:56  marbobo
 *+ Fehlerbehandlung
 *
 *Revision 1.8  2004/05/05 12:38:57  marbobo
 *+ Anpassung an DisplayFileServlet
 *
 *Revision 1.7  2004/05/05 10:59:36  marbobo
 *+ Bugfix
 *
 *Revision 1.6  2004/05/05 00:28:11  marbobo
 *+ Logdatein anzeigen
 *
 *Revision 1.5  2004/03/09 18:40:11  bigmac
 *Reimplemented
 *
 *Revision 1.4  2003/12/24 10:12:09  bigmac
 *Projektweite Import-Optimierung
 *
 *Revision 1.3  2003/11/27 17:11:49  teng
 *die Aenderung der SysParameters in DB wird sofort durchgefuehrt fuer die SysParameterManagers, die bei WebShare registiert sind
 *
 *Revision 1.2  2003/11/18 17:04:32  teng
 *DB update nach submit fertig
 *
 *Revision 1.1  2003/11/11 11:55:30  teng
 **** empty log message ***
 *
 *
 */

package vkc.administration.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
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 java.io.File;
import java.util.Iterator;

import vkc.administration.form.SystemParametersForm;
import vkc.db.entities.VKCSysParameter;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.exception.DBException;
import vkc.util.StringUtils;
import vkc.view.WebShare;

public class SystemParametersAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/systemParameters.jsp");
	private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

	public ActionForward execute( ActionMapping mapping,
								  ActionForm f, 
								  HttpServletRequest request, 
								  HttpServletResponse response )
	throws ServletException {
		try {
			SystemParametersForm form = (SystemParametersForm)f;
			VKCUser user = form.getUser();

			if (!user.hasAdminPrivileges()) {
				request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");
				
				return INFO_FORWARD;
			}

			if (request.getParameter("action.save") != null) {
				return handleSaveAction(form, request);
			} else if (request.getParameter("action.back") != null) {
				form.setViewMode(SystemParametersForm.VIEW_MODE_PARAMETERS);
				
				return handleInitializeAction(form, request);
			} else if (request.getParameter("action.show.file") != null) {
				return handleFileViewAction(form, request);
			} else {
				return handleInitializeAction(form, request);
			}
		} catch (Exception ex) {
			throw new ServletException(ex);
		}
	}
	
	private ActionForward handleFileViewAction(SystemParametersForm form, HttpServletRequest request) {
		VKCSysParameter parameter = form.findParam(form.getSelectedParamterId());
		
		if (parameter != null) {
			File file = new File(parameter.getValuestring());
			
			if (file.length() == 0) {
			    file.delete();
			}
			
			if (!file.exists()) {
				ActionErrors errors = new ActionErrors();
				errors.add("error.fileNotExistent", new ActionError("error.fileNotExistent"));
				saveErrors(request, errors);
			} else {
				request.setAttribute("parameter", parameter);
				form.setViewMode(SystemParametersForm.VIEW_MODE_FILE);
			}
		}
		
		return DEFAULT_FORWARD;
	}

	private ActionForward handleInitializeAction(SystemParametersForm form, HttpServletRequest request) {
		if (request.getParameter("selectedContext") == null) {
			// Default-Context setzen
			form.setSelectedContext("SYSTEMINFO");
		}
		
		for (Iterator iter = form.getSystemParameters().iterator(); iter.hasNext() ;) {
			VKCSysParameter param = (VKCSysParameter)iter.next();

			if (param.isBooleanType()) {
				String value = StringUtils.normalize(param.getValuestring());
				
				if (value == null || value.equals("0")) {
					param.setValuestring("");
				} else {
					param.setValuestring("on");
				}
			}
		}

		return DEFAULT_FORWARD;
	}

	private ActionForward handleSaveAction(SystemParametersForm form, HttpServletRequest request) throws DBException {
		for (Iterator iter = form.getSelectedContextParameters().iterator(); iter.hasNext() ;) {
			VKCSysParameter param = (VKCSysParameter)iter.next();
			
			if (!param.isReadOnly()) {
				String value = null;
				
				if (param.isBooleanType()) {
					value = "on".equals(param.getValuestring()) ? "1" : "0" ;
				} else {
					value = param.getValuestring();
				}	

				// save it persistent into DB
				VKCSysParameterSerializer.saveValue(param.getContext(), param.getName(), value);
			}
		}

		return DEFAULT_FORWARD;
	}
}