/*
 (C) 2004 Ibermatica S.A.

 This file is part of IberAgents.

 IberAgents 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.

 IberAgents 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 IberAgents; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.ibertest.personal;

import com.iberagents.Agent;
import com.iberagents.Finder;
import com.iberagents.PlatformException;
import com.iberagents.bean.edit.BeanEditor;
import com.iberagents.bean.serial.Serializer;
import com.iberagents.user.DatabaseRealm;
import com.iberagents.user.UserComponent;
import com.iberagents.user.UserPreferences;
import com.iberagents.user.UserRealm;
import com.iberagents.web.RequestParameters;
import com.iberagents.web.ResponsePage;
import com.iberagents.web.page.PageLink;
import com.iberagents.web.page.ParametrizablePage;
import com.iberagents.web.page.TransformationPage;

/**
 * Tests abilities of a personal agent: preferences edition, continous update.
 * @author Alex Fernandez.
 */
public class TestPersonalAgent implements UserComponent, Agent
{
    private TestPersonalPreferences preferences = new TestPersonalPreferences();
    private static DatabaseRealm realm = new DatabaseRealm(
            TestPersonalPreferences.class);

    /**
     * Return user preferences.
     */
    public UserPreferences getPreferences()
    {
        return this.preferences;
    }

    /**
     * Set new user preferences.
     */
    public void setPreferences(UserPreferences prefs)
    {
        this.preferences = (TestPersonalPreferences) prefs;
    }

    /**
     * Return the realm: where users are stored.
     */
    public UserRealm getRealm()
    {
        return realm;
    }

    /**
     * Modify preferences
     * @param parameters from the query string.
     */
    public void modifyPreferences(RequestParameters parameters)
            throws PlatformException
    {
        BeanEditor editor = (BeanEditor) Finder.findLocal(BeanEditor.class);
        editor.editBean(this.preferences, parameters);
    }

    /**
     * Show preferences for edition.
     * @param parameters from the query string.
     * @return a page with all preferences.
     */
    public ResponsePage editPreferencesPage(RequestParameters parameters)
            throws PlatformException
    {
        BeanEditor editor = (BeanEditor) Finder.findLocal(BeanEditor.class);
        // get the page to edit preferences
        ParametrizablePage page = editor.editBeanPage(this.preferences,
            parameters);
        // set command and page type after edition
        page.setCommand("modifyPreferences");
        page.setPageType("editPreferencesPage");
        // create link that points to main page
        PageLink link = page.createLink("main");
        link.setParameter("show", "");
        // return our page
        return page;
    }

    /**
     * Ignore commands.
     */
    public void command(String command, RequestParameters parameters)
    {
    }

    /**
     * Show the agent page.
     */
    public ResponsePage show(String arg0, RequestParameters arg1)
            throws PlatformException
    {
        this.preferences.increasePageViews();
        Serializer serializer = (Serializer) Finder.findLocal(Serializer.class);
        return new TransformationPage(serializer.serialize(this.preferences),
                "testAgent.xsl");
    }

    /**
     * Act once: increase acts by one.
     */
    public void act()
    {
        this.preferences.increaseActs();
    }

    /**
     * Return a delay of one second.
     */
    public long getDelay()
    {
        return 1000;
    }

    /**
     * Never finishes.
     */
    public boolean finished()
    {
        return false;
    }
}