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.export;
019:
020: import java.io.File;
021:
022: import org.apache.lenya.cms.cocoon.source.SourceUtil;
023: import org.apache.lenya.cms.publication.Area;
024: import org.apache.lenya.cms.publication.Publication;
025: import org.apache.lenya.cms.publication.PublicationException;
026: import org.apache.lenya.cms.publication.PublicationUtil;
027: import org.apache.lenya.cms.publication.URLInformation;
028: import org.apache.lenya.cms.usecase.AbstractUsecase;
029:
030: /**
031: * Import content.
032: */
033: public class Import extends AbstractUsecase {
034:
035: protected void initParameters() {
036: super .initParameters();
037:
038: Publication publication;
039: try {
040: publication = PublicationUtil.getPublicationFromUrl(
041: this .manager, getDocumentFactory(), getSourceURL());
042: } catch (PublicationException e) {
043: throw new RuntimeException(e);
044: }
045: String path = getExampleContentPath(publication);
046: if (!new File(path).exists()) {
047: path = getExampleContentPath(getDefaultPub());
048: }
049: setParameter("path", path);
050: }
051:
052: protected String getExampleContentPath(Publication publication) {
053: return publication.getDirectory().getAbsolutePath().replace(
054: File.separatorChar, '/')
055: + "/example-content";
056: }
057:
058: protected Publication getDefaultPub() {
059: Publication defaultPub;
060: try {
061: defaultPub = getDocumentFactory().getPublication("default");
062: } catch (PublicationException e) {
063: throw new RuntimeException(e);
064: }
065: return defaultPub;
066: }
067:
068: protected void doCheckPreconditions() throws Exception {
069: super .doCheckPreconditions();
070: Area area = getArea();
071: if (area.getDocuments().length > 0) {
072: addErrorMessage("You can't import anything because this publication already contains content.");
073: }
074: }
075:
076: protected Area getArea() {
077: String url = getSourceURL();
078: URLInformation info = new URLInformation(url);
079: String pubId = info.getPublicationId();
080: String areaName = info.getArea();
081: Area area;
082: try {
083: area = getDocumentFactory().getPublication(pubId).getArea(
084: areaName);
085: } catch (PublicationException e) {
086: throw new RuntimeException(e);
087: }
088: return area;
089: }
090:
091: protected void doCheckExecutionConditions() throws Exception {
092: super .doCheckExecutionConditions();
093: String path = getParameterAsString("path");
094: String baseUri = "file://" + path;
095: String sitetreeUri = baseUri + "/sitetree.xml";
096: if (!SourceUtil.exists(sitetreeUri, this .manager)) {
097: addErrorMessage("The sitetree file does not exist in this directory.");
098: }
099: }
100:
101: protected void doExecute() throws Exception {
102: super .doExecute();
103: String path = getParameterAsString("path");
104: Importer importer = new Importer(this.manager, getLogger());
105: importer.importContent(getDefaultPub(), getArea(), path);
106: }
107:
108: }
|