blob: a5c881376f3b9e2898d8832fa82df1a21295839e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.lenya.blog.cms.usecases;
import java.util.ArrayList;
import java.util.List;
import org.apache.lenya.cms.publication.Document;
import org.apache.lenya.cms.publication.DocumentManager;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.usecase.DocumentUsecase;
import org.apache.lenya.cms.usecase.UsecaseException;
import org.apache.lenya.cms.workflow.WorkflowUtil;
/**
* Deactivate usecase handler.
*
* @version $Id: Deactivate.java 264805 2005-08-30 16:20:15Z andreas $
*/
public class Deactivate extends DocumentUsecase {
/**
* Checks if the workflow event is supported and the parent of the document exists in the live
* area.
*
* @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
*/
protected void doCheckPreconditions() throws Exception {
super.doCheckPreconditions();
if (!hasErrors()) {
if (!getSourceDocument().getArea().equals(Publication.AUTHORING_AREA)) {
addErrorMessage("This usecase can only be invoked from the authoring area.");
return;
}
String event = getEvent();
if (!WorkflowUtil.canInvoke(this.manager,
getSession(),
getLogger(),
getSourceDocument(),
event)) {
addInfoMessage("The document cannot be deactivated because the workflow event cannot be invoked.");
}
}
}
/**
* @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
*/
protected org.apache.lenya.cms.repository.Node[] getNodesToLock() throws UsecaseException {
try {
List nodes = new ArrayList();
Document doc = getSourceDocument();
Document liveDoc = doc.getAreaVersion(Publication.LIVE_AREA);
nodes.add(doc.getRepositoryNode());
nodes.add(liveDoc.getRepositoryNode());
nodes.add(liveDoc.area().getSite().getRepositoryNode());
nodes.add(doc.area().getSite().getRepositoryNode());
return (org.apache.lenya.cms.repository.Node[]) nodes.toArray(new org.apache.lenya.cms.repository.Node[nodes.size()]);
} catch (Exception e) {
throw new UsecaseException(e);
}
}
/**
* @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
*/
protected void doExecute() throws Exception {
super.doExecute();
deactivate(getSourceDocument());
}
/**
* Deactivates a document.
*
* @param authoringDocument The authoring document.
*/
protected void deactivate(Document authoringDocument) {
boolean success = false;
DocumentManager documentManager = null;
try {
Document liveDocument = authoringDocument.getAreaVersion(Publication.LIVE_AREA);
documentManager = (DocumentManager) this.manager.lookup(DocumentManager.ROLE);
documentManager.delete(liveDocument);
WorkflowUtil.invoke(this.manager,
getSession(),
getLogger(),
authoringDocument,
getEvent());
success = true;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Deactivate document [" + authoringDocument + "]. Success: ["
+ success + "]");
}
if (documentManager != null) {
this.manager.release(documentManager);
}
}
}
/**
* @return The event to invoke.
*/
private String getEvent() {
return "deactivate";
}
}