001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.value.mapper;
034:
035: import com.flexive.shared.exceptions.FxInvalidParameterException;
036: import com.flexive.shared.exceptions.FxRuntimeException;
037: import com.flexive.shared.search.query.PropertyValueComparator;
038: import com.flexive.shared.search.query.ValueComparator;
039: import com.flexive.shared.structure.FxDataType;
040: import com.flexive.shared.structure.FxSelectList;
041: import com.flexive.shared.structure.FxSelectListItem;
042: import com.flexive.shared.value.FxLargeNumber;
043: import com.flexive.shared.value.FxSelectOne;
044: import com.flexive.shared.value.FxString;
045:
046: import java.util.List;
047:
048: /**
049: * Maps the given ordinal input type to a select list. The select list must contain at least one element.
050: *
051: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: * @version $Rev: 181 $
053: */
054: public class SelectOneInputMapper extends
055: InputMapper<FxLargeNumber, FxSelectOne> {
056: private final FxSelectList selectList;
057:
058: /**
059: * Create a new select-one input mapper. The input mapper will select a select item
060: * of the given list based on the ordinal value of the value passed
061: * to {@link #encode(com.flexive.shared.value.FxLargeNumber FxLargeNumber)} and return
062: * the appropriate {@link com.flexive.shared.value.FxSelectOne} object. If no valid value is selected,
063: * the first select list value will be used.
064: *
065: * @param selectList the select list. If null or empty, a RuntimeException will be trhown.
066: */
067: public SelectOneInputMapper(FxSelectList selectList) {
068: if (selectList == null) {
069: throw new FxInvalidParameterException("SELECTLIST",
070: "ex.content.value.mapper.select.empty")
071: .asRuntimeException();
072: } else if (selectList.getItems().isEmpty()) {
073: // we need an "empty" element
074: new FxSelectListItem(-1, selectList, -1, new FxString(""));
075: }
076: this .selectList = selectList;
077: }
078:
079: /** {@inheritDoc} */
080: @Override
081: public FxSelectOne encode(FxLargeNumber value) {
082: if (value.isMultiLanguage()) {
083: throw new FxInvalidParameterException("VALUE",
084: "ex.content.value.mapper.select.singleLanguage")
085: .asRuntimeException();
086: }
087: FxSelectListItem item;
088: try {
089: item = selectList.getItem(value.getDefaultTranslation());
090: } catch (FxRuntimeException e) {
091: item = selectList.getItems().get(0); // select first item as default
092: }
093: return new FxSelectOne(value.isMultiLanguage(), item);
094: }
095:
096: /** {@inheritDoc} */
097: @Override
098: public List<? extends ValueComparator> getAvailableValueComparators() {
099: return PropertyValueComparator
100: .getAvailable(FxDataType.SelectOne);
101: }
102: }
|