001/*
002 *  Copyright 2013 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.observation;
017
018import java.util.Iterator;
019import java.util.Map;
020import java.util.Objects;
021
022import org.ametys.core.user.UserIdentity;
023
024/**
025 * Abstraction of an event.
026 */
027public class Event
028{
029    private UserIdentity _issuer;
030    private String _id;
031    private Map<String, Object> _args;
032
033    /**
034     * Creates an event.
035     * @param eventId the event identifier.
036     * @param issuer the issuer responsible of this event.
037     * @param args the event arguments.
038     */
039    public Event(String eventId, UserIdentity issuer, Map<String, Object> args)
040    {
041        _issuer = issuer;
042        _id = eventId;
043        _args = args;
044    }
045    
046    /**
047     * Retrieves the issuer responsible of this event.
048     * @return the issuer.
049     */
050    public UserIdentity getIssuer()
051    {
052        return _issuer;
053    }
054    
055    /**
056     * Retrieves the event identifier.
057     * @return the event identifier.
058     */
059    public String getId()
060    {
061        return _id;
062    }
063    
064    /**
065     * Returns the arguments.
066     * @return the arguments.
067     */
068    public Map<String, Object> getArguments()
069    {
070        return _args;
071    }
072    
073    @Override
074    public String toString()
075    {
076        StringBuilder event = new StringBuilder();
077        
078        event.append("event[id: ");
079        event.append(_id);
080        event.append(", issuer: ");
081        event.append(_issuer);
082        event.append(", args: [");
083        
084        Iterator<String> it = _args.keySet().iterator();
085        while (it.hasNext())
086        {
087            String key = it.next();
088            
089            event.append(key);
090            event.append(" = ");
091            event.append(Objects.toString(_args.get(key)));
092            
093            if (it.hasNext())
094            {
095                event.append(", ");
096            }
097        }
098        
099        event.append("]]");
100        
101        return event.toString();
102    }
103}