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.workflow.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.lenya.cms.linking.LinkManager;
027: import org.apache.lenya.cms.publication.Document;
028: import org.apache.lenya.cms.publication.DocumentManager;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.site.NodeSet;
031: import org.apache.lenya.cms.site.SiteNode;
032: import org.apache.lenya.cms.site.SiteUtil;
033: import org.apache.lenya.cms.usecase.UsecaseException;
034: import org.apache.lenya.cms.workflow.usecases.InvokeWorkflow;
035:
036: /**
037: * Deactivate usecase handler.
038: *
039: * @version $Id: Deactivate.java 589677 2007-10-29 15:33:17Z andreas $
040: */
041: public class Deactivate extends InvokeWorkflow {
042:
043: protected static final String LINKS_TO_DOCUMENT = "linksToDocument";
044:
045: /**
046: * Checks if the workflow event is supported and the parent of the document exists in the live
047: * area.
048: *
049: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
050: */
051: protected void doCheckPreconditions() throws Exception {
052: super .doCheckPreconditions();
053:
054: if (!hasErrors()) {
055:
056: Document doc = getSourceDocument();
057:
058: if (!doc.getArea().equals(Publication.AUTHORING_AREA)) {
059: addErrorMessage("This usecase can only be invoked from the authoring area.");
060: return;
061: }
062:
063: if (!doc.existsAreaVersion(Publication.LIVE_AREA)) {
064: addErrorMessage("This usecase can only be invoked when the live version exists.");
065: } else {
066: Document liveDoc = doc
067: .getAreaVersion(Publication.LIVE_AREA);
068: NodeSet subSite = SiteUtil.getSubSite(this .manager,
069: liveDoc.getLink().getNode());
070: SiteNode node = liveDoc.getLink().getNode();
071: subSite.remove(node);
072:
073: if (!subSite.isEmpty()) {
074: addErrorMessage("You can't deactivate this document because it has children.");
075: }
076: setParameter(LINKS_TO_DOCUMENT, new LinkList(
077: this .manager, doc));
078: }
079: }
080: }
081:
082: /**
083: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
084: */
085: protected org.apache.lenya.cms.repository.Node[] getNodesToLock()
086: throws UsecaseException {
087: try {
088: List nodes = new ArrayList();
089:
090: Document doc = getSourceDocument();
091: if (doc != null) {
092: nodes.add(doc.getRepositoryNode());
093: Document liveDoc = doc
094: .getAreaVersion(Publication.LIVE_AREA);
095: nodes.add(liveDoc.getRepositoryNode());
096: nodes.add(liveDoc.area().getSite().getRepositoryNode());
097: }
098: return (org.apache.lenya.cms.repository.Node[]) nodes
099: .toArray(new org.apache.lenya.cms.repository.Node[nodes
100: .size()]);
101:
102: } catch (Exception e) {
103: throw new UsecaseException(e);
104: }
105: }
106:
107: /**
108: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
109: */
110: protected void doExecute() throws Exception {
111: super .doExecute();
112: deactivate(getSourceDocument());
113: }
114:
115: /**
116: * Deactivates a document.
117: *
118: * @param authoringDocument The authoring document.
119: */
120: protected void deactivate(Document authoringDocument) {
121:
122: boolean success = false;
123:
124: DocumentManager documentManager = null;
125: try {
126: Document liveDocument = authoringDocument
127: .getAreaVersion(Publication.LIVE_AREA);
128:
129: documentManager = (DocumentManager) this .manager
130: .lookup(DocumentManager.ROLE);
131: documentManager.delete(liveDocument);
132:
133: success = true;
134: } catch (Exception e) {
135: throw new RuntimeException(e);
136: } finally {
137: if (getLogger().isDebugEnabled()) {
138: getLogger().debug(
139: "Deactivate document [" + authoringDocument
140: + "]. Success: [" + success + "]");
141: }
142: if (documentManager != null) {
143: this .manager.release(documentManager);
144: }
145: }
146:
147: }
148:
149: protected String getEvent() {
150: return "deactivate";
151: }
152:
153: /**
154: * A list of links pointing to a document. Allows lazy loading rom the usecase view.
155: */
156: public static class LinkList {
157:
158: private Document document;
159: private Document[] documents;
160: private ServiceManager manager;
161:
162: /**
163: * @param manager The manager.
164: * @param doc The document to resolve the links from.
165: */
166: public LinkList(ServiceManager manager, Document doc) {
167: this .manager = manager;
168: this .document = doc;
169: }
170:
171: /**
172: * @return The link documents.
173: */
174: public Document[] getDocuments() {
175: if (this .documents == null) {
176: this .documents = getLinksToDocument();
177: }
178: return this .documents;
179: }
180:
181: protected Document[] getLinksToDocument() {
182: Set docs = new HashSet();
183: LinkManager linkMgr = null;
184: try {
185: linkMgr = (LinkManager) this .manager
186: .lookup(LinkManager.ROLE);
187: Document liveVersion = this .document
188: .getAreaVersion(Publication.LIVE_AREA);
189: Document[] referencingDocs = linkMgr
190: .getReferencingDocuments(liveVersion);
191: for (int d = 0; d < referencingDocs.length; d++) {
192: Document doc = referencingDocs[d];
193: if (doc.getArea().equals(Publication.LIVE_AREA)) {
194: docs.add(doc);
195: }
196: }
197: } catch (Exception e) {
198: throw new RuntimeException(e);
199: } finally {
200: if (linkMgr != null) {
201: this .manager.release(linkMgr);
202: }
203: }
204: return (Document[]) docs.toArray(new Document[docs.size()]);
205: }
206:
207: }
208:
209: }
|