001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.ui.web.directory;
021
022import java.io.Serializable;
023
024import org.apache.commons.lang.StringUtils;
025
026/**
027 * This class represents a list of directory items { item1, item2, item3, .. }, where item <i> represents the item
028 * selected for combo with index i in a chain. The value of a chain select is a list of Selection objects.
029 *
030 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
031 */
032
033public class Selection implements Serializable {
034
035    private static final long serialVersionUID = 2448256162710214563L;
036
037    private final DirectorySelectItem[] columns;
038
039    public Selection(DirectorySelectItem[] columns) {
040        this.columns = columns;
041    }
042
043    public DirectorySelectItem[] getColumns() {
044        return columns;
045    }
046
047    public DirectorySelectItem getColumn(int i) {
048        return columns[i];
049    }
050
051    public String getColumnValue(int i) {
052        if (i >= columns.length || columns[i] == null) {
053            return null;
054        }
055        return (String) columns[i].getValue();
056    }
057
058    public String[] getValues() {
059        String[] values = new String[columns.length];
060        for (int i = 0; i < columns.length; i++) {
061            values[i] = (String) columns[i].getValue();
062        }
063        return values;
064    }
065
066    public String getValue() {
067        return getValue(null);
068    }
069
070    public String getValue(String keySeparator) {
071        String[] values = getValues();
072        return StringUtils.join(values, keySeparator != null ? keySeparator : ChainSelect.DEFAULT_KEY_SEPARATOR);
073    }
074
075    public String[] getLabels() {
076        String[] labels = new String[columns.length];
077        for (int i = 0; i < columns.length; i++) {
078            labels[i] = columns[i].getLabel();
079        }
080        return labels;
081    }
082
083    public String getParentKey(int index, boolean qualifiedParentKeys, String keySeparator) {
084        if (index == 0) {
085            return null;
086        }
087
088        String parentValue = null;
089        if (qualifiedParentKeys) {
090            String[] keys = new String[index];
091            for (int i = 0; i < index; i++) {
092                keys[i] = getColumnValue(i);
093            }
094            parentValue = StringUtils.join(keys, keySeparator != null ? keySeparator
095                    : ChainSelect.DEFAULT_KEY_SEPARATOR);
096        } else {
097            parentValue = getColumnValue(index - 1);
098        }
099
100        return parentValue;
101    }
102
103    @Override
104    public String toString() {
105        return getValue(null);
106    }
107
108    /**
109     * @return
110     */
111    public int getSize() {
112        return columns.length;
113    }
114
115}