Bázové třídy pro datový model. Bázovka pro servisy kde se nastavuje

vlastník záznamu.
multitenant
Josef Rokos 11 years ago
parent 6462fb3806
commit 7393a2d2e2

@ -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;
}
}

@ -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();
}

@ -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<T extends OwnedDataModel> extends AbstractService<T> {
@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();
}
}
Loading…
Cancel
Save