@SuppressWarnings("unchecked")
@Override
public void validate(final Form< ? > form)
{
final DateTimePanel startDateTimePanel = (DateTimePanel) dependentFormComponentsWithCost2[0];
final DropDownChoice<Integer> stopHourOfDayDropDownChoice = (DropDownChoice<Integer>) dependentFormComponentsWithCost2[1];
final DropDownChoice<Integer> stopMinuteDropDownChoice = (DropDownChoice<Integer>) dependentFormComponentsWithCost2[2];
final DateHolder startDate = new DateHolder(startDateTimePanel.getConvertedInput());
final DateHolder stopDate = new DateHolder(startDate.getTimestamp());
stopDate.setHourOfDay(stopHourOfDayDropDownChoice.getConvertedInput());
stopDate.setMinute(stopMinuteDropDownChoice.getConvertedInput());
if (stopDate.getTimeOfDay() < startDate.getTimeOfDay()) { // Stop time is
// before start time. Assuming next day for stop time:
stopDate.add(Calendar.DAY_OF_MONTH, 1);
}
data.setStartTime(startDate.getTimestamp());
data.setStopTime(stopDate.getTimestamp());
if (data.getDuration() < 60000) {
// Duration is less than 60 seconds.
stopMinuteDropDownChoice.error(getString("timesheet.error.zeroDuration"));
} else if (data.getDuration() > TimesheetDao.MAXIMUM_DURATION) {
stopMinuteDropDownChoice.error(getString("timesheet.error.maximumDurationExceeded"));
}
if (cost2Exists == true) {
if (cost2Choice != null && cost2Choice.getConvertedInput() == null) {
// cost2Choice is always != null (but may-be invisible) if cost2 entries does exist in the system.
// Kost2 is not available for current task.
final TaskNode taskNode = taskTree.getTaskNodeById(data.getTaskId());
if (taskNode != null) {
final List<Integer> descendents = taskNode.getDescendantIds();
for (final Integer taskId : descendents) {
if (CollectionUtils.isNotEmpty(taskTree.getKost2List(taskId)) == true) {
// But Kost2 is available for sub task, so user should book his time sheet
// on a sub task with kost2s.
if (cost2Choice.isVisible()) {
cost2Choice.error(getString("timesheet.error.kost2NeededChooseSubTask"));
} else {
error(getString("timesheet.error.kost2NeededChooseSubTask"));
}
break;
}
}
}
}
}
}
});
parentPage.preInit();
gridBuilder.newGridPanel();
if (isNew() == true) {
addTemplatesRow();
}
{
// Task
final FieldsetPanel fs = gridBuilder.newFieldset(getString("task"));
final TaskSelectPanel taskSelectPanel = new TaskSelectPanel(fs, new PropertyModel<TaskDO>(data, "task"), parentPage, "taskId") {
@Override
protected void selectTask(final TaskDO task)
{
super.selectTask(task);
refresh(); // Task was changed. Therefore update the kost2 list.
}
/**
* @see org.projectforge.web.task.TaskSelectPanel#onModelSelected(org.apache.wicket.ajax.AjaxRequestTarget,
* org.projectforge.task.TaskDO)
*/
@Override
protected void onModelSelected(final AjaxRequestTarget target, final TaskDO taskDO)
{
refresh();
super.onModelSelected(target, taskDO);
if (cost2ChoiceFieldset != null) {
target.add(cost2ChoiceFieldset.getFieldset());
}
}
};
taskSelectPanel.setAutocompleteOnlyTaskBookableForTimesheets(true);
fs.add(taskSelectPanel);
taskSelectPanel.init();
taskSelectPanel.setRequired(true);
}
if (cost2Exists == true) {
// Cost 2 entries does exist in the data-base.
cost2ChoiceFieldset = gridBuilder.newFieldset(getString("fibu.kost2"));
cost2ChoiceFieldset.getFieldset().setOutputMarkupId(true);
cost2ChoiceFieldset.getFieldset().setOutputMarkupPlaceholderTag(true);
cost2List = taskTree.getKost2List(data.getTaskId());
final LabelValueChoiceRenderer<Integer> cost2ChoiceRenderer = getCost2LabelValueChoiceRenderer(parentPage.getBaseDao(), cost2List,
data, null);
cost2Choice = createCost2ChoiceRenderer(cost2ChoiceFieldset.getDropDownChoiceId(), parentPage.getBaseDao(), taskTree,
cost2ChoiceRenderer, data, cost2List);
cost2ChoicePanel = cost2ChoiceFieldset.add(cost2Choice);
dependentFormComponentsWithCost2[3] = cost2Choice;
updateCost2ChoiceValidation();
}
{
// User
final FieldsetPanel fs = gridBuilder.newFieldset(getString("user"));
PFUserDO user = data.getUser();
if (Hibernate.isInitialized(user) == false) {
user = userGroupCache.getUser(user.getId());
data.setUser(user);
}
userSelectPanel = new UserSelectPanel(fs.newChildId(), new PropertyModel<PFUserDO>(data, "user"), parentPage, "userId");
userSelectPanel.setRequired(true);
fs.add(userSelectPanel);
userSelectPanel.init();
}
{
// Time period
final FieldsetPanel fs = gridBuilder.newFieldset(getString("timePeriod"));
final DateTimePanel startDateTimePanel = new DateTimePanel(fs.newChildId(), new PropertyModel<Date>(data, "startTime"),
(DateTimePanelSettings) DateTimePanelSettings.get().withSelectStartStopTime(true).withTargetType(java.sql.Timestamp.class)
.withRequired(true), DatePrecision.MINUTE_15);
dependentFormComponentsWithCost2[0] = dependentFormComponentsWithoutCost2[0] = startDateTimePanel;
fs.add(startDateTimePanel);
WicketUtils.addTooltip(startDateTimePanel.getDateField(), new Model<String>() {
@Override
public String getObject()
{
final StringBuffer buf = new StringBuffer();
if (data.getStartTime() != null) {