001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the Educational Community
003     * License, Version 2.0 (the "License"); you may not use this file except in
004     * compliance with the License. You may obtain a copy of the License at
005     * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or
006     * agreed to in writing, software distributed under the License is distributed
007     * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
008     * express or implied. See the License for the specific language governing
009     * permissions and limitations under the License.
010     */
011    package org.kuali.student.r2.common.entity;
012    
013    import java.util.Date;
014    
015    import javax.persistence.Column;
016    import javax.persistence.Embeddable;
017    import javax.persistence.MappedSuperclass;
018    import javax.persistence.Temporal;
019    import javax.persistence.TemporalType;
020    
021    import org.kuali.student.common.util.security.SecurityUtils;
022    import org.kuali.student.r2.common.dto.ContextInfo;
023    import org.kuali.student.r2.common.dto.MetaInfo;
024    import org.kuali.student.r2.common.infc.HasMeta;
025    import org.kuali.student.r2.common.infc.Meta;
026    
027    @MappedSuperclass
028    @Embeddable
029    public abstract class MetaEntity extends BaseVersionEntity {
030    
031        // Hibernate will not allow @Version in @Embeddable for some annoying reason
032        // @Version
033        // private long versionInd;
034        @Temporal(TemporalType.TIMESTAMP)
035        @Column(name="CREATETIME", updatable = false, nullable=false)
036        private Date createTime;
037    
038        @Column(name="CREATEID", updatable = false, nullable=false)
039        private String createId;
040    
041        @Temporal(TemporalType.TIMESTAMP)
042        @Column(name="UPDATETIME")
043        private Date updateTime;
044    
045        @Column(name="UPDATEID")
046        private String updateId;
047    
048        // public long getVersionInd() {
049        // return versionInd;
050        // }
051        // public void setVersionInd(long versionInd) {
052        // this.versionInd = versionInd;
053        // }
054        protected MetaEntity() {
055        }
056    
057        // TODO - need a BaseEntity(HasMeta) to deal w/ version, id, and other
058        // fields
059        public MetaEntity(HasMeta hasMeta) {
060            if (null != hasMeta) {
061                Meta meta = hasMeta.getMeta();
062                if (null != meta) {
063                    this.setCreateTime(meta.getCreateTime());
064                    this.setCreateId(meta.getCreateId());
065                    this.setUpdateTime(meta.getUpdateTime());
066                    this.setUpdateId(meta.getUpdateId());
067                    this.setVersionNumber(null != meta.getVersionInd() ? Long.valueOf(meta.getVersionInd()) : null);
068                }
069            }
070        }
071    
072    
073    
074        public void setEntityCreated(ContextInfo context) {
075    
076            if (context != null) {
077                    this.setCreateTime(context.getCurrentDate());
078                    this.setCreateId(context.getPrincipalId());
079    
080                    setEntityUpdated(context);
081            }
082        }
083    
084        public void setEntityUpdated (ContextInfo context) {
085    
086            if (context != null) {
087                    this.setUpdateTime(context.getCurrentDate());
088                    this.setUpdateId(context.getPrincipalId());
089            }
090        }
091        public Date getCreateTime() {
092            return createTime;
093        }
094    
095        public void setCreateTime(Date createTime) {
096            this.createTime = createTime;
097        }
098    
099        public String getCreateId() {
100            return createId;
101        }
102    
103        public void setCreateId(String createId) {
104            this.createId = createId;
105        }
106    
107        public Date getUpdateTime() {
108            return updateTime;
109        }
110    
111        public void setUpdateTime(Date updateTime) {
112            this.updateTime = updateTime;
113        }
114    
115        public String getUpdateId() {
116            return updateId;
117        }
118    
119        public void setUpdateId(String updateId) {
120            this.updateId = updateId;
121        }
122    
123        @Override
124        protected void onPrePersist() {
125            super.onPrePersist();
126            if (createTime == null) {
127                setCreateTime(new Date());
128            }
129            if (updateTime == null) {
130                setUpdateTime(new Date());
131            }
132            if (createId == null) {
133                setCreateId(SecurityUtils.getCurrentUserId());
134            }
135            if (updateId == null) {
136                setUpdateId(SecurityUtils.getCurrentUserId());
137            }
138    
139        }
140    
141        @Override
142        protected void onPreUpdate() {
143            super.onPreUpdate();
144            // This code should not be here, but hibernate is calling update
145            // callback instead of prepersit if the id is not null.        
146            if (createTime == null) {
147                setCreateTime(new Date());
148            }
149            if (updateTime == null) {
150                setUpdateTime(new Date());
151            }
152            if (createId == null) {
153                setCreateId(SecurityUtils.getCurrentUserId());
154            }
155            if (updateId == null) {
156                setUpdateId(SecurityUtils.getCurrentUserId());
157            }
158        }
159    
160        public MetaInfo toDTO() {
161            MetaInfo miInfo = new MetaInfo();
162            miInfo.setCreateId(getCreateId());
163            miInfo.setCreateTime(getCreateTime());
164            miInfo.setUpdateId(getUpdateId());
165            miInfo.setUpdateTime(getUpdateTime());
166            if (null != getVersionNumber()) {
167                miInfo.setVersionInd(new Long(getVersionNumber()).toString());
168            }
169            return miInfo;
170        }
171    }