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.blog.cms.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.DocumentManager;
025: import org.apache.lenya.cms.publication.Publication;
026: import org.apache.lenya.cms.usecase.DocumentUsecase;
027: import org.apache.lenya.cms.usecase.UsecaseException;
028: import org.apache.lenya.cms.workflow.WorkflowUtil;
029:
030: /**
031: * Deactivate usecase handler.
032: *
033: * @version $Id: Deactivate.java 264805 2005-08-30 16:20:15Z andreas $
034: */
035: public class Deactivate extends DocumentUsecase {
036:
037: /**
038: * Checks if the workflow event is supported and the parent of the document exists in the live
039: * area.
040: *
041: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
042: */
043: protected void doCheckPreconditions() throws Exception {
044: super .doCheckPreconditions();
045: if (!hasErrors()) {
046: if (!getSourceDocument().getArea().equals(
047: Publication.AUTHORING_AREA)) {
048: addErrorMessage("This usecase can only be invoked from the authoring area.");
049: return;
050: }
051: String event = getEvent();
052: if (!WorkflowUtil.canInvoke(this .manager, getSession(),
053: getLogger(), getSourceDocument(), event)) {
054: addInfoMessage("The document cannot be deactivated because the workflow event cannot be invoked.");
055: }
056: }
057: }
058:
059: /**
060: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
061: */
062: protected org.apache.lenya.cms.repository.Node[] getNodesToLock()
063: throws UsecaseException {
064: try {
065: List nodes = new ArrayList();
066: Document doc = getSourceDocument();
067: Document liveDoc = doc
068: .getAreaVersion(Publication.LIVE_AREA);
069: nodes.add(doc.getRepositoryNode());
070: nodes.add(liveDoc.getRepositoryNode());
071: nodes.add(liveDoc.area().getSite().getRepositoryNode());
072: nodes.add(doc.area().getSite().getRepositoryNode());
073: return (org.apache.lenya.cms.repository.Node[]) nodes
074: .toArray(new org.apache.lenya.cms.repository.Node[nodes
075: .size()]);
076: } catch (Exception e) {
077: throw new UsecaseException(e);
078: }
079: }
080:
081: /**
082: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
083: */
084: protected void doExecute() throws Exception {
085: super .doExecute();
086: deactivate(getSourceDocument());
087: }
088:
089: /**
090: * Deactivates a document.
091: *
092: * @param authoringDocument The authoring document.
093: */
094: protected void deactivate(Document authoringDocument) {
095:
096: boolean success = false;
097:
098: DocumentManager documentManager = null;
099: try {
100: Document liveDocument = authoringDocument
101: .getAreaVersion(Publication.LIVE_AREA);
102:
103: documentManager = (DocumentManager) this .manager
104: .lookup(DocumentManager.ROLE);
105: documentManager.delete(liveDocument);
106:
107: WorkflowUtil.invoke(this .manager, getSession(),
108: getLogger(), authoringDocument, getEvent());
109: success = true;
110: } catch (Exception e) {
111: throw new RuntimeException(e);
112: } finally {
113: if (getLogger().isDebugEnabled()) {
114: getLogger().debug(
115: "Deactivate document [" + authoringDocument
116: + "]. Success: [" + success + "]");
117: }
118: if (documentManager != null) {
119: this .manager.release(documentManager);
120: }
121: }
122:
123: }
124:
125: /**
126: * @return The event to invoke.
127: */
128: private String getEvent() {
129: return "deactivate";
130: }
131: }
|