/**********************************************************************************
* $URL$
* $Id$
***********************************************************************************
*
* Copyright (c) 2004-2005 The Regents of the University of Michigan, Trustees of Indiana University,
*                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
*
* Licensed under the Educational Community License Version 1.0 (the "License");
* By obtaining, using and/or copying this Original Work, you agree that you have read,
* understand, and will comply with the terms and conditions of the Educational Community License.
* You may obtain a copy of the License at:
*
*      http://cvs.sakaiproject.org/licenses/license_1_0.html
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**********************************************************************************/

package org.sakaiproject.tool.assessment.ui.listener.author;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.tool.assessment.ui.bean.author.ItemAuthorBean;
import org.sakaiproject.tool.assessment.ui.listener.util.ContextUtil;

/**
 * <p>Title: Samigo</p>
 * <p>Description: Sakai Assessment Manager</p>
 * <p>Copyright: Copyright (c) 2004 Sakai Project</p>
 * <p>Organization: Sakai Project</p>
 * @version $Id$
 */

public class StartInsertItemListener implements ValueChangeListener
{
    private static Log log = LogFactory.getLog(StartInsertItemListener.class);


  /**
   * Standard process action method.
   * @param ae ValueChangeEvent
   * @throws AbortProcessingException
   */
  public void processValueChange(ValueChangeEvent ae) throws AbortProcessingException
  {
    log.info("StartInsertItemListener valueChangeLISTENER.");
    ItemAuthorBean itemauthorbean = (ItemAuthorBean) ContextUtil.lookupBean("itemauthor");

    String olditemtype = (String) ae.getOldValue();
    String selectedvalue= (String) ae.getNewValue();
    String newitemtype = null;
    String insertItemPosition = null;
    String insertToSection = null;

    // only set itemtype when the value has indeed changed.
    if ((selectedvalue!=null) && (!selectedvalue.equals("")) ){
      String[] strArray = selectedvalue.split(",");
      newitemtype = strArray[0].trim();
      insertToSection = strArray[1].trim();
      insertItemPosition= strArray[2].trim();
      itemauthorbean.setItemType(newitemtype);
      itemauthorbean.setInsertToSection(insertToSection);
      itemauthorbean.setInsertPosition(insertItemPosition);
      itemauthorbean.setInsertType(newitemtype);
      itemauthorbean.setItemNo(String.valueOf(Integer.parseInt(insertItemPosition) +1));

    StartCreateItemListener listener = new StartCreateItemListener();

    if (!listener.startCreateItem(itemauthorbean))
    {
      throw new RuntimeException("failed to startCreatItem.");
    }


    }

  }


}

@@ -1742,16 +1742,210 @@
 extUtil;
+%0Aimport javax.faces.component.UIComponent;%0Aimport javax.faces.component.UISelectOne;%0Aimport java.util.List;%0Aimport javax.faces.component.UISelectItem;%0Aimport javax.faces.component.UISelectItems;
 %0A%0A/**%0A *
@@ -3024,24 +3024,45 @@
 split(%22,%22);%0A
+%0A      try%0A      %7B%0A  
       newite
@@ -3081,32 +3081,1278 @@
 rray%5B0%5D.trim();%0A
+        ///////// SAK-3114: ///////////////////////////////////////////%0A        // note: you must include at least one selectItem in the form%0A        // type#,p#,q#%0A        // the rest, in a selectItems will get the p#,q# added in.%0A        ///////////////////////////////////////////////////////////////%0A        if (strArray.length %3C 2)%0A        %7B%0A          UISelectOne comp = (UISelectOne) ae.getComponent();%0A          List children = comp.getChildren();%0A          // right now there are two kids selectItems & selectItem%0A          // we use loop to keep this flexible%0A          for (int i = 0; i %3C children.size(); i++) %7B%0A%0A            if (children.get(i) instanceof UISelectItem)%0A            %7B%0A              UISelectItem selectItem = (UISelectItem) children.get(i);%0A              log.info(%22***%22 + i + %22***%22);%0A              log.info(%22selectItem.getItemValue()=%22+selectItem.getItemValue());%0A              String itemValue =  (String) selectItem.getItemValue();%0A              String%5B%5D insertArray = itemValue.split(%22,%22);%0A              // add in p#,q#%0A              insertToSection = insertArray%5B1%5D.trim();%0A              insertItemPosition = insertArray%5B2%5D.trim();%0A              break;%0A            %7D%0A          %7D%0A        %7D%0A        else%0A        %7B%0A    
       insertToSe
@@ -4375,24 +4375,28 @@
 %5B1%5D.trim();%0A
+    
       insert
@@ -4407,16 +4407,17 @@
 Position
+ 
 = strArr
@@ -4426,24 +4426,190 @@
 %5B2%5D.trim();%0A
+        %7D%0A      %7D%0A      catch (Exception ex)%0A      %7B%0A        log.warn(%22unable to process value change: %22 + ex);%0A        ex.printStackTrace();%0A        return;%0A      %7D%0A
       itemau

use the copy from pool q# and p#