
sirius.biz.model.AddressData Maven / Gradle / Ivy
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.model;
import sirius.biz.web.Autoloaded;
import sirius.db.mixing.Column;
import sirius.db.mixing.Composite;
import sirius.db.mixing.annotations.BeforeSave;
import sirius.db.mixing.annotations.Length;
import sirius.db.mixing.annotations.NullAllowed;
import sirius.db.mixing.annotations.Transient;
import sirius.db.mixing.annotations.Trim;
import sirius.kernel.commons.Strings;
import sirius.kernel.health.Exceptions;
import sirius.kernel.nls.Formatter;
import sirius.kernel.nls.NLS;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* Provides a street address which can be embedded into other entities or mixins.
*/
public class AddressData extends Composite {
/**
* As there are many different requirements for what a valid address might be, these can be specified per
* AddressData using one of the following requirements.
*/
public enum Requirements {
/**
* Each value within the address can filled or empty
*/
NONE,
/**
* If one of the fields is filled, all others have to be filled
*/
NOT_PARTIAL,
/**
* All fields have to be filled
*/
FULL_ADDRESS,
}
@Transient
protected final Requirements requirements;
@Transient
protected String fieldLabel;
/**
* Contains the street and street number.
*/
public static final Column STREET = Column.named("street");
@Trim
@NullAllowed
@Autoloaded
@Length(255)
private String street;
/**
* Contains the ZIP code.
*/
public static final Column ZIP = Column.named("zip");
@Trim
@NullAllowed
@Autoloaded
@Length(9)
private String zip;
/**
* Contains the name of the city.
*/
public static final Column CITY = Column.named("city");
@Trim
@NullAllowed
@Autoloaded
@Length(255)
private String city;
/**
* Creates a new instance with the given requirement.
*
* @param requirements determines which fields are required in certain constellations
* @param fieldLabel the name of the compund field which represents the address
*/
public AddressData(Requirements requirements, @Nullable String fieldLabel) {
this.requirements = requirements;
this.fieldLabel = Strings.isEmpty(fieldLabel) ? NLS.get("Model.address") : fieldLabel;
}
@BeforeSave
protected void onSave() {
if (requirements == Requirements.NONE) {
return;
}
boolean allEmpty = areAllFieldsEmpty();
boolean oneEmpty = isAnyFieldEmpty();
if (oneEmpty) {
if (requirements == Requirements.FULL_ADDRESS) {
throw Exceptions.createHandled()
.withNLSKey("AddressData.fullAddressRequired")
.set("name", fieldLabel)
.handle();
}
if (!allEmpty && requirements == Requirements.NOT_PARTIAL) {
throw Exceptions.createHandled()
.withNLSKey("AddressData.partialAddressRejected")
.set("name", fieldLabel)
.handle();
}
}
}
/**
* Determines if at least one field in the address is empty.
*
* @return true if at least one field is empty, false otherwise
*/
public boolean isAnyFieldEmpty() {
return Strings.isEmpty(street) || Strings.isEmpty(zip) || Strings.isEmpty(city);
}
/**
* Determines if all fields are empty.
*
* @return true all fields are empty, false otherwise
*/
public boolean areAllFieldsEmpty() {
return Strings.isEmpty(street) && Strings.isEmpty(zip) && Strings.isEmpty(city);
}
/**
* Sets all fields to null.
*/
public void clear() {
street = null;
zip = null;
city = null;
}
@Override
public String toString() {
return Formatter.create("[${steet} ][${zip} ]${city}")
.set("steet", street)
.set("zip", zip)
.set("city", city)
.smartFormat();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof AddressData)) {
return false;
}
return Strings.areEqual(street, ((AddressData) obj).street)
&& Strings.areEqual(zip,
((AddressData) obj).zip)
&& Strings.areEqual(city, ((AddressData) obj).city);
}
@Override
public int hashCode() {
return Objects.hash(street, zip, city);
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy