001/*
002 * (C) Copyright 2010 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 * Contributors:
016 *     Nuxeo - initial API and implementation
017 */
018
019package org.nuxeo.ecm.platform.ui.web.component.tree;
020
021import static org.jboss.seam.ScopeType.CONVERSATION;
022import static org.jboss.seam.annotations.Install.FRAMEWORK;
023
024import java.io.Serializable;
025import java.util.List;
026
027import javax.faces.component.UIComponent;
028import javax.faces.component.UIInput;
029import javax.faces.event.ActionEvent;
030
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Install;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.annotations.web.RequestParameter;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.DocumentRef;
039import org.nuxeo.ecm.core.api.PathRef;
040import org.nuxeo.ecm.platform.ui.web.component.list.UIEditableList;
041import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
042
043/**
044 * Action to handle tree widget.
045 *
046 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
047 * @since 5.4
048 */
049@Name("treeWidgetActions")
050@Scope(CONVERSATION)
051@Install(precedence = FRAMEWORK)
052public class TreeWidgetActions implements Serializable {
053
054    private static final long serialVersionUID = 1L;
055
056    @In(create = true, required = false)
057    protected transient CoreSession documentManager;
058
059    @RequestParameter
060    protected String selectionListId;
061
062    @RequestParameter
063    protected String selectionInputId;
064
065    @RequestParameter
066    protected String selectedPath;
067
068    @SuppressWarnings("unchecked")
069    public void addSelectionToList(ActionEvent event) {
070        UIComponent component = event.getComponent();
071        if (component == null) {
072            return;
073        }
074        UIComponent base = ComponentUtils.getBase(component);
075        UIEditableList list = ComponentUtils.getComponent(base, selectionListId, UIEditableList.class);
076
077        if (list != null) {
078            List<String> values = (List<String>) list.getEditableModel().getWrappedData();
079            // add selected value to the list
080            if (!values.contains(selectedPath)) {
081                list.addValue(selectedPath);
082            }
083        }
084    }
085
086    public void setUIInputValue(ActionEvent event) {
087        UIComponent component = event.getComponent();
088        if (component == null) {
089            return;
090        }
091        UIComponent base = ComponentUtils.getBase(component);
092        UIInput uiInput = ComponentUtils.getComponent(base, selectionInputId, UIInput.class);
093
094        if (uiInput != null) {
095            uiInput.setSubmittedValue(selectedPath);
096        }
097    }
098
099    /**
100     * Returns the {@code DocumentModel} referenced by the given path if exists, {@code null} otherwise.
101     */
102    public DocumentModel getDocumentFromPath(String path) {
103        // handle root document differently as user may not have browse rights
104        // on it
105        if ("/".equals(path)) {
106            return documentManager.getRootDocument();
107        }
108        DocumentRef ref = new PathRef(path);
109        return documentManager.exists(ref) ? documentManager.getDocument(new PathRef(path)) : null;
110    }
111
112}