/*
* 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;
import com.fiveht.tick.api.parser.EntryParser;
import java.beans.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
/**
* This service class is used to create a time entry associated with a
* particular task. The project isn't necessary, as it will be derived from
* the task.
*
* @author Nathan Crause
*/
public final class CreateEntryService extends Service<Entry> {
/**
* Constructs a new instance of this service.
*
* @param company the name of the company within tickspot
* @param emailAddress the email address associated with the account within
* the company's account in Tickspot
* @param password the account's password (as identified by the emailAddress)
* @param taskId the unique ID number of task to which we are recording
* hours
* @param hours the number of hours to record against the given task
* @param date the date on which the hours are to be recorded
*/
public CreateEntryService(String company, String emailAddress,
String password, Integer taskId, BigDecimal hours, Date date) {
super(company, emailAddress, password);
this.taskId = taskId;
this.hours = hours;
this.date = date;
}
private Integer taskId;
/**
* Get the value of taskId
*
* @return the value of taskId
*/
public Integer getTaskId() {
return taskId;
}
private BigDecimal hours;
/**
* Get the value of hours
*
* @return the value of hours
*/
public BigDecimal getHours() {
return hours;
}
private Date date;
/**
* Get the value of date
*
* @return the value of date
*/
public Date getDate() {
return date;
}
private String notes;
public static final String PROP_NOTES = "notes";
/**
* Get the value of notes
*
* @return the value of notes
*/
public String getNotes() {
return notes;
}
/**
* Set the value of notes
*
* @param notes new value of notes
* @throws java.beans.PropertyVetoException
*/
public void setNotes(String notes) throws java.beans.PropertyVetoException {
String oldNotes = this.notes;
vetoableChangeSupport.fireVetoableChange(PROP_NOTES, oldNotes, notes);
this.notes = notes;
propertyChangeSupport.firePropertyChange(PROP_NOTES, oldNotes, notes);
}
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
/**
* Add PropertyChangeListener.
*
* @param listener
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
/**
* Remove PropertyChangeListener.
*
* @param listener
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
private VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);
/**
* Add VetoableChangeListener.
*
* @param listener
*/
public void addVetoableChangeListener(VetoableChangeListener listener) {
vetoableChangeSupport.addVetoableChangeListener(listener);
}
/**
* Remove VetoableChangeListener.
*
* @param listener
*/
public void removeVetoableChangeListener(VetoableChangeListener listener) {
vetoableChangeSupport.removeVetoableChangeListener(listener);
}
@Override
public String getPath() {
return "create_entry";
}
@Override
public Entry request() {
try {
Document doc = post(getParameters());
EntryParser parser = new EntryParser();
return parser.parse(doc.getDocumentElement());
}
catch (Throwable thrown) {
Logger.getLogger(CreateEntryService.class.getName()).log(Level.SEVERE, null, thrown);
throw new RuntimeException("PANIC!", thrown);
}
}
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* This builds a map of request parameters for posting.
*
* @return
*/
private Map<String, Object> getParameters() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("task_id", getTaskId());
params.put("hours", getHours());
params.put("date", dateFormat.format(getDate()));
if (notes != null) params.put("notes", getNotes());
return params;
}
}