/*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2022 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*/
package org.mxchange.jaddressbook.model.addressbook;
+import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
+import org.apache.commons.lang3.StringUtils;
import org.mxchange.jaddressbook.model.addressbook.status.AddressbokStatus;
+import org.mxchange.jcoreutils.comparable.ComparableUtils;
+import org.mxchange.jcoreutils.enums.EnumUtils;
import org.mxchange.jusercore.model.user.LoginUser;
import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.utils.UserUtils;
/**
- * A POJO for user addressbooks
+ * A POJO for user address books
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
@Table (name = "addressbooks")
@NamedQueries (
{
+ @NamedQuery (name = "AllAddressbooks", query = "SELECT a FROM addressbooks AS a ORDER BY a.addressbookId ASC"),
@NamedQuery (name = "AllUsersAddressbooks", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :param ORDER BY a.addressbookId ASC"),
- @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)"),
- @NamedQuery (name = "SearchAddressbookById", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookId = :id")
+ @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)")
}
)
@SuppressWarnings ("PersistenceUnitPresent")
*/
@Basic (optional = false)
@Temporal (value = TemporalType.TIMESTAMP)
- @Column (name = "addressbook_created", nullable = false, updatable = false)
- private Date addressbookCreated;
+ @Column (name = "addressbook_entry_created", updatable = false, nullable = false)
+ private Date addressbookEntryCreated;
+
+ /**
+ * When this address book has been created
+ */
+ @Temporal (value = TemporalType.TIMESTAMP)
+ @Column (name = "addressbook_entry_updated", insertable = false, nullable = false)
+ private Date addressbookEntryUpdated;
/**
* Id number
private String addressbookName;
/**
- * Adress book status
+ * Address book status
*/
@Basic (optional = false)
@Enumerated (EnumType.STRING)
* @param addressbookUser User instance
*/
public UserAddressbook (final String addressbookName, final User addressbookUser) {
- // Call default constructor
+ // Invoke default constructor
this();
+ // Check all parameters
+ if (null == addressbookName) {
+ // Throw NPE
+ throw new NullPointerException("Parameter 'addressbookName' is null"); //NOI18N
+ } else if (addressbookName.isEmpty()) {
+ // Throw IAE
+ throw new IllegalArgumentException("Parameter 'addressbookName' is empty"); //NOI18N
+ } else if (null == addressbookUser) {
+ // Throw NPE again
+ throw new NullPointerException("Parameter 'addressbookUser' is null"); //NOI18N
+ } else if (addressbookUser.getUserId() == null) {
+ // Throw it again
+ throw new NullPointerException("addressbookUser.userId is null"); //NOI18N
+ } else if (addressbookUser.getUserId() < 1) {
+ // Throw IAE again
+ throw new IllegalArgumentException(MessageFormat.format("addressbookUser.userId={0} is invalid", addressbookUser.getUserId())); //NOI18N
+ }
+
// Set name and user instance
this.addressbookName = addressbookName;
this.addressbookUser = addressbookUser;
* Default constructor
*/
public UserAddressbook () {
- // Set status to UNLOCKED
- this.addressbookStatus = AddressbokStatus.UNLOCKED;
+ }
+
+ @Override
+ public int compareTo (final Addressbook addressbook) {
+ // Check parameter and return 0 if equal
+ if (null == addressbook) {
+ // Should not happen
+ throw new NullPointerException("addressbook is null"); //NOI18N
+ } else if (addressbook.equals(this)) {
+ // Same object, below "expensive" code can be safely skipped
+ return 0;
+ }
+
+ // All comparators
+ final int comparators[] = {
+ // First name ...
+ StringUtils.compare(this.getAddressbookName(), addressbook.getAddressbookName()),
+ // ... status
+ EnumUtils.compare(this.getAddressbookStatus(), addressbook.getAddressbookStatus()),
+ // .. then user
+ UserUtils.compare(this.getAddressbookUser(), addressbook.getAddressbookUser())
+ };
+
+ // Check all values
+ final int comparison = ComparableUtils.checkAll(comparators);
+
+ // Return value
+ return comparison;
}
@Override
return false;
}
- final Addressbook other = (Addressbook) object;
+ final Addressbook addressbook = (Addressbook) object;
- if (!Objects.equals(this.getAddressbookName(), other.getAddressbookName())) {
+ if (!Objects.equals(this.getAddressbookId(), addressbook.getAddressbookId())) {
+ return false;
+ } else if (!Objects.equals(this.getAddressbookLastLocked(), addressbook.getAddressbookLastLocked())) {
return false;
- } else if (!Objects.equals(this.getAddressbookUser(), other.getAddressbookUser())) {
+ } else if (!Objects.equals(this.getAddressbookLastLockedReason(), addressbook.getAddressbookLastLockedReason())) {
+ return false;
+ } else if (!Objects.equals(this.getAddressbookName(), addressbook.getAddressbookName())) {
+ return false;
+ } else if (!Objects.equals(this.getAddressbookStatus(), addressbook.getAddressbookStatus())) {
+ return false;
+ } else if (!Objects.equals(this.getAddressbookUser(), addressbook.getAddressbookUser())) {
return false;
}
}
@Override
- public int hashCode () {
- int hash = 7;
- hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
- hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());
- return hash;
+ @SuppressWarnings ("ReturnOfDateField")
+ public Date getAddressbookEntryCreated () {
+ return this.addressbookEntryCreated;
}
@Override
- public Date getAddressbookCreated () {
- return this.addressbookCreated;
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+ public void setAddressbookEntryCreated (final Date addressbookEntryCreated) {
+ this.addressbookEntryCreated = addressbookEntryCreated;
}
@Override
- public void setAddressbookCreated (final Date addressbookCreated) {
- this.addressbookCreated = addressbookCreated;
+ @SuppressWarnings ("ReturnOfDateField")
+ public Date getAddressbookEntryUpdated () {
+ return this.addressbookEntryUpdated;
+ }
+
+ @Override
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+ public void setAddressbookEntryUpdated (final Date addressbookEntryUpdated) {
+ this.addressbookEntryUpdated = addressbookEntryUpdated;
}
@Override
}
@Override
+ @SuppressWarnings ("ReturnOfDateField")
public Date getAddressbookLastLocked () {
return this.addressbookLastLocked;
}
@Override
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
public void setAddressbookLastLocked (final Date addressbookLastLocked) {
this.addressbookLastLocked = addressbookLastLocked;
}
this.addressbookUser = addressbookUser;
}
+ @Override
+ public int hashCode () {
+ int hash = 7;
+
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookId());
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLocked());
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLockedReason());
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookStatus());
+ hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());
+
+ return hash;
+ }
+
}