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 org.apache.avalon.framework.service.ServiceSelector;
021: import org.apache.lenya.cms.publication.Document;
022: import org.apache.lenya.cms.publication.Publication;
023: import org.apache.lenya.cms.repository.Node;
024: import org.apache.lenya.cms.site.SiteManager;
025: import org.apache.lenya.cms.site.SiteStructure;
026: import org.apache.lenya.cms.site.tree.SiteTree;
027: import org.apache.lenya.cms.site.tree.SiteTreeNode;
028: import org.apache.lenya.cms.usecase.DocumentUsecase;
029: import org.apache.lenya.cms.usecase.UsecaseException;
030:
031: /**
032: * Nudge a document one position up or down.
033: *
034: * @version $Id: Nudge.java 179568 2005-06-02 09:27:26Z jwkaltz $
035: */
036: public class Nudge extends DocumentUsecase {
037:
038: protected static final String MESSAGE_AREA = "nudge-error-area";
039: protected static final String MESSAGE_DIRECTION = "nudge-error-direction";
040: protected static final String MESSAGE_DIRECTION_UNKNOWN = "nudge-error-direction-unknown";
041: protected static final String MESSAGE_ISLIVE = "nudge-error-islive";
042: protected static final String DIRECTION = "direction";
043: protected static final String UP = "up";
044: protected static final String DOWN = "down";
045:
046: /**
047: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
048: */
049: protected void doCheckPreconditions() throws Exception {
050: super .doCheckPreconditions();
051:
052: if (hasErrors()) {
053: return;
054: }
055:
056: Document doc = getSourceDocument();
057: SiteStructure liveSite = doc.getPublication().getArea(
058: Publication.LIVE_AREA).getSite();
059: if (liveSite.contains(doc.getPath())) {
060: addErrorMessage(MESSAGE_ISLIVE);
061: }
062:
063: Publication publication = doc.getPublication();
064:
065: ServiceSelector selector = null;
066: SiteManager siteManager = null;
067: try {
068: selector = (ServiceSelector) this .manager
069: .lookup(SiteManager.ROLE + "Selector");
070: siteManager = (SiteManager) selector.select(publication
071: .getSiteManagerHint());
072: SiteStructure structure = siteManager.getSiteStructure(doc
073: .getFactory(), publication, doc.getArea());
074: if (structure instanceof SiteTree) {
075:
076: SiteTreeNode node = (SiteTreeNode) doc.getLink()
077: .getNode();
078: SiteTreeNode[] siblings = null;
079:
080: String direction = getParameterAsString(DIRECTION);
081: if (direction.equals(UP)) {
082: siblings = node.getPrecedingSiblings();
083: } else if (direction.equals(DOWN)) {
084: siblings = node.getNextSiblings();
085: } else {
086: addErrorMessage(MESSAGE_DIRECTION_UNKNOWN,
087: new String[] { direction });
088: }
089:
090: if (siblings != null && siblings.length == 0) {
091: addErrorMessage(MESSAGE_DIRECTION);
092: }
093: } else {
094: addErrorMessage(MESSAGE_AREA);
095: }
096: } catch (Exception e) {
097: throw new RuntimeException(e);
098: } finally {
099: if (selector != null) {
100: if (siteManager != null) {
101: selector.release(siteManager);
102: }
103: this .manager.release(selector);
104: }
105: }
106: }
107:
108: /**
109: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
110: */
111: protected Node[] getNodesToLock() throws UsecaseException {
112: Node[] nodes = new Node[0];
113: if (getSourceDocument() != null) {
114: Node node = getSourceDocument().area().getSite()
115: .getRepositoryNode();
116: nodes = new Node[] { node };
117: }
118: return nodes;
119: }
120:
121: /**
122: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
123: */
124: protected void doExecute() throws Exception {
125: super .doExecute();
126:
127: Publication publication = getSourceDocument().getPublication();
128: ServiceSelector selector = null;
129: SiteManager siteManager = null;
130: try {
131: selector = (ServiceSelector) this .manager
132: .lookup(SiteManager.ROLE + "Selector");
133: siteManager = (SiteManager) selector.select(publication
134: .getSiteManagerHint());
135: SiteStructure structure = siteManager.getSiteStructure(
136: getSourceDocument().getFactory(), publication,
137: getSourceDocument().getArea());
138: if (structure instanceof SiteTree) {
139:
140: SiteTree tree = (SiteTree) structure;
141: String direction = getParameterAsString(DIRECTION);
142: if (direction.equals(UP)) {
143: tree.moveUp(getSourceDocument().getPath());
144: } else if (direction.equals(DOWN)) {
145: tree.moveDown(getSourceDocument().getPath());
146: } else {
147: throw new IllegalArgumentException(
148: "The direction [" + direction
149: + "] is not supported.");
150: }
151: } else {
152: throw new IllegalStateException(
153: "This operation is only supported for site trees.");
154: }
155: } catch (Exception e) {
156: throw new RuntimeException(e);
157: } finally {
158: if (selector != null) {
159: if (siteManager != null) {
160: selector.release(siteManager);
161: }
162: this.manager.release(selector);
163: }
164: }
165:
166: }
167: }
|