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

org.fryske_akademy.ejb.AbstractCrudServiceEnvers Maven / Gradle / Ivy

There is a newer version: 10.6
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fryske_akademy.ejb;

/*-
 * #%L
 * ejbCrudApi
 * %%
 * Copyright (C) 2018 Fryske Akademy
 * %%
 * 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.
 * #L%
 */
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fryske_akademy.jpa.RevInfo;
import org.fryske_akademy.jpa.RevisionInfo;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.AuditQuery;
import org.hibernate.envers.query.criteria.internal.IdentifierEqAuditExpression;

/**
 * 
 * @author eduard
 */
public abstract class AbstractCrudServiceEnvers extends AbstractCrudService implements Auditing{
    
    private static final Logger LOGGER = Logger.getLogger(AbstractCrudServiceEnvers.class.getName());

    @Override
    public  T getRevision(Number n, Class type) {
        if (!type.isAnnotationPresent(Audited.class)) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine("No @Audited annotation on " + type.getName());
            }
            return null;
        }
        return (T) AuditReaderFactory.get(getEntityManager()).createQuery().forEntitiesAtRevision(type, n).getSingleResult();
    }

    @Override
    public  List> getRevisionInfo(Serializable id, Integer max, Class type) {
        List> result = new ArrayList<>(5);
        if (!type.isAnnotationPresent(Audited.class)) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine("No @Audited annotation on " + type.getName());
            }
            return result;
        }
        if (id == null) {
            return result;
        }
        AuditQuery query = AuditReaderFactory.get(getEntityManager()).createQuery().forRevisionsOfEntity(type, false, true).addOrder(AuditEntity.revisionNumber().desc());
        query.add(new IdentifierEqAuditExpression(null, id, true)).setMaxResults((max == null) ? 5 : max);
        List resultList = query.getResultList();
        if (resultList.isEmpty()) {
            return result;
        }
        for (Object[] object : resultList) {
            RevisionType rt = (RevisionType) object[2];
            switch (rt) {
                case ADD:
                    result.add(new RevInfo((T) object[0], RevInfo.TYPE.CREATE, (RevisionInfo) object[1]));
                    break;
                case MOD:
                    result.add(new RevInfo((T) object[0], RevInfo.TYPE.UPDATE, (RevisionInfo) object[1]));
                    break;
                case DEL:
                    result.add(new RevInfo((T) object[0], RevInfo.TYPE.DELETE, (RevisionInfo) object[1]));
                    break;
            }
        }
        return result;
    }

    @Override
    public  List getRevisionNumbers(Object id, Class type) {
        if (!type.isAnnotationPresent(Audited.class)) {
            return Collections.emptyList();
        }
        return AuditReaderFactory.get(getEntityManager()).getRevisions(type, id);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy