com.webbfontaine.valuewebb.action.rimm.CacheCleaner.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.action.rimm.CacheCleaner.java

Source

package com.webbfontaine.valuewebb.action.rimm;

import com.webbfontaine.valuewebb.model.util.Utils;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import com.webbfontaine.valuewebb.validation.InfoHandling;
import org.ajax4jsf.model.KeepAlive;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import javax.persistence.EntityManager;

/**
 * Copyrights 2002-2014 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * Developer: nigiyan
 * Date: 31/05/2014
 */

@Name("cacheCleaner")
@KeepAlive
public class CacheCleaner {
    private static final Log LOGGER = Logging.getLog(CacheCleaner.class);

    private String entityClass;

    public Object clean() {

        if (StringUtils.trimToNull(entityClass) != null) {

            EntityManager entityManager = Utils.getEntityManager();
            SessionFactory sf = ((Session) entityManager.getDelegate()).getSessionFactory();
            EntityPersister classMetadata = (EntityPersister) sf.getClassMetadata(entityClass);
            if (classMetadata != null && classMetadata.hasCache()) {
                LOGGER.info("Evicting cache for {0}", entityClass);
                sf.evictEntity(entityClass);
                sf.evictQueries();
                InfoHandling.getInstance().setInfoList("Cache evicted");
            } else {
                ErrorHandling.addFacesMessageError(null, "Entity is not cached");
            }
        }
        return "";
    }

    public String getEntityClass() {
        return entityClass;
    }

    public void setEntityClass(String entityClass) {
        this.entityClass = entityClass;
    }
}