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