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 Delete 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: Document liveDocument = getSourceDocument().getAreaVersion(
052: Publication.LIVE_AREA);
053: if (liveDocument.exists()) {
054: addErrorMessage("The document cannot be deleted because it's Live, deactivate it first");
055: return;
056: }
057: String event = getEvent();
058: if (!WorkflowUtil.canInvoke(this .manager, getSession(),
059: getLogger(), getSourceDocument(), event)) {
060: addInfoMessage("The document cannot be deactivated because the workflow event cannot be invoked.");
061: }
062: }
063: }
064:
065: /**
066: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
067: */
068: protected org.apache.lenya.cms.repository.Node[] getNodesToLock()
069: throws UsecaseException {
070: try {
071: List nodes = new ArrayList();
072: Document doc = getSourceDocument();
073: nodes.add(doc.getRepositoryNode());
074: nodes.add(doc.area().getSite().getRepositoryNode());
075: return (org.apache.lenya.cms.repository.Node[]) nodes
076: .toArray(new org.apache.lenya.cms.repository.Node[nodes
077: .size()]);
078: } catch (Exception e) {
079: throw new UsecaseException(e);
080: }
081: }
082:
083: /**
084: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
085: */
086: protected void doExecute() throws Exception {
087: super .doExecute();
088: delete(getSourceDocument());
089: setTargetDocument(getDocumentFactory().get(
090: getSourceDocument().getPublication(),
091: Publication.AUTHORING_AREA, "/feeds/all/index"));
092: }
093:
094: /**
095: * Deletes a document.
096: * @param document The document to delete.
097: */
098: protected void delete(Document document) {
099: DocumentManager documentManager = null;
100: boolean success = false;
101: try {
102: documentManager = (DocumentManager) this .manager
103: .lookup(DocumentManager.ROLE);
104: documentManager.delete(document);
105: WorkflowUtil.invoke(this .manager, getSession(),
106: getLogger(), document, getEvent());
107: success = true;
108: } catch (Exception e) {
109: throw new RuntimeException(e);
110: } finally {
111: if (getLogger().isDebugEnabled()) {
112: getLogger().debug(
113: "Delete document [" + getSourceDocument()
114: + "]. Success: [" + success + "]");
115: }
116: if (documentManager != null) {
117: this .manager.release(documentManager);
118: }
119: }
120: }
121:
122: /**
123: * @return The event to invoke.
124: */
125: private String getEvent() {
126: return "delete";
127: }
128: }
|