Showing posts with label isNullable. Show all posts
Showing posts with label isNullable. Show all posts

Saturday, July 26, 2014

Check Null Value Using Python

fieldNullable = None
fieldNotNullable = 'Some Value'
fieldBoolean = False

if fieldNullable is None:
    print("fieldNullable is null/None")
else:
    print("fieldNullable is not null/None")

if fieldNotNullable is None:
    print("fieldNotNullable is null/None")
else:
    print("fieldNotNullable is not null/None")

if fieldBoolean is None:
    print("fieldBoolean is null/None")
else:
    print("fieldBoolean is not null/None")


class TheClass1:
    def __main__(self, theObject):
        self.theObject = theObject


theClass1 = TheClass1()
if theClass1 is None:
    print("theClass1 is null/None")
else:
    print("theClass1 is not null/None")
    
if theClass1 == None:
    print("theClass1 is null/None")
else:
    print("theClass1 is not null/None")






class TheClass2:
    def __eq__(self, theObject):
        self.theObject = theObject
        #Always return true when check using ==
        return True


theClass2 = TheClass2()
if theClass2 is None:
    print("theClass2 is null/None")
else:
    print("theClass2 is not null/None")
    
if theClass2 == None:
    print("theClass2 is null/None")
else:
    print("theClass2 is not null/None")   

fieldNullable is null/None
fieldNotNullable is not null/None
fieldBoolean is not null/None
theClass1 is not null/None
theClass1 is not null/None
theClass2 is not null/None
theClass2 is null/None 

Monday, September 30, 2013

Grails: how to get/set a meta-constraint on domain class property such as belongsTo or other variables

In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. These object has the property constrainedProperties. To have access to any properties such in this example: 'readonly' you have to do this:

Your domain class:


class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}


The DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:


def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly



def domainClass = new DefaultGrailsDomainClass(it.metaClass.getTheClass())
def constrainedProperties = domainClass.constrainedProperties;
if(constrainedProperties.containsKey("subscription")) {
    ConstrainedProperty v = (ConstrainedProperty) constrainedProperties.get("subscription");
    /**
     * Now check
     */
    if(v.isNullable()) {
        println "Nullable";
    }
    if(v.isBlank()) {
        println "Blank";
    }
    /**
     * There are may other constraints
     * http://grails.org/doc/latest/api/org/codehaus/groovy/grails/validation/ConstrainedProperty.html 
     * Java/Grails Class ConstrainedProperty
     */ 
 }