1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.orchestra.dynaForm.metadata.impl.jsf;
20
21 import java.util.Iterator;
22
23 import org.apache.myfaces.orchestra.dynaForm.jsf.component.DynaConfig;
24 import org.apache.myfaces.orchestra.dynaForm.jsf.component.DynaConfigs;
25 import org.apache.myfaces.orchestra.dynaForm.jsf.component.DynaForm;
26 import org.apache.myfaces.orchestra.dynaForm.metadata.Extractor;
27 import org.apache.myfaces.orchestra.dynaForm.metadata.MetaDataWritable;
28 import org.apache.myfaces.orchestra.dynaForm.metadata.MetaFieldWritable;
29
30 /**
31 * Extract metadata from a DynaForm UIComponent.
32 * <p>
33 * This looks for child DynaConfig components attached to the DynaForm and merges
34 * any overrides they declare with the current values of a MetaData object. This
35 * allows basic information about an entity to be determined by other means, then
36 * for any components attached to the DynaForm component to override those settings.
37 */
38 public abstract class AbstractJsfExtractor implements Extractor
39 {
40 public AbstractJsfExtractor()
41 {
42 }
43
44 /**
45 * Copy metadata overrides attached to a DynaForm UIComponent into an
46 * existing MetaData instance.
47 * <p>
48 * The specified entity must be an instance of DynaForm (a JSF UIComponent).
49 */
50 public void getMetaData(MetaDataWritable metaData, Object entity)
51 {
52 if (!(entity instanceof DynaForm))
53 {
54 throw new IllegalArgumentException("passed entity argument not a DynaForm: " + entity);
55 }
56
57 create(metaData, (DynaForm) entity);
58 }
59
60 /**
61 * create the metadata out of the dynaConfigs for the given component
62 */
63 @SuppressWarnings("unchecked")
64 protected void create(MetaDataWritable metaData, DynaForm dynaForm)
65 {
66 DynaConfigs formConfig = dynaForm.getFormConfigs();
67 if (formConfig == null)
68 {
69 return;
70 }
71
72 Iterator<DynaConfig> entries = formConfig.iterator();
73 while (entries.hasNext())
74 {
75 DynaConfig dynaConfig = entries.next();
76 String name = dynaConfig.getFor();
77 if (name == null)
78 {
79 throw new IllegalArgumentException("'for' in config tag required");
80 }
81
82 if (metaData.isWantedField(name))
83 {
84 MetaFieldWritable field = metaData.getOrCreateField(name);
85
86 initFromConfig(field, dynaConfig);
87 }
88 }
89 }
90
91 protected abstract void initFromConfig(MetaFieldWritable field, DynaConfig config);
92 }