/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * LoggingBasicServiceBean.java
 */

package org.eu.adapt.demonstrator.bs.demo;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.HashMap;
import java.io.Serializable;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.LoggingFacility_xsd.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.LoggingFacility_wsdl.LoggingFacilityLogPortType;

public class LoggingBasicServiceBean implements SessionBean, LoggingFacilityLogPortType
{
    public void logEvent(LogEventRequestType logEventRequest)
    {
        synchronized (_demoUserLogMap)
        {
            List demoUserLogEvents = (List) _demoUserLogMap.get(logEventRequest.getDemoUserID());
            if (demoUserLogEvents == null)
            {
                demoUserLogEvents = new LinkedList();
                _demoUserLogMap.put(logEventRequest.getDemoUserID(), demoUserLogEvents);
            }

            List repositoryLogEvents = (List) _repositoryLogMap.get(_repositoryId);
            if (repositoryLogEvents == null)
            {
                repositoryLogEvents = new LinkedList();
                _repositoryLogMap.put(_repositoryId, repositoryLogEvents);
            }

            LogEntry logEntry = new LogEntry();

            logEntry._demoUserID       = logEventRequest.getDemoUserID();
            logEntry._repositoryID     = _repositoryId;
            logEntry._serviceID        = logEventRequest.getServiceID();
            logEntry._eventID          = logEventRequest.getEventID();
            logEntry._eventDescription = logEventRequest.getEventDescription();
            logEntry._timestamp        = Calendar.getInstance().toString();

            demoUserLogEvents.add(logEntry);
            repositoryLogEvents.add(logEntry);
        }
    }

    public GetEventsResponseType getEvents(GetEventsRequestType getEventsRequest)
        throws GetEventsFaultType
    {
        GetEventsResponseType getEventsResponse = new GetEventsResponseType();

        synchronized (_demoUserLogMap)
        {
            List demoUserLogEvents = (List) _demoUserLogMap.get(getEventsRequest.getDemoUserID());

            GetEventsResponseType_LogEntry[] logEntries = null;
            if (demoUserLogEvents != null)
            {
                logEntries = new GetEventsResponseType_LogEntry[demoUserLogEvents.size()];

                Iterator logEventIterator = demoUserLogEvents.iterator();
                int index = 0;
                while (logEventIterator.hasNext())
                {
                    LogEntry logEvent = (LogEntry) logEventIterator.next();

                    logEntries[index] = new GetEventsResponseType_LogEntry();
                    logEntries[index].setServiceID(logEvent._serviceID);
                    logEntries[index].setEventID(String.valueOf(logEvent._eventID));
                    logEntries[index].setEventDescription(logEvent._eventDescription);
                    logEntries[index].setTimestamp(logEvent._timestamp);

                    index++;
                }
            }
            else
                logEntries = new GetEventsResponseType_LogEntry[0];

            getEventsResponse.setLogEntry(logEntries);
        }

        return getEventsResponse;
    }

    public void setSessionContext(SessionContext sessionContext)
    {
        _sessionContext = sessionContext;
    }

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

    public void ejbCreate()
    {
    }

    public void ejbActivate()
    {
    }

    public void ejbPassivate()
    {
    }

    public void ejbLoad()
    {
    }

    public void ejbStore()
    {
    }    

    public void ejbRemove()
    {
    }
    
    private class LogEntry implements Serializable
    {
        public String _demoUserID;
        public String _repositoryID;
        public String _serviceID;
        public String _eventID;
        public String _eventDescription;
        public String _timestamp;
    }

    private SessionContext _sessionContext = null;

    private String  _repositoryId     = null;
    private HashMap _demoUserLogMap   = new HashMap();
    private HashMap _repositoryLogMap = new HashMap();    
}