Commit 1805b003 authored by Matija Obreza's avatar Matija Obreza

BasicModel implements Persistable

- Updated equals() code for entities: NULL id means objects are not equal
parent 73f5ecb7
......@@ -786,7 +786,7 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
@Override
public Boolean isTransient(final Object entity) {
if (entity instanceof BasicModel) {
return !((BasicModel) entity).isPersisted();
return ((BasicModel) entity).isNew();
}
// TODO Use Spring field access methods?
try {
......
......@@ -122,7 +122,7 @@ public class AuditTrailServiceTest extends ServiceTest {
public void testTransactionalListQuery() {
ExampleAuditedEntity entity = exampleAuditedEntityService.testList();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.isPersisted(), is(true));
assertThat(entity.isNew(), is(false));
assertThat(listAuditLogs(entity), hasSize(1));
entity = exampleAuditedEntityService.get(entity.getId());
......@@ -140,7 +140,7 @@ public class AuditTrailServiceTest extends ServiceTest {
public void testDelete() {
ExampleAuditedEntity entity = exampleAuditedEntityService.testList();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.isPersisted(), is(true));
assertThat(entity.isNew(), is(false));
assertThat(entity.getName(), is("Test 3"));
assertThat(listAuditLogs(entity), hasSize(1));
......
......@@ -22,6 +22,8 @@ import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import org.springframework.data.domain.Persistable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
......@@ -29,7 +31,7 @@ import com.fasterxml.jackson.annotation.JsonView;
* The Class BasicModel.
*/
@MappedSuperclass
public class BasicModel extends EmptyModel {
public class BasicModel extends EmptyModel implements Persistable<Long> {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 2709998920148999956L;
......@@ -39,7 +41,7 @@ public class BasicModel extends EmptyModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
private Long id = InMemoryIdGenerator.nextId();
private Long id = null; // InMemoryIdGenerator.nextId();
/*
* (non-Javadoc)
......@@ -68,8 +70,8 @@ public class BasicModel extends EmptyModel {
*/
@JsonIgnore
@Transient
public final boolean isPersisted() {
return (id != null) && (id.longValue() > 0);
public final boolean isNew() {
return (id == null) || (id.longValue() < 0);
}
/*
......@@ -100,10 +102,8 @@ public class BasicModel extends EmptyModel {
return false;
}
final BasicModel other = (BasicModel) obj;
if (id == null) {
if (other.id != null) {
return false;
}
if (id == null || other.id == null) {
return false;
} else if (!id.equals(other.id)) {
return false;
}
......
......@@ -103,8 +103,9 @@ public abstract class VersionedModel extends BasicModel implements Activatable {
if (getClass() != obj.getClass())
return false;
VersionedModel other = (VersionedModel) obj;
if (active != other.active)
return false;
// We're only interested in id+version combination
// if (active != other.active)
// return false;
if (version == null) {
if (other.version != null)
return false;
......
......@@ -41,7 +41,7 @@ public class UuidModelTest extends ServiceTest {
@Test
public void testConstructor() {
UuidEntity entity = new UuidEntity();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.getId(), nullValue());
assertThat(entity.getUuid(), is(nullValue()));
assertThat(entity.getVersion(), nullValue());
}
......
......@@ -39,7 +39,7 @@ public class VersionedModelTest extends ServiceTest {
@Test
public void testConstructor() {
VersionedEntity entity = new VersionedEntity();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.getId(), nullValue());
assertThat(entity.getVersion(), nullValue());
assertThat(entity.isActive(), is(true));
}
......
......@@ -168,7 +168,7 @@ public class CustomAclServiceImpl implements CustomAclService {
final AclSid ownerSid = SecurityContextUtil.getCurrentUser();
if (ownerSid == null) {
LOG.warn("No SID in security context, not assigning creator permissions");
} else if (!ownerSid.isPersisted()) {
} else if (ownerSid.isNew()) {
LOG.warn("Owner SID not persisted, not assigning creator permissions");
} else {
objectIdentity.setOwnerSid(ownerSid);
......@@ -202,7 +202,7 @@ public class CustomAclServiceImpl implements CustomAclService {
if (objectIdentity.getOwnerSid() == null) {
final AclSid ownerSid = SecurityContextUtil.getCurrentUser();
if (ownerSid != null && ownerSid.isPersisted()) {
if (ownerSid != null && ! ownerSid.isNew()) {
objectIdentity.setOwnerSid(ownerSid);
// Grant permissions to owner
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment