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