package org.incode.eurocommercial.contactapp.fixture.scenarios.demo;

import java.util.Collections;

import org.apache.isis.applib.fixturescripts.FixtureScript;

import org.incode.eurocommercial.contactapp.dom.contacts.Contact;
import org.incode.eurocommercial.contactapp.dom.contacts.ContactRepository;
import org.incode.eurocommercial.contactapp.dom.country.Country;
import org.incode.eurocommercial.contactapp.dom.country.CountryRepository;
import org.incode.eurocommercial.contactapp.dom.group.ContactGroup;
import org.incode.eurocommercial.contactapp.dom.group.ContactGroupRepository;
import lombok.Getter;
import lombok.Setter;

public class ContactImport implements org.isisaddons.module.excel.dom.ExcelFixtureRowHandler {

    @Getter @Setter
    private String country;
    @Getter @Setter
    private String address;
    @Getter @Setter
    private String group;
    @Getter @Setter
    private String company;
    @Getter @Setter
    private String name;
    @Getter @Setter
    private String office;
    @Getter @Setter
    private String mobile;
    @Getter @Setter
    private String home;
    @Getter @Setter
    private String email;
    @Getter @Setter
    private String role;
    @Getter @Setter
    private String note;
    @Getter @Setter
    private Integer disorder;

    @Override
    public java.util.List<Object> handleRow(
            final FixtureScript.ExecutionContext executionContext,
            final org.isisaddons.module.excel.dom.ExcelFixture excelFixture,
            final Object previousRow) {

        if(country  != null && country .equals("")) country  = null;
        if(address  != null && address .equals("")) address  = null;
        if(group    != null && group   .equals("")) group    = null;
        if(company  != null && company .equals("")) company  = null;
        if(name     != null && name    .equals("")) name     = null;
        if(office   != null && office  .equals("")) office   = null;
        if(mobile   != null && mobile  .equals("")) mobile   = null;
        if(home     != null && home    .equals("")) home     = null;
        if(email    != null && email   .equals("")) email    = null;
        if(role     != null && role    .equals("")) role     = null;
        if(note     != null && note    .equals("")) note     = null;
        if(disorder != null && disorder.equals("")) disorder = null;

        final ContactImport previousContactRow = (ContactImport) previousRow;
        if(previousContactRow != null) {
            if(country == null) country = previousContactRow.getCountry();
            if(group   == null) group   = previousContactRow.getGroup();
        }

        Country country = countryRepository.findOrCreate(this.country);
        ContactGroup contactGroup = contactGroupRepository.findOrCreate(country, group);

        if(address  != null) contactGroup.setAddress(address);
        if(disorder != null) {
            int displayOrder = disorder;
            contactGroup.setDisplayOrder(displayOrder);
        }
        Contact contact = null;
        if(name == null) {
            if(office != null) contactGroup.addContactNumber("Office", office);
            if(mobile != null) contactGroup.addContactNumber("Mobile", mobile);
            if(home   != null) contactGroup.addContactNumber("Home", home);
            if(email  != null) contactGroup.setEmail(email);
        }
        else {
            contact = contactRepository.findOrCreate(name, company, email, note, office, mobile, home);
            contact.addContactRole(contactGroup, role, null);
        }
        executionContext.addResult(excelFixture, contact);

        return Collections.singletonList(contact);
    }

    @javax.inject.Inject
    CountryRepository countryRepository;
    @javax.inject.Inject
    ContactRepository contactRepository;
    @javax.inject.Inject
    ContactGroupRepository contactGroupRepository;

}

@@ -94,16 +94,56 @@
 tions;%0A%0A
+import com.google.common.base.Strings;%0A%0A
 import o
@@ -1577,27 +1577,24 @@
         
-if(
 country 
  != null
@@ -1589,56 +1589,38 @@
 try 
- != null && country .equals(%22%22)) country  = null
+= Strings.emptyToNull(country)
 ;%0A  
@@ -1621,35 +1621,32 @@
 y);%0A        
-if(
 address 
  != null && 
@@ -1637,56 +1637,38 @@
 ess 
- != null && address .equals(%22%22)) address  = null
+= Strings.emptyToNull(address)
 ;%0A  
@@ -1677,67 +1677,42 @@
     
-if(
 group 
-   != null && group   .equals(%22%22)) group    = null
+= Strings.emptyToNull(group)
 ;%0A  
@@ -1721,19 +1721,16 @@
     
-if(
 company 
  != 
@@ -1729,56 +1729,38 @@
 any 
- != null && company .equals(%22%22)) company  = null
+= Strings.emptyToNull(company)
 ;%0A  
@@ -1769,67 +1769,40 @@
     
-if(
 name 
-    != null && name    .equals(%22%22)) name     = null
+= Strings.emptyToNull(name)
 ;%0A  
@@ -1811,67 +1811,44 @@
     
-if(
 office 
-  != null && office  .equals(%22%22)) office   = null
+= Strings.emptyToNull(office)
 ;%0A  
@@ -1857,67 +1857,44 @@
     
-if(
 mobile 
-  != null && mobile  .equals(%22%22)) mobile   = null
+= Strings.emptyToNull(mobile)
 ;%0A  
@@ -1903,67 +1903,40 @@
     
-if(
 home 
-    != null && home    .equals(%22%22)) home     = null
+= Strings.emptyToNull(home)
 ;%0A  
@@ -1945,67 +1945,42 @@
     
-if(
 email 
-   != null && email   .equals(%22%22)) email    = null
+= Strings.emptyToNull(email)
 ;%0A  
@@ -1989,67 +1989,40 @@
     
-if(
 role 
-    != null && role    .equals(%22%22)) role     = null
+= Strings.emptyToNull(role)
 ;%0A  
@@ -2031,136 +2031,40 @@
     
-if(
 note 
-    != null && note    .equals(%22%22)) note     = null;%0A        if(disorder != null && disorder.equals(%22%22)) disorder = null
+= Strings.emptyToNull(note)
 ;%0A%0A 

Use Strings.emptyToNull in ContactImport