001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/citations/tags/sakai_2-4-1/citations-impl/impl/src/java/org/sakaiproject/citation/impl/BasicCitationService.java $
003: * $Id: BasicCitationService.java 22624 2007-03-14 20:17:21Z jimeng@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.citation.impl;
021:
022: import java.util.Collection;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.Stack;
029: import java.util.Vector;
030:
031: import org.sakaiproject.citation.api.Citation;
032: import org.sakaiproject.citation.api.CitationCollection;
033: import org.sakaiproject.citation.api.Schema;
034: import org.sakaiproject.citation.impl.BaseCitationService.BasicCitationCollection;
035: import org.sakaiproject.entity.api.Entity;
036: import org.sakaiproject.entity.api.HttpAccess;
037: import org.sakaiproject.entity.api.Reference;
038: import org.sakaiproject.entity.api.ResourceProperties;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041:
042: /**
043: * This is a test class to implement a concrete CitationService using the Storage interface to define the
044: * interactions that will be needed when we do a DB implementation. This version uses hashtables to
045: * store the java objects in memory, rather than serializing them and putting them in a persistent store.
046: */
047: public class BasicCitationService extends BaseCitationService {
048: public class BasicCitationStorage implements Storage {
049: protected Map m_collections;
050: protected Map m_citations;
051: protected Map m_schemas;
052:
053: /**
054: */
055: public BasicCitationStorage() {
056: super ();
057: m_collections = new Hashtable();
058: m_citations = new Hashtable();
059: m_schemas = new Hashtable();
060: }
061:
062: /* (non-Javadoc)
063: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#open()
064: */
065: public void open() {
066: }
067:
068: /* (non-Javadoc)
069: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#close()
070: */
071: public void close() {
072: m_collections.clear();
073: m_collections = null;
074:
075: m_citations.clear();
076: m_citations = null;
077:
078: m_schemas.clear();
079: m_schemas = null;
080:
081: }
082:
083: /* (non-Javadoc)
084: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getCitation(java.lang.String)
085: */
086: public Citation getCitation(String citationId) {
087: return (Citation) m_citations.get(citationId);
088: }
089:
090: /* (non-Javadoc)
091: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getCollection(java.lang.String)
092: */
093: public CitationCollection getCollection(String collectionId) {
094: return (CitationCollection) m_collections.get(collectionId);
095: }
096:
097: /* (non-Javadoc)
098: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchema(java.lang.String)
099: */
100: public Schema getSchema(String schemaId) {
101: return (Schema) m_schemas.get(schemaId);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchemaNames()
106: */
107: public Collection getSchemaNames() {
108: return m_schemas.keySet();
109: }
110:
111: /* (non-Javadoc)
112: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchemas()
113: */
114: public List getSchemas() {
115: List list = new Vector();
116: Iterator it = m_schemas.keySet().iterator();
117: while (it.hasNext()) {
118: String key = (String) it.next();
119: Schema schema = (Schema) m_schemas.get(key);
120: {
121: list.add(new BasicSchema(schema));
122: }
123: }
124: return list;
125: }
126:
127: /* (non-Javadoc)
128: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putSchema(org.sakaiproject.citation.api.Schema)
129: */
130: public Schema addSchema(Schema schema) {
131: m_schemas.put(schema.getIdentifier(), schema);
132: return schema;
133: }
134:
135: /* (non-Javadoc)
136: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putSchemas(java.util.Collection)
137: */
138: public void putSchemas(Collection schemas) {
139: Iterator it = schemas.iterator();
140: while (it.hasNext()) {
141: Object obj = it.next();
142: if (obj instanceof Schema) {
143: m_schemas.put(((Schema) obj).getIdentifier(), obj);
144: }
145: }
146:
147: }
148:
149: /* (non-Javadoc)
150: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkCitation(java.lang.String)
151: */
152: public boolean checkCitation(String citationId) {
153: return this .m_citations.containsKey(citationId);
154: }
155:
156: /* (non-Javadoc)
157: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkCollection(java.lang.String)
158: */
159: public boolean checkCollection(String collectionId) {
160: return this .m_collections.containsKey(collectionId);
161: }
162:
163: /* (non-Javadoc)
164: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkSchema(java.lang.String)
165: */
166: public boolean checkSchema(String schemaId) {
167: return this .m_schemas.containsKey(schemaId);
168: }
169:
170: /* (non-Javadoc)
171: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#listSchemas()
172: */
173: public List listSchemas() {
174: Set names = this .m_schemas.keySet();
175:
176: return new Vector(names);
177: }
178:
179: /* (non-Javadoc)
180: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putCollection(java.util.Map, java.util.List)
181: */
182: public CitationCollection addCollection(Map attributes,
183: List citations) {
184: // need to create a collection (referred to below as "edit")
185: BasicCitationCollection edit = new BasicCitationCollection(
186: attributes, citations);
187:
188: this .m_collections.put(edit.getId(), edit);
189:
190: return edit;
191: }
192:
193: /* (non-Javadoc)
194: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeCitation(org.sakaiproject.citation.api.CitationEdit)
195: */
196: public void removeCitation(Citation edit) {
197: this .m_citations.remove(edit.getId());
198: }
199:
200: /* (non-Javadoc)
201: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeCollection(org.sakaiproject.citation.api.CitationCollectionEdit)
202: */
203: public void removeCollection(CitationCollection edit) {
204: this .m_collections.remove(edit.getId());
205: }
206:
207: /* (non-Javadoc)
208: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#updateSchema(org.sakaiproject.citation.api.Schema)
209: */
210: public void updateSchema(Schema schema) {
211: this .m_schemas.put(schema.getIdentifier(), schema);
212: }
213:
214: /* (non-Javadoc)
215: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#updateSchemas(java.util.Collection)
216: */
217: public void updateSchemas(Collection schemas) {
218: Iterator it = schemas.iterator();
219: while (it.hasNext()) {
220: Schema schema = (Schema) it.next();
221: this .m_schemas.put(schema.getIdentifier(), schema);
222: }
223: }
224:
225: /* (non-Javadoc)
226: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#saveCitation(org.sakaiproject.citation.api.Citation)
227: */
228: public void saveCitation(Citation edit) {
229: this .m_citations.put(edit.getId(), edit);
230: }
231:
232: /* (non-Javadoc)
233: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#saveCollection(java.util.Collection)
234: */
235: public void saveCollection(CitationCollection collection) {
236: this .m_collections.put(collection.getId(), collection);
237: }
238:
239: /* (non-Javadoc)
240: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#addCitation(java.lang.String)
241: */
242: public Citation addCitation(String mediatype) {
243: BasicCitation citation = new BasicCitation(mediatype);
244: this .m_citations.put(citation.getId(), citation);
245: return citation;
246: }
247:
248: /* (non-Javadoc)
249: * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeSchema(org.sakaiproject.citation.api.Schema)
250: */
251: public void removeSchema(Schema schema) {
252: m_schemas.remove(schema.getIdentifier());
253:
254: }
255:
256: public CitationCollection copyAll(String collectionId) {
257: BasicCitationCollection original = (BasicCitationCollection) this .m_collections
258: .get(collectionId);
259: CitationCollection copy = null;
260:
261: if (original != null) {
262: copy = new BasicCitationCollection();
263: Iterator it = original.iterator();
264: while (it.hasNext()) {
265: BasicCitation citation = (BasicCitation) it.next();
266: BasicCitation newCite = new BasicCitation();
267: newCite.copy(citation);
268: copy.add(newCite);
269: m_citations.put(newCite.getId(), newCite);
270: }
271: m_collections.put(copy.getId(), copy);
272: }
273:
274: return copy;
275: }
276:
277: }
278:
279: /* (non-Javadoc)
280: * @see org.sakaiproject.citation.impl.BaseCitationService#newStorage()
281: */
282: public Storage newStorage() {
283: return new BasicCitationStorage();
284: }
285: }
|