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