How can i abstract the following controller business logic into a bean or service?

The following code is in my ui controller and works perfectly but when i try to abstract it into a bean or service, i get the following exception: RuntimeException: Unable to create instance of screen class class .

‘’’
//setting value of billingAddress
if (billingAddressField.getValue() == null) {

            billingAddressValue = companyNameField.getValue() + "," + "\n";

            if (companyAddressField.getValue() != null) {

                billingAddressValue = companyAddressField.getValue() + "," + "\n";

                if (cityField.getValue() != null) {

                    billingAddressValue += cityField.getValue().getCity() + "," + "\n";
                }

                if (countryField.getValue() != null) {

                    billingAddressValue += countryField.getValue().getCountry() + "," + "\n";
                }

            }

            if (companyAddressField.getValue() == null) {

                if (cityField.getValue() != null) {

                    billingAddressValue = cityField.getValue().getCity() + "," + "\n";
                }

                if (countryField.getValue() != null) {

                    billingAddressValue += countryField.getValue().getCountry() + "," + "\n";
                }

            }

            billingAddressField.setValue(billingAddressValue);

        }

‘’’

Hi,

in order to help you I think it is necessary that you explain how you tried to create a dedicated bean that embodies this logic, which annotations you have set etc.

Perhaps you can share a small demo project that shows the error you are facing and explain the concrete classes.

Cheers
Mario

My schema is as follows:

Client SuperClass:

package com.company.erp.entity.crm.client.superclasses;

import com.company.erp.entity.general.country.City;
import com.company.erp.entity.general.country.Country;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.StandardEntity;
import com.haulmont.cuba.core.entity.annotation.Lookup;
import com.haulmont.cuba.core.entity.annotation.LookupType;

import javax.persistence.*;
import javax.validation.constraints.Email;

@NamePattern("%s|displayName")
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue("Client")
@Table(name = "ERP_CLIENT")
@Entity(name = "erp_Client")

public class Client extends StandardEntity {
    private static final long serialVersionUID = 4780989409314858228L;

    @Column(name = "DISPLAY_NAME")
    protected String displayName;

    @Email(message = "Please enter a valid email address!")
    @Column(name = "EMAIL")
    protected String email;

    @Column(name = "PREFERRED_CONTACT_PHONE")
    protected String preferredContactPhone;

    @Column(name = "FAX_NUMBER")
    protected String faxNumber;

    @Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "open", "clear"})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COUNTRY_ID")
    protected Country country;

    @Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "open", "clear"})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CITY_ID")
    protected City city;

    @Lob
    @Column(name = "SHIPPING_ADDRESS")
    protected String shippingAddress;

    @Lob
    @Column(name = "BILLING_ADDRESS")
    protected String billingAddress;

    public String getFaxNumber() {
        return faxNumber;
    }

    public void setFaxNumber(String faxNumber) {
        this.faxNumber = faxNumber;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getShippingAddress() {
        return shippingAddress;
    }

    public void setShippingAddress(String shippingAddress) {
        this.shippingAddress = shippingAddress;
    }


     public String getPreferredContactPhone() {
        return preferredContactPhone;
    }

    public void setPreferredContactPhone(String preferredContactPhone) {
        this.preferredContactPhone = preferredContactPhone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }
}

ClientCompany subclass:

package com.company.erp.entity.crm.client.subclasses.company;

import com.company.erp.entity.crm.client.subclasses.company.joined.Company_ContactPersons;
import com.company.erp.entity.crm.client.superclasses.Client;
import com.haulmont.chile.core.annotations.Composition;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.annotation.CaseConversion;
import com.haulmont.cuba.core.entity.annotation.ConversionType;
import com.haulmont.cuba.core.entity.annotation.OnDelete;
import com.haulmont.cuba.core.global.DeletePolicy;

import javax.persistence.*;
import javax.validation.constraints.Email;
import java.util.List;

@NamePattern("%s|displayName")
@PrimaryKeyJoinColumn(name = "ID", referencedColumnName = "ID")
@DiscriminatorValue("COMPANY")
@Table(name = "ERP_CLIENT_COMPANY")
@Entity(name = "erp_ClientCompany")

public class ClientCompany extends Client {
    private static final long serialVersionUID = 7311058099581783252L;

    @Column(name = "COMPANY_NAME", nullable = false)
    protected String companyName;

    @Column(name = "COMPANY_ADDRESS")
    protected String companyAddress;

    @CaseConversion(type = ConversionType.UPPER)
    @Column(name = "BRN")
    protected String brn;

    @CaseConversion(type = ConversionType.UPPER)
    @Column(name = "VAT_NUMBER")
    protected String vatNumber;

    @Email(message = "Not an email!")
    @Column(name = "SECONDARY_EMAIL")
    protected String secondaryEmail;

    @Column(name = "ALTERNATE_PHONE")
    protected String alternatePhone;

    @Composition
    @OnDelete(DeletePolicy.CASCADE)
    @OneToMany(mappedBy = "clientCompany")
    protected List<Company_ContactPersons> contactPerson;

    public String getSecondaryEmail() {
        return secondaryEmail;
    }

    public void setSecondaryEmail(String secondaryEmail) {
        this.secondaryEmail = secondaryEmail;
    }

    public String getVatNumber() {
        return vatNumber;
    }

    public void setVatNumber(String vatNumber) {
        this.vatNumber = vatNumber;
    }

    public String getBrn() {
        return brn;
    }

    public void setBrn(String brn) {
        this.brn = brn;
    }

    public List<Company_ContactPersons> getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(List<Company_ContactPersons> contactPerson) {
        this.contactPerson = contactPerson;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyAddress() {
        return companyAddress;
    }

    public void setCompanyAddress(String companyAddress) {
        this.companyAddress = companyAddress;
    }

    public String getAlternatePhone() {
        return alternatePhone;
    }

    public void setAlternatePhone(String alternatePhone) {
        this.alternatePhone = alternatePhone;
    }

}

Here is the service i created:

package com.company.erp.service;

import org.springframework.stereotype.Service;

@Service(BillingShippingAddressGeneratorService.NAME)
public class BillingShippingAddressGeneratorServiceBean implements BillingShippingAddressGeneratorService {

    @Override
    public String GenerateBillingShippingAddress(String companyName, String companyAddress, String city, String country) {

        String billingAddressValue;

        billingAddressValue = companyName + "," + "\n";

        if (companyAddress != null) {

            billingAddressValue = companyAddress + "," + "\n";

            if (city != null) {

                billingAddressValue += city + "," + "\n";
            }

            if (country != null) {

                billingAddressValue += country + "," + "\n";
            }

        }

        if (companyAddress == null) {

            if (city != null) {

                billingAddressValue = city + "," + "\n";
            }

            if (country != null) {

                billingAddressValue += country + "," + "\n";
            }

        }

        return billingAddressValue;

    }

}



Here is the interface:

package com.company.erp.service;

public interface BillingShippingAddressGeneratorService {
    String NAME = "erp_BillingShippingAddressGeneratorService";

    String GenerateBillingShippingAddress(String companyName, String companyAddress, String city, String country);

    }

Here is the controller implementation:

package com.company.erp.web.screens.crm.client.clientcompany;

import com.company.erp.entity.general.country.City;
import com.company.erp.entity.general.country.Country;
import com.company.erp.service.BillingShippingAddressGeneratorService;
import com.haulmont.cuba.gui.components.LookupPickerField;
import com.haulmont.cuba.gui.components.TextArea;
import com.haulmont.cuba.gui.components.TextField;
import com.haulmont.cuba.gui.screen.*;
import com.company.erp.entity.crm.client.subclasses.company.ClientCompany;

import javax.inject.Inject;

@UiController("erp_ClientCompany.edit")
@UiDescriptor("client-company-edit.xml")
@EditedEntityContainer("clientCompanyDc")
@LoadDataBeforeShow
public class ClientCompanyEdit extends StandardEditor<ClientCompany> {

    @Inject
    protected TextField<String> companyNameField;
    @Inject
    protected TextField<String> displayNameField;
    @Inject
    protected LookupPickerField<Country> countryField;
    @Inject
    protected LookupPickerField<City> cityField;
    @Inject
    protected TextField<String> companyAddressField;
    @Inject
    protected TextArea<String> billingAddressField;
    @Inject
    private BillingShippingAddressGeneratorService billingShippingAddressGeneratorService;

    String displayNameValue;
    String companyName = companyNameField.getValue();
    String companyAddress = companyAddressField.getValue();
    String city = cityField.getValue().getCity();
    String country = countryField.getValue().getCountry();
    String billingShippingValue;

    @Subscribe
    protected void onBeforeCommitChanges(BeforeCommitChangesEvent event) {

        //setting value of displayName
        displayNameValue = companyNameField.getValue();

        displayNameField.setValue(displayNameValue);

        //setting value of billingAddress
        if (billingAddressField.getValue() == null) {

            billingShippingValue = billingShippingAddressGeneratorService.GenerateBillingShippingAddress(companyName,companyAddress,city,country);
            billingAddressField.setValue(billingShippingValue);

        }