package org.estatio.module.lease.dom.party;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;

import org.joda.time.LocalDate;

import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.apache.isis.applib.query.QueryDefault;
import org.apache.isis.applib.services.repository.RepositoryService;

import org.estatio.module.party.dom.Party;

@DomainService(nature = NatureOfService.DOMAIN,
        repositoryFor = TenantAdministrationStatus.class,
        objectType = "party.TenantAdministrationStatusRepository")
public class TenantAdministrationStatusRepository {

    public TenantAdministrationStatus findStatus(final Party tenant){
        return repositoryService.uniqueMatch(
                new QueryDefault<>(
                        TenantAdministrationStatus.class,
                        "findStatus",
                        "tenant", tenant));
    }

    public TenantAdministrationStatus upsert(final AdministrationStatus status, final Party tenant, @Nullable final LocalDate judicalRedressDate){
        TenantAdministrationStatus tenantStatus = findStatus(tenant);
        if (tenantStatus != null) {
            tenantStatus.setStatus(status);
            tenantStatus.setJudicialRedressDate(judicalRedressDate);
        } else {
            tenantStatus = create(status, tenant, judicalRedressDate);
        }
        return tenantStatus;
    }

    private TenantAdministrationStatus create(final AdministrationStatus status, final Party tenant, final LocalDate judicalRedressDate) {
        TenantAdministrationStatus tenantStatus = new TenantAdministrationStatus();
        tenantStatus.setTenant(tenant);
        tenantStatus.setStatus(status);
        tenantStatus.setJudicialRedressDate(judicalRedressDate);
        repositoryService.persistAndFlush(tenantStatus);
        return tenantStatus;
    }

    public List<TenantAdministrationStatus> listAll(){
        return repositoryService.allInstances(TenantAdministrationStatus.class);
    }

    @Inject
    RepositoryService repositoryService;
}

@@ -1084,32 +1084,33 @@
  LocalDate judic
+i
 alRedressDate)%7B%0A
@@ -1304,32 +1304,33 @@
 edressDate(judic
+i
 alRedressDate);%0A
@@ -1397,24 +1397,25 @@
 enant, judic
+i
 alRedressDat
@@ -1582,24 +1582,25 @@
 alDate judic
+i
 alRedressDat
@@ -1817,16 +1817,17 @@
 te(judic
+i
 alRedres

fixes typo