package org.estatio.dom.lease;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.VersionStrategy;

import org.estatio.dom.EstatioTransactionalObject;
import org.estatio.dom.asset.Unit;
import org.joda.time.LocalDate;

import org.apache.isis.applib.annotation.Hidden;
import org.apache.isis.applib.annotation.MemberOrder;
import org.apache.isis.applib.annotation.Optional;
import org.apache.isis.applib.annotation.Title;
import org.apache.isis.applib.annotation.Where;

@PersistenceCapable
@javax.jdo.annotations.Version(strategy = VersionStrategy.VERSION_NUMBER, column = "VERSION")
public class LeaseUnit extends EstatioTransactionalObject implements Comparable<LeaseUnit> {

    // {{ Lease (property)
    private Lease lease;

    @Title(sequence = "1", append = ":")
    @MemberOrder(sequence = "1")
    @Hidden(where = Where.REFERENCES_PARENT)
    public Lease getLease() {
        return lease;
    }

    public void setLease(final Lease lease) {
        this.lease = lease;
    }

    public void modifyLease(final Lease lease) {
        Lease currentLease = getLease();
        // check for no-op
        if (lease == null || lease.equals(currentLease)) {
            return;
        }
        // delegate to parent to associate
        lease.addToUnits(this);
        // additional business logic
        // onModifyLease(currentLease, lease);
    }

    public void clearLease() {
        Lease currentLease = getLease();
        // check for no-op
        if (currentLease == null) {
            return;
        }
        // delegate to parent to dissociate
        currentLease.removeFromUnits(this);
        // additional business logic
        // onClearLease(currentLease);
    }

    // }}

    // {{ Unit (property)
    private Unit unit;

    @Title(sequence = "2", append = ":")
    @MemberOrder(sequence = "2")
    @Hidden(where = Where.REFERENCES_PARENT)
    public Unit getUnit() {
        return unit;
    }

    public void setUnit(final Unit unit) {
        this.unit = unit;
    }

    public void modifyUnit(final Unit unit) {
        Unit currentUnit = getUnit();
        // check for no-op
        if (unit == null || unit.equals(currentUnit)) {
            return;
        }
        // delegate to parent to associate
        unit.addToLeases(this);
        // additional business logic
        // onModifyUnit(currentUnit, unit);
    }

    public void clearUnit() {
        Unit currentUnit = getUnit();
        // check for no-op
        if (currentUnit == null) {
            return;
        }
        // delegate to parent to dissociate
        currentUnit.removeFromLeases(this);
        // additional business logic
        // onClearUnit(currentUnit);
    }

    // {{ StartDate (property)
    private LocalDate startDate;

    @Persistent
    @Optional
    @MemberOrder(sequence = "3")
    public LocalDate getStartDate() {
        return startDate;
    }

    public void setStartDate(final LocalDate startDate) {
        this.startDate = startDate;
    }

    // }}

    // {{ EndDate (property)
    // meaning, all meant to bound an item to a period.
    private LocalDate endDate;

    @Persistent
    @Optional
    @MemberOrder(sequence = "4")
    public LocalDate getEndDate() {
        return endDate;
    }

    public void setEndDate(final LocalDate endDate) {
        this.endDate = endDate;
    }

    // }}

    // {{ TenancyStartDate (property)
    private LocalDate tenancyStartDate;

    @Optional
    @MemberOrder(sequence = "4")
    public LocalDate getTenancyStartDate() {
        return tenancyStartDate;
    }

    public void setTenancyStartDate(final LocalDate tenancyStartDate) {
        this.tenancyStartDate = tenancyStartDate;
    }

    // }}

    // {{ TenancyEndDate (property)
    private LocalDate tenancyEndDate;

    @Optional
    @MemberOrder(sequence = "5")
    public LocalDate getTenancyEndDate() {
        return tenancyEndDate;
    }

    public void setTenancyEndDate(final LocalDate tenancyEndDate) {
        this.tenancyEndDate = tenancyEndDate;
    }

    // }}

    // {{ Brand (property)
    private String brand;

    @Optional
    @MemberOrder(sequence = "6")
    public String getBrand() {
        return brand;
    }

    public void setBrand(final String brand) {
        this.brand = brand;
    }

    // }}

    // {{ Sector (property)
    private String sector;

    @Optional
    @MemberOrder(sequence = "7")
    public String getSector() {
        return sector;
    }

    public void setSector(final String sector) {
        this.sector = sector;
    }

    // }}

    // {{ Activity (property)
    private String activity;

    @Optional
    @MemberOrder(sequence = "8")
    public String getActivity() {
        return activity;
    }

    public void setActivity(final String activity) {
        this.activity = activity;
    }

    // }}

    @Override
    @Hidden
    public int compareTo(LeaseUnit o) {
        return this.getUnit().getReference().compareTo(o.getUnit().getReference());
    }
}

@@ -744,35 +744,8 @@
  %7B%0A%0A
-    // %7B%7B Lease (property)%0A
     
@@ -1310,92 +1310,8 @@
 s);%0A
-        // additional business logic%0A        // onModifyLease(currentLease, lease);%0A
     
@@ -1574,123 +1574,10 @@
     
-    // additional business logic%0A        // onClearLease(currentLease);%0A    %7D%0A%0A    // %7D%7D%0A%0A    // %7B%7B Unit (property)
+%7D%0A
 %0A   
@@ -2124,89 +2124,8 @@
 s);%0A
-        // additional business logic%0A        // onModifyUnit(currentUnit, unit);%0A
     
@@ -2383,115 +2383,10 @@
     
-    // additional business logic%0A        // onClearUnit(currentUnit);%0A    %7D%0A%0A    // %7B%7B StartDate (property)
+%7D%0A
 %0A   
@@ -2653,105 +2653,8 @@
    %7D
-%0A%0A    // %7D%7D%0A%0A    // %7B%7B EndDate (property)%0A    // meaning, all meant to bound an item to a period.
 %0A   
@@ -2909,57 +2909,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A    // %7B%7B TenancyStartDate (property)%0A
     
@@ -3211,55 +3211,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A    // %7B%7B TenancyEndDate (property)%0A
     
@@ -3499,46 +3499,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A    // %7B%7B Brand (property)%0A
     
@@ -3715,47 +3715,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A    // %7B%7B Sector (property)%0A
     
@@ -3938,49 +3938,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A    // %7B%7B Activity (property)%0A
     
@@ -4175,19 +4175,8 @@
  %7D%0A%0A
-    // %7D%7D%0A%0A
     
@@ -4249,29 +4249,244 @@
     
-return this.getUnit()
+Unit thisUnit = this.getUnit();%0A        Unit otherUnit = o.getUnit();%0A        if (thisUnit == null && otherUnit == null) return 0;%0A        if (thisUnit == null) return 1;%0A        if (otherUnit == null) return -1;%0A        return thisUnit
 .get
@@ -4508,26 +4508,24 @@
 pareTo(o
-.get
+ther
 Unit
-()
 .getRefe

add null checks to compareTo