1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.kuali.student.r2.common.entity; |
12 | |
|
13 | |
import java.util.Date; |
14 | |
|
15 | |
import javax.persistence.Column; |
16 | |
import javax.persistence.Embeddable; |
17 | |
import javax.persistence.MappedSuperclass; |
18 | |
import javax.persistence.Temporal; |
19 | |
import javax.persistence.TemporalType; |
20 | |
|
21 | |
import org.kuali.student.common.util.security.SecurityUtils; |
22 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
23 | |
import org.kuali.student.r2.common.dto.MetaInfo; |
24 | |
import org.kuali.student.r2.common.infc.HasMeta; |
25 | |
import org.kuali.student.r2.common.infc.Meta; |
26 | |
|
27 | |
@MappedSuperclass |
28 | |
@Embeddable |
29 | |
public abstract class MetaEntity extends BaseVersionEntity { |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
@Temporal(TemporalType.TIMESTAMP) |
35 | |
@Column(name="CREATETIME", updatable = false, nullable=false) |
36 | |
private Date createTime; |
37 | |
|
38 | |
@Column(name="CREATEID", updatable = false, nullable=false) |
39 | |
private String createId; |
40 | |
|
41 | |
@Temporal(TemporalType.TIMESTAMP) |
42 | |
@Column(name="UPDATETIME") |
43 | |
private Date updateTime; |
44 | |
|
45 | |
@Column(name="UPDATEID") |
46 | |
private String updateId; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | protected MetaEntity() { |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public MetaEntity(HasMeta hasMeta) { |
60 | 0 | if (null != hasMeta) { |
61 | 0 | Meta meta = hasMeta.getMeta(); |
62 | 0 | if (null != meta) { |
63 | 0 | this.setCreateTime(meta.getCreateTime()); |
64 | 0 | this.setCreateId(meta.getCreateId()); |
65 | 0 | this.setUpdateTime(meta.getUpdateTime()); |
66 | 0 | this.setUpdateId(meta.getUpdateId()); |
67 | 0 | this.setVersionNumber(null != meta.getVersionInd() ? Long.valueOf(meta.getVersionInd()) : null); |
68 | |
} |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
public void setEntityCreated(ContextInfo context) { |
74 | |
|
75 | 0 | if (context != null) { |
76 | 0 | this.setCreateTime(context.getCurrentDate()); |
77 | 0 | this.setCreateId(context.getPrincipalId()); |
78 | |
|
79 | 0 | setEntityUpdated(context); |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
public void setEntityUpdated (ContextInfo context) { |
84 | |
|
85 | 0 | if (context != null) { |
86 | 0 | this.setUpdateTime(context.getCurrentDate()); |
87 | 0 | this.setUpdateId(context.getPrincipalId()); |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
public Date getCreateTime() { |
92 | 0 | return createTime; |
93 | |
} |
94 | |
|
95 | |
public void setCreateTime(Date createTime) { |
96 | 0 | this.createTime = createTime; |
97 | 0 | } |
98 | |
|
99 | |
public String getCreateId() { |
100 | 0 | return createId; |
101 | |
} |
102 | |
|
103 | |
public void setCreateId(String createId) { |
104 | 0 | this.createId = createId; |
105 | 0 | } |
106 | |
|
107 | |
public Date getUpdateTime() { |
108 | 0 | return updateTime; |
109 | |
} |
110 | |
|
111 | |
public void setUpdateTime(Date updateTime) { |
112 | 0 | this.updateTime = updateTime; |
113 | 0 | } |
114 | |
|
115 | |
public String getUpdateId() { |
116 | 0 | return updateId; |
117 | |
} |
118 | |
|
119 | |
public void setUpdateId(String updateId) { |
120 | 0 | this.updateId = updateId; |
121 | 0 | } |
122 | |
|
123 | |
@Override |
124 | |
protected void onPrePersist() { |
125 | 0 | super.onPrePersist(); |
126 | 0 | if (createTime == null) { |
127 | 0 | setCreateTime(new Date()); |
128 | |
} |
129 | 0 | if (updateTime == null) { |
130 | 0 | setUpdateTime(new Date()); |
131 | |
} |
132 | 0 | if (createId == null) { |
133 | 0 | setCreateId(SecurityUtils.getCurrentUserId()); |
134 | |
} |
135 | 0 | if (updateId == null) { |
136 | 0 | setUpdateId(SecurityUtils.getCurrentUserId()); |
137 | |
} |
138 | |
|
139 | 0 | } |
140 | |
|
141 | |
@Override |
142 | |
protected void onPreUpdate() { |
143 | 0 | super.onPreUpdate(); |
144 | |
|
145 | |
|
146 | 0 | if (createTime == null) { |
147 | 0 | setCreateTime(new Date()); |
148 | |
} |
149 | 0 | if (updateTime == null) { |
150 | 0 | setUpdateTime(new Date()); |
151 | |
} |
152 | 0 | if (createId == null) { |
153 | 0 | setCreateId(SecurityUtils.getCurrentUserId()); |
154 | |
} |
155 | 0 | if (updateId == null) { |
156 | 0 | setUpdateId(SecurityUtils.getCurrentUserId()); |
157 | |
} |
158 | 0 | } |
159 | |
|
160 | |
public MetaInfo toDTO() { |
161 | 0 | MetaInfo miInfo = new MetaInfo(); |
162 | 0 | miInfo.setCreateId(getCreateId()); |
163 | 0 | miInfo.setCreateTime(getCreateTime()); |
164 | 0 | miInfo.setUpdateId(getUpdateId()); |
165 | 0 | miInfo.setUpdateTime(getUpdateTime()); |
166 | 0 | if (null != getVersionNumber()) { |
167 | 0 | miInfo.setVersionInd(new Long(getVersionNumber()).toString()); |
168 | |
} |
169 | 0 | return miInfo; |
170 | |
} |
171 | |
} |