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.publication.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.List;
023:
024: import org.apache.avalon.framework.service.ServiceSelector;
025: import org.apache.lenya.cms.publication.Publication;
026: import org.apache.lenya.cms.publication.PublicationUtil;
027: import org.apache.lenya.cms.publication.templating.Instantiator;
028: import org.apache.lenya.cms.usecase.AbstractUsecase;
029:
030: /**
031: * Create a new publication based on a template publication.
032: *
033: * @version $Id: CreatePublicationFromTemplate.java 568041 2007-08-21 09:47:23Z andreas $
034: */
035: public class CreatePublicationFromTemplate extends AbstractUsecase {
036:
037: protected static final String AVAILABLE_TEMPLATES = "availableTemplates";
038: protected static final String PUBLICATION_ID = "publicationId";
039: protected static final String PUBLICATION_NAME = "publicationName";
040: protected static final String TEMPLATE = "template";
041:
042: /**
043: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
044: */
045: protected void initParameters() {
046: super .initParameters();
047:
048: Publication[] pubs = getDocumentFactory().getPublications();
049: List templates = new ArrayList();
050: for (int i = 0; i < pubs.length; i++) {
051: if (pubs[i].getInstantiatorHint() != null) {
052: templates.add(pubs[i].getId());
053: }
054: }
055: Collections.sort(templates);
056: setParameter(AVAILABLE_TEMPLATES, templates);
057: }
058:
059: /**
060: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
061: */
062: protected void doCheckExecutionConditions() throws Exception {
063: super .doCheckExecutionConditions();
064:
065: String publicationId = getParameterAsString(PUBLICATION_ID)
066: .trim();
067:
068: if (!PublicationUtil.isValidPublicationID(publicationId)) {
069: addErrorMessage("Please enter a valid publication ID!");
070: } else {
071: if (getDocumentFactory().existsPublication(publicationId)) {
072: addErrorMessage("A publication with this ID already exists.");
073: }
074: }
075: }
076:
077: /**
078: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
079: */
080: protected void doExecute() throws Exception {
081: super .doExecute();
082:
083: String templateId = getParameterAsString(TEMPLATE);
084:
085: ServiceSelector selector = null;
086: Instantiator instantiator = null;
087:
088: try {
089:
090: Publication template = getDocumentFactory().getPublication(
091: templateId);
092: String name = getParameterAsString(PUBLICATION_NAME);
093:
094: selector = (ServiceSelector) this .manager
095: .lookup(Instantiator.ROLE + "Selector");
096: instantiator = (Instantiator) selector.select(template
097: .getInstantiatorHint());
098:
099: instantiator.instantiate(template,
100: getParameterAsString(PUBLICATION_ID), name);
101:
102: } finally {
103: if (selector != null) {
104: if (instantiator != null) {
105: selector.release(instantiator);
106: }
107: this.manager.release(selector);
108: }
109: }
110:
111: }
112:
113: }
|