001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.jcr.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.service.ServiceSelector;
028: import org.apache.excalibur.source.SourceResolver;
029: import org.apache.lenya.cms.cocoon.source.SourceUtil;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.publication.DocumentFactory;
032: import org.apache.lenya.cms.publication.Publication;
033: import org.apache.lenya.cms.repository.Node;
034: import org.apache.lenya.cms.site.SiteManager;
035: import org.apache.lenya.cms.usecase.AbstractUsecase;
036:
037: /**
038: * Import a publication into the JCR repository.
039: */
040: public class Import extends AbstractUsecase {
041:
042: protected static final String PUBLICATIONS = "publications";
043: protected static final String PUBLICATION = "publication";
044:
045: protected void initParameters() {
046: super .initParameters();
047: Publication[] pubs = getDocumentFactory().getPublications();
048: List pubList = Arrays.asList(pubs);
049: setParameter(PUBLICATIONS, pubList);
050: }
051:
052: protected void doExecute() throws Exception {
053: super .doExecute();
054: String pubId = getParameterAsString(PUBLICATION);
055: DocumentFactory factory = getDocumentFactory();
056: ServiceSelector selector = null;
057: SiteManager siteManager = null;
058: SourceResolver resolver = null;
059: try {
060: resolver = (SourceResolver) this .manager
061: .lookup(SourceResolver.ROLE);
062: Publication pub = factory.getPublication(pubId);
063: selector = (ServiceSelector) this .manager
064: .lookup(SiteManager.ROLE + "Selector");
065: siteManager = (SiteManager) selector.select(pub
066: .getSiteManagerHint());
067:
068: List nodes = new ArrayList();
069: Map uri2meta = new HashMap();
070:
071: String[] areas = { Publication.AUTHORING_AREA,
072: Publication.LIVE_AREA, Publication.TRASH_AREA,
073: Publication.ARCHIVE_AREA };
074: for (int i = 0; i < areas.length; i++) {
075: Document[] docs = siteManager.getDocuments(factory,
076: pub, areas[i]);
077: for (int j = 0; j < docs.length; j++) {
078: nodes.add(docs[j].getRepositoryNode());
079: /*
080: final String lenyaUri = docs[j].getSourceURI();
081: final String sourcePath = lenyaUri.substring("lenya://".length());
082: final String contextUri = "context://" + sourcePath + ".meta";
083: * MetaDataManager meta = new MetaDataManager(contextUri, this.manager,
084: * getLogger()); uri2meta.put(docs[j].getSourceURI(), meta);
085: */
086: }
087: nodes.add(siteManager.getSiteStructure(factory, pub,
088: areas[i]).getRepositoryNode());
089: }
090:
091: for (Iterator i = nodes.iterator(); i.hasNext();) {
092: Node node = (Node) i.next();
093: final String lenyaUri = node.getSourceURI();
094: final String sourcePath = lenyaUri.substring("lenya://"
095: .length());
096: final String contextUri = "context://" + sourcePath;
097: final String jcrUri = "jcr://" + sourcePath;
098: if (SourceUtil.exists(contextUri, this .manager)) {
099: SourceUtil.copy(resolver, contextUri, jcrUri);
100: /*
101: * MetaDataManager sourceMgr = (MetaDataManager) uri2meta.get(lenyaUri); if
102: * (sourceMgr != null) { MetaDataManager jcrMgr = new JCRMetaDataManager(jcrUri,
103: * this.manager, getLogger()); jcrMgr.replaceMetaData(sourceMgr); }
104: */
105: } else {
106: addInfoMessage("The source [" + contextUri
107: + "] does not exist.");
108: }
109: }
110:
111: } finally {
112: if (selector != null) {
113: if (siteManager != null) {
114: selector.release(siteManager);
115: }
116: this.manager.release(selector);
117: }
118: if (resolver != null) {
119: this.manager.release(resolver);
120: }
121: }
122:
123: }
124:
125: }
|