package com.translationdata.messenger.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.translationdata.messenger.database.DatabaseClass;
import com.translationdata.messenger.model.Message;

public class MessageService {
	private Map<Long, Message> messages = DatabaseClass.getMessages();

// This gets executes on every HTTP request so it never remembers changes to the data...
/*	public MessageService() {
		{
		messages.put(1L, new Message(1, "Hello Java", "Almon"));
		messages.put(2L, new Message(2, "Hello Jersey", "Almon"));
		}
	}*/
	
	public List<Message> getAllMessages() {
				
		return new ArrayList<Message>(messages.values());
	}
	
	public Message getMessage(Long id) {
		return messages.get(id);
	}
	
	public Message addMessage(Message message) {
		message.setId(messages.size() + 1);
		messages.put(message.getId(), message);
		return message;
	}
	
	public Message updateMessage(Message message) {
		if(message.getId() <= 0) {
			return null;
		}
		messages.put(message.getId(), message);
		return message;
		
	}
	
	public void removeMessage(long id) {
		messages.remove(id);
	}
}

@@ -65,24 +65,51 @@
 .ArrayList;%0A
+import java.util.Calendar;%0A
 import java.
@@ -1160,10 +1160,361 @@
 id);%0A%09%7D%0A
+%09%0A%09public List%3CMessage%3E getAllMessagesForYear(int year) %7B%0A%09%09List%3CMessage%3E messagesForYear = new ArrayList%3C%3E();%0A%09%09Calendar cal = Calendar.getInstance();%0A%09%09for(Message message : messages.values()) %7B%0A%09%09%09cal.setTime(message.getCreated());%0A%09%09%09if(cal.get(Calendar.YEAR) == year) %7B%0A%09%09%09%09messagesForYear.add(message);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return messagesForYear;%0A%09%7D%0A%09%0A%09%0A
 %7D%0A

Add new function