All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.iternine.jeppetto.dao.mongodb.enhance.updateObject.vm Maven / Gradle / Ivy

The newest version!
#* @vtlvariable name="getters" type="javassist.CtMethod[]" *#
#* @vtlvariable name="base" type="javassist.CtClass" *#
#* @vtlvariable name="_" type="org.iternine.jeppetto.enhance.TemplateHelper" *#
#* @vtlvariable name="updateObjectHelper" type="org.iternine.jeppetto.dao.mongodb.enhance.UpdateObjectHelper" *#
#set( $baseName = $base.getName() )
#set( $Q = '"')
public $_.cls("${baseName}$UpdateObject").ext($base).impl("org.iternine.jeppetto.dao.mongodb.enhance.UpdateObject") {

    #set( $thisName = $_.clsName() )

    //-------------------------------------------------------------
    // Variables - Private
    //-------------------------------------------------------------

    $_.field("private final String prefix;")
    $_.field("private final java.util.Map __updates = new java.util.HashMap();")


    //-------------------------------------------------------------
    // Methods - Overrides
    //-------------------------------------------------------------

    #foreach ( $getter in $getters )
    #set( $getterName = $getter.getName() )
    #set( $field = $_.fieldFor($getterName) )
    #set( $setterName = $_.asSetter($field) )
    #set ( $returnType = $_.returnTypeOf($getter) )
    #set ( $returnTypeName = $returnType.getName() )

    ## Special case blobs
    #if ( $returnTypeName == '[B')
        #set ( $returnTypeName = 'byte[]' )
    #end


    #if ( !$updateObjectHelper.needsNoConversion($returnType) )
    $_.method("
    public $returnTypeName $getterName() {
        $returnTypeName value = ($returnTypeName) __updates.get(${Q}$field${Q});

        if (value == null) {
            #if ( $updateObjectHelper.isAssignableFromList($returnType) )
            value = new org.iternine.jeppetto.dao.mongodb.enhance.UpdateList();
            #elseif ( $updateObjectHelper.isAssignableFromMap($returnType) )
            value = new org.iternine.jeppetto.dao.mongodb.enhance.UpdateMap();
            #elseif ( $updateObjectHelper.isAssignableFromSet($returnType) )
            value = new org.iternine.jeppetto.dao.mongodb.enhance.UpdateSet();
            #else
            value = ($returnTypeName) org.iternine.jeppetto.dao.mongodb.enhance.EnhancerHelper.getUpdateObjectEnhancer($returnTypeName .class).newInstance();
            #end

            ((org.iternine.jeppetto.dao.mongodb.enhance.UpdateObject) value).setPrefix(prefix + ${Q}${field}${Q} + '.');

            __updates.put(${Q}$field${Q}, value);
        }

        return value;
    }")
    #end

    $_.method("
    public void $setterName($returnTypeName value) {
        // TODO: handle id
        #if ( $returnType.isPrimitive() )
        __updates.put(${Q}$field${Q}, org.iternine.jeppetto.enhance.ReferenceUtil.asObject(value));
        #else
        __updates.put(${Q}$field${Q}, value);
        #end
    }")
    #end


    //-------------------------------------------------------------
    // Implementation - UpdateObject
    //-------------------------------------------------------------

    $_.method("
    public com.mongodb.DBObject getUpdateClause() {
        com.mongodb.BasicDBObject updateClause = new com.mongodb.BasicDBObject();
        com.mongodb.BasicDBObject sets = new com.mongodb.BasicDBObject();

        // To simplify the code in the for-loop, we assume we'll have some set values. If we don't, we'll remove it
        // from updateClause below.
        updateClause.put(${Q}\$set${Q}, sets);

        for (java.util.Iterator iterator = __updates.entrySet().iterator(); iterator.hasNext(); ) {
            java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
            Object value = entry.getValue();

            if (value instanceof org.iternine.jeppetto.dao.mongodb.enhance.UpdateObject) {
                com.mongodb.DBObject innerClause = ((org.iternine.jeppetto.dao.mongodb.enhance.UpdateObject) value).getUpdateClause();

                for (java.util.Iterator innerIterator = innerClause.keySet().iterator(); innerIterator.hasNext(); ) {
                    String updateOperation = (String) innerIterator.next();
                    com.mongodb.DBObject updateValue = (com.mongodb.DBObject) updateClause.get(updateOperation);

                    if (updateValue == null) {
                        updateClause.put(updateOperation, innerClause.get(updateOperation));
                    } else {
                        updateValue.putAll((org.bson.BSONObject) innerClause.get(updateOperation));
                    }
                }
            } else {
                sets.put(prefix + entry.getKey(), org.iternine.jeppetto.dao.mongodb.enhance.DBObjectUtil.toDBObject(entry.getValue()));
            }
        }

        if (sets.size() == 0) {
            updateClause.removeField(${Q}\$set${Q});
        }

        return updateClause;
    }
    ")


    $_.method("
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }")

    $_.method("
    public java.util.Map __getUpdates() {
        return null;
    }
    ")
// declare a "setValue()" method that can be used for extraValues (e.g. __olv)?
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy