diff --git a/src/main/java/info/bukova/isspst/data/BaseData.java b/src/main/java/info/bukova/isspst/data/BaseData.java new file mode 100644 index 00000000..189ee627 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/BaseData.java @@ -0,0 +1,94 @@ +package info.bukova.isspst.data; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MappedSuperclass; +import javax.persistence.Transient; + +@MappedSuperclass +public abstract class BaseData implements OwnedDataModel { + + @Id + @Column(name="ID") + @GeneratedValue + private int id; + @Column(name="CREATED") + private Date created; + @Column(name="MODIFIED") + private Date modified; + @ManyToOne(fetch=FetchType.EAGER) + @JoinColumn(name="OWNED_BY_ID") + private User ownedBy; + @ManyToOne(fetch=FetchType.EAGER) + @JoinColumn(name="MODIFIED_BY_ID") + private User modifiedBy; + @Transient + private boolean valid; + + @Override + public void setId(int id) { + this.id = id; + } + + @Override + public int getId() { + return this.id; + } + + @Override + public void setCreated(Date created) { + this.created = created; + } + + @Override + public Date getCreated() { + return this.created; + } + + @Override + public void setModified(Date modified) { + this.modified = modified; + } + + @Override + public Date getModified() { + return this.modified; + } + + @Override + public boolean isValid() { + return this.valid; + } + + @Override + public void setValid(boolean valid) { + this.valid = valid; + } + + @Override + public void setOwnedBy(User user) { + this.ownedBy = user; + } + + @Override + public User getOwnedBy() { + return this.ownedBy; + } + + @Override + public void setModifiedBy(User user) { + this.modifiedBy = user; + } + + @Override + public User getModifiedBy() { + return this.modifiedBy; + } + +} diff --git a/src/main/java/info/bukova/isspst/data/OwnedDataModel.java b/src/main/java/info/bukova/isspst/data/OwnedDataModel.java new file mode 100644 index 00000000..99bd7100 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/OwnedDataModel.java @@ -0,0 +1,10 @@ +package info.bukova.isspst.data; + +public interface OwnedDataModel extends DataModel { + + public void setOwnedBy(User user); + public User getOwnedBy(); + public void setModifiedBy(User user); + public User getModifiedBy(); + +} diff --git a/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java b/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java new file mode 100644 index 00000000..9a6831cf --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java @@ -0,0 +1,37 @@ +package info.bukova.isspst.services; + +import java.util.Date; + +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.transaction.annotation.Transactional; + +import info.bukova.isspst.data.OwnedDataModel; +import info.bukova.isspst.data.User; + +public class AbstractOwnedService extends AbstractService { + + @Override + @Transactional + public void add(T entity) { + entity.setCreated(new Date()); + entity.setOwnedBy(getLoggedInUser()); + dao.add(entity); + } + + @Override + @Transactional + public void update(T entity) { + if (entity.getCreated() == null) { + add(entity); + } else { + entity.setModifiedBy(getLoggedInUser()); + entity.setModified(new Date()); + dao.modify(entity); + } + } + + protected User getLoggedInUser() { + return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + } + +}