001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: ChainSelectActionsBean.java 28950 2008-01-11 13:35:06Z tdelprat $
020 */
021
022package org.nuxeo.ecm.webapp.directory;
023
024import static org.jboss.seam.ScopeType.SESSION;
025
026import java.io.Serializable;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.Iterator;
030import java.util.LinkedHashMap;
031import java.util.List;
032
033import javax.faces.application.FacesMessage;
034import javax.faces.component.UIComponent;
035import javax.faces.context.FacesContext;
036import javax.faces.event.ActionEvent;
037
038import org.apache.commons.logging.Log;
039import org.apache.commons.logging.LogFactory;
040import org.jboss.seam.annotations.Name;
041import org.jboss.seam.annotations.Scope;
042import org.nuxeo.ecm.platform.ui.web.directory.ChainSelect;
043import org.nuxeo.ecm.platform.ui.web.directory.ChainSelectStatus;
044import org.nuxeo.ecm.platform.ui.web.directory.Selection;
045import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
046
047/**
048 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a>
049 */
050@Name("chainSelectActions")
051@Scope(SESSION)
052public class ChainSelectActionsBean implements ChainSelectActions, Serializable {
053
054    private static final long serialVersionUID = 27502317512904295L;
055
056    private static final Log log = LogFactory.getLog(ChainSelectActionsBean.class);
057
058    private ChainSelect getChainSelect(ActionEvent event) {
059        UIComponent component = event.getComponent();
060        while (!(component instanceof ChainSelect)) {
061            component = component.getParent();
062        }
063        return (ChainSelect) component;
064    }
065
066    public void add(ActionEvent event) {
067        ChainSelect chainSelect = getChainSelect(event);
068        FacesContext context = FacesContext.getCurrentInstance();
069        boolean allowBranchSelection = chainSelect.getBooleanProperty("allowBranchSelection", false);
070        boolean allowRootSelection = chainSelect.getBooleanProperty("allowRootSelection", false);
071        int size = chainSelect.getSize();
072        String clientId = chainSelect.getClientId(context);
073
074        LinkedHashMap<String, Selection> map = new LinkedHashMap<String, Selection>();
075        for (Selection selection : chainSelect.getComponentValue()) {
076            map.put(selection.getValue(chainSelect.getKeySeparator()), selection);
077        }
078        for (Selection selection : chainSelect.getSelections()) {
079            int selectionSize = selection.getSize();
080            if (!allowRootSelection && selectionSize == 0) {
081                String messageStr = ComponentUtils.translate(context, "label.chainSelect.empty_selection");
082                FacesMessage message = new FacesMessage(messageStr);
083                context.addMessage(clientId, message);
084                chainSelect.setValid(false);
085                return;
086            }
087            if (!allowBranchSelection && selectionSize > 0 && selectionSize != size) {
088                String messageStr = ComponentUtils.translate(context, "label.chainSelect.incomplete_selection");
089                FacesMessage message = new FacesMessage(messageStr);
090                context.addMessage(clientId, message);
091                chainSelect.setValid(false);
092                return;
093            }
094
095            map.put(selection.getValue(chainSelect.getKeySeparator()), selection);
096        }
097
098        Selection[] componentValue = map.values().toArray(new Selection[0]);
099
100        String[] submittedValue;
101        if (componentValue.length == 0) {
102            submittedValue = null;
103        } else {
104            submittedValue = new String[componentValue.length];
105            for (int i = 0; i < componentValue.length; i++) {
106                submittedValue[i] = componentValue[i].getValue(chainSelect.getKeySeparator());
107            }
108        }
109
110        chainSelect.setComponentValue(componentValue);
111        chainSelect.setSubmittedValue(submittedValue);
112        context.renderResponse();
113        log.debug("add: submittedValue=" + ChainSelect.format(submittedValue));
114    }
115
116    public void delete(ActionEvent event) {
117        FacesContext context = FacesContext.getCurrentInstance();
118        ChainSelect chainSelect = getChainSelect(event);
119        List<Selection> componentValueList = new ArrayList<Selection>();
120        componentValueList.addAll(Arrays.asList(chainSelect.getComponentValue()));
121
122        String value = context.getExternalContext().getRequestParameterMap().get(ChainSelectStatus.REMOVE_ID);
123
124        for (Iterator<Selection> i = componentValueList.iterator(); i.hasNext();) {
125            Selection selection = i.next();
126            if (selection.getValue(chainSelect.getKeySeparator()).equals(value)) {
127                i.remove();
128            }
129        }
130        Selection[] componentValue = componentValueList.toArray(new Selection[0]);
131        String[] submittedValue = null;
132        if (componentValue.length != 0) {
133            submittedValue = new String[componentValue.length];
134            for (int i = 0; i < componentValue.length; i++) {
135                submittedValue[i] = componentValue[i].getValue(chainSelect.getKeySeparator());
136            }
137        }
138
139        chainSelect.setComponentValue(componentValue);
140        chainSelect.setSubmittedValue(submittedValue);
141        context.renderResponse();
142    }
143}