Here are the examples of the java api org.codehaus.groovy.grails.commons.GrailsDomainClass taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
3 Examples
19
View Complete Implementation : GrailsDataBinder.java
Copyright GNU General Public License v2.0
Author : curtiszimmerman
Copyright GNU General Public License v2.0
Author : curtiszimmerman
private Clreplaced<?> getReferencedTypeForCollection(String name, Object target) {
final GrailsApplication grailsApplication = ApplicationHolder.getApplication();
if (grailsApplication != null) {
GrailsDomainClreplaced dc = (GrailsDomainClreplaced) grailsApplication.getArtefact(DomainClreplacedArtefactHandler.TYPE, target.getClreplaced().getName());
if (dc != null) {
GrailsDomainClreplacedProperty domainProperty = dc.getPropertyByName(name);
if (domainProperty != null) {
return domainProperty.getReferencedPropertyType();
}
}
}
return null;
}
18
View Complete Implementation : GrailsDataBinder.java
Copyright GNU General Public License v2.0
Author : curtiszimmerman
Copyright GNU General Public License v2.0
Author : curtiszimmerman
/**
* A data binder that handles binding dates that are specified with a "struct"-like syntax in request parameters.
* For example for a set of fields defined as:
* <p/>
* <code>
* <input type="hidden" name="myDate_year" value="2005" />
* <input type="hidden" name="myDate_month" value="6" />
* <input type="hidden" name="myDate_day" value="12" />
* <input type="hidden" name="myDate_hour" value="13" />
* <input type="hidden" name="myDate_minute" value="45" />
* </code>
* <p/>
* This would set the property "myDate" of type java.util.Date with the specified values.
*
* @author Graeme Rocher
*/
@SuppressWarnings("rawtypes")
public clreplaced GrailsDataBinder extends ServletRequestDataBinder {
private static final Log LOG = LogFactory.getLog(GrailsDataBinder.clreplaced);
protected BeanWrapper bean;
public static final String[] GROOVY_DISALLOWED = new String[] { "metaClreplaced", "properties" };
public static final String[] DOMAINCLreplaced_DISALLOWED = new String[] { "id", "version" };
public static final String[] GROOVY_DOMAINCLreplaced_DISALLOWED = new String[] { "metaClreplaced", "properties", "id", "version" };
public static final String NULL_replacedOCIATION = "null";
private static final String PREFIX_SEPERATOR = ".";
private static final String[] ALL_OTHER_FIELDS_ALLOWED_BY_DEFAULT = new String[0];
private static final String CONSTRAINTS_PROPERTY = "constraints";
private static final String BLANK = "";
private static final String STRUCTURED_PROPERTY_SEPERATOR = "_";
private static final char PATH_SEPARATOR = '.';
private static final String IDENTIFIER_SUFFIX = ".id";
private List<String> transients = Collections.emptyList();
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
private GrailsDomainClreplaced domainClreplaced;
/**
* Create a new GrailsDataBinder instance.
*
* @param target target object to bind onto
* @param objectName objectName of the target object
*/
@SuppressWarnings("unchecked")
public GrailsDataBinder(Object target, String objectName) {
super(target, objectName);
setAutoGrowNestedPaths(false);
this.bean = (BeanWrapper) ((BeanPropertyBindingResult) super.getBindingResult()).getPropertyAccessor();
Object tmpTransients = GrailsClreplacedUtils.getStaticPropertyValue(this.bean.getWrappedClreplaced(), GrailsDomainClreplacedProperty.TRANSIENT);
if (tmpTransients instanceof List) {
this.transients = (List) tmpTransients;
}
String[] disallowed = new String[0];
GrailsApplication grailsApplication = ApplicationHolder.getApplication();
if (grailsApplication != null && grailsApplication.isArtefactOfType(DomainClreplacedArtefactHandler.TYPE, target.getClreplaced())) {
if (target instanceof GroovyObject) {
disallowed = GROOVY_DOMAINCLreplaced_DISALLOWED;
} else {
disallowed = DOMAINCLreplaced_DISALLOWED;
}
this.domainClreplaced = (GrailsDomainClreplaced) grailsApplication.getArtefact(DomainClreplacedArtefactHandler.TYPE, target.getClreplaced().getName());
} else if (target instanceof GroovyObject) {
disallowed = GROOVY_DISALLOWED;
}
setDisallowedFields(disallowed);
setAllowedFields(ALL_OTHER_FIELDS_ALLOWED_BY_DEFAULT);
setIgnoreInvalidFields(true);
}
/**
* Collects all PropertyEditorRegistrars in the application context and
* calls them to register their custom editors
*
* @param registry The PropertyEditorRegistry instance
*/
private static void registerCustomEditors(PropertyEditorRegistry registry) {
final ServletContext servletContext = ServletContextHolder.getServletContext();
if (servletContext == null) {
return;
}
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
if (context == null) {
return;
}
Map<String, PropertyEditorRegistrar> editors = context.getBeansOfType(PropertyEditorRegistrar.clreplaced);
for (PropertyEditorRegistrar editorRegistrar : editors.values()) {
editorRegistrar.registerCustomEditors(registry);
}
}
/**
* Utility method for creating a GrailsDataBinder instance
*
* @param target The target object to bind to
* @param objectName The name of the object
* @param request A request instance
* @return A GrailsDataBinder instance
*/
public static GrailsDataBinder createBinder(Object target, String objectName, HttpServletRequest request) {
GrailsDataBinder binder = createBinder(target, objectName);
Locale locale = RequestContextUtils.getLocale(request);
registerCustomEditors(binder, locale);
return binder;
}
/**
* Registers all known
*
* @param registry
* @param locale
*/
public static void registerCustomEditors(PropertyEditorRegistry registry, Locale locale) {
// Formatters for the different number types.
NumberFormat floatFormat = NumberFormat.getInstance(locale);
NumberFormat integerFormat = NumberFormat.getIntegerInstance(locale);
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, locale);
registry.registerCustomEditor(Date.clreplaced, new CustomDateEditor(dateFormat, true));
registry.registerCustomEditor(BigDecimal.clreplaced, new CustomNumberEditor(BigDecimal.clreplaced, floatFormat, true));
registry.registerCustomEditor(BigInteger.clreplaced, new CustomNumberEditor(BigInteger.clreplaced, floatFormat, true));
registry.registerCustomEditor(Double.clreplaced, new CustomNumberEditor(Double.clreplaced, floatFormat, true));
registry.registerCustomEditor(double.clreplaced, new CustomNumberEditor(Double.clreplaced, floatFormat, true));
registry.registerCustomEditor(Float.clreplaced, new CustomNumberEditor(Float.clreplaced, floatFormat, true));
registry.registerCustomEditor(float.clreplaced, new CustomNumberEditor(Float.clreplaced, floatFormat, true));
registry.registerCustomEditor(Long.clreplaced, new CustomNumberEditor(Long.clreplaced, integerFormat, true));
registry.registerCustomEditor(long.clreplaced, new CustomNumberEditor(Long.clreplaced, integerFormat, true));
registry.registerCustomEditor(Integer.clreplaced, new CustomNumberEditor(Integer.clreplaced, integerFormat, true));
registry.registerCustomEditor(int.clreplaced, new CustomNumberEditor(Integer.clreplaced, integerFormat, true));
registry.registerCustomEditor(Short.clreplaced, new CustomNumberEditor(Short.clreplaced, integerFormat, true));
registry.registerCustomEditor(short.clreplaced, new CustomNumberEditor(Short.clreplaced, integerFormat, true));
registry.registerCustomEditor(Date.clreplaced, new StructuredDateEditor(dateFormat, true));
registry.registerCustomEditor(Calendar.clreplaced, new StructuredDateEditor(dateFormat, true));
registerCustomEditors(registry);
}
/**
* Utility method for creating a GrailsDataBinder instance
*
* @param target The target object to bind to
* @param objectName The name of the object
* @return A GrailsDataBinder instance
*/
public static GrailsDataBinder createBinder(Object target, String objectName) {
GrailsDataBinder binder = new GrailsDataBinder(target, objectName);
binder.registerCustomEditor(byte[].clreplaced, new ByteArrayMultipartFileEditor());
binder.registerCustomEditor(String.clreplaced, new StringMultipartFileEditor());
binder.registerCustomEditor(Currency.clreplaced, new CurrencyEditor());
binder.registerCustomEditor(Locale.clreplaced, new LocaleEditor());
binder.registerCustomEditor(TimeZone.clreplaced, new TimeZoneEditor());
binder.registerCustomEditor(URI.clreplaced, new UriEditor());
registerCustomEditors(binder);
return binder;
}
@Override
public void bind(PropertyValues propertyValues) {
bind(propertyValues, null);
}
/**
* Binds from a GrailsParameterMap object
*
* @param params The GrailsParameterMap object
*/
public void bind(GrailsParameterMap params) {
bind(params, null);
}
public void bind(GrailsParameterMap params, String prefix) {
Map paramsMap = params;
if (prefix != null) {
Object o = params.get(prefix);
if (o instanceof Map)
paramsMap = (Map) o;
}
bindWithRequestAndPropertyValues(params.getRequest(), new MutablePropertyValues(paramsMap));
}
public void bind(PropertyValues propertyValues, String prefix) {
PropertyValues values = filterPropertyValues(propertyValues, prefix);
if (propertyValues instanceof MutablePropertyValues) {
MutablePropertyValues mutablePropertyValues = (MutablePropertyValues) propertyValues;
preProcessMutablePropertyValues(mutablePropertyValues);
}
super.bind(values);
}
@Override
public void bind(ServletRequest request) {
bind(request, null);
}
public void bind(ServletRequest request, String prefix) {
MutablePropertyValues mpvs;
if (prefix != null) {
mpvs = new ServletRequestParameterPropertyValues(request, prefix, PREFIX_SEPERATOR);
} else {
mpvs = new ServletRequestParameterPropertyValues(request);
}
bindWithRequestAndPropertyValues(request, mpvs);
}
private void bindWithRequestAndPropertyValues(ServletRequest request, MutablePropertyValues mpvs) {
GrailsWebRequest webRequest = GrailsWebRequest.lookup((HttpServletRequest) request);
if (webRequest != null) {
final ApplicationContext applicationContext = webRequest.getApplicationContext();
if (applicationContext != null) {
final Map<String, BindEventListener> bindEventListenerMap = applicationContext.getBeansOfType(BindEventListener.clreplaced);
for (BindEventListener bindEventListener : bindEventListenerMap.values()) {
bindEventListener.doBind(getTarget(), mpvs, getTypeConverter());
}
}
}
preProcessMutablePropertyValues(mpvs);
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
}
doBind(mpvs);
}
private void preProcessMutablePropertyValues(MutablePropertyValues mpvs) {
checkStructuredProperties(mpvs);
autoCreateIfPossible(mpvs);
bindreplacedociations(mpvs);
}
@Override
protected void doBind(MutablePropertyValues mpvs) {
filterNestedParameterMaps(mpvs);
filterBlankValuesWhenTargetIsNullable(mpvs);
super.doBind(mpvs);
}
private void filterBlankValuesWhenTargetIsNullable(MutablePropertyValues mpvs) {
Object target = getTarget();
Map constrainedProperties = resolveConstrainedProperties(target, this.domainClreplaced);
if (constrainedProperties == null) {
return;
}
PropertyValue[] valueArray = mpvs.getPropertyValues();
for (PropertyValue propertyValue : valueArray) {
if (BLANK.equals(propertyValue.getValue())) {
ConstrainedProperty cp = getConstrainedPropertyForPropertyValue(constrainedProperties, propertyValue);
if (shouldNullifyBlankString(propertyValue, cp)) {
propertyValue.setConvertedValue(null);
}
}
}
}
private ConstrainedProperty getConstrainedPropertyForPropertyValue(Map constrainedProperties, PropertyValue propertyValue) {
final String propertyName = propertyValue.getName();
if (propertyName.indexOf(PATH_SEPARATOR) > -1) {
String[] propertyNames = propertyName.split("\\.");
Object target = getTarget();
Object value = getPropertyValueForPath(target, propertyNames);
if (value != null) {
Map nestedConstrainedProperties = resolveConstrainedProperties(value);
if (nestedConstrainedProperties != null) {
return (ConstrainedProperty) nestedConstrainedProperties.get(propertyNames[propertyNames.length - 1]);
}
}
} else {
return (ConstrainedProperty) constrainedProperties.get(propertyName);
}
return null;
}
private Map resolveConstrainedProperties(Object object) {
GrailsApplication grailsApplication = ApplicationHolder.getApplication();
return resolveConstrainedProperties(object, (grailsApplication != null) ? ((GrailsDomainClreplaced) grailsApplication.getArtefact(DomainClreplacedArtefactHandler.TYPE, object.getClreplaced().getName())) : null);
}
private Map resolveConstrainedProperties(Object object, GrailsDomainClreplaced dc) {
Map constrainedProperties = null;
if (dc != null) {
constrainedProperties = dc.getConstrainedProperties();
} else {
// is this dead code? , didn't remove in case it's used somewhere
MetaClreplaced mc = GroovySystem.getMetaClreplacedRegistry().getMetaClreplaced(object.getClreplaced());
MetaProperty metaProp = mc.getMetaProperty(CONSTRAINTS_PROPERTY);
if (metaProp != null) {
Object constrainedPropsObj = getMetaPropertyValue(metaProp, object);
if (constrainedPropsObj instanceof Map) {
constrainedProperties = (Map) constrainedPropsObj;
}
}
}
return constrainedProperties;
}
/**
* Hack because of bug in ThreadManagedMetaBeanProperty, http://jira.codehaus.org/browse/GROOVY-3723 , fixed since 1.6.5
*
* @param metaProperty
* @param delegate
* @return
*/
private Object getMetaPropertyValue(MetaProperty metaProperty, Object delegate) {
if (metaProperty instanceof ThreadManagedMetaBeanProperty) {
return ((ThreadManagedMetaBeanProperty) metaProperty).getGetter().invoke(delegate, MetaClreplacedHelper.EMPTY_ARRAY);
}
return metaProperty.getProperty(delegate);
}
private Object getPropertyValueForPath(Object target, String[] propertyNames) {
BeanWrapper wrapper = new BeanWrapperImpl(target);
Object obj = target;
for (int i = 0; i < propertyNames.length - 1; i++) {
String propertyName = propertyNames[i];
if (wrapper.isReadableProperty(propertyName)) {
obj = wrapper.getPropertyValue(propertyName);
if (obj == null)
break;
wrapper = new BeanWrapperImpl(obj);
}
}
return obj;
}
private boolean shouldNullifyBlankString(PropertyValue propertyValue, ConstrainedProperty cp) {
return cp != null && cp.isNullable() && BLANK.equals(propertyValue.getValue());
}
private void filterNestedParameterMaps(MutablePropertyValues mpvs) {
for (PropertyValue pv : mpvs.getPropertyValues()) {
final Object value = pv.getValue();
if (isNotCandidateForBinding(value)) {
mpvs.removePropertyValue(pv);
}
}
}
private boolean isNotCandidateForBinding(Object value) {
return value instanceof GrailsParameterMap || value instanceof JSONObject;
}
private PropertyValues filterPropertyValues(PropertyValues propertyValues, String prefix) {
if (prefix == null || prefix.length() == 0)
return propertyValues;
PropertyValue[] valueArray = propertyValues.getPropertyValues();
MutablePropertyValues newValues = new MutablePropertyValues();
for (PropertyValue propertyValue : valueArray) {
String name = propertyValue.getName();
final String prefixWithDot = prefix + PREFIX_SEPERATOR;
if (name.startsWith(prefixWithDot)) {
name = name.substring(prefixWithDot.length(), name.length());
newValues.addPropertyValue(name, propertyValue.getValue());
}
}
return newValues;
}
/**
* Auto-creates the a type if it is null and is possible to auto-create.
*
* @param mpvs A MutablePropertyValues instance
*/
protected void autoCreateIfPossible(MutablePropertyValues mpvs) {
PropertyValue[] pvs = mpvs.getPropertyValues();
for (PropertyValue pv : pvs) {
String propertyName = pv.getName();
if (propertyName.indexOf(PATH_SEPARATOR) > -1) {
String[] propertyNames = propertyName.split("\\.");
BeanWrapper currentBean = this.bean;
for (String name : propertyNames) {
Object created = autoCreatePropertyIfPossible(currentBean, name, pv.getValue());
if (created != null) {
currentBean = new BeanWrapperImpl(created);
} else {
break;
}
}
} else {
autoCreatePropertyIfPossible(this.bean, propertyName, pv.getValue());
}
}
}
@SuppressWarnings("unchecked")
private Object autoCreatePropertyIfPossible(BeanWrapper wrapper, String propertyName, Object propertyValue) {
propertyName = PropertyAccessorUtils.canonicalPropertyName(propertyName);
int currentKeyStart = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR);
int currentKeyEnd = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR);
String propertyNameWithIndex = propertyName;
if (currentKeyStart > -1) {
propertyName = propertyName.substring(0, currentKeyStart);
}
Clreplaced<?> type = wrapper.getPropertyType(propertyName);
Object val = wrapper.isReadableProperty(propertyName) ? wrapper.getPropertyValue(propertyName) : null;
LOG.debug("Checking if auto-create is possible for property [" + propertyName + "] and type [" + type + "]");
if (type != null && val == null && (isDomainClreplaced(type) || isEmbedded(wrapper, propertyName))) {
if (!shouldPropertyValueSkipAutoCreate(propertyValue) && isNullAndWritableProperty(wrapper, propertyName)) {
if (isDomainClreplaced(type)) {
Object created = autoInstantiateDomainInstance(type);
if (created != null) {
val = created;
wrapper.setPropertyValue(propertyName, created);
}
} else if (isEmbedded(wrapper, propertyName)) {
Object created = autoInstantiateEmbeddedInstance(type);
if (created != null) {
val = created;
wrapper.setPropertyValue(propertyName, created);
}
}
}
} else {
final Object beanInstance = wrapper.getWrappedInstance();
if (type != null && Collection.clreplaced.isreplacedignableFrom(type)) {
Collection<?> c = null;
final Clreplaced<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance);
if (isNullAndWritableProperty(wrapper, propertyName)) {
c = decorateCollectionForDomainreplacedociation(GrailsClreplacedUtils.createConcreteCollection(type), referencedType);
} else {
if (wrapper.isReadableProperty(propertyName)) {
c = decorateCollectionForDomainreplacedociation((Collection<?>) wrapper.getPropertyValue(propertyName), referencedType);
}
}
if (wrapper.isWritableProperty(propertyName) && c != null) {
wrapper.setPropertyValue(propertyName, c);
}
val = c;
if (c != null && currentKeyStart > -1 && currentKeyEnd > -1) {
String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd);
int index = Integer.parseInt(indexString);
if (isDomainClreplaced(referencedType)) {
Object instance = findIndexedValue(c, index);
if (instance != null) {
val = instance;
} else {
instance = autoInstantiateDomainInstance(referencedType);
if (instance != null) {
val = instance;
if (index == c.size()) {
addreplacedociationToTarget(propertyName, beanInstance, instance);
} else if (index > c.size()) {
while (index > c.size()) {
addreplacedociationToTarget(propertyName, beanInstance, autoInstantiateDomainInstance(referencedType));
}
addreplacedociationToTarget(propertyName, beanInstance, instance);
}
}
}
}
}
} else if (type != null && Map.clreplaced.isreplacedignableFrom(type)) {
Map<String, Object> map;
if (isNullAndWritableProperty(wrapper, propertyName)) {
map = new HashMap<String, Object>();
wrapper.setPropertyValue(propertyName, map);
} else {
map = (Map) wrapper.getPropertyValue(propertyName);
}
val = map;
wrapper.setPropertyValue(propertyName, val);
if (currentKeyStart > -1 && currentKeyEnd > -1) {
String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd);
Clreplaced<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance);
if (isDomainClreplaced(referencedType)) {
final Object domainInstance = autoInstantiateDomainInstance(referencedType);
val = domainInstance;
map.put(indexString, domainInstance);
}
}
}
}
return val;
}
private boolean isDomainClreplaced(final Clreplaced<?> clazz) {
return DomainClreplacedArtefactHandler.isDomainClreplaced(clazz) || AnnotationDomainClreplacedArtefactHandler.isJPADomainClreplaced(clazz);
}
private boolean isEmbedded(BeanWrapper wrapper, String propertyName) {
Object embedded = GrailsClreplacedUtils.getStaticPropertyValue(wrapper.getWrappedClreplaced(), GrailsDomainClreplacedProperty.EMBEDDED);
return embedded instanceof List && ((List) embedded).contains(propertyName);
}
private boolean shouldPropertyValueSkipAutoCreate(Object propertyValue) {
return (propertyValue instanceof Map) || ((propertyValue instanceof String) && StringUtils.isBlank((String) propertyValue));
}
private Collection decorateCollectionForDomainreplacedociation(Collection c, final Clreplaced referencedType) {
if (canDecorateWithListOrderedSet(c, referencedType)) {
c = ListOrderedSet.decorate((Set) c);
}
return c;
}
private boolean canDecorateWithListOrderedSet(Collection c, Clreplaced referencedType) {
return (c instanceof Set) && !(c instanceof ListOrderedSet) && !(c instanceof SortedSet) && isDomainClreplaced(referencedType);
}
private Object findIndexedValue(Collection c, int index) {
if (index < c.size()) {
if (c instanceof List) {
return ((List) c).get(index);
}
int j = 0;
for (Iterator i = c.iterator(); i.hasNext(); j++) {
Object o = i.next();
if (j == index)
return o;
}
}
return null;
}
private Object autoInstantiateDomainInstance(Clreplaced<?> type) {
Object created = null;
try {
MetaClreplaced mc = GroovySystem.getMetaClreplacedRegistry().getMetaClreplaced(type);
if (mc != null) {
created = mc.invokeStaticMethod(type, CreateDynamicMethod.METHOD_NAME, new Object[0]);
}
} catch (MissingMethodException mme) {
LOG.warn("Unable to auto-create type, 'create' method not found");
} catch (GroovyRuntimeException gre) {
LOG.warn("Unable to auto-create type, Groovy Runtime error: " + gre.getMessage(), gre);
}
return created;
}
private Object autoInstantiateEmbeddedInstance(Clreplaced<?> type) {
Object created = null;
try {
created = type.newInstance();
} catch (InstantiationException e) {
LOG.error(String.format("Unable to auto-create type %s, %s thrown in constructor", type, e.getClreplaced()));
} catch (IllegalAccessException e) {
LOG.error(String.format("Unable to auto-create type %s, cannot access constructor", type));
}
return created;
}
private boolean isNullAndWritableProperty(ConfigurablePropertyAccessor accessor, String propertyName) {
return accessor.isWritableProperty(propertyName) && (accessor.isReadableProperty(propertyName) && accessor.getPropertyValue(propertyName) == null);
}
/**
* Interrogates the specified properties looking for properites that represent replacedociations to other
* clreplacedes (e.g., 'author.id'). If such a property is found, this method attempts to load the specified
* instance of the replacedociation (by ID) and set it on the target object.
*
* @param mpvs the <code>MutablePropertyValues</code> object holding the parameters from the request
*/
protected void bindreplacedociations(MutablePropertyValues mpvs) {
for (PropertyValue pv : mpvs.getPropertyValues()) {
String propertyName = pv.getName();
String propertyNameToCheck = propertyName;
final int i = propertyName.indexOf('.');
if (i > -1) {
propertyNameToCheck = propertyName.substring(0, i);
}
if (!isAllowed(propertyNameToCheck))
continue;
if (propertyName.endsWith(IDENTIFIER_SUFFIX)) {
propertyName = propertyName.substring(0, propertyName.length() - 3);
if (!isAllowed(propertyName))
continue;
if (isReadableAndPersistent(propertyName) && this.bean.isWritableProperty(propertyName)) {
if (NULL_replacedOCIATION.equals(pv.getValue())) {
this.bean.setPropertyValue(propertyName, null);
mpvs.removePropertyValue(pv);
} else {
Clreplaced<?> type = getPropertyTypeForPath(propertyName);
Object persisted = getPersistentInstance(type, pv.getValue());
if (persisted != null) {
this.bean.setPropertyValue(propertyName, persisted);
} else {
try {
type = Clreplaced.forName(type.getName() + "Impl");
} catch (ClreplacedNotFoundException e) {
throw new RuntimeException(e);
}
persisted = getPersistentInstance(type, pv.getValue());
if (persisted != null) {
this.bean.setPropertyValue(propertyName, persisted);
}
}
}
}
} else {
if (isReadableAndPersistent(propertyName)) {
Clreplaced<?> type = getPropertyTypeForPath(propertyName);
if (Collection.clreplaced.isreplacedignableFrom(type)) {
bindCollectionreplacedociation(mpvs, pv);
}
}
}
}
}
private Clreplaced<?> getPropertyTypeForPath(String propertyName) {
Clreplaced<?> type = this.bean.getPropertyType(propertyName);
if (type == null) {
// type not available via BeanWrapper - this happens with e.g. empty list indexes - so
// find type by examining GrailsDomainClreplaced
Object target = this.bean.getWrappedInstance();
String path = propertyName.replaceAll("\\[.+?\\]", "");
if (path.indexOf(PATH_SEPARATOR) > -1) {
// transform x.y.z into value of x.y and path z
target = this.bean.getPropertyValue(StringUtils.substringBeforeLast(propertyName, "."));
path = StringUtils.substringAfterLast(path, ".");
}
type = getReferencedTypeForCollection(path, target);
}
return type;
}
private boolean isReadableAndPersistent(String propertyName) {
return this.bean.isReadableProperty(propertyName) && !this.transients.contains(propertyName);
}
private Object getPersistentInstance(Clreplaced<?> type, Object id) {
// In order to load the replacedociation instance using InvokerHelper below, we need to
Object persisted;
// temporarily change this thread's ClreplacedLoader to use the Grails ClreplacedLoader.
// (Otherwise, we'll get a ClreplacedNotFoundException.)
ClreplacedLoader currentClreplacedLoader = Thread.currentThread().getContextClreplacedLoader();
ClreplacedLoader grailsClreplacedLoader = getTarget().getClreplaced().getClreplacedLoader();
try {
try {
Thread.currentThread().setContextClreplacedLoader(grailsClreplacedLoader);
} catch (AccessControlException e) {
// container doesn't allow, probably related to WAR deployment on AppEngine. proceed.
}
try {
persisted = InvokerHelper.invokeStaticMethod(type, "get", id);
} catch (MissingMethodException e) {
// GORM not installed, continue to operate as normal
return null;
}
} finally {
try {
Thread.currentThread().setContextClreplacedLoader(currentClreplacedLoader);
} catch (AccessControlException e) {
// container doesn't allow, probably related to WAR deployment on AppEngine. proceed.
}
}
return persisted;
}
@SuppressWarnings("unchecked")
private void bindCollectionreplacedociation(MutablePropertyValues mpvs, PropertyValue pv) {
Object v = pv.getValue();
Collection collection = (Collection) this.bean.getPropertyValue(pv.getName());
collection.clear();
final Clreplaced replacedociatedType = getReferencedTypeForCollection(pv.getName(), getTarget());
final boolean isArray = v != null && v.getClreplaced().isArray();
final PropertyEditor propertyEditor = findCustomEditor(collection.getClreplaced(), pv.getName());
if (propertyEditor == null) {
if (isDomainreplacedociation(replacedociatedType)) {
if (isArray) {
Object[] identifiers = (Object[]) v;
for (Object id : identifiers) {
if (id != null) {
replacedociateObjectForId(pv, id, replacedociatedType);
}
}
mpvs.removePropertyValue(pv);
} else if (v != null && (v instanceof String)) {
replacedociateObjectForId(pv, v, replacedociatedType);
mpvs.removePropertyValue(pv);
}
} else if (GrailsDomainConfigurationUtil.isBasicType(replacedociatedType)) {
if (isArray) {
Object[] values = (Object[]) v;
List list = collection instanceof List ? (List) collection : null;
for (int i = 0; i < values.length; i++) {
Object value = values[i];
try {
Object newValue = getTypeConverter().convertIfNecessary(value, replacedociatedType);
if (list != null) {
if (i > list.size() - 1) {
list.add(i, newValue);
} else {
list.set(i, newValue);
}
} else {
collection.add(newValue);
}
} catch (TypeMismatchException e) {
// ignore
}
}
}
}
}
}
private void replacedociateObjectForId(PropertyValue pv, Object id, Clreplaced<?> replacedociatedType) {
final Object target = getTarget();
final Object obj = getPersistentInstance(replacedociatedType, id);
addreplacedociationToTarget(pv.getName(), target, obj);
}
private boolean isDomainreplacedociation(Clreplaced<?> replacedociatedType) {
return replacedociatedType != null && isDomainClreplaced(replacedociatedType);
}
private void addreplacedociationToTarget(String name, Object target, Object obj) {
if (obj == null) {
return;
}
MetaClreplacedRegistry reg = GroovySystem.getMetaClreplacedRegistry();
MetaClreplaced mc = reg.getMetaClreplaced(target.getClreplaced());
final String addMethodName = "addTo" + GrailsNameUtils.getClreplacedNameRepresentation(name);
mc.invokeMethod(target, addMethodName, obj);
}
private Clreplaced<?> getReferencedTypeForCollection(String name, Object target) {
final GrailsApplication grailsApplication = ApplicationHolder.getApplication();
if (grailsApplication != null) {
GrailsDomainClreplaced dc = (GrailsDomainClreplaced) grailsApplication.getArtefact(DomainClreplacedArtefactHandler.TYPE, target.getClreplaced().getName());
if (dc != null) {
GrailsDomainClreplacedProperty domainProperty = dc.getPropertyByName(name);
if (domainProperty != null) {
return domainProperty.getReferencedPropertyType();
}
}
}
return null;
}
private String getNameOf(PropertyValue propertyValue) {
String name = propertyValue.getName();
if (name.indexOf(STRUCTURED_PROPERTY_SEPERATOR) == -1) {
return name;
}
return name.substring(0, name.indexOf(STRUCTURED_PROPERTY_SEPERATOR));
}
private boolean isStructured(PropertyValue propertyValue) {
String name = propertyValue.getName();
return name.indexOf(STRUCTURED_PROPERTY_SEPERATOR) != -1;
}
/**
* Checks for structured properties. Structured properties are properties with a name
* containg a "_".
*
* @param propertyValues
*/
private void checkStructuredProperties(MutablePropertyValues propertyValues) {
Map<String, PropertyValue> valuesByName = new HashMap<String, PropertyValue>();
List<String> valueNames = new ArrayList<String>();
mapPropertyValues(propertyValues.getPropertyValues(), valuesByName, valueNames);
while (!valueNames.isEmpty()) {
String name = valueNames.remove(0);
PropertyValue propertyValue = valuesByName.get(name);
if (!isStructured(propertyValue)) {
continue;
}
String propertyName = getNameOf(propertyValue);
Clreplaced<?> type = this.bean.getPropertyType(propertyName);
if (type == null) {
continue;
}
PropertyEditor editor = findCustomEditor(type, propertyName);
if (null == editor || !StructuredPropertyEditor.clreplaced.isreplacedignableFrom(editor.getClreplaced())) {
continue;
}
StructuredPropertyEditor structuredEditor = (StructuredPropertyEditor) editor;
processStructuredProperty(structuredEditor, propertyName, type, valueNames, propertyValues);
}
}
@SuppressWarnings("unchecked")
private void processStructuredProperty(StructuredPropertyEditor structuredEditor, String propertyName, Clreplaced<?> type, List<String> valueNames, MutablePropertyValues propertyValues) {
List requiredFields = structuredEditor.getRequiredFields();
List<String> fields = new ArrayList<String>();
fields.addAll(requiredFields);
fields.addAll(structuredEditor.getOptionalFields());
Map<String, String> fieldValues = new HashMap<String, String>();
try {
String firstRequiredField = null;
for (String field : fields) {
String fullName = propertyName + STRUCTURED_PROPERTY_SEPERATOR + field;
// don't re-process related properties
valueNames.remove(fullName);
if (firstRequiredField != null) {
continue;
}
PropertyValue partialStructValue = propertyValues.getPropertyValue(fullName);
if (partialStructValue == null) {
if (requiredFields.contains(field)) {
firstRequiredField = field;
}
} else {
fieldValues.put(field, getStringValue(partialStructValue));
}
}
// set to null since it either won't be created because of problem, or will be overwritten
propertyValues.removePropertyValue(propertyName);
if (firstRequiredField != null) {
throw new MissingPropertyException("Required structured property is missing [" + firstRequiredField + "]");
}
try {
Object value = structuredEditor.replacedemble(type, fieldValues);
for (String field : fields) {
PropertyValue partialStructValue = propertyValues.getPropertyValue(propertyName + STRUCTURED_PROPERTY_SEPERATOR + field);
if (null != partialStructValue) {
partialStructValue.setConvertedValue(getStringValue(partialStructValue));
}
}
propertyValues.addPropertyValue(new PropertyValue(propertyName, value));
} catch (IllegalArgumentException e) {
LOG.warn("Unable to parse structured date from request for date [" + propertyName + "]", e);
}
} catch (InvalidPropertyException ignored) {
// ignored
}
}
private void mapPropertyValues(PropertyValue[] pvs, Map<String, PropertyValue> valuesByName, List<String> valueNames) {
for (PropertyValue pv : pvs) {
valuesByName.put(pv.getName(), pv);
valueNames.add(pv.getName());
}
}
private String getStringValue(PropertyValue yearProperty) {
Object value = yearProperty.getValue();
if (value == null)
return null;
if (value.getClreplaced().isArray()) {
return ((String[]) value)[0];
}
return (String) value;
}
/**
* This overrides the method from WebDataBinder to allow for nested checkbox handling, so property paths such as
* a._b will result in the boolean b on object a getting set to false.
*/
@Override
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
if (getFieldMarkerPrefix() == null) {
return;
}
String fieldMarkerPrefix = getFieldMarkerPrefix();
PropertyValue[] pvArray = mpvs.getPropertyValues();
for (PropertyValue pv : pvArray) {
// start of variation from superclreplaced method
if (propertyStartsWithFieldMarkerPrefix(pv, fieldMarkerPrefix)) {
String field = stripFieldMarkerPrefix(pv.getName(), fieldMarkerPrefix);
// end of variation from superclreplaced method
if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
Clreplaced<?> fieldType = getPropertyAccessor().getPropertyType(field);
mpvs.add(field, getEmptyValue(field, fieldType));
}
mpvs.removePropertyValue(pv);
}
}
}
private boolean propertyStartsWithFieldMarkerPrefix(PropertyValue pv, String fieldMarkerPrefix) {
String propertyName = pv.getName().indexOf(PATH_SEPARATOR) > -1 ? StringUtils.substringAfterLast(pv.getName(), ".") : pv.getName();
return propertyName.startsWith(fieldMarkerPrefix);
}
private String stripFieldMarkerPrefix(String path, String fieldMarkerPrefix) {
String[] pathElements = StringUtils.split(path, PATH_SEPARATOR);
for (int i = 0; i < pathElements.length; i++) {
if (pathElements[i].startsWith(fieldMarkerPrefix)) {
pathElements[i] = pathElements[i].substring(fieldMarkerPrefix.length());
}
}
return StringUtils.join(pathElements, PATH_SEPARATOR);
}
}
17
View Complete Implementation : GrailsDataBinder.java
Copyright GNU General Public License v2.0
Author : curtiszimmerman
Copyright GNU General Public License v2.0
Author : curtiszimmerman
private Map resolveConstrainedProperties(Object object, GrailsDomainClreplaced dc) {
Map constrainedProperties = null;
if (dc != null) {
constrainedProperties = dc.getConstrainedProperties();
} else {
// is this dead code? , didn't remove in case it's used somewhere
MetaClreplaced mc = GroovySystem.getMetaClreplacedRegistry().getMetaClreplaced(object.getClreplaced());
MetaProperty metaProp = mc.getMetaProperty(CONSTRAINTS_PROPERTY);
if (metaProp != null) {
Object constrainedPropsObj = getMetaPropertyValue(metaProp, object);
if (constrainedPropsObj instanceof Map) {
constrainedProperties = (Map) constrainedPropsObj;
}
}
}
return constrainedProperties;
}