001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.component;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.List;
023import java.util.Map;
024import java.util.Map.Entry;
025
026import javax.el.PropertyNotFoundException;
027import javax.faces.model.ListDataModel;
028import javax.faces.model.SelectItem;
029import javax.faces.model.SelectItemGroup;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033
034/**
035 * Helper for select items management
036 *
037 * @since 6.0
038 */
039public abstract class SelectItemsFactory extends SelectItemFactory {
040
041    private static final Log log = LogFactory.getLog(SelectItemsFactory.class);
042
043    @Override
044    protected abstract String getVar();
045
046    @Override
047    protected abstract SelectItem createSelectItem();
048
049    @SuppressWarnings({ "unchecked", "rawtypes" })
050    public List<SelectItem> createSelectItems(Object value) {
051        Object varValue = saveRequestMapVarValue();
052        try {
053            List items = new ArrayList();
054            if (value instanceof ListDataModel) {
055                ListDataModel ldm = (ListDataModel) value;
056                value = ldm.getWrappedData();
057            }
058
059            if (value instanceof Object[]) {
060                Object[] array = (Object[]) value;
061                for (Object currentItem : array) {
062                    SelectItem[] res = createSelectItemsFrom(currentItem);
063                    if (res != null) {
064                        items.addAll(Arrays.asList(res));
065                    }
066                }
067            } else if (value instanceof Collection) {
068                Collection collection = (Collection) value;
069                for (Object currentItem : collection) {
070                    SelectItem[] res = createSelectItemsFrom(currentItem);
071                    if (res != null) {
072                        items.addAll(Arrays.asList(res));
073                    }
074                }
075            } else if (value instanceof Map) {
076                Map map = (Map) value;
077                for (Object obj : map.entrySet()) {
078                    Entry currentItem = (Entry) obj;
079                    SelectItem[] res = createSelectItemsFrom(currentItem);
080                    if (res != null) {
081                        items.addAll(Arrays.asList(res));
082                    }
083                }
084            } else if (value != null) {
085                log.warn("Could not map values to select items, value is not supported: " + value);
086            }
087            return items;
088        } finally {
089            restoreRequestMapVarValue(varValue);
090        }
091    }
092
093    protected SelectItem[] createSelectItemsFrom(Object item) {
094        if (item instanceof SelectItemGroup) {
095            return ((SelectItemGroup) item).getSelectItems();
096        } else {
097            SelectItem selectItem = null;
098            try {
099                putIteratorToRequestParam(item);
100                selectItem = createSelectItem();
101                removeIteratorFromRequestParam();
102            } catch (PropertyNotFoundException e) {
103                if (item instanceof SelectItem) {
104                    // maybe lookup was not necessary
105                } else {
106                    throw e;
107                }
108            }
109            if (selectItem != null) {
110                return new SelectItem[] { selectItem };
111            } else if (item instanceof SelectItem) {
112                // no transformation performed
113                return new SelectItem[] { (SelectItem) item };
114            }
115        }
116        return null;
117    }
118
119    @Override
120    public SelectItem createSelectItem(Object value) {
121        throw new IllegalArgumentException("Use createSelectItems instead");
122    }
123
124}