/*
 * 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.modules.news.usecases;

import java.util.ArrayList;
import java.util.List;

import org.apache.lenya.cms.workflow.usecases.InvokeWorkflow;
import org.apache.lenya.modules.collection.CollectionWrapper;
import org.apache.lenya.modules.news.NewsWrapper;

/**
 * Edit the properties of a news document.
 */
public class Edit extends InvokeWorkflow {

    protected static final String INCLUDE_ITEM_NUMBER = "includeItems";
    protected static final String SHOW_ALL_LANGUAGES = "showAllLanguages";
    protected static final String NEWS_WRAPPER = "newsWrapper";
    protected static final String NUMBERS = "numbers";
    protected static final String TYPE = "type";
    protected static final String HREF = "href";
    
    protected void initParameters() {
        super.initParameters();
        NewsWrapper news = new NewsWrapper(getSourceDocument(), getLogger());
        setParameter(NEWS_WRAPPER, news);
        
        setParameter(INCLUDE_ITEM_NUMBER, new Short(news.getIncludeItemNumber()));
        setParameter(SHOW_ALL_LANGUAGES, new Boolean(news.showAllLanguages()));
        setParameter(TYPE, news.getType());
        setParameter(HREF, news.getHref());
        
        List numbers = new ArrayList();
        for (int i = 1; i <= 10; i++) {
            numbers.add(new Integer(i));
        }
        setParameter(NUMBERS, numbers);
    }

    protected void doExecute() throws Exception {
        super.doExecute();
        
        String numberString = getParameterAsString(INCLUDE_ITEM_NUMBER);
        short number = Short.parseShort(numberString);
        
        String allLanguagesString = getBooleanCheckboxParameter(SHOW_ALL_LANGUAGES);
        boolean allLanguages = Boolean.valueOf(allLanguagesString).booleanValue();
        
        // we must create a new wrapper, because a new (modifiable) session is used
        NewsWrapper news = new NewsWrapper(getSourceDocument(), getLogger());
        news.setIncludeItemNumber(number);
        news.setShowAllLanguages(allLanguages);
        String type = getParameterAsString(TYPE);
        news.setType(type);
        if (type.equals(CollectionWrapper.TYPE_LINK)) {
            news.setHref(getParameterAsString(HREF));
        }
        news.save();
    }

}

@@ -910,16 +910,66 @@
 .List;%0A%0A
+import org.apache.lenya.cms.publication.Document;%0A
 import o
@@ -1022,16 +1022,16 @@
 rkflow;%0A
-
 import o
@@ -1665,24 +1665,99 @@
 rameters();%0A
+        Document doc = getSourceDocument();%0A        if (doc != null) %7B%0A    
         News
@@ -1787,35 +1787,19 @@
 Wrapper(
-getSourceDocument()
+doc
 , getLog
@@ -1806,32 +1806,36 @@
 ger());%0A        
+  
+  
 setParameter(NEW
@@ -1852,33 +1852,41 @@
  news);%0A        
-%0A
+    %0A    
         setParam
@@ -1952,32 +1952,36 @@
 er()));%0A        
+    
 setParameter(SHO
@@ -2036,32 +2036,36 @@
 es()));%0A        
+    
 setParameter(TYP
@@ -2084,32 +2084,36 @@
 ype());%0A        
+ 
+   
 setParameter(HRE
@@ -2140,25 +2140,32 @@
         
-%0A
+    %0A   
         
 List num
@@ -2156,16 +2156,17 @@
         
+ 
 List num
@@ -2185,24 +2185,28 @@
 rrayList();%0A
+    
         for 
@@ -2229,24 +2229,28 @@
  10; i++) %7B%0A
+    
             
@@ -2278,34 +2278,42 @@
 er(i));%0A        
-%7D%0A
+    %7D%0A    
         setParam
@@ -2332,24 +2332,34 @@
 , numbers);%0A
+        %7D%0A
     %7D%0A%0A    p

Check document existence before news editor usecase parameter initialization.