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.site.usecases;
019:
020: import java.util.Arrays;
021:
022: import org.apache.lenya.cms.publication.Document;
023: import org.apache.lenya.cms.publication.DocumentBuilder;
024: import org.apache.lenya.cms.publication.DocumentException;
025: import org.apache.lenya.cms.publication.DocumentLocator;
026: import org.apache.lenya.cms.publication.Publication;
027:
028: /**
029: * Usecase to create a document.
030: *
031: * @version $Id: CreateDocument.java 568678 2007-08-22 16:12:12Z andreas $
032: */
033: public class CreateDocument extends Create {
034:
035: protected static final String PARENT_PATH = "parentPath";
036:
037: protected static final String DOCUMENT_TYPE = "doctype";
038:
039: protected static final String RELATION = "relation";
040: protected static final String RELATIONS = "relations";
041: protected static final String RELATION_CHILD = "child";
042: protected static final String RELATION_BEFORE = "sibling before";
043: protected static final String RELATION_AFTER = "sibling after";
044: protected static final String PATH_PROVIDED = "pathProvided";
045:
046: /**
047: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
048: */
049: protected void initParameters() {
050: super .initParameters();
051:
052: Document parent = getSourceDocument();
053: if (parent == null) {
054: setParameter(PARENT_PATH, "");
055: } else {
056: try {
057: setParameter(PARENT_PATH, parent.getPath());
058: } catch (DocumentException e) {
059: throw new RuntimeException(e);
060: }
061: }
062:
063: String[] languages = getPublication().getLanguages();
064: if (languages.length == 0) {
065: addErrorMessage("The publication doesn't contain any languages!");
066: }
067: setParameter(LANGUAGES, languages);
068:
069: Document sourceDoc = getSourceDocument();
070: String[] childOnly = { RELATION_CHILD };
071: String[] childAndAfter = { RELATION_CHILD, RELATION_AFTER };
072: String[] relations = sourceDoc == null ? childOnly
073: : childAndAfter;
074:
075: setParameter(RELATIONS, relations);
076: setParameter(RELATION, RELATION_CHILD);
077:
078: String path = getParameterAsString(PATH);
079: boolean provided = path != null && !path.equals("");
080: setParameter(PATH_PROVIDED, Boolean.valueOf(provided));
081: }
082:
083: /**
084: * Override this method to support other relations.
085: * @return The supported relations.
086: */
087: protected String[] getSupportedRelations() {
088: return new String[] { RELATION_CHILD, RELATION_AFTER };
089: }
090:
091: /**
092: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
093: */
094: protected void doCheckExecutionConditions() throws Exception {
095: super .doCheckExecutionConditions();
096:
097: String relation = getParameterAsString(RELATION);
098:
099: if (!Arrays.asList(getSupportedRelations()).contains(relation)) {
100: addErrorMessage("The relation '" + relation
101: + "' is not supported.");
102: }
103:
104: Publication pub = getPublication();
105:
106: DocumentBuilder builder = pub.getDocumentBuilder();
107: boolean provided = getParameterAsBoolean(PATH_PROVIDED, false);
108:
109: if (provided) {
110: String newPath = getNewDocumentPath();
111: if (pub.getArea(getArea()).getSite().contains(newPath)) {
112: String[] params = { newPath };
113: addErrorMessage("path-already-exists", params);
114: }
115: } else {
116: String nodeName = getNodeName();
117: if (nodeName.equals("")) {
118: addErrorMessage("missing-node-name");
119: } else if (!builder.isValidDocumentName(nodeName)) {
120: addErrorMessage("node-name-special-characters");
121: }
122: }
123: }
124:
125: protected String getNodeName() {
126: return getParameterAsString(NODE_NAME).trim();
127: }
128:
129: protected boolean isPathValid() {
130: String nodeName = getNewDocumentName();
131: DocumentBuilder builder = getPublication().getDocumentBuilder();
132: return !nodeName.trim().equals("")
133: && builder.isValidDocumentName(nodeName);
134: }
135:
136: /**
137: * @see Create#getNewDocumentName()
138: */
139: protected String getNewDocumentName() {
140: String nodeName;
141: if (getParameterAsBoolean(PATH_PROVIDED, false)) {
142: final String path = getParameterAsString(PATH);
143: nodeName = path.substring(path.lastIndexOf("/") + 1);
144: } else {
145: nodeName = getNodeName();
146: }
147: return nodeName;
148: }
149:
150: /**
151: * @return The relation between the source document and the created
152: * document.
153: */
154: protected String getRelation() {
155: return getParameterAsString(RELATION);
156: }
157:
158: /**
159: * @see Create#getNewDocumentPath()
160: */
161: protected String getNewDocumentPath() {
162: if (getParameterAsBoolean(PATH_PROVIDED, false)) {
163: return getParameterAsString(PATH);
164: } else {
165: String relation = getRelation();
166: Document sourceDoc = getSourceDocument();
167: if (sourceDoc == null) {
168: return "/" + getNewDocumentName();
169: } else {
170: DocumentLocator sourceLoc = getSourceDocument()
171: .getLocator();
172: if (relation.equals(RELATION_CHILD)) {
173: return sourceLoc.getChild(getNewDocumentName())
174: .getPath();
175: } else if (relation.equals(RELATION_BEFORE)
176: || relation.equals(RELATION_AFTER)) {
177: return sourceLoc.getParent().getChild(
178: getNewDocumentName()).getPath();
179: } else {
180: throw new IllegalStateException(
181: "unsupported relation " + relation);
182: }
183: }
184: }
185: }
186:
187: /**
188: * @see org.apache.lenya.cms.site.usecases.Create#getDocumentTypeName()
189: */
190: protected String getDocumentTypeName() {
191: return getParameterAsString(DOCUMENT_TYPE);
192: }
193:
194: protected String getSourceExtension() {
195: return "xml";
196: }
197:
198: protected boolean createVersion() {
199: return false;
200: }
201:
202: }
|