001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.businessobject;
017    
018    import java.sql.Timestamp;
019    import java.util.Date;
020    import java.util.LinkedHashMap;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.ojb.broker.PersistenceBroker;
024    import org.apache.ojb.broker.PersistenceBrokerException;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kim.bo.Person;
027    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    
030    public abstract class TimestampedBusinessObjectBase extends PersistableBusinessObjectBase implements TimestampedBusinessObject {
031        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TimestampedBusinessObjectBase.class);
032    
033        private Timestamp lastUpdate;
034        private String lastUpdateUserId;
035    
036        /**
037         * @see org.kuali.kfs.sys.businessobject.TimestampedBusinessObject#getLastUpdate()
038         */
039        public Timestamp getLastUpdate() {
040            return this.lastUpdate;
041        }
042    
043        /**
044         * @see org.kuali.kfs.sys.businessobject.TimestampedBusinessObject#getLastUpdateUser()
045         */
046        public Person getLastUpdateUser() {
047            Person user = null;
048            if (StringUtils.isNotBlank(lastUpdateUserId)) {
049                user = SpringContext.getBean(org.kuali.rice.kim.service.PersonService.class).getPersonByPrincipalName(lastUpdateUserId);
050            }
051    
052            return user;
053        }
054    
055        /**
056         * @see org.kuali.kfs.sys.businessobject.TimestampedBusinessObject#getLastUpdateUserId()
057         */
058        public String getLastUpdateUserId() {
059            return this.lastUpdateUserId;
060        }
061    
062        /**
063         * @param lastUpdateUserId The lastUpdateUserId to set.
064         */
065        public void setLastUpdateUserId(String lastUpdateUserId) {
066            this.lastUpdateUserId = lastUpdateUserId;
067        }
068    
069        /**
070         * @param lastUpdate The lastUpdate to set.
071         */
072        public void setLastUpdate(Timestamp lastUpdate) {
073            this.lastUpdate = lastUpdate;
074        }
075    
076        public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException {
077            super.beforeInsert(broker);
078    
079            lastUpdate = new Timestamp((new Date()).getTime());
080            lastUpdateUserId = GlobalVariables.getUserSession().getPerson().getPrincipalName();
081        }
082    
083        public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException {
084            super.beforeUpdate(broker);
085    
086            lastUpdate = new Timestamp((new Date()).getTime());
087            lastUpdateUserId = GlobalVariables.getUserSession().getPerson().getPrincipalName();
088        }
089    }