package be.ac.ulb.code.wit.resources.serializers.atom;

import java.io.Reader;

import org.apache.abdera.parser.ParseException;

import be.ac.ulb.code.wit.contacts.Contacts;
import be.ac.ulb.code.wit.resources.serializers.helpers.GroupDeserializer;
import be.ac.ulb.code.wit.resources.serializers.helpers.MalformedRequestException;

/**
 * An atom deserializer that creates, renames or deletes 
 * contact groups, using data from atom entries.
 * 
 * @author fpicalausa
 *
 */
public class AtomGroupDeserializer extends GroupDeserializer {
	public AtomGroupDeserializer(Contacts contacts) {
		super(contacts);
	}
	
	public AtomGroupDeserializer(Contacts contacts, String group) {
		super(contacts, group);
	}
	
	/**
	 * Update the current group or create a new one, 
	 * with the information contained in an atom entry
	 * that can be read from the given reader.
	 * @param data The reader from which the atom entry containing
	 * the new information about the group is to be read.
	 * @throws MalformedRequestException if the given entry is
	 * not correct.
	 */
	public void updateGroup(final Reader data) throws MalformedRequestException {
		try {
			setGroup(AtomHelpers.parseEntry(data).getTitle());
		}
		catch (ParseException e) {
			throw new MalformedRequestException(e);
		}
	}
}
