| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| IdentifierSelectionModel |
|
| 4.0;4 |
| 1 | /* |
|
| 2 | * Copyright 2004 Chris Nelson |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
|
| 7 | * Unless required by applicable law or agreed to in writing, |
|
| 8 | * software distributed under the License is distributed on an "AS IS" BASIS, |
|
| 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 10 | * See the License for the specific language governing permissions and limitations under the License. |
|
| 11 | */ |
|
| 12 | package org.trails.component; |
|
| 13 | ||
| 14 | import java.util.List; |
|
| 15 | ||
| 16 | import ognl.Ognl; |
|
| 17 | import org.apache.commons.lang.StringUtils; |
|
| 18 | import org.trails.TrailsRuntimeException; |
|
| 19 | ||
| 20 | ||
| 21 | public class IdentifierSelectionModel extends AbstractPropertySelectionModel |
|
| 22 | { |
|
| 23 | 24 | private String idProperty = "id"; |
| 24 | ||
| 25 | public IdentifierSelectionModel(List instances, String idProperty) |
|
| 26 | { |
|
| 27 | 7 | super(instances); |
| 28 | 7 | this.idProperty = idProperty; |
| 29 | 7 | } |
| 30 | ||
| 31 | public IdentifierSelectionModel(List instances, String idProperty, boolean allowNone) |
|
| 32 | { |
|
| 33 | 17 | super(instances, allowNone); |
| 34 | 17 | this.idProperty = idProperty; |
| 35 | 17 | } |
| 36 | ||
| 37 | /* (non-Javadoc) |
|
| 38 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) |
|
| 39 | */ |
|
| 40 | public String getValue(int index) |
|
| 41 | { |
|
| 42 | try |
|
| 43 | { |
|
| 44 | 3 | if (allowNone && index == 0) |
| 45 | { |
|
| 46 | 1 | return DEFAULT_NONE_VALUE; |
| 47 | } else |
|
| 48 | { |
|
| 49 | 2 | return Ognl.getValue(idProperty,instances.get(index)).toString(); |
| 50 | } |
|
| 51 | 0 | } catch (Exception e) |
| 52 | { |
|
| 53 | 0 | throw new TrailsRuntimeException(e); |
| 54 | } |
|
| 55 | } |
|
| 56 | ||
| 57 | /* (non-Javadoc) |
|
| 58 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) |
|
| 59 | */ |
|
| 60 | public Object translateValue(String value) |
|
| 61 | { |
|
| 62 | 6 | if (StringUtils.isEmpty(value)) return null; |
| 63 | 5 | List realInstances = allowNone ? instances.subList(1, instances.size()) : instances; |
| 64 | try |
|
| 65 | { |
|
| 66 | 5 | if (allowNone) |
| 67 | { |
|
| 68 | 3 | if (value.equals(DEFAULT_NONE_VALUE)) return null; |
| 69 | } |
|
| 70 | 4 | List matches = (List) Ognl.getValue( |
| 71 | 4 | "#root.{? #this." + idProperty + ".toString() == \"" + value + "\" }", |
| 72 | 4 | realInstances); |
| 73 | 4 | if (matches.size() > 0) return matches.get(0); |
| 74 | 1 | return null; |
| 75 | 0 | } catch (Exception e) |
| 76 | { |
|
| 77 | 0 | throw new TrailsRuntimeException(e); |
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| 81 | ||
| 82 | public boolean isDisabled(int i) |
|
| 83 | { |
|
| 84 | 0 | return false; |
| 85 | } |
|
| 86 | } |