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.ArrayList;
021: import java.util.List;
022:
023: import org.apache.lenya.cms.publication.Document;
024: import org.apache.lenya.cms.publication.DocumentBuildException;
025: import org.apache.lenya.cms.publication.DocumentBuilder;
026: import org.apache.lenya.cms.publication.DocumentException;
027: import org.apache.lenya.cms.publication.DocumentLocator;
028: import org.apache.lenya.cms.publication.DocumentManager;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.repository.Node;
031: import org.apache.lenya.cms.site.NodeIterator;
032: import org.apache.lenya.cms.site.NodeSet;
033: import org.apache.lenya.cms.site.SiteException;
034: import org.apache.lenya.cms.site.SiteNode;
035: import org.apache.lenya.cms.site.SiteStructure;
036: import org.apache.lenya.cms.site.SiteUtil;
037: import org.apache.lenya.cms.usecase.DocumentUsecase;
038: import org.apache.lenya.cms.usecase.UsecaseException;
039: import org.apache.lenya.transaction.TransactionException;
040:
041: /**
042: * Change the node ID of a document.
043: *
044: * @version $Id: ChangeNodeID.java 559056 2007-07-24 14:14:33Z rfrovarp $
045: */
046: public class ChangeNodeID extends DocumentUsecase {
047:
048: protected static final String NODE_ID = "nodeId";
049:
050: /**
051: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
052: */
053: protected void initParameters() {
054: super .initParameters();
055: Document document = getSourceDocument();
056: if (document != null) {
057: setParameter(NODE_ID, document.getName());
058: }
059: }
060:
061: /**
062: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
063: */
064: protected Node[] getNodesToLock() throws UsecaseException {
065:
066: List nodes = new ArrayList();
067:
068: try {
069: if (getSourceDocument() != null) {
070: Node siteNode = getSourceDocument().area().getSite()
071: .getRepositoryNode();
072: nodes.add(siteNode);
073:
074: Document sourceDocument = getSourceDocument();
075:
076: NodeSet subsite = SiteUtil.getSubSite(this .manager,
077: sourceDocument.getLink().getNode());
078: for (NodeIterator i = subsite.ascending(); i.hasNext();) {
079: SiteNode node = i.next();
080: String[] languages = node.getLanguages();
081: for (int l = 0; l < languages.length; l++) {
082: Document doc = node.getLink(languages[l])
083: .getDocument();
084: nodes.add(doc.getRepositoryNode());
085: }
086: }
087: }
088: } catch (Exception e) {
089: throw new UsecaseException(e);
090: }
091:
092: return (Node[]) nodes.toArray(new Node[nodes.size()]);
093: }
094:
095: protected List getAllLanguageVersionNodes(Document doc)
096: throws DocumentException, TransactionException,
097: DocumentBuildException {
098: String[] languages = doc.getLanguages();
099: List nodes = new ArrayList();
100: for (int i = 0; i < languages.length; i++) {
101: nodes.add(doc.getTranslation(languages[i])
102: .getRepositoryNode());
103: }
104: return nodes;
105: }
106:
107: /**
108: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
109: */
110: protected void doCheckPreconditions() throws Exception {
111: super .doCheckPreconditions();
112: if (hasErrors()) {
113: return;
114: }
115:
116: if (!getSourceDocument().getArea().equals(
117: Publication.AUTHORING_AREA)) {
118: addErrorMessage("This usecase can only be invoked in the authoring area!");
119: } else {
120: if (getSourceDocument().existsAreaVersion(
121: Publication.LIVE_AREA)) {
122: addErrorMessage("This usecase cannot be invoked when the live version exists!");
123: }
124: }
125: }
126:
127: /**
128: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
129: */
130: protected void doCheckExecutionConditions() throws Exception {
131: super .doCheckExecutionConditions();
132:
133: String nodeId = getParameterAsString(NODE_ID);
134: DocumentBuilder builder = getSourceDocument().getPublication()
135: .getDocumentBuilder();
136: if (!builder.isValidDocumentName(nodeId)) {
137: addErrorMessage("The document ID is not valid.");
138: } else {
139: DocumentLocator target = getTargetLocator();
140: Publication pub = getDocumentFactory().getPublication(
141: target.getPublicationId());
142: SiteStructure site = pub.getArea(target.getArea())
143: .getSite();
144: if (site.contains(target.getPath(), target.getLanguage())) {
145: addErrorMessage("The document does already exist.");
146: }
147: }
148: }
149:
150: protected DocumentLocator getTargetLocator()
151: throws DocumentBuildException, SiteException,
152: DocumentException {
153: String nodeId = getParameterAsString(NODE_ID);
154: Document doc = getSourceDocument();
155: DocumentLocator loc = DocumentLocator.getLocator(doc
156: .getPublication().getId(), doc.getArea(),
157: doc.getPath(), doc.getLanguage());
158: DocumentLocator parent = loc.getParent();
159: return parent.getChild(nodeId);
160: }
161:
162: /**
163: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
164: */
165: protected void doExecute() throws Exception {
166: super .doExecute();
167:
168: Document targetDoc;
169: Document source = getSourceDocument();
170: DocumentLocator target = getTargetLocator();
171: DocumentManager documentManager = null;
172: try {
173: documentManager = (DocumentManager) this .manager
174: .lookup(DocumentManager.ROLE);
175: documentManager.moveAll(source.area(), source.getPath(),
176: source.area(), target.getPath());
177: targetDoc = getDocumentFactory().get(target);
178: } finally {
179: if (documentManager != null) {
180: this .manager.release(documentManager);
181: }
182: }
183:
184: setTargetDocument(targetDoc);
185: }
186:
187: /**
188: * Returns the resulting document when the node ID would be changed.
189: * @return A document.
190: */
191: protected String getNewDocumentId() {
192: String nodeId = getParameterAsString(NODE_ID);
193:
194: String oldPath;
195: try {
196: oldPath = getSourceDocument().getPath();
197: } catch (DocumentException e) {
198: throw new RuntimeException(e);
199: }
200: int lastSlashIndex = oldPath.lastIndexOf("/");
201: String strippedDocumentId = oldPath.substring(0,
202: lastSlashIndex + 1);
203: String newDocumentId = strippedDocumentId + nodeId;
204:
205: return newDocumentId;
206: }
207: }
|