001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: ChainSelectActionsBean.java 28950 2008-01-11 13:35:06Z tdelprat $ 018 */ 019 020package org.nuxeo.ecm.webapp.directory; 021 022import static org.jboss.seam.ScopeType.SESSION; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.Iterator; 028import java.util.LinkedHashMap; 029import java.util.List; 030 031import javax.faces.application.FacesMessage; 032import javax.faces.component.UIComponent; 033import javax.faces.context.FacesContext; 034import javax.faces.event.ActionEvent; 035 036import org.apache.commons.logging.Log; 037import org.apache.commons.logging.LogFactory; 038import org.jboss.seam.annotations.Name; 039import org.jboss.seam.annotations.Scope; 040import org.nuxeo.ecm.platform.ui.web.directory.ChainSelect; 041import org.nuxeo.ecm.platform.ui.web.directory.ChainSelectStatus; 042import org.nuxeo.ecm.platform.ui.web.directory.Selection; 043import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; 044 045/** 046 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a> 047 */ 048@Name("chainSelectActions") 049@Scope(SESSION) 050public class ChainSelectActionsBean implements ChainSelectActions, Serializable { 051 052 private static final long serialVersionUID = 27502317512904295L; 053 054 private static final Log log = LogFactory.getLog(ChainSelectActionsBean.class); 055 056 private ChainSelect getChainSelect(ActionEvent event) { 057 UIComponent component = event.getComponent(); 058 while (!(component instanceof ChainSelect)) { 059 component = component.getParent(); 060 } 061 return (ChainSelect) component; 062 } 063 064 public void add(ActionEvent event) { 065 ChainSelect chainSelect = getChainSelect(event); 066 FacesContext context = FacesContext.getCurrentInstance(); 067 boolean allowBranchSelection = chainSelect.getBooleanProperty("allowBranchSelection", false); 068 boolean allowRootSelection = chainSelect.getBooleanProperty("allowRootSelection", false); 069 int size = chainSelect.getSize(); 070 String clientId = chainSelect.getClientId(context); 071 072 LinkedHashMap<String, Selection> map = new LinkedHashMap<String, Selection>(); 073 for (Selection selection : chainSelect.getComponentValue()) { 074 map.put(selection.getValue(chainSelect.getKeySeparator()), selection); 075 } 076 for (Selection selection : chainSelect.getSelections()) { 077 int selectionSize = selection.getSize(); 078 if (!allowRootSelection && selectionSize == 0) { 079 String messageStr = ComponentUtils.translate(context, "label.chainSelect.empty_selection"); 080 FacesMessage message = new FacesMessage(messageStr); 081 context.addMessage(clientId, message); 082 chainSelect.setValid(false); 083 return; 084 } 085 if (!allowBranchSelection && selectionSize > 0 && selectionSize != size) { 086 String messageStr = ComponentUtils.translate(context, "label.chainSelect.incomplete_selection"); 087 FacesMessage message = new FacesMessage(messageStr); 088 context.addMessage(clientId, message); 089 chainSelect.setValid(false); 090 return; 091 } 092 093 map.put(selection.getValue(chainSelect.getKeySeparator()), selection); 094 } 095 096 Selection[] componentValue = map.values().toArray(new Selection[0]); 097 098 String[] submittedValue; 099 if (componentValue.length == 0) { 100 submittedValue = null; 101 } else { 102 submittedValue = new String[componentValue.length]; 103 for (int i = 0; i < componentValue.length; i++) { 104 submittedValue[i] = componentValue[i].getValue(chainSelect.getKeySeparator()); 105 } 106 } 107 108 chainSelect.setComponentValue(componentValue); 109 chainSelect.setSubmittedValue(submittedValue); 110 context.renderResponse(); 111 log.debug("add: submittedValue=" + ChainSelect.format(submittedValue)); 112 } 113 114 public void delete(ActionEvent event) { 115 FacesContext context = FacesContext.getCurrentInstance(); 116 ChainSelect chainSelect = getChainSelect(event); 117 List<Selection> componentValueList = new ArrayList<Selection>(); 118 componentValueList.addAll(Arrays.asList(chainSelect.getComponentValue())); 119 120 String value = context.getExternalContext().getRequestParameterMap().get(ChainSelectStatus.REMOVE_ID); 121 122 for (Iterator<Selection> i = componentValueList.iterator(); i.hasNext();) { 123 Selection selection = i.next(); 124 if (selection.getValue(chainSelect.getKeySeparator()).equals(value)) { 125 i.remove(); 126 } 127 } 128 Selection[] componentValue = componentValueList.toArray(new Selection[0]); 129 String[] submittedValue = null; 130 if (componentValue.length != 0) { 131 submittedValue = new String[componentValue.length]; 132 for (int i = 0; i < componentValue.length; i++) { 133 submittedValue[i] = componentValue[i].getValue(chainSelect.getKeySeparator()); 134 } 135 } 136 137 chainSelect.setComponentValue(componentValue); 138 chainSelect.setSubmittedValue(submittedValue); 139 context.renderResponse(); 140 } 141}