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.FxReference;
043: import com.flexive.shared.value.FxSelectOne;
044: import com.flexive.shared.value.FxString;
045:
046: import java.util.List;
047:
048: /**
049: * Input mapper for mapping a FxReference property to a select list. The select
050: * list item IDs must match the FxPK id values of the select list.
051: *
052: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: * @version $Rev: 181 $
054: */
055: public class FxPkSelectOneInputMapper extends
056: InputMapper<FxReference, FxSelectOne> {
057: private final FxSelectList selectList;
058:
059: /**
060: * Create a new select-one input mapper. The input mapper will select a select item
061: * of the given list based on the PK value of the value passed
062: * to {@link #encode(com.flexive.shared.value.FxReference FxReference)} and return
063: * the appropriate {@link FxSelectOne} object. If no valid value is selected,
064: * the first select list value will be used.
065: *
066: * @param selectList the select list. If null or empty, a RuntimeException will be trhown.
067: */
068: public FxPkSelectOneInputMapper(FxSelectList selectList) {
069: if (selectList == null) {
070: throw new FxInvalidParameterException("SELECTLIST",
071: "ex.content.value.mapper.select.empty")
072: .asRuntimeException();
073: } else if (selectList.getItems().isEmpty()) {
074: // we need an "empty" element
075: new FxSelectListItem(-1, selectList, -1, new FxString(""));
076: }
077: this .selectList = selectList;
078: }
079:
080: /** {@inheritDoc} */
081: @Override
082: public FxSelectOne encode(FxReference value) {
083: if (value.isMultiLanguage()) {
084: throw new FxInvalidParameterException("VALUE",
085: "ex.content.value.mapper.select.singleLanguage")
086: .asRuntimeException();
087: }
088: FxSelectListItem item;
089: try {
090: item = selectList.getItem(value.getDefaultTranslation()
091: .getId());
092: } catch (FxRuntimeException e) {
093: item = selectList.getItems().get(0); // select first item as default
094: }
095: return new FxSelectOne(value.isMultiLanguage(), item);
096: }
097:
098: /** {@inheritDoc} */
099: @Override
100: public List<? extends ValueComparator> getAvailableValueComparators() {
101: return PropertyValueComparator
102: .getAvailable(FxDataType.SelectOne);
103: }
104: }
|