/*
 * 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.Publication;
import org.apache.lenya.cms.site.SiteException;
import org.apache.lenya.cms.site.SiteManager;
import org.apache.lenya.cms.usecase.DocumentUsecase;

/**
 * Change the label of a document.
 * 
 * @version $Id$
 */
public class ChangeLabel extends DocumentUsecase {

    protected static final String LABEL = "label";
    protected static final String DOCUMENT_ID = "documentId";

    /**
     * @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!");
        }
    }

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
     */
    protected void initParameters() {
        super.initParameters();
        Document document = getSourceDocument();
        if (document != null) {
            setParameter(DOCUMENT_ID, document.getId());
            try {
                SiteManager _manager = document.getPublication().getSiteManager(document
                        .getIdentityMap());
                setParameter(LABEL, _manager.getLabel(document));
            } catch (SiteException e) {
                throw new RuntimeException(e);
            }
        }
    }

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

        Document document = getSourceDocument();
        SiteManager _manager = document.getPublication().getSiteManager(document.getIdentityMap());

        String label = getParameterAsString(LABEL);
        _manager.setLabel(document, label);
    }
}
@@ -777,56 +777,8 @@
 on;%0A
-import org.apache.lenya.cms.site.SiteException;%0A
 impo
@@ -819,16 +819,16 @@
 anager;%0A
+
 import o
@@ -1365,32 +1365,33 @@
 turn;%0A        %7D%0A
+%0A
         if (!get
@@ -1543,16 +1543,156 @@
 rea!%22);%0A
+        %7D else if (!getSourceDocument().exists()) %7B%0A            addErrorMessage(%22This usecase can only be invoked on existing documents.%22);%0A
         
@@ -1901,32 +1901,50 @@
 urceDocument();%0A
+        try %7B%0A    
         if (docu
@@ -1955,20 +1955,45 @@
  != null
-) %7B%0A
+ && document.exists()) %7B%0A    
         
@@ -2045,26 +2045,8 @@
 ));%0A
-            try %7B%0A
     
@@ -2257,16 +2257,26 @@
    %7D
+%0A        %7D
  catch (
 Site
@@ -2271,20 +2271,16 @@
  catch (
-Site
 Exceptio
@@ -2298,20 +2298,16 @@
         
-  
-  
 throw ne
@@ -2329,30 +2329,16 @@
 ion(e);%0A
-            %7D%0A
         

check if document exists in checkPreconditions()