001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/calendar/tags/sakai_2-4-1/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/DbCalendarService.java $
003: * $Id: DbCalendarService.java 8050 2006-04-20 17:39:55Z 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.calendar.impl;
021:
022: import java.util.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.sakaiproject.calendar.api.Calendar;
027: import org.sakaiproject.calendar.api.CalendarEdit;
028: import org.sakaiproject.calendar.api.CalendarEvent;
029: import org.sakaiproject.calendar.api.CalendarEventEdit;
030: import org.sakaiproject.db.api.SqlService;
031: import org.sakaiproject.util.BaseDbDoubleStorage;
032: import org.sakaiproject.util.StorageUser;
033:
034: /**
035: * <p>DbCalendarService fills out the BaseCalendarService with a database implementation.</p>
036: * <p>The sql scripts in src/sql/chef_calendar.sql must be run on the database.</p>
037: */
038: public class DbCalendarService extends BaseCalendarService {
039: /** Our logger. */
040: private static Log M_log = LogFactory
041: .getLog(DbCalendarService.class);
042:
043: /** The name of the db table holding calendar calendars. */
044: protected String m_cTableName = "CALENDAR_CALENDAR";
045:
046: /** The name of the db table holding calendar events. */
047: protected String m_rTableName = "CALENDAR_EVENT";
048:
049: /** If true, we do our locks in the remote database, otherwise we do them here. */
050: protected boolean m_locksInDb = true;
051:
052: protected static final String[] FIELDS = { "EVENT_START",
053: "EVENT_END" };
054:
055: /*******************************************************************************
056: * Constructors, Dependencies and their setter methods
057: *******************************************************************************/
058:
059: /** Dependency: SqlService */
060: protected SqlService m_sqlService = null;
061:
062: /**
063: * Dependency: SqlService.
064: * @param service The SqlService.
065: */
066: public void setSqlService(SqlService service) {
067: m_sqlService = service;
068: }
069:
070: /**
071: * Configuration: set the table name for the container.
072: * @param path The table name for the container.
073: */
074: public void setContainerTableName(String name) {
075: m_cTableName = name;
076: }
077:
078: /**
079: * Configuration: set the table name for the resource.
080: * @param path The table name for the resource.
081: */
082: public void setResourceTableName(String name) {
083: m_rTableName = name;
084: }
085:
086: /**
087: * Configuration: set the locks-in-db
088: * @param path The storage path.
089: */
090: public void setLocksInDb(String value) {
091: m_locksInDb = new Boolean(value).booleanValue();
092: }
093:
094: /** Configuration: to run the ddl on init or not. */
095: protected boolean m_autoDdl = false;
096:
097: /**
098: * Configuration: to run the ddl on init or not.
099: *
100: * @param value
101: * the auto ddl value.
102: */
103: public void setAutoDdl(String value) {
104: m_autoDdl = new Boolean(value).booleanValue();
105: }
106:
107: /*******************************************************************************
108: * Init and Destroy
109: *******************************************************************************/
110:
111: /**
112: * Final initialization, once all dependencies are set.
113: */
114: public void init() {
115: try {
116: // if we are auto-creating our schema, check and create
117: if (m_autoDdl) {
118: m_sqlService.ddl(this .getClass().getClassLoader(),
119: "sakai_calendar");
120: }
121:
122: super .init();
123:
124: M_log.info("init(): tables: " + m_cTableName + " "
125: + m_rTableName + " locks-in-db: " + m_locksInDb);
126: } catch (Throwable t) {
127: M_log.warn("init(): ", t);
128: }
129: }
130:
131: /*******************************************************************************
132: * BaseCalendarService extensions
133: *******************************************************************************/
134:
135: /**
136: * Construct a Storage object.
137: * @return The new storage object.
138: */
139: protected Storage newStorage() {
140: return new DbStorage(this );
141:
142: } // newStorage
143:
144: /*******************************************************************************
145: * Storage implementation
146: *******************************************************************************/
147:
148: /**
149: * Covers for the BaseDbStorage, providing Chat parameters
150: * Note: base class containers are reference based, this service is still id based - converted here %%%
151: */
152: protected class DbStorage extends BaseDbDoubleStorage implements
153: Storage {
154: /**
155: * Construct.
156: * @param user The StorageUser class to call back for creation of Resource and Edit objects.
157: */
158: public DbStorage(StorageUser user) {
159: // TODO: what about owner, draft?
160: super (m_cTableName, "CALENDAR_ID", m_rTableName,
161: "EVENT_ID", "CALENDAR_ID", "EVENT_START", /* owner, draft, pubview */
162: null, null, null, FIELDS, m_locksInDb, "calendar",
163: "event", user, m_sqlService);
164:
165: } // DbStorage
166:
167: /** Calendar **/
168:
169: public boolean checkCalendar(String ref) {
170: return super .getContainer(ref) != null;
171: }
172:
173: public Calendar getCalendar(String ref) {
174: return (Calendar) super .getContainer(ref);
175: }
176:
177: public List getCalendars() {
178: return super .getAllContainers();
179: }
180:
181: public CalendarEdit putCalendar(String ref) {
182: return (CalendarEdit) super .putContainer(ref);
183: }
184:
185: public CalendarEdit editCalendar(String ref) {
186: return (CalendarEdit) super .editContainer(ref);
187: }
188:
189: public void commitCalendar(CalendarEdit edit) {
190: super .commitContainer(edit);
191: }
192:
193: public void cancelCalendar(CalendarEdit edit) {
194: super .cancelContainer(edit);
195: }
196:
197: public void removeCalendar(CalendarEdit edit) {
198: super .removeContainer(edit);
199: }
200:
201: /** Event **/
202:
203: public boolean checkEvent(Calendar calendar, String messageId) {
204: return super .checkResource(calendar, messageId);
205: }
206:
207: public CalendarEvent getEvent(Calendar calendar, String id) {
208: return (CalendarEvent) super .getResource(calendar, id);
209: }
210:
211: public List getEvents(Calendar calendar) {
212: return super .getAllResources(calendar);
213: }
214:
215: public CalendarEventEdit putEvent(Calendar calendar, String id) {
216: return (CalendarEventEdit) super .putResource(calendar, id,
217: null);
218: }
219:
220: public CalendarEventEdit editEvent(Calendar calendar,
221: String messageId) {
222: return (CalendarEventEdit) super .editResource(calendar,
223: messageId);
224: }
225:
226: public void commitEvent(Calendar calendar,
227: CalendarEventEdit edit) {
228: super .commitResource(calendar, edit);
229: }
230:
231: public void cancelEvent(Calendar calendar,
232: CalendarEventEdit edit) {
233: super .cancelResource(calendar, edit);
234: }
235:
236: public void removeEvent(Calendar calendar,
237: CalendarEventEdit edit) {
238: super .removeResource(calendar, edit);
239: }
240:
241: } // DbStorage
242:
243: } // DbCachedCalendarService
|