/*
 * HeadsUp Agile
 * Copyright 2009-2014 Heads Up Development Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.headsupdev.agile.app.milestones;

import org.headsupdev.agile.storage.HibernateStorage;
import org.headsupdev.agile.storage.issues.Milestone;
import org.headsupdev.agile.storage.issues.MilestoneGroup;
import org.headsupdev.agile.web.HeadsUpPage;
import org.headsupdev.agile.web.components.DateTimeWithTimeZoneField;
import org.headsupdev.agile.web.components.IdPatternValidator;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

import java.util.Date;

/**
 * The form used when editing / creating a milestone
 *
 * @author Andrew Williams
 * @version $Id$
 * @since 1.0
 */
public class EditMilestoneForm
        extends Panel
{
    Milestone milestone;
    boolean creating;

    public EditMilestoneForm( String id, final Milestone mile, final boolean creating, final HeadsUpPage owner )
    {
        super( id );
        this.milestone = mile;
        this.creating = creating;

        Form<Milestone> form = new Form<Milestone>( "edit" )
        {
            public void onSubmit()
            {
                if ( !creating )
                {
                    milestone = (Milestone) ( (HibernateStorage) owner.getStorage() ).getHibernateSession().merge( milestone );
                }
                milestone.setUpdated( new Date() );

                submitParent();

                PageParameters params = new PageParameters();
                params.add( "project", milestone.getProject().getId() );
                params.add( "id", milestone.getName() );
                setResponsePage( owner.getPageClass( "milestones/view" ), params );
            }

        };

        layout( form );
        add( form );
    }

    public void layout( Form<Milestone> form )
    {
        form.setModel( new CompoundPropertyModel<Milestone>( milestone ) );

        form.add( new Label( "project", milestone.getProject().getAlias() ) );
        if ( creating )
        {
            form.add( new TextField<String>( "name" ).add( new IdPatternValidator() ).setRequired( true ) );
            form.add( new WebMarkupContainer( "name-label" ).setVisible( false ) );
//            form.add( new Label( "created", new FormattedDateModel( new Date() ) ) );
        }
        else
        {
            form.add( new Label( "name-label", milestone.getName() ) );
            form.add( new WebMarkupContainer( "name" ).setVisible( false ) );
//            form.add( new Label( "created", new FormattedDateModel( milestone.getCreated() ) ) );
        }
        form.add( new DateTimeWithTimeZoneField( "startDate" ) );
        form.add( new DateTimeWithTimeZoneField( "dueDate" ) );

        form.add( new TextArea( "description" ) );
    }

    public void submitParent()
    {
        // allow others to override
    }

    public Milestone getMilestone()
    {
        return milestone;
    }

    public boolean isCreating()
    {
        return creating;
    }


}
@@ -783,16 +783,103 @@
 tones;%0A%0A
+import org.apache.wicket.model.PropertyModel;%0Aimport org.headsupdev.agile.api.Manager;%0A
 import o
@@ -982,67 +982,8 @@
 ne;%0A
-import org.headsupdev.agile.storage.issues.MilestoneGroup;%0A
 impo
@@ -2798,16 +2798,22 @@
  layout(
+ final
  Form%3CMi
@@ -3718,24 +3718,775 @@
 d( %22dueDate%22
+, new PropertyModel%3CDate%3E(%0A                new PropertyModel%3CMilestone%3E( this, %22milestone%22 ), %22dueDate%22 )%0A        %7B%0A            @Override%0A            public void setObject( Date object )%0A            %7B%0A                // this piece of work is ensuring that we attach the milestone to the model before the wicket value%0A                // setting occurs which will require milestone lookup on a group if due is not null%0A                if ( !creating )%0A                %7B%0A                    milestone = (Milestone) ( (HibernateStorage) Manager.getStorageInstance() ).getHibernateSession().merge( milestone );%0A                    form.getModel().setObject( milestone );%0A                %7D%0A%0A                super.setObject( object );%0A            %7D%0A        %7D
  ) );%0A%0A     

Fix an issue where the dueDate calculations of a milestoneGroup could crash the edit milestone page. We needed to merge the object into the new session earlier in the lifecycle if dueDate is being set.