com.lm.lic.manager.hibernate.FloatingLicenseConfigDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.lm.lic.manager.hibernate.FloatingLicenseConfigDAO.java

Source

package com.lm.lic.manager.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for HashedLicMechConfig entities. Transaction control of
 * the save(), update() and delete() operations can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods provides additional information for how to
 * configure it for the desired type of transaction control.
 * 
 * @see com.lm.lic.manager.hibernate.HashedLicMechConfig
 * @author Ibrahim Mustafa
 */

public class FloatingLicenseConfigDAO extends HibernateDaoSupport {
    private static final Log log = LogFactory.getLog(FloatingLicenseConfigDAO.class);

    public static final String MAX_NUM_USERS = "maxNumUsers";
    public static final String NUM_ACTIVE_USERS = "numActiveUsers";
    public static final String DOMAIN_NAME = "domainName";
    public static final String GATEWAY_IP_ADDRESS = "gatewayIpAddress";
    public static final String ALGORITHM = "algorithm";
    public static final String CREATED_BY = "createdBy";
    public static final String MODIFIED_BY = "modifiedBy";

    @Override
    protected void initDao() {
    }

    public void save(FloatingLicenseConfig transientInstance) {
        log.debug("saving FloatingLicenseConfig instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(FloatingLicenseConfig persistentInstance) {
        log.debug("deleting FloatingLicenseConfig instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public FloatingLicenseConfig findById(java.lang.Long id) {
        log.debug("getting FloatingLicenseConfig instance with id: " + id);
        try {
            FloatingLicenseConfig instance = (FloatingLicenseConfig) getHibernateTemplate()
                    .get("com.lm.lic.manager.hibernate.FloatingLicenseConfig", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public FloatingLicenseConfig findByLicenseId(java.lang.Long licenseId) {
        log.debug("getting FloatingLicenseConfig instance for license with id: " + licenseId);
        FloatingLicenseConfig flc = null;
        try {
            String queryString = "from FloatingLicenseConfig as flc where flc.license.id= ?";
            @SuppressWarnings("unchecked")
            List<FloatingLicenseConfig> flcs = getHibernateTemplate().find(queryString, licenseId);
            flc = flcs != null && flcs.size() > 0 ? flcs.get(0) : null;
        } catch (RuntimeException re) {
            log.error("find by license id failed", re);
            throw re;
        }
        return flc;
    }

    @SuppressWarnings("unchecked")
    public List<FloatingLicenseConfig> findByExample(FloatingLicenseConfig instance) {
        log.debug("finding FloatingLicenseConfig instance by example");
        try {
            List<FloatingLicenseConfig> results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
    public List<FloatingLicenseConfig> findByProperty(String propertyName, Object value) {
        log.debug("finding FloatingLicenseConfig instance with property: " + propertyName + ", value: " + value);
        try {
            String queryString = "from FloatingLicenseConfig as model where model." + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<FloatingLicenseConfig> findByMaxNumUsers(Object keyFormat) {
        return findByProperty(MAX_NUM_USERS, keyFormat);
    }

    public List<FloatingLicenseConfig> findByNnumActiveUsers(Object base) {
        return findByProperty(NUM_ACTIVE_USERS, base);
    }

    public List<FloatingLicenseConfig> findByDomainName(Object segment) {
        return findByProperty(DOMAIN_NAME, segment);
    }

    public List<FloatingLicenseConfig> findByGatewayIpAddress(Object length) {
        return findByProperty(GATEWAY_IP_ADDRESS, length);
    }

    public List<FloatingLicenseConfig> findByCreatedBy(Object createdBy) {
        return findByProperty(CREATED_BY, createdBy);
    }

    public List<FloatingLicenseConfig> findByModifiedBy(Object modifiedBy) {
        return findByProperty(MODIFIED_BY, modifiedBy);
    }

    @SuppressWarnings("unchecked")
    public List<FloatingLicenseConfig> findAll() {
        log.debug("finding all FloatingLicenseConfig instances");
        try {
            String queryString = "from FloatingLicenseConfig";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public FloatingLicenseConfig merge(FloatingLicenseConfig detachedInstance) {
        log.debug("merging FloatingLicenseConfig instance");
        try {
            FloatingLicenseConfig result = getHibernateTemplate().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(FloatingLicenseConfig instance) {
        log.debug("attaching dirty FloatingLicenseConfig instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(FloatingLicenseConfig instance) {
        log.debug("attaching clean FloatingLicenseConfig instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static FloatingLicenseConfigDAO getFromApplicationContext(ApplicationContext ctx) {
        return (FloatingLicenseConfigDAO) ctx.getBean("FloatingLicenseConfigDAO");
    }

    /**
     * @param isvId
     * @param prodKey
     * @param licKey
     * @param domain
     * @return
     */
    @SuppressWarnings("unchecked")
    public FloatingLicenseConfig findFloatingLicenseConfigForProductAndLicKey(Long isvId, String prodKey,
            String licKey) {
        log.debug("finding FloatingLicenseConfig instance");
        FloatingLicenseConfig floatingLicenseConfig = null;
        List<FloatingLicenseConfig> floatingLicenseConfigs = null;
        try {
            String queryString = "from FloatingLicenseConfig as flc where flc.isv.id= ? and flc.product.productKey= ? and flc.license.licKey= ?";
            floatingLicenseConfigs = getHibernateTemplate().find(queryString, isvId, prodKey, licKey);
            if (floatingLicenseConfigs != null && floatingLicenseConfigs.size() > 0)
                floatingLicenseConfig = floatingLicenseConfigs.get(0);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
        return floatingLicenseConfig;
    }

    /**
     * @param isvId
     * @param prodKey
     * @param licKey
     * @param domain
     * @return
     */
    @SuppressWarnings("unchecked")
    public FloatingLicenseConfig findFloatingLicenseConfigForProductAndLicKeyForEmail(Long isvId, String prodKey,
            String licKey, String domain) {
        log.debug("finding FloatingLicenseConfig instance");
        FloatingLicenseConfig floatingLicenseConfig = null;
        List<FloatingLicenseConfig> floatingLicenseConfigs = null;
        try {
            String queryString = "from FloatingLicenseConfig as flc where flc.isv.id= ? and flc.product.productKey= ? and flc.license.licKey= ? and flc.clientEmail= ?";
            floatingLicenseConfigs = getHibernateTemplate().find(queryString, isvId, prodKey, licKey, domain);
            if (floatingLicenseConfigs != null && floatingLicenseConfigs.size() > 0)
                floatingLicenseConfig = floatingLicenseConfigs.get(0);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
        return floatingLicenseConfig;
    }
}