/*
 * Copyright 2015-2016 EuregJUG.
 *
 * 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.
 */
package eu.euregjug.site.posts;

import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Michael J. Simons, 2015-12-29
 */
public class PostRepositoryImpl implements PostRepositoryExt {

    private final EntityManager entityManager;

    public PostRepositoryImpl(final EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<PostEntity> getPrevious(final PostEntity post) {
        return getRelatedPost("PostEntity.getPrevious", post);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<PostEntity> getNext(final PostEntity post) {
        return getRelatedPost("PostEntity.getNext", post);
    }

    Optional<PostEntity> getRelatedPost(final String query, final PostEntity post) {
        Optional<PostEntity> rv;
        try {
            rv = Optional.of(
                    entityManager.createNamedQuery(query, PostEntity.class)
                    .setParameter("id", post.getId())
                    .setMaxResults(1)
                    .getSingleResult()
            );
        } catch (NoResultException e) {
            rv = Optional.empty();
        }
        return rv;
    }
}

@@ -1859,30 +1859,336 @@
 ch (
-NoResultException e) %7B
+@SuppressWarnings(%7B%22squid:S1166%22%7D) NoResultException e) %7B%0A            // The only reason the query call is implemented manually%0A            // is the fact the I cannot use @Query and limiting%0A            // the result set in a database agnostic way%0A            // (for example through the method name (findXXX) or an annotation)
 %0A   

Document the reason of manually implementing query