/* ======================================================================
   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.ResourceBundle;

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

/**
 * Class to track changes to a resource.
 * Although it doesn't define any new intance variables we subclass event to
 * keep Event clean of constants.
 * @author buckett
 */
public class ResourceEvent extends Event
{
    private static ResourceBundle resourceBundle = ResourceBundle
    .getBundle(ResourceEvent.class.getName());
    
    public static final ResourceEventType EVENT_CREATE = new ResourceEventType("event.create");
    public static final ResourceEventType EVENT_EDIT = new ResourceEventType("event.edit");
    public static final ResourceEventType EVENT_DELETE = new ResourceEventType("event.delete");
    public static final ResourceEventType EVENT_RESTORE = new ResourceEventType("event.restore");
    
    public static class ResourceEventType extends IntStringEnum
    {
        protected ResourceEventType(String name)
        {
            super(name, ResourceEventType.class);
        }
        
        public static ResourceEventType getMapping(int index)
        {
            return (ResourceEventType)getConst(index,ResourceEventType.class);
        }
        
        public static ResourceEventType getMapping(String name)
        {
            return (ResourceEventType)getConst(name, ResourceEventType.class);
        }
        
    }
    
    /**
     * Empty constructor used by the database layer.
     */
    public ResourceEvent()
    {
        super();
    }
    
    /**
     * Create an event about a resource.
     * @param type The type of event.
     * @param resource The resource to which the event pertains.
     */
    public ResourceEvent(ResourceEventType type, Resource resource)
    {
        setEventCode(type.getId());
        setResource(resource);
        setImportance(Event.IMPORTANCE_USER_MIN);
    }
    
    public String messageTitle()
    {
        String params[] = { "Someone" };
        return MessageFormat.format(resourceBundle
            .getString(ResourceEventType.getMapping(event_code).getName()),
            params);
    }
    
    public void printMessage(PrintWriter out, boolean html)
    {
        Object params[] = { "Someone" };
        try
        {
            params[0] = getActiveUser();
        }
        catch (BuildingServerException bse)
        {}
        out.write(MessageFormat.format(resourceBundle
            .getString(ResourceEventType.getMapping(event_code).getName()),
            params));
    }
}
