001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/profile/tags/sakai_2-4-1/common-composite-component/src/java/org/sakaiproject/component/common/manager/PersistableHelper.java $
003: * $Id: PersistableHelper.java 8424 2006-04-27 20:23:44Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.common.manager;
021:
022: import java.util.Date;
023:
024: import org.apache.commons.beanutils.PropertyUtils;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.api.common.manager.Persistable;
028: import org.sakaiproject.tool.api.Session;
029: import org.sakaiproject.tool.api.SessionManager;
030:
031: /**
032: * @author <a href="mailto:lance@indiana.edu">Lance Speelmon</a>
033: */
034: public class PersistableHelper {
035: private static final String SYSTEM = "SYSTEM";
036:
037: private static final Log LOG = LogFactory
038: .getLog(PersistableHelper.class);
039:
040: private static final String LASTMODIFIEDDATE = "lastModifiedDate";
041:
042: private static final String LASTMODIFIEDBY = "lastModifiedBy";
043:
044: private static final String CREATEDDATE = "createdDate";
045:
046: private static final String CREATEDBY = "createdBy";
047:
048: private SessionManager sessionManager; // dep inj
049:
050: public void modifyPersistableFields(Persistable persistable) {
051: Date now = new Date(); // time sensitive
052: if (LOG.isDebugEnabled()) {
053: LOG.debug("modifyPersistableFields(Persistable "
054: + persistable + ")");
055: }
056: if (persistable == null)
057: throw new IllegalArgumentException(
058: "Illegal persistable argument passed!");
059:
060: try {
061: String actor = getActor();
062:
063: PropertyUtils.setProperty(persistable, LASTMODIFIEDBY,
064: actor);
065: PropertyUtils.setProperty(persistable, LASTMODIFIEDDATE,
066: now);
067: } catch (Exception e) {
068: LOG.error(e);
069: throw new Error(e);
070: }
071: }
072:
073: public void createPersistableFields(Persistable persistable) {
074: Date now = new Date(); // time sensitive
075: if (LOG.isDebugEnabled()) {
076: LOG.debug("modifyPersistableFields(Persistable "
077: + persistable + ")");
078: }
079: if (persistable == null)
080: throw new IllegalArgumentException(
081: "Illegal persistable argument passed!");
082:
083: try {
084: String actor = getActor();
085:
086: PropertyUtils.setProperty(persistable, LASTMODIFIEDBY,
087: actor);
088: PropertyUtils.setProperty(persistable, LASTMODIFIEDDATE,
089: now);
090: PropertyUtils.setProperty(persistable, CREATEDBY, actor);
091: PropertyUtils.setProperty(persistable, CREATEDDATE, now);
092: } catch (Exception e) {
093: LOG.error(e);
094: throw new Error(e);
095: }
096: }
097:
098: private String getActor() {
099: LOG.debug("getActor()");
100:
101: String actor = null;
102: Session session = sessionManager.getCurrentSession();
103: if (session != null) {
104: actor = session.getUserId();
105: } else {
106: return SYSTEM;
107: }
108: if (actor == null || actor.length() < 1) {
109: return SYSTEM;
110: } else {
111: return actor;
112: }
113: }
114:
115: /**
116: * Dependency injection.
117: *
118: * @param sessionManager
119: * The sessionManager to set.
120: */
121: public void setSessionManager(SessionManager sessionManager) {
122: if (LOG.isDebugEnabled()) {
123: LOG.debug("setSessionManager(SessionManager "
124: + sessionManager + ")");
125: }
126:
127: this.sessionManager = sessionManager;
128: }
129: }
|