Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 SpringSource
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.codehaus.groovy.grails.plugins.web.api;
 
 import java.util.Map;
 
Enhancements made to domain classes for data binding.

Author(s):
Graeme Rocher
Since:
2.0
 
 @SuppressWarnings("rawtypes")
 public class ControllersDomainBindingApi {
 
     public static final String AUTOWIRE_DOMAIN_METHOD = "autowireDomain";

    
Autowires the instance

Parameters:
instance The target instance
 
     public static void initialize(Object instance) {
         autowire(instance);
     }

    
A map based constructor that binds the named arguments to the target instance

Parameters:
instance The target instance
namedArgs The named arguments
 
     public static void initialize(Object instanceMap namedArgs) {
         GrailsDomainClass dc = getDomainClass(instance);
         if (dc == null) {
             DataBindingUtils.bindObjectToInstance(instancenamedArgs);
         }
         else {
             DataBindingUtils.bindObjectToDomainInstance(dcinstancenamedArgs);
             DataBindingUtils.assignBidirectionalAssociations(instancenamedArgsdc);
         }
         autowire(instance);
     }

    
Binds the source object to the properties of the target instance converting any types as necessary

Parameters:
instance The instance
bindingSource The binding source
Returns:
The BindingResult
 
     public BindingResult setProperties(Object instanceObject bindingSource) {
         GrailsDomainClass dc = getDomainClass(instance);
         if (dc == null) {
             return DataBindingUtils.bindObjectToInstance(instancebindingSource);
         }
         return DataBindingUtils.bindObjectToDomainInstance(dcinstancebindingSource);
     }

    
Returns a map of the objects properties that can be used to during binding to bind a subset of properties

Parameters:
instance The instance
Returns:
An instance of org.codehaus.groovy.grails.web.binding.DataBindingLazyMetaPropertyMap
 
     public Map getProperties(Object instance) {
         return new DataBindingLazyMetaPropertyMap(instance);
     }
 
     private static GrailsDomainClass getDomainClass(Object instance) {
         GrailsDomainClass domainClass = GrailsMetaClassUtils.getPropertyIfExists(instance.GrailsDomainClass.class);
         if (domainClass == null) {
             GrailsWebRequest webRequest = GrailsWebRequest.lookup();
             if (webRequest != null) {
                 ApplicationContext applicationContext = webRequest.getApplicationContext();
                if(applicationContext != null) {
                    GrailsApplication grailsApplication = applicationContext.containsBean(.) ?
                        applicationContext.getBean(.GrailsApplication.class) : null;
                    if (grailsApplication != null) {
                        domainClass = (GrailsDomainClassgrailsApplication.getArtefact(.instance.getClass().getName());
                    }
                }
            }
        }
        return domainClass;
    }
    private static void autowire(Object instance) {
        GrailsMetaClassUtils.invokeMethodIfExists(instancenew Object[] { instance });
    }
New to GrepCode? Check out our FAQ X