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

org.iternine.jeppetto.dao.mongodb.enhance.EnhancerHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011-2017 Jeppetto and Jonathan Thompson
 *
 * 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.iternine.jeppetto.dao.mongodb.enhance;


import org.iternine.jeppetto.dao.EntityVelocityEnhancer;
import org.iternine.jeppetto.dao.updateobject.UpdateObjectVelocityEnhancer;
import org.iternine.jeppetto.enhance.Enhancer;
import org.iternine.jeppetto.enhance.NoOpEnhancer;

import java.util.HashMap;
import java.util.Map;


public class EnhancerHelper {

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

    private static final Map dirtyableDBObjectEnhancers = new HashMap();
    private static final Map updateObjectEnhancers = new HashMap();


    //-------------------------------------------------------------
    // Methods - Public - Static
    //-------------------------------------------------------------

    /**
     * Creates a new dirtyableDBObject Enhancer for the given class. If the class already implements
     * {@link DirtyableDBObject}, then a special "no-op" enhancer will be returned that
     * doesn't do any special enhancement. Otherwise, a byte-code enhancer is returned.
     *
     * @param baseClass class for which to create an enhancer
     *
     * @return new enhancer
     */
    @SuppressWarnings( { "unchecked" })
    public static  Enhancer getDirtyableDBObjectEnhancer(Class baseClass) {
        if (dirtyableDBObjectEnhancers.containsKey(baseClass)) {
            return (Enhancer) dirtyableDBObjectEnhancers.get(baseClass);
        }

        synchronized (dirtyableDBObjectEnhancers) {
            Enhancer enhancer;

            if (dirtyableDBObjectEnhancers.get(baseClass) != null) {
                enhancer = (Enhancer) dirtyableDBObjectEnhancers.get(baseClass);
            } else if (DirtyableDBObject.class.isAssignableFrom(baseClass)) {
                enhancer = new NoOpEnhancer(baseClass);

                dirtyableDBObjectEnhancers.put(baseClass, enhancer);
            } else {
                enhancer = new EntityVelocityEnhancer(baseClass) {
                    //-------------------------------------------------------------
                    // Implementation - Enhancer
                    //-------------------------------------------------------------

                    @Override
                    public boolean needsEnhancement(Object object) {
                        return object != null && !(object instanceof DirtyableDBObject);
                    }


                    //-------------------------------------------------------------
                    // Implementation - VelocityEnhancer
                    //-------------------------------------------------------------

                    @Override
                    protected String getTemplateLocation() {
                        return "org/iternine/jeppetto/dao/mongodb/enhance/dirtyableDBObject.vm";
                    }
                };

                dirtyableDBObjectEnhancers.put(baseClass, enhancer);
            }

            return enhancer;
        }
    }


    /**
     * Creates a new dirtyableDBObject Enhancer for the given class. If the class already implements
     * {@link DirtyableDBObject}, then a special "no-op" enhancer will be returned that
     * doesn't do any special enhancement. Otherwise, a byte-code enhancer is returned.
     *
     * @param baseClass class for which to create an enhancer
     *
     * @return new enhancer
     */
    @SuppressWarnings( { "unchecked" })
    public static  Enhancer getUpdateObjectEnhancer(Class baseClass) {
        if (updateObjectEnhancers.containsKey(baseClass)) {
            return (Enhancer) updateObjectEnhancers.get(baseClass);
        }

        synchronized (updateObjectEnhancers) {
            Enhancer enhancer;

            if (updateObjectEnhancers.get(baseClass) != null) {
                enhancer = (Enhancer) updateObjectEnhancers.get(baseClass);
            } else if (UpdateObject.class.isAssignableFrom(baseClass)) {
                enhancer = new NoOpEnhancer(baseClass);

                updateObjectEnhancers.put(baseClass, enhancer);
            } else {
                enhancer = new UpdateObjectVelocityEnhancer(baseClass) {
                    //-------------------------------------------------------------
                    // Implementation - Enhancer
                    //-------------------------------------------------------------

                    @Override
                    public boolean needsEnhancement(Object object) {
                        return object != null && !(object instanceof UpdateObject);
                    }


                    //-------------------------------------------------------------
                    // Implementation - VelocityEnhancer
                    //-------------------------------------------------------------

                    @Override
                    protected String getTemplateLocation() {
                        return "org/iternine/jeppetto/dao/mongodb/enhance/updateObject.vm";
                    }


                    @Override
                    protected Map getAdditionalContextItems() {
                        Map contextItems = super.getAdditionalContextItems();

                        contextItems.put("updateObjectHelper", new UpdateObjectHelper());

                        return contextItems;
                    }
                };

                updateObjectEnhancers.put(baseClass, enhancer);
            }

            return enhancer;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy