001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/event/tags/sakai_2-4-1/event-impl/impl/src/java/org/sakaiproject/event/impl/DbNotificationService.java $
003: * $Id: DbNotificationService.java 7319 2006-04-01 20:02:05Z 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.event.impl;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.db.api.SqlService;
029: import org.sakaiproject.event.api.Notification;
030: import org.sakaiproject.event.api.NotificationEdit;
031: import org.sakaiproject.util.BaseDbSingleStorage;
032: import org.sakaiproject.util.StorageUser;
033:
034: /**
035: * <p>
036: * DbNotificationService is ... %%%.
037: * </p>
038: */
039: public abstract class DbNotificationService extends
040: BaseNotificationService {
041: /** Our logger. */
042: private static Log M_log = LogFactory
043: .getLog(DbNotificationService.class);
044:
045: /** Table name for users. */
046: protected String m_tableName = "SAKAI_NOTIFICATION";
047:
048: /** If true, we do our locks in the remote database, otherwise we do them here. */
049: protected boolean m_locksInDb = true;
050:
051: /**********************************************************************************************************************************************************************************************************************************************************
052: * Dependencies
053: *********************************************************************************************************************************************************************************************************************************************************/
054:
055: /**
056: * @return the MemoryService collaborator.
057: */
058: protected abstract SqlService sqlService();
059:
060: /**********************************************************************************************************************************************************************************************************************************************************
061: * Configuration
062: *********************************************************************************************************************************************************************************************************************************************************/
063:
064: /**
065: * Configuration: set the table name
066: *
067: * @param path
068: * The table name.
069: */
070: public void setTableName(String name) {
071: m_tableName = name;
072: }
073:
074: /**
075: * Configuration: set the locks-in-db
076: *
077: * @param value
078: * The locks-in-db value.
079: */
080: public void setLocksInDb(String value) {
081: m_locksInDb = new Boolean(value).booleanValue();
082: }
083:
084: /** Configuration: to run the ddl on init or not. */
085: protected boolean m_autoDdl = false;
086:
087: /**
088: * Configuration: to run the ddl on init or not.
089: *
090: * @param value
091: * the auto ddl value.
092: */
093: public void setAutoDdl(String value) {
094: m_autoDdl = new Boolean(value).booleanValue();
095: }
096:
097: /**********************************************************************************************************************************************************************************************************************************************************
098: * Init and Destroy
099: *********************************************************************************************************************************************************************************************************************************************************/
100:
101: /**
102: * Final initialization, once all dependencies are set.
103: */
104: public void init() {
105: try {
106: // if we are auto-creating our schema, check and create
107: if (m_autoDdl) {
108: sqlService().ddl(this .getClass().getClassLoader(),
109: "sakai_notification");
110: }
111:
112: super .init();
113:
114: M_log.info(".init(): table: " + m_tableName
115: + " locks-in-db: " + m_locksInDb);
116: } catch (Throwable t) {
117: M_log.warn("init(): ", t);
118: }
119: }
120:
121: /**********************************************************************************************************************************************************************************************************************************************************
122: * BaseNotificationService extensions
123: *********************************************************************************************************************************************************************************************************************************************************/
124:
125: /**
126: * Construct a Storage object.
127: *
128: * @return The new storage object.
129: */
130: protected Storage newStorage() {
131: return new DbStorage(this );
132: }
133:
134: /**********************************************************************************************************************************************************************************************************************************************************
135: * Storage implementation
136: *********************************************************************************************************************************************************************************************************************************************************/
137:
138: protected class DbStorage extends BaseDbSingleStorage implements
139: Storage {
140: /**
141: * Construct.
142: *
143: * @param user
144: * The StorageUser class to call back for creation of Resource and Edit objects.
145: */
146: public DbStorage(StorageUser user) {
147: super (m_tableName, "NOTIFICATION_ID", null, m_locksInDb,
148: "notification", user, sqlService());
149: }
150:
151: public boolean check(String id) {
152: return super .checkResource(id);
153: }
154:
155: public Notification get(String id) {
156: return (Notification) super .getResource(id);
157: }
158:
159: public List getAll() {
160: return super .getAllResources();
161: }
162:
163: /**
164: * Get a Set of all the notifications that are interested in this Event function. Note: instead of this looking, we could have an additional "key" in storage of the event... -ggolen
165: *
166: * @param function
167: * The Event function
168: * @return The Set (Notification) of all the notifications that are interested in this Event function.
169: */
170: public List getAll(String function) {
171: List rv = new Vector();
172: if (function == null)
173: return rv;
174:
175: List all = super .getAllResources();
176: for (Iterator it = all.iterator(); it.hasNext();) {
177: Notification notification = (Notification) it.next();
178: if (notification.containsFunction(function)) {
179: rv.add(notification);
180: }
181: }
182:
183: return rv;
184: }
185:
186: public NotificationEdit put(String id) {
187: return (NotificationEdit) super .putResource(id, null);
188: }
189:
190: public NotificationEdit edit(String id) {
191: return (NotificationEdit) super .editResource(id);
192: }
193:
194: public void commit(NotificationEdit edit) {
195: super .commitResource(edit);
196: }
197:
198: public void cancel(NotificationEdit edit) {
199: super .cancelResource(edit);
200: }
201:
202: public void remove(NotificationEdit edit) {
203: super.removeResource(edit);
204: }
205: }
206: }
|