/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  Licensed 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.cms.site.usecases;

import org.apache.lenya.cms.publication.Document;
import org.apache.lenya.cms.publication.DocumentIdentityMap;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.publication.util.DocumentHelper;
import org.apache.lenya.cms.publication.util.DocumentSet;
import org.apache.lenya.cms.site.SiteManager;
import org.apache.lenya.cms.usecase.DocumentUsecase;

/**
 * Delete a document and all its descendants, including all language versions.
 * The documents are moved to the trash.
 * 
 * @version $Id:$
 */
public class Delete extends DocumentUsecase {

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
     */
    protected void doCheckPreconditions() throws Exception {
        super.doCheckPreconditions();
        if (!getErrorMessages().isEmpty()) {
            return;
        }

        if (!getSourceDocument().getArea().equals(Publication.AUTHORING_AREA)) {
            addErrorMessage("This usecase can only be invoked in the authoring area!");
        }
        
        Document document = getSourceDocument();
        DocumentIdentityMap identityMap = getUnitOfWork().getIdentityMap();
        SiteManager manager = document.getPublication().getSiteManager(identityMap);
        DocumentSet set = new DocumentSet(manager.getRequiringResources(document));
        set.add(document);
        Document[] documents = set.getDocuments();
        for (int i = 0; i < documents.length; i++) {
            Document liveVersion = identityMap.getFactory().getAreaVersion(documents[i], Publication.LIVE_AREA);
            if (liveVersion.exists()) {
                addErrorMessage("Cannot delete because document [" + liveVersion + "] is live!");
            }
        }
    }

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
     */
    protected void doExecute() throws Exception {
        super.doExecute();

        Document source = getSourceDocument();
        DocumentIdentityMap identityMap = source.getIdentityMap();

        Document target = identityMap.getFactory().getAreaVersion(source, Publication.TRASH_AREA);
        getDocumentManager().moveAll(source, target);

        Document parent = source.getIdentityMap().getFactory().getParent(source, "/index");
        setTargetDocument(DocumentHelper.getExistingLanguageVersion(parent));
    }

}
@@ -953,16 +953,79 @@
 entSet;%0A
+import org.apache.lenya.cms.publication.util.UniqueDocumentId;%0A
 import o
@@ -1763,24 +1763,16 @@
       %7D%0A
-        
 %0A       
@@ -2277,16 +2277,36 @@
 ents%5Bi%5D,
+%0A                   
  Publica
@@ -2781,73 +2781,209 @@
     
-Document target = identityMap.getFactory().getAreaVersion(source,
+String desiredId = %22/%22 + source.getName();%0A        UniqueDocumentId helper = new UniqueDocumentId();%0A        String availableId = helper.computeUniqueDocumentId(source.getPublication(),%0A               
  Pub
@@ -3001,27 +3001,27 @@
 ASH_AREA
-);%0A
+,%0A 
         
 getDocum
@@ -3016,55 +3016,27 @@
     
-getDocumentManager().moveAll(source, target
+       desiredId
 );%0A
-%0A
     
@@ -3052,28 +3052,18 @@
 ent 
-p
+t
 ar
+g
 e
-n
 t = 
-source.getI
+i
 dent
@@ -3068,18 +3068,16 @@
 ntityMap
-()
 .getFact
@@ -3089,31 +3089,44 @@
 .get
-Parent(source, %22/index%22
+(Publication.TRASH_AREA, availableId
 );%0A 
@@ -3136,14 +3136,8 @@
     
-setTar
 getD
@@ -3147,58 +3147,76 @@
 ment
-(DocumentHelper.getExistingLanguageVersion(parent)
+Manager().moveAll(source, target);%0A%0A        setTargetDocument(target
 );%0A 

use unique document ID in trash area