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

org.fryske_akademy.services.CrudReadService Maven / Gradle / Ivy

The 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.services;

/*-
 * #%L
 * jpaservices
 * %%
 * 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 org.fryske_akademy.jpa.Param;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * Generic interface for read operations on entities, the dynamic methods offer powerfull support for building queries
 * including a bit of syntax in user input.
 *
 * @see org.fryske_akademy.jpa.OPERATOR
 * @see org.fryske_akademy.jpa.JpqlBuilder
 * @see Param
 * @author eduard
 */
public interface CrudReadService {

    enum SORTORDER {
        DESC, ASC, NONE;

        public static Map order(String key, SORTORDER order) {
            return new Builder().add(key, order).build();
        }

        public static class Builder {

            private final Map sort = new LinkedHashMap<>(2);

            public Builder add(String key, SORTORDER o) {
                sort.put(key, o);
                return this;
            }

            public Map build() {
                return sort;
            }
        }
    }

    /**
     * Return a default page size when no max is given, negative value means no limit
     *
     * @return
     */
    int getDefaultPageSize();

     T find(Serializable id, Class type);

     List findAll(Class type);

     Stream streamAll(Class type);

    /**
     * 
     * @param 
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param sort
     * @param params
     * @param type
     * @return 
     */
     List findDynamic(Integer first, Integer max, Map sort, List params, Class type);

    /**
     * 
     * @param 
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param sort
     * @param params
     * @param type
     * @return 
     */
     Stream streamDynamic(Integer first, Integer max, Map sort, List params, Class type);

    /**
     * Call this using the same parameters as in findDynamic to get to know the total number of results
     * @param params
     * @param type
     * @return 
     */
    int countDynamic(List params, Class type);

    /**
     * 
     * @param 
     * @param namedQuery
     * @param params
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param type
     * @return 
     */
     List find(String namedQuery, List params, Integer first, Integer max, Class type);

    /**
     * query using your own jpql
     * @param jpql
     * @return
     */
     List findJpql(String jpql, Class clazz);

    /**
     * query using your own sql and a wrapper entity class
     * @param sql
     * @return
     */
     List findNative(String sql, Class clazz);

    /**
     * query using your own sql
     * @param sql
     * @return for select queries a list of object or object[]
     */
    List findNative(String sql);
    /**
     * 
     * @param 
     * @param namedQuery
     * @param params
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param type
     * @return 
     */
     Stream stream(String namedQuery, List params, Integer first, Integer max, Class type);

    /**
     * 
     * @param 
     * @param namedNativeQuery
     * @param params
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param type
     * @return 
     */
     List findNative(String namedNativeQuery, List params, Integer first, Integer max, Class type);

    /**
     * 
     * @param 
     * @param namedNativeQuery
     * @param params
     * @param first defaults to 0
     * @param max defaults to {@link #getDefaultPageSize() }
     * @param type
     * @return 
     */
     Stream streamNative(String namedNativeQuery, List params, Integer first, Integer max, Class type);

    /**
     * Call this using the same parameters as in {@link #find(java.lang.String, java.util.List, java.lang.Integer, java.lang.Integer, java.lang.Class) }
     * to get to know the total number of results
     *
     * @param namedQuery
     * @param params
     * @return
     */
    int count(String namedQuery, List params);

    /**
     * return one result or null, multiple results throws an exception
     *
     * @param 
     * @param namedQuery
     * @param params
     * @param type
     * @return
     */
     T findOne(String namedQuery, List params, Class type);

    /**
     * return one result, no or multiple results throws an exception
     *
     * @param 
     * @param namedQuery
     * @param params
     * @param type
     * @return
     */
     T findExactlyOne(String namedQuery, List params, Class type) throws PreventEJBLoggingException;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy