001: package com.silvermindsoftware.hitch.config;
002:
003: /**
004: * Copyright 2007 Brandon Goodin
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import com.silvermindsoftware.hitch.AbstractBinder;
020: import com.silvermindsoftware.hitch.Binder;
021: import com.silvermindsoftware.hitch.ReadOnly;
022: import com.silvermindsoftware.hitch.meta.FormMeta;
023: import com.silvermindsoftware.hitch.reflect.ClassManager;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import java.lang.reflect.Field;
028: import java.lang.reflect.Method;
029: import java.util.Arrays;
030: import java.util.List;
031: import java.util.Map;
032:
033: public class BinderConfig {
034:
035: private static final Log log = LogFactory
036: .getLog(BinderConfig.class);
037: private FormMeta formMeta;
038: private FormConfig formConfig;
039:
040: public BinderConfig(Class componentClass, Binder binder) {
041: this (componentClass, binder, new FormConfig());
042: }
043:
044: public BinderConfig(Class componentClass, Binder binder,
045: FormConfig formConfig) {
046: AbstractBinder abstractBinder = (AbstractBinder) binder;
047: this .formMeta = abstractBinder.getFormMeta(componentClass);
048: this .formConfig = formConfig;
049: }
050:
051: public BinderConfig bindModel(ModelObjectConfig modelObjectConfig) {
052:
053: bindCompleteModel(modelObjectConfig.getModelField(),
054: modelObjectConfig.getModelField(), modelObjectConfig
055: .isDefault(), modelObjectConfig.isAutoBind(),
056: modelObjectConfig.getIgnoreFields());
057:
058: return this ;
059: }
060:
061: /**
062: * @param modelField
063: * @param ignoreFields
064: */
065: public BinderConfig bindDefaultModel(String modelField,
066: String... ignoreFields) {
067: return bindCompleteModel("[default]", modelField, true, true,
068: ignoreFields);
069: }
070:
071: /**
072: * @param modelField
073: * @param ignoreFields
074: */
075: public BinderConfig bindModel(String modelField,
076: String... ignoreFields) {
077: return bindCompleteModel(modelField, modelField, false, true,
078: ignoreFields);
079: }
080:
081: /**
082: * @param modelField
083: */
084: public BinderConfig bindDefaultModel(String modelField) {
085: return bindCompleteModel("[default]", modelField, true, true);
086: }
087:
088: /**
089: * @param modelField
090: */
091: public BinderConfig bindModel(String modelField) {
092: return bindCompleteModel(modelField, modelField, false, true);
093: }
094:
095: private BinderConfig bindCompleteModel(String modelFieldName,
096: String modelField, boolean defaultModel,
097: boolean autoBindModelObject, String... ignoreFields) {
098: formMeta.putModelMeta(modelFieldName, modelField);
099: autoBind(modelField, defaultModel, autoBindModelObject,
100: ignoreFields);
101: return this ;
102: }
103:
104: private void autoBind(String modelField, boolean defaultModel,
105: boolean autoBindModelObject, String... ignoreFields) {
106:
107: List ignoreFieldsList = Arrays.asList(ignoreFields);
108:
109: if (formConfig.isAutoBind() && autoBindModelObject) {
110:
111: Class formClass = formMeta.getComponentClass();
112:
113: Field field = null;
114: try {
115: field = ClassManager.getClassInfo(formClass).getField(
116: modelField);
117: } catch (NoSuchFieldException e) {
118: //ignored
119: }
120:
121: // iterate modelObject fields
122: Class modelObjectType = field.getType();
123:
124: if (!Map.class.isAssignableFrom(modelObjectType)) { // can't autobind maps
125: for (Method moMethod : ClassManager.getClassInfo(
126: modelObjectType).getSetters()) {
127: String componentFieldName = moMethod.getName()
128: .substring(3, 4).toLowerCase()
129: + moMethod.getName().substring(4);
130:
131: Field formClassField = null;
132:
133: try {
134: formClassField = ClassManager.getClassInfo(
135: formClass).getField(componentFieldName);
136:
137: if (!ignoreFieldsList.contains(formClassField
138: .getName())) {
139:
140: formMeta.addComponentMeta(
141: defaultModel ? "[default]" : field
142: .getName(),
143: componentFieldName, formClassField,
144: void.class, new String[] {}, true,
145: ReadOnly.DEFAULT, moMethod
146: .getParameterTypes()[0]);
147:
148: }
149:
150: } catch (NoSuchFieldException e) {
151: // ignored
152: }
153: }
154: }
155: }
156: }
157:
158: /**
159: * @param boundComponentConfig
160: */
161: public BinderConfig bindComponentToDefault(
162: BoundComponentConfig boundComponentConfig) {
163: formMeta.addComponentMeta("[default]", boundComponentConfig,
164: false);
165: return this ;
166: }
167:
168: /**
169: * @param boundComponentConfigs
170: */
171: public BinderConfig bindComponentToDefault(
172: BoundComponentConfig... boundComponentConfigs) {
173: for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
174: formMeta.addComponentMeta("[default]",
175: boundComponentConfig, false);
176: }
177: return this ;
178: }
179:
180: /**
181: * @param modelId
182: * @param boundComponentConfig
183: */
184: public BinderConfig bindComponent(String modelId,
185: BoundComponentConfig boundComponentConfig) {
186: formMeta.addComponentMeta(modelId, boundComponentConfig, false);
187: return this ;
188: }
189:
190: /**
191: * @param modelId
192: * @param boundComponentConfigs
193: */
194: public BinderConfig bindComponent(String modelId,
195: BoundComponentConfig... boundComponentConfigs) {
196: for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
197: formMeta.addComponentMeta(modelId, boundComponentConfig,
198: false);
199: }
200: return this;
201: }
202:
203: }
|