/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import backupAndRecovery.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * Library for detete protocol. Methods required to parse and build detetion
 * requests and responses are provided.
 *
 * All messages are expected to be in XML format.
 *
 * @author Marcel Duris
 */
public class ProtocolDelete {

    public static String ROOT = "delete";

    /**
     * Build request to delete file identified by backup metadata.
     * 
     * @param meta metadata of file to be backed up
     * @return byte array to be placed directly into UDP packet
     */
    public static byte[] buildRequest(BackupMetaContainer meta) {
        Document request = DocumentHelper.createDocument();
        Element root = request.addElement(ROOT);
        root.add(meta.getXmlDocument().getRootElement());

        return request.asXML().getBytes();
    }

    /**
     * Parses request to get metadata of file requested to be deleted.
     *
     * @param msg Xml document received as request over network
     * @return metadata container of requested backup
     */
    public static BackupMetaContainer getMeta(Document msg) {
        BackupMetaContainer meta = new BackupMetaContainer((Element) msg.getRootElement().elements().get(0));
        return meta;
    }


}
