An ChannelFactory is a ModelFactory that can wrap the variable in a CodeControl and produce an ChennelModel for a given CharID
| 34 | * produce an ChennelModel for a given CharID |
| 35 | */ |
| 36 | public class ChannelFactory implements ModelFactory |
| 37 | { |
| 38 | /** |
| 39 | * The global ObjectWrapperFacet used to wrap the current value of a |
| 40 | * variable |
| 41 | */ |
| 42 | private final ObjectWrapperFacet wrapperFacet = FacetLibrary.getFacet(ObjectWrapperFacet.class); |
| 43 | |
| 44 | /** |
| 45 | * The CControl for which this ChannelFactory can produce a ModelFactory. |
| 46 | */ |
| 47 | private final CControl control; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a new ChannelFactory for the given CodeControl. |
| 51 | * |
| 52 | * @param control |
| 53 | * The CControl for which this ChannelFactory will produce ModelFactory |
| 54 | * objects |
| 55 | */ |
| 56 | public ChannelFactory(CControl control) |
| 57 | { |
| 58 | this.control = control; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public TemplateModel generate(CharID id) |
| 63 | { |
| 64 | Optional<String> feature = control.getControllingFeature(); |
| 65 | Object value; |
| 66 | if (feature.isPresent() && ControlUtilities.isFeatureEnabled(Globals.getContext(), feature.get())) |
| 67 | { |
| 68 | String channelName = ControlUtilities.getControlToken(Globals.getContext(), control); |
| 69 | value = ChannelUtilities.readGlobalChannel(id, channelName); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | value = ""; |
| 74 | } |
| 75 | try |
| 76 | { |
| 77 | return wrapperFacet.wrap(id, value); |
| 78 | } |
| 79 | catch (TemplateModelException e) |
| 80 | { |
| 81 | throw new IllegalArgumentException( |
| 82 | "Unable to generate wrapper for Channel: " + control.getName() |
| 83 | + " value type: " + value.getClass().getCanonicalName(), |
| 84 | e); |
| 85 | } |
| 86 | } |
| 87 | } |