001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/user/tags/sakai_2-4-1/user-impl/impl/src/java/org/sakaiproject/user/impl/DbPreferencesService.java $
003: * $Id: DbPreferencesService.java 7318 2006-04-01 20:01:20Z 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.user.impl;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.db.api.SqlService;
025: import org.sakaiproject.user.api.Preferences;
026: import org.sakaiproject.user.api.PreferencesEdit;
027: import org.sakaiproject.util.BaseDbSingleStorage;
028: import org.sakaiproject.util.StorageUser;
029:
030: /**
031: * <p>
032: * DbPreferencesService is an extension of the BasePreferencesService with database storage.
033: * </p>
034: */
035: public abstract class DbPreferencesService extends
036: BasePreferencesService {
037: /** Our log (commons). */
038: private static Log M_log = LogFactory
039: .getLog(DbPreferencesService.class);
040:
041: /** Table name for realms. */
042: protected String m_tableName = "SAKAI_PREFERENCES";
043:
044: /** If true, we do our locks in the remote database, otherwise we do them here. */
045: protected boolean m_locksInDb = true;
046:
047: /**********************************************************************************************************************************************************************************************************************************************************
048: * Dependencies
049: *********************************************************************************************************************************************************************************************************************************************************/
050:
051: /**
052: * @return the MemoryService collaborator.
053: */
054: protected abstract SqlService sqlService();
055:
056: /**********************************************************************************************************************************************************************************************************************************************************
057: * Configuration
058: *********************************************************************************************************************************************************************************************************************************************************/
059:
060: /**
061: * Configuration: set the table name
062: *
063: * @param path
064: * The table name.
065: */
066: public void setTableName(String name) {
067: m_tableName = name;
068: }
069:
070: /**
071: * Configuration: set the locks-in-db
072: *
073: * @param value
074: * The locks-in-db value.
075: */
076: public void setLocksInDb(String value) {
077: m_locksInDb = new Boolean(value).booleanValue();
078: }
079:
080: /** Configuration: to run the ddl on init or not. */
081: protected boolean m_autoDdl = false;
082:
083: /**
084: * Configuration: to run the ddl on init or not.
085: *
086: * @param value
087: * the auto ddl value.
088: */
089: public void setAutoDdl(String value) {
090: m_autoDdl = new Boolean(value).booleanValue();
091: }
092:
093: /**********************************************************************************************************************************************************************************************************************************************************
094: * Init and Destroy
095: *********************************************************************************************************************************************************************************************************************************************************/
096:
097: /**
098: * Final initialization, once all dependencies are set.
099: */
100: public void init() {
101: try {
102: // if we are auto-creating our schema, check and create
103: if (m_autoDdl) {
104: sqlService().ddl(this .getClass().getClassLoader(),
105: "sakai_preferences");
106: }
107:
108: super .init();
109:
110: M_log.info("init(): table: " + m_tableName
111: + " locks-in-db: " + m_locksInDb);
112: } catch (Throwable t) {
113: M_log.warn("init(): ", t);
114: }
115: }
116:
117: /**********************************************************************************************************************************************************************************************************************************************************
118: * BasePreferencesService extensions
119: *********************************************************************************************************************************************************************************************************************************************************/
120:
121: /**
122: * Construct a Storage object.
123: *
124: * @return The new storage object.
125: */
126: protected Storage newStorage() {
127: return new DbStorage(this );
128: }
129:
130: /**********************************************************************************************************************************************************************************************************************************************************
131: * Storage implementation
132: *********************************************************************************************************************************************************************************************************************************************************/
133:
134: /**
135: * Covers for the BaseXmlFileStorage, providing Preferences and PreferencesEdit parameters
136: */
137: protected class DbStorage extends BaseDbSingleStorage implements
138: Storage {
139: /**
140: * Construct.
141: *
142: * @param realm
143: * The StorageUser class to call back for creation of Resource and Edit objects.
144: */
145: public DbStorage(StorageUser user) {
146: super (m_tableName, "PREFERENCES_ID", null, m_locksInDb,
147: "preferences", user, sqlService());
148: }
149:
150: public boolean check(String id) {
151: return super .checkResource(id);
152: }
153:
154: public Preferences get(String id) {
155: return (Preferences) super .getResource(id);
156: }
157:
158: public PreferencesEdit put(String id) {
159: return (PreferencesEdit) super .putResource(id, null);
160: }
161:
162: public PreferencesEdit edit(String id) {
163: return (PreferencesEdit) super .editResource(id);
164: }
165:
166: public void commit(PreferencesEdit edit) {
167: super .commitResource(edit);
168: }
169:
170: public void cancel(PreferencesEdit edit) {
171: super .cancelResource(edit);
172: }
173:
174: public void remove(PreferencesEdit edit) {
175: super.removeResource(edit);
176: }
177: }
178: }
|