package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.List; import net.sourceforge.fenixedu.dataTransferObject.person.MacAddressRegistryBean; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import pt.iscte.ci.metadata.ISCTE; import pt.iscte.dsi.utils.MacAddressUtils; @ISCTE(author = "Paulo Zenida") public class MacAddress extends MacAddress_Base { private MacAddress() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public MacAddress(final Person person, final String address) { this(); super.setPerson(person); activate(); setAddress(address); } public MacAddress(MacAddressRegistryBean macAddressRegistryBean) { this(macAddressRegistryBean.getPerson(), macAddressRegistryBean.getAddress()); setIsActive(macAddressRegistryBean.getIsActive()); } private static boolean isMacAddressUnique(final MacAddress currentObject, final String address) { for (final MacAddress macAddress : RootDomainObject.getInstance().getMacAddresses()) { if (macAddress != currentObject && macAddress.getAddress().equals(address)) { return false; } } return true; } public void activate() { if (getPerson() == null) { throw new DomainException("error.cannot.set.active.without.person.associated"); } for (final MacAddress macAddress : getPerson().getMacAddresses()) { macAddress.setIsActive(Boolean.FALSE); } setIsActive(Boolean.TRUE); } @Override public void setAddress(String address) { if (!MacAddressUtils.isValidMacAddress(address)) { throw new DomainException("error.invalid.mac.address.format"); } if (!isMacAddressUnique(this, address)) { throw new DomainException("error.not.unique.mac.address"); } super.setAddress(address); } @Override public void setPerson(Person person) { throw new UnsupportedOperationException("error.person.cannot.be.changed"); } public void edit(final MacAddressRegistryBean macAddressRegistryBean) { setAddress(macAddressRegistryBean.getAddress()); setIsActive(macAddressRegistryBean.getIsActive()); } public void delete() { super.setPerson(null); removeRootDomainObject(); deleteDomainObject(); } public static List getAllActiveMacAddresses() { final List result = new ArrayList(); for (final MacAddress macAddress : RootDomainObject.getInstance().getMacAddresses()) { if (macAddress.getIsActive() == Boolean.TRUE) { result.add(macAddress); } } return result; } public static MacAddress readByMacAddress(String address) { for (final MacAddress macAddress : RootDomainObject.getInstance().getMacAddresses()) { if (macAddress.getAddress().equals(address)) { return macAddress; } } return null; } }