001: package org.sakaibrary.osid.repository.xserver;
002:
003: import org.sakaibrary.xserver.XServerException;
004: import org.sakaibrary.xserver.XServer;
005: import org.sakaiproject.citation.util.impl.CQL2XServerFindCommand;
006:
007: /*******************************************************************************
008: * Copyright (c) 2003, 2004, 2005 The Regents of the University of Michigan,
009: * Trustees of Indiana University, Board of Trustees of the Leland Stanford,
010: * Jr., University, and The MIT Corporation
011: *
012: * Licensed under the Educational Community License Version 1.0 (the "License");
013: * By obtaining, using and/or copying this Original Work, you agree that you
014: * have read, understand, and will comply with the terms and conditions of the
015: * Educational Community License. You may obtain a copy of the License at:
016: *
017: * http://cvs.sakaiproject.org/licenses/license_1_0.html
018: *
019: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
025: * SOFTWARE.
026: *
027: ******************************************************************************/
028:
029: /**
030: *
031: * @author gbhatnag
032: */
033: public class Repository implements org.osid.repository.Repository {
034: private static final long serialVersionUID = 1L;
035:
036: private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
037: .getLog("org.sakaibrary.osid.repository.xserver.Repository");
038:
039: private org.osid.shared.Id id = null;
040: private String idString = null;
041: private String displayName = null;
042: private String description = null;
043:
044: // Types
045: private org.osid.shared.Type repositoryType = new Type(
046: "sakaibrary", "repository", "metasearch");
047:
048: private org.osid.shared.Type assetType = new Type("sakaibrary",
049: "asset", "citation");
050:
051: private org.osid.shared.Type searchPropertiesType = new Type(
052: "sakaibrary", "properties", "asynchMetasearch");
053:
054: private org.osid.shared.Type searchStatusPropertiesType = new Type(
055: "sakaibrary", "properties", "metasearchStatus");
056:
057: private org.osid.shared.Type recordStructureType = new Type(
058: "sakaibrary", "recordStructure", "citation");
059:
060: // Properties
061: private org.osid.shared.Properties searchStatusProperties = null;
062: private org.osid.shared.Properties searchProperties = null;
063:
064: // Vectors for Iterators
065: private java.util.Vector searchTypeVector = new java.util.Vector();
066:
067: /**
068: * Constructs a Repository
069: *
070: * @param displayName
071: * @param description
072: * @param idString
073: * @param searchTypeVector
074: * @param idManager
075: * @throws org.osid.repository.RepositoryException
076: */
077: protected Repository(String displayName, String description,
078: String idString, java.util.Vector searchTypeVector,
079: org.osid.id.IdManager idManager)
080: throws org.osid.repository.RepositoryException {
081: this .displayName = displayName;
082: this .description = description;
083: this .idString = idString;
084: this .searchTypeVector = searchTypeVector;
085:
086: try {
087: this .id = idManager.getId(this .idString);
088: } catch (Throwable t) {
089: LOG.warn(t.getMessage());
090: }
091: }
092:
093: public String getDisplayName()
094: throws org.osid.repository.RepositoryException {
095: return this .displayName;
096: }
097:
098: public void updateDisplayName(String displayName)
099: throws org.osid.repository.RepositoryException {
100: // this data is Consumer read-only
101: throw new org.osid.repository.RepositoryException(
102: org.osid.OsidException.UNIMPLEMENTED);
103: }
104:
105: public String getDescription()
106: throws org.osid.repository.RepositoryException {
107: return this .description;
108: }
109:
110: public void updateDescription(String description)
111: throws org.osid.repository.RepositoryException {
112: // this data is Consumer read-only
113: throw new org.osid.repository.RepositoryException(
114: org.osid.OsidException.UNIMPLEMENTED);
115: }
116:
117: public org.osid.shared.Id getId()
118: throws org.osid.repository.RepositoryException {
119: return this .id;
120: }
121:
122: public org.osid.shared.Type getType()
123: throws org.osid.repository.RepositoryException {
124: return this .repositoryType;
125: }
126:
127: public org.osid.repository.Asset createAsset(String displayName,
128: String description, org.osid.shared.Type assetType)
129: throws org.osid.repository.RepositoryException {
130: if ((displayName == null) || (description == null)
131: || (assetType == null)) {
132: throw new org.osid.repository.RepositoryException(
133: org.osid.shared.SharedException.NULL_ARGUMENT);
134: }
135: if (!assetType.isEqual(this .assetType)) {
136: throw new org.osid.repository.RepositoryException(
137: org.osid.shared.SharedException.UNKNOWN_TYPE);
138: }
139: throw new org.osid.repository.RepositoryException(
140: org.osid.OsidException.UNIMPLEMENTED);
141: }
142:
143: public void deleteAsset(org.osid.shared.Id assetId)
144: throws org.osid.repository.RepositoryException {
145: if (assetId == null) {
146: throw new org.osid.repository.RepositoryException(
147: org.osid.shared.SharedException.NULL_ARGUMENT);
148: }
149: throw new org.osid.repository.RepositoryException(
150: org.osid.OsidException.UNIMPLEMENTED);
151: }
152:
153: public org.osid.repository.AssetIterator getAssets()
154: throws org.osid.repository.RepositoryException {
155: throw new org.osid.repository.RepositoryException(
156: org.osid.OsidException.UNIMPLEMENTED);
157: }
158:
159: public org.osid.repository.AssetIterator getAssetsByType(
160: org.osid.shared.Type assetType)
161: throws org.osid.repository.RepositoryException {
162: if (assetType == null) {
163: throw new org.osid.repository.RepositoryException(
164: org.osid.shared.SharedException.NULL_ARGUMENT);
165: }
166: throw new org.osid.repository.RepositoryException(
167: org.osid.OsidException.UNIMPLEMENTED);
168: }
169:
170: public org.osid.shared.TypeIterator getAssetTypes()
171: throws org.osid.repository.RepositoryException {
172: java.util.Vector results = new java.util.Vector();
173: results.addElement(this .assetType);
174:
175: try {
176: return new TypeIterator(results);
177: } catch (Throwable t) {
178: LOG.warn(t.getMessage());
179: throw new org.osid.repository.RepositoryException(
180: org.osid.OsidException.OPERATION_FAILED);
181: }
182: }
183:
184: public org.osid.repository.RecordStructureIterator getRecordStructures()
185: throws org.osid.repository.RepositoryException {
186: java.util.Vector results = new java.util.Vector();
187: results.addElement(RecordStructure.getInstance());
188: return new RecordStructureIterator(results);
189: }
190:
191: public org.osid.repository.RecordStructureIterator getMandatoryRecordStructures(
192: org.osid.shared.Type assetType)
193: throws org.osid.repository.RepositoryException {
194: if (assetType == null) {
195: throw new org.osid.repository.RepositoryException(
196: org.osid.shared.SharedException.NULL_ARGUMENT);
197: }
198: if (assetType.isEqual(this .assetType)) {
199: java.util.Vector results = new java.util.Vector();
200: results.addElement(RecordStructure.getInstance());
201: return new RecordStructureIterator(results);
202: }
203: throw new org.osid.repository.RepositoryException(
204: org.osid.shared.SharedException.UNKNOWN_TYPE);
205: }
206:
207: public org.osid.shared.TypeIterator getSearchTypes()
208: throws org.osid.repository.RepositoryException {
209: try {
210: return new TypeIterator(this .searchTypeVector);
211: } catch (Throwable t) {
212: LOG.warn(t.getMessage());
213: throw new org.osid.repository.RepositoryException(
214: org.osid.OsidException.OPERATION_FAILED);
215: }
216: }
217:
218: public org.osid.shared.TypeIterator getStatusTypes()
219: throws org.osid.repository.RepositoryException {
220: throw new org.osid.repository.RepositoryException(
221: org.osid.OsidException.UNIMPLEMENTED);
222: }
223:
224: public org.osid.shared.Type getStatus(org.osid.shared.Id assetId)
225: throws org.osid.repository.RepositoryException {
226: if (assetId == null) {
227: throw new org.osid.repository.RepositoryException(
228: org.osid.shared.SharedException.NULL_ARGUMENT);
229: }
230: throw new org.osid.repository.RepositoryException(
231: org.osid.OsidException.UNIMPLEMENTED);
232: }
233:
234: public boolean validateAsset(org.osid.shared.Id assetId)
235: throws org.osid.repository.RepositoryException {
236: if (assetId == null) {
237: throw new org.osid.repository.RepositoryException(
238: org.osid.shared.SharedException.NULL_ARGUMENT);
239: }
240: throw new org.osid.repository.RepositoryException(
241: org.osid.OsidException.UNIMPLEMENTED);
242: }
243:
244: public void invalidateAsset(org.osid.shared.Id assetId)
245: throws org.osid.repository.RepositoryException {
246: if (assetId == null) {
247: throw new org.osid.repository.RepositoryException(
248: org.osid.shared.SharedException.NULL_ARGUMENT);
249: }
250: throw new org.osid.repository.RepositoryException(
251: org.osid.OsidException.UNIMPLEMENTED);
252: }
253:
254: public org.osid.repository.Asset getAsset(org.osid.shared.Id assetId)
255: throws org.osid.repository.RepositoryException {
256: if (assetId == null) {
257: throw new org.osid.repository.RepositoryException(
258: org.osid.shared.SharedException.NULL_ARGUMENT);
259: }
260: throw new org.osid.repository.RepositoryException(
261: org.osid.OsidException.UNIMPLEMENTED);
262: }
263:
264: public org.osid.repository.Asset getAssetByDate(
265: org.osid.shared.Id assetId, long date)
266: throws org.osid.repository.RepositoryException {
267: if (assetId == null) {
268: throw new org.osid.repository.RepositoryException(
269: org.osid.shared.SharedException.NULL_ARGUMENT);
270: }
271: throw new org.osid.repository.RepositoryException(
272: org.osid.OsidException.UNIMPLEMENTED);
273: }
274:
275: public org.osid.shared.LongValueIterator getAssetDates(
276: org.osid.shared.Id assetId)
277: throws org.osid.repository.RepositoryException {
278: if (assetId == null) {
279: throw new org.osid.repository.RepositoryException(
280: org.osid.shared.SharedException.NULL_ARGUMENT);
281: }
282: throw new org.osid.repository.RepositoryException(
283: org.osid.OsidException.UNIMPLEMENTED);
284: }
285:
286: public org.osid.repository.AssetIterator getAssetsBySearch(
287: java.io.Serializable searchCriteria,
288: org.osid.shared.Type searchType,
289: org.osid.shared.Properties searchProperties)
290: throws org.osid.repository.RepositoryException {
291: java.util.ArrayList<String> databaseIds = null;
292: String guid = null;
293: String baseUrl = null;
294: String username = null;
295: String password = null;
296: boolean knownPropertiesType = false;
297: String criteria = null;
298:
299: /* check parameters */
300: if (searchCriteria == null || searchType == null) {
301: throw new org.osid.repository.RepositoryException(
302: org.osid.shared.SharedException.NULL_ARGUMENT);
303: }
304:
305: if (!(searchCriteria instanceof String)) {
306: LOG.warn("getAssetsBySearch() invalid search criteria: "
307: + searchCriteria);
308: throw new org.osid.repository.RepositoryException(
309: org.osid.OsidException.OPERATION_FAILED);
310: }
311:
312: // check searchProperties
313: if (searchProperties == null) {
314: throw new org.osid.repository.RepositoryException(
315: org.osid.shared.SharedException.NULL_ARGUMENT);
316: } else {
317: // check searchProperties type
318: try {
319: knownPropertiesType = searchProperties.getType()
320: .isEqual(searchPropertiesType);
321: } catch (org.osid.shared.SharedException se) {
322: LOG.warn("Unable to check searchProperties Type");
323: }
324: if (!knownPropertiesType) {
325: LOG.warn("searchProperties are of unknown type");
326: throw new org.osid.repository.RepositoryException(
327: org.osid.shared.SharedException.UNKNOWN_TYPE);
328: }
329:
330: // check if required fields are part of searchProperties
331: try {
332: databaseIds = (java.util.ArrayList<String>) searchProperties
333: .getProperty("databaseIds");
334: guid = (String) searchProperties.getProperty("guid");
335: baseUrl = (String) searchProperties
336: .getProperty("baseUrl");
337: username = (String) searchProperties
338: .getProperty("username");
339: password = (String) searchProperties
340: .getProperty("password");
341: } catch (org.osid.shared.SharedException se) {
342: LOG
343: .warn(
344: "Problem getting guid from org.osid.shared.Properties "
345: + "object passed to getAssetsBySearch().",
346: se);
347: throw new org.osid.repository.RepositoryException(
348: org.osid.OsidException.OPERATION_FAILED);
349: }
350:
351: if (guid == null || guid.trim().equals("")
352: || baseUrl == null || baseUrl.trim().equals("")
353: || username == null || username.trim().equals("")
354: || password == null || password.trim().equals("")) {
355: LOG.warn("required search property is null or empty:"
356: + "\n guid: " + guid + "\n baseUrl: "
357: + baseUrl + "\n username: " + username
358: + "\n password: " + password);
359: throw new org.osid.repository.RepositoryException(
360: org.osid.OsidException.NULL_ARGUMENT);
361: }
362:
363: if (databaseIds == null || databaseIds.size() == 0) {
364: LOG
365: .warn("ERROR: databaseIds from org.osid.shared.Properties is "
366: + "null or empty");
367: throw new org.osid.repository.RepositoryException(
368: org.osid.OsidException.OPERATION_FAILED);
369: }
370: }
371:
372: // check search type
373: for (int i = 0; i < searchTypeVector.size(); i++) {
374: org.osid.shared.Type type = (org.osid.shared.Type) (searchTypeVector
375: .elementAt(i));
376:
377: if (!type.isEqual(searchType)) {
378: LOG.warn("searchType is of unknown type");
379: throw new org.osid.repository.RepositoryException(
380: org.osid.shared.SharedException.UNKNOWN_TYPE);
381: }
382: }
383:
384: /*
385: * parameter checking clear, conduct an X-Server metasearch
386: */
387:
388: // update searchProperties
389: this .searchProperties = searchProperties;
390:
391: // convert the cql-formatted searchCriteria to X-Server formatted find_command
392: criteria = doCQL2FindCommand((String) searchCriteria);
393:
394: // setup Metasearch session
395: org.sakaibrary.xserver.session.MetasearchSessionManager msm = org.sakaibrary.xserver.session.MetasearchSessionManager
396: .getInstance();
397: org.sakaibrary.xserver.session.MetasearchSession metasearchSession = msm
398: .getMetasearchSession(guid);
399:
400: if (metasearchSession == null) {
401: metasearchSession = new org.sakaibrary.xserver.session.MetasearchSession(
402: guid);
403: }
404:
405: // update searchStatusProperties
406: java.util.Properties searchStatusProperties = new java.util.Properties();
407: searchStatusProperties.put("status", "searching");
408: searchStatusProperties.put("statusMessage",
409: "search has just begun");
410:
411: // establish the following session items for a guaranteed fresh session
412: metasearchSession.setLoggedIn(false);
413: metasearchSession.setUsername(username);
414: metasearchSession.setPassword(password);
415: metasearchSession.setSessionId(null);
416: metasearchSession.setBaseUrl(baseUrl);
417: metasearchSession.setRepositoryId(this .id);
418: metasearchSession.setRepositoryDisplayName(this .displayName);
419: metasearchSession.setSearchProperties(searchProperties);
420: metasearchSession
421: .setSearchStatusProperties(searchStatusProperties);
422: metasearchSession
423: .setSingleSearchSource((databaseIds.size() > 1) ? false
424: : true);
425: metasearchSession.setGotMergeError(false);
426: metasearchSession.setFoundGroupNumber(null);
427: metasearchSession.setMergedGroupNumber(null);
428: metasearchSession.setRecordsSetNumber(null);
429: metasearchSession.setNumRecordsFound(new Integer(0));
430: metasearchSession.setNumRecordsFetched(new Integer(0));
431: metasearchSession.setNumRecordsMerged(new Integer(0));
432: msm.putMetasearchSession(guid, metasearchSession);
433:
434: // get an XServer
435: XServer xserver = null;
436:
437: try {
438: xserver = new XServer(guid);
439: } catch (XServerException xse) {
440: LOG.warn("X-Server error " + xse.getErrorCode() + " - "
441: + xse.getErrorText());
442: throw new org.osid.repository.RepositoryException(
443: org.sakaibrary.osid.repository.xserver.MetasearchException.METASEARCH_ERROR);
444: }
445:
446: try {
447: // initiate the asynchronous search
448: xserver.initAsynchSearch(criteria, databaseIds);
449: } catch (XServerException xse) {
450: LOG.warn("X-Server error " + xse.getErrorCode() + " - "
451: + xse.getErrorText());
452: throw new org.osid.repository.RepositoryException(
453: org.sakaibrary.osid.repository.xserver.MetasearchException.METASEARCH_ERROR);
454: }
455:
456: // return an empty AssetIterator
457: return new AssetIterator(guid);
458: }
459:
460: public org.osid.shared.Id copyAsset(org.osid.repository.Asset asset)
461: throws org.osid.repository.RepositoryException {
462: throw new org.osid.repository.RepositoryException(
463: org.osid.OsidException.UNIMPLEMENTED);
464: }
465:
466: public org.osid.repository.RecordStructureIterator getRecordStructuresByType(
467: org.osid.shared.Type recordStructureType)
468: throws org.osid.repository.RepositoryException {
469: if (recordStructureType == null) {
470: throw new org.osid.repository.RepositoryException(
471: org.osid.shared.SharedException.NULL_ARGUMENT);
472: }
473: if (recordStructureType.isEqual(this .recordStructureType)) {
474: java.util.Vector results = new java.util.Vector();
475: results.addElement(RecordStructure.getInstance());
476: return new RecordStructureIterator(results);
477: }
478: throw new org.osid.repository.RepositoryException(
479: org.osid.shared.SharedException.UNKNOWN_TYPE);
480: }
481:
482: public org.osid.shared.PropertiesIterator getProperties()
483: throws org.osid.repository.RepositoryException {
484: java.util.Vector results = new java.util.Vector();
485:
486: results.addElement(searchProperties);
487: String guid = null;
488: try {
489: guid = (String) searchProperties.getProperty("guid");
490: } catch (org.osid.shared.SharedException se) {
491: LOG.warn("getProperties() could not get guid: "
492: + se.getMessage(), se);
493: throw new org.osid.repository.RepositoryException(
494: org.osid.OsidException.OPERATION_FAILED);
495: }
496:
497: if (searchProperties != null) {
498: try {
499: XServer xserver = new XServer(guid);
500: searchStatusProperties = xserver
501: .getSearchStatusProperties();
502: } catch (XServerException xse) {
503: // ignore
504: }
505: } else {
506: searchStatusProperties = null;
507: }
508:
509: results.addElement(searchStatusProperties);
510: try {
511: return new PropertiesIterator(results);
512: } catch (Throwable t) {
513: LOG.warn(t.getMessage());
514: throw new org.osid.repository.RepositoryException(
515: org.osid.OsidException.OPERATION_FAILED);
516: }
517: }
518:
519: public org.osid.shared.Properties getPropertiesByType(
520: org.osid.shared.Type propertiesType)
521: throws org.osid.repository.RepositoryException {
522: if (propertiesType == null) {
523: throw new org.osid.repository.RepositoryException(
524: org.osid.shared.SharedException.NULL_ARGUMENT);
525: }
526:
527: org.osid.shared.PropertiesIterator pi = getProperties();
528:
529: try {
530: while (pi.hasNextProperties()) {
531: org.osid.shared.Properties properties = pi
532: .nextProperties();
533: if (properties.getType().isEqual(propertiesType)) {
534: return properties;
535: }
536: }
537: } catch (org.osid.shared.SharedException se) {
538: throw new org.osid.repository.RepositoryException(
539: org.osid.OsidException.OPERATION_FAILED);
540: }
541:
542: // didn't find a match for the given type
543: throw new org.osid.repository.RepositoryException(
544: org.osid.shared.SharedException.UNKNOWN_TYPE);
545: }
546:
547: public org.osid.shared.TypeIterator getPropertyTypes()
548: throws org.osid.repository.RepositoryException {
549: java.util.Vector results = new java.util.Vector();
550: results.addElement(searchPropertiesType);
551: results.addElement(searchStatusPropertiesType);
552:
553: try {
554: return new TypeIterator(results);
555: } catch (Throwable t) {
556: LOG.warn(t.getMessage());
557: throw new org.osid.repository.RepositoryException(
558: org.osid.OsidException.OPERATION_FAILED);
559: }
560: }
561:
562: public boolean supportsUpdate()
563: throws org.osid.repository.RepositoryException {
564: return false;
565: }
566:
567: public boolean supportsVersioning()
568: throws org.osid.repository.RepositoryException {
569: return false;
570: }
571:
572: private String doCQL2FindCommand(String cqlString) {
573: CQL2XServerFindCommand cql2Xserver = new CQL2XServerFindCommand();
574: return cql2Xserver.doCQL2MetasearchCommand(cqlString);
575: }
576: }
|