/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */
package org.bodington.server.events;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;

import org.bodington.server.BuildingServerException;
import org.bodington.server.realm.User;
import org.bodington.server.resources.Resource;
import org.bodington.util.IntStringEnum;

public class EasyEditEvent extends Event
{

    
    private static ResourceBundle resourceBundle = ResourceBundle
        .getBundle(EasyEditEvent.class.getName());
    
    public static EasyEditEventType EDIT = new EasyEditEventType("event.edit");
    
    public static class EasyEditEventType extends IntStringEnum
    {
        protected EasyEditEventType(String name)
        {
            super(name, EasyEditEventType.class);
        }
        
        public static EasyEditEventType getMapping(int index)
        {
            return (EasyEditEventType)getConst(index,EasyEditEventType.class);
        }
        
        public static EasyEditEventType getMapping(String name)
        {
            return (EasyEditEventType)getConst(name, EasyEditEventType.class);
        }
        
    }
    
    public static EasyEditEvent findLatest(Resource resource) throws BuildingServerException 
    {
       Enumeration events = findPersistentObjects("resource_id = "+ resource.getResourceId(), "event_time DESC LIMIT 1", EasyEditEvent.class.getName());
       return (EasyEditEvent)((events.hasMoreElements())?events.nextElement():null);
    }

    public static Enumeration findEvents(Resource resource, int start, int size) throws BuildingServerException
    {
        StringBuffer where = new StringBuffer();
        where.append( "resource_id = " );
        where.append( resource.getResourceId().toString() );
        where.append( " AND event_code = " );
        where.append( EDIT.getId() );
        
        StringBuffer order = new StringBuffer();
        order.append("event_time desc ");
        order.append("LIMIT ");
        order.append(size);
        order.append(" OFFSET ");
        order.append(start);
        return findPersistentObjects(where.toString(), order
            .toString(), EasyEditEvent.class.getName());
    }

    public EasyEditEvent()
    {
        super();
    }
    
    /**
     * Create a new event.
     * @param resource The homecontainer in which this event occured.
     * @param active The user who created the event.
     * @param type The type of event.
     */
    public EasyEditEvent(Resource resource, User active, EasyEditEventType type)
    {
        resource_id = resource.getResourceId();
        active_user_id = active.getUserId();
        event_code = type.getId();
        setImportance(Event.IMPORTANCE_MANAGEMENT_MIN);
    }
    
    public String messageTitle()
    {
        String params[] = {"Someone", "Someone"};
        return MessageFormat.format(resourceBundle
            .getString(EasyEditEventType.getMapping(event_code).getName()),
            params);
    }
    
    public void printMessage(PrintWriter out, boolean html)
    {
        Object params[] = { "Someone", "Someone" };
        try
        {
            params[0] = getActiveUser();
            params[1] = getPassiveUser();
        }
        catch (BuildingServerException bse)
        {}
        out.write(MessageFormat.format(resourceBundle
            .getString(EasyEditEventType.getMapping(event_code).getName()),
            params));
    }

    
    
}
