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.avalon.framework.service.ServiceException;
024: import org.apache.lenya.cms.publication.Area;
025: import org.apache.lenya.cms.publication.Document;
026: import org.apache.lenya.cms.publication.DocumentBuildException;
027: import org.apache.lenya.cms.publication.DocumentException;
028: import org.apache.lenya.cms.publication.DocumentFactory;
029: import org.apache.lenya.cms.publication.DocumentLocator;
030: import org.apache.lenya.cms.publication.DocumentManager;
031: import org.apache.lenya.cms.publication.Publication;
032: import org.apache.lenya.cms.publication.PublicationException;
033: import org.apache.lenya.cms.publication.URLInformation;
034: import org.apache.lenya.cms.repository.Node;
035: import org.apache.lenya.cms.site.NodeSet;
036: import org.apache.lenya.cms.site.SiteException;
037: import org.apache.lenya.cms.site.SiteNode;
038: import org.apache.lenya.cms.site.SiteUtil;
039: import org.apache.lenya.cms.usecase.AbstractUsecase;
040: import org.apache.lenya.cms.usecase.UsecaseException;
041:
042: /**
043: * Paste a document from the clipboard.
044: *
045: * @version $Id: Paste.java 602869 2007-12-10 11:41:12Z andreas $
046: */
047: public class Paste extends AbstractUsecase {
048:
049: protected static final String CLIPBOARD_LABEL = "clipboardLabel";
050:
051: /**
052: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
053: */
054: protected void doCheckPreconditions() throws Exception {
055: super .doCheckPreconditions();
056:
057: if (hasErrors()) {
058: return;
059: }
060:
061: if (!getArea().getName().equals(Publication.AUTHORING_AREA)) {
062: addErrorMessage("only-in-authoring-area");
063: }
064:
065: Clipboard clipboard = new ClipboardHelper()
066: .getClipboard(getContext());
067: if (clipboard == null) {
068: addErrorMessage("clipboard-empty");
069: } else {
070: Document doc = getSourceDocument();
071: if (doc != null) {
072: Document clippedDoc = clipboard.getDocument(
073: getDocumentFactory(), doc.getPublication());
074: String uuid = clippedDoc.getUUID();
075: SiteNode node = doc.getLink().getNode();
076: if (clipboard.getMethod() == Clipboard.METHOD_CUT) {
077: if (willPasteInOwnSubtree(node, uuid)) {
078: addErrorMessage("will-paste-in-own-subtree");
079: }
080: }
081: }
082: }
083: }
084:
085: protected boolean willPasteInOwnSubtree(SiteNode node, String uuid)
086: throws SiteException {
087: String nodeUuid = node.getUuid();
088: if (nodeUuid.equals(uuid)) {
089: return true;
090: } else if (!node.isTopLevel()) {
091: return willPasteInOwnSubtree(node.getParent(), uuid);
092: } else {
093: return false;
094: }
095: }
096:
097: protected Document getSourceDocument() {
098: Document doc = null;
099: try {
100: DocumentFactory factory = getDocumentFactory();
101: String sourceUrl = getParameterAsString(SOURCE_URL);
102: if (factory.isDocument(sourceUrl)) {
103: doc = factory.getFromURL(sourceUrl);
104: }
105: } catch (DocumentBuildException e) {
106: throw new RuntimeException(e);
107: }
108: return doc;
109: }
110:
111: /**
112: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
113: */
114: protected void initParameters() {
115: super .initParameters();
116:
117: Clipboard clipboard = new ClipboardHelper()
118: .getClipboard(getContext());
119: if (clipboard != null) {
120: String label;
121: try {
122: Publication pub = getPublication();
123: label = clipboard
124: .getDocument(getDocumentFactory(), pub)
125: .getLink().getLabel();
126: } catch (Exception e) {
127: throw new RuntimeException(e);
128: }
129: setParameter(CLIPBOARD_LABEL, label);
130: }
131: }
132:
133: protected Publication getPublication() {
134: URLInformation info = new URLInformation(getSourceURL());
135: String pubId = info.getPublicationId();
136: try {
137: return getDocumentFactory().getPublication(pubId);
138: } catch (PublicationException e) {
139: throw new RuntimeException(e);
140: }
141: }
142:
143: /**
144: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
145: */
146: protected Node[] getNodesToLock() throws UsecaseException {
147: List nodes = new ArrayList();
148:
149: try {
150:
151: Clipboard clipboard = new ClipboardHelper()
152: .getClipboard(getContext());
153: if (clipboard != null) {
154:
155: Node siteNode = getArea().getSite().getRepositoryNode();
156: nodes.add(siteNode);
157:
158: DocumentFactory map = getDocumentFactory();
159: Publication pub = getPublication();
160: Document clippedDocument = clipboard.getDocument(map,
161: pub);
162:
163: NodeSet subsite = SiteUtil.getSubSite(this .manager,
164: clippedDocument.getLink().getNode());
165: Document[] subsiteDocs = subsite.getDocuments();
166:
167: for (int i = 0; i < subsiteDocs.length; i++) {
168: if (clipboard.getMethod() == Clipboard.METHOD_CUT) {
169: nodes.add(subsiteDocs[i].getRepositoryNode());
170: }
171: }
172: }
173:
174: } catch (Exception e) {
175: throw new UsecaseException(e);
176: }
177:
178: return (Node[]) nodes.toArray(new Node[nodes.size()]);
179: }
180:
181: protected Area getArea() {
182: Publication pub = getPublication();
183: URLInformation info = new URLInformation(getSourceURL());
184: try {
185: return pub.getArea(info.getArea());
186: } catch (PublicationException e) {
187: throw new RuntimeException(e);
188: }
189: }
190:
191: /**
192: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
193: */
194: protected void doExecute() throws Exception {
195: super .doExecute();
196:
197: DocumentFactory identityMap = getDocumentFactory();
198: ClipboardHelper helper = new ClipboardHelper();
199:
200: Clipboard clipboard = helper.getClipboard(getContext());
201: Publication pub = getPublication();
202: Document clippedDocument = clipboard.getDocument(identityMap,
203: pub);
204:
205: final String targetPath = getTargetPath();
206: final Area area = clippedDocument.area();
207: DocumentManager documentManager = null;
208: try {
209: documentManager = (DocumentManager) this .manager
210: .lookup(DocumentManager.ROLE);
211:
212: if (clipboard.getMethod() == Clipboard.METHOD_COPY) {
213: documentManager.copyAll(area,
214: clippedDocument.getPath(), area, targetPath);
215: } else if (clipboard.getMethod() == Clipboard.METHOD_CUT) {
216: documentManager.moveAll(area,
217: clippedDocument.getPath(), area, targetPath);
218: } else {
219: throw new RuntimeException(
220: "This clipboard method is not supported!");
221: }
222: } finally {
223: if (documentManager != null) {
224: this .manager.release(documentManager);
225: }
226: }
227:
228: helper.removeClipboard(getContext());
229: }
230:
231: protected String getTargetPath() throws SiteException,
232: DocumentBuildException, ServiceException, DocumentException {
233: DocumentFactory identityMap = getDocumentFactory();
234: Clipboard clipboard = new ClipboardHelper()
235: .getClipboard(getContext());
236: Publication pub = getPublication();
237: Document clippedDocument = clipboard.getDocument(identityMap,
238: pub);
239:
240: String targetArea = getArea().getName();
241: String language = clippedDocument.getLanguage();
242: String nodeId = clippedDocument.getName();
243:
244: Document sourceDoc = getSourceDocument();
245: String basePath = sourceDoc != null ? sourceDoc.getPath() : "";
246:
247: String potentialPath = basePath + "/" + nodeId;
248:
249: DocumentLocator potentialLoc = DocumentLocator.getLocator(
250: getPublication().getId(), targetArea, potentialPath,
251: language);
252: return SiteUtil.getAvailableLocator(this.manager,
253: getDocumentFactory(), potentialLoc).getPath();
254: }
255: }
|