/*
* Copyright (C) 2012 FiveHT Media Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.fiveht.tick.api.parser;
import com.fiveht.tick.api.Entry;
import com.fiveht.tick.api.Task;
import java.beans.PropertyVetoException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author Nathan Crause
*/
public class EntryParser extends Parser<Entry> {
@Override
public Entry parse(Element element) {
Entry entry = new Entry();
try {
entry.setId(getInt(element, "id"));
entry.setTaskId(getInt(element, "task_id"));
entry.setUserId(getInt(element, "user_id"));
entry.setDate(getDate(element, "date"));
entry.setHours(getDecimal(element, "hours"));
entry.setNotes(get(element, "notes"));
entry.setCreatedAt(getDatetime(element, "created_at"));
entry.setUpdatedAt(getDatetime(element, "updated_at"));
entry.setUserEmail(get(element, "user_email"));
entry.setTask(parseTask(element));
}
catch (PropertyVetoException ex) {
throw new RuntimeException(
"PANIC! This is a new object - there should be no vetoing",
ex);
}
return entry;
}
private Task parseTask(Element parent) {
NodeList list = parent.getElementsByTagName("task");
if (list.getLength() != 1) {
throw new RuntimeException("Unexpectedly received "
+ list.getLength() + " tasks (expected only one)");
}
TaskParser parser = new TaskParser();
return parser.parse((Element) list.item(0));
}
}