org.sparkcommerce.openadmin.server.service.persistence.ArchiveStatusPersistenceEventHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.sparkcommerce.openadmin.server.service.persistence.ArchiveStatusPersistenceEventHandler.java

Source

/*
 * #%L
 * SparkCommerce Open Admin Platform
 * %%
 * Copyright (C) 2009 - 2014 Spark Commerce
 * %%
 * 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%
 */
package org.sparkcommerce.openadmin.server.service.persistence;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sparkcommerce.common.exception.ServiceException;
import org.sparkcommerce.common.persistence.Status;
import org.sparkcommerce.openadmin.dto.CriteriaTransferObject;
import org.sparkcommerce.openadmin.dto.PersistencePackage;
import org.sparkcommerce.openadmin.server.service.persistence.module.EmptyFilterValues;
import org.sparkcommerce.openadmin.server.service.persistence.module.criteria.FieldPath;
import org.sparkcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder;
import org.sparkcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping;
import org.sparkcommerce.openadmin.server.service.persistence.module.criteria.Restriction;
import org.sparkcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider;
import org.springframework.stereotype.Component;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

/**
 * Adds {@link FilterMapping} to the {@link CriteriaTransferObject}'s {@link CriteriaTransferObject#getAdditionalFilterMappings()}
 * in order to exclude by default any entities that are archived.
 * 
 * @author Phillip Verheyden (phillipuniverse)
 */
@Component("blArchiveStatusPersistenceEventHandler")
public class ArchiveStatusPersistenceEventHandler extends PersistenceManagerEventHandlerAdapter {

    private static final Log LOG = LogFactory.getLog(ArchiveStatusPersistenceEventHandler.class);

    @Override
    public PersistenceManagerEventHandlerResponse preFetch(PersistenceManager persistenceManager,
            PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
        try {
            Class<?>[] entityClasses = persistenceManager.getDynamicEntityDao()
                    .getAllPolymorphicEntitiesFromCeiling(
                            Class.forName(persistencePackage.getCeilingEntityFullyQualifiedClassname()));
            boolean isArchivable = false;
            for (Class<?> entity : entityClasses) {
                if (Status.class.isAssignableFrom(entity)) {
                    isArchivable = true;
                    break;
                }
            }
            if (isArchivable && !persistencePackage.getPersistencePerspective().getShowArchivedFields()) {
                FilterMapping filterMapping = new FilterMapping()
                        .withFieldPath(new FieldPath().withTargetProperty("archiveStatus.archived"))
                        .withDirectFilterValues(new EmptyFilterValues()).withRestriction(new Restriction()
                                .withPredicateProvider(new PredicateProvider<Character, Character>() {
                                    @Override
                                    public Predicate buildPredicate(CriteriaBuilder builder,
                                            FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity,
                                            String fullPropertyName, Path<Character> explicitPath,
                                            List<Character> directValues) {
                                        return builder.or(builder.equal(explicitPath, 'N'),
                                                builder.isNull(explicitPath));
                                    }
                                }));
                cto.getAdditionalFilterMappings().add(filterMapping);
            }
            return new PersistenceManagerEventHandlerResponse().withStatus(
                    PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED);
        } catch (ClassNotFoundException e) {
            LOG.error("Could not find the class " + persistencePackage.getCeilingEntityFullyQualifiedClassname()
                    + " to " + "compute polymorphic entity types for. Assuming that the entity is not archivable");
            return new PersistenceManagerEventHandlerResponse().withStatus(
                    PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.NOT_HANDLED);
        }
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }

}