001/*
002 * (C) Copyright 2013 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.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 *     <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
016 */
017package org.nuxeo.ecm.platform.ui.select2;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.List;
023import java.util.regex.Pattern;
024
025import javax.faces.component.UIComponent;
026import javax.faces.context.FacesContext;
027import javax.faces.convert.Converter;
028
029import org.apache.commons.lang.StringUtils;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.intercept.BypassInterceptors;
032
033/**
034 * Select2 converter.
035 *
036 * @since 5.7.3
037 */
038@Name("select2Converter")
039@BypassInterceptors
040@org.jboss.seam.annotations.faces.Converter
041public class Select2Converter implements Serializable, Converter {
042
043    private static final long serialVersionUID = 1L;
044
045    protected static final String SEP = ",";
046
047    protected static final String SEP_ATTRIBUTE_NAME = "separator";
048
049    public static String getSeparator() {
050        return SEP;
051    }
052
053    public static String getSeparator(UIComponent component) {
054        String widget_separator = (String) component.getAttributes().get(SEP_ATTRIBUTE_NAME);
055        return StringUtils.isNotBlank(widget_separator) ? widget_separator : getSeparator();
056    }
057
058    @Override
059    public Object getAsObject(FacesContext context, UIComponent component, String value) {
060        if (value == null || value.isEmpty()) {
061            return null;
062        } else {
063            String[] values = value.split(Pattern.quote(getSeparator(component)));
064            // Be careful here, if we just return Arrays.asList(values), the
065            // resulting list will be unmodifiable and this might cause an error
066            // if something try to add elements. Let's make sure it'll be
067            // modifiable
068            return new ArrayList<String>(Arrays.asList(values));
069        }
070    }
071
072    @Override
073    @SuppressWarnings("rawtypes")
074    public String getAsString(FacesContext context, UIComponent component, Object value) {
075        if (value == null) {
076            return null;
077        } else {
078            String stringValue = "";
079            final String separator = getSeparator(component);
080            if (value instanceof List) {
081                for (Object v : (List) value) {
082                    stringValue += v.toString() + separator;
083                }
084            } else if (value instanceof Object[]) {
085                for (Object v : (Object[]) value) {
086                    stringValue += v.toString() + separator;
087                }
088            }
089            if (stringValue.endsWith(separator)) {
090                stringValue = stringValue.substring(0, stringValue.length() - separator.length());
091            }
092            return stringValue;
093        }
094    }
095
096}