From 7393a2d2e21241b397ca88149074ddec340755bb Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sun, 11 May 2014 13:40:26 +0200 Subject: [PATCH] =?UTF-8?q?B=C3=A1zov=C3=A9=20t=C5=99=C3=ADdy=20pro=20dato?= =?UTF-8?q?v=C3=BD=20model.=20B=C3=A1zovka=20pro=20servisy=20kde=20se=20na?= =?UTF-8?q?stavuje=20vlastn=C3=ADk=20z=C3=A1znamu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../info/bukova/isspst/data/BaseData.java | 94 +++++++++++++++++++ .../bukova/isspst/data/OwnedDataModel.java | 10 ++ .../isspst/services/AbstractOwnedService.java | 37 ++++++++ 3 files changed, 141 insertions(+) create mode 100644 src/main/java/info/bukova/isspst/data/BaseData.java create mode 100644 src/main/java/info/bukova/isspst/data/OwnedDataModel.java create mode 100644 src/main/java/info/bukova/isspst/services/AbstractOwnedService.java 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(); + } + +}