01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/archive/tags/sakai_2-4-1/import-impl/src/java/org/sakaiproject/importer/impl/BasicImportService.java $
03: * $Id: BasicImportService.java 17726 2006-11-01 15:39:28Z lance@indiana.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.importer.impl;
21:
22: import java.util.Collection;
23: import java.util.Iterator;
24: import java.util.List;
25:
26: import org.sakaiproject.importer.api.HandlesImportable;
27: import org.sakaiproject.importer.api.ImportDataSource;
28: import org.sakaiproject.importer.api.ImportFileParser;
29: import org.sakaiproject.importer.api.ImportService;
30: import org.sakaiproject.importer.api.Importable;
31:
32: import org.sakaiproject.component.api.ServerConfigurationService;
33:
34: public class BasicImportService implements ImportService {
35:
36: private List parsers;
37: private List resourceHandlers;
38: private ServerConfigurationService configService = org.sakaiproject.component.cover.ServerConfigurationService
39: .getInstance();
40:
41: public void doImportItems(Collection importables, String siteId) {
42: HandlesImportable handler = null;
43: for (Iterator i = importables.iterator(); i.hasNext();) {
44: Importable thing = (Importable) i.next();
45: for (Iterator j = resourceHandlers.iterator(); j.hasNext();) {
46: handler = (HandlesImportable) j.next();
47: if (handler.canHandleType(thing.getTypeName())) {
48: handler.handle(thing, siteId);
49: }
50: }
51: }
52:
53: }
54:
55: public boolean isValidArchive(byte[] archiveFileData) {
56: boolean isValid = false;
57: for (Iterator i = this .parsers.iterator(); i.hasNext();) {
58: if (((ImportFileParser) i.next())
59: .isValidArchive(archiveFileData)) {
60: isValid = true;
61: break;
62: }
63: }
64: return isValid;
65: }
66:
67: public ImportDataSource parseFromFile(byte[] archiveFileData) {
68: for (Iterator i = this .parsers.iterator(); i.hasNext();) {
69: ImportFileParser parser = (ImportFileParser) i.next();
70: if (parser.isValidArchive(archiveFileData)) {
71: return parser.parse(archiveFileData, configService
72: .getSakaiHomePath()
73: + "archive");
74: }
75: }
76: // invalid or unsupported archive file
77: // TODO this should probably throw an exception
78: return null;
79: }
80:
81: public void registerParser(ImportFileParser parser) {
82: this .parsers.add(parser);
83: }
84:
85: public void setParsers(List parsers) {
86: this .parsers = parsers;
87: }
88:
89: public void registerResourceHandler(HandlesImportable handler) {
90: this .resourceHandlers.add(handler);
91: }
92:
93: public void setResourceHandlers(List resourceHandlers) {
94: this.resourceHandlers = resourceHandlers;
95: }
96:
97: }
|