[ 
https://issues.apache.org/jira/browse/ABDERA-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12598732#action_12598732
 ] 

Remy Gendron commented on ABDERA-168:
-------------------------------------

I have added the following to my adapter ancestor class...  

 /**
     * On intercept une requête pour une collection (getEntries()) qui sera 
faite
     * par addFeedDetails, afin de traiter automatiquement un filtre par 
categories
     * pour les adapteurs qui n'implante pas le support des catégories.
     * 
     * Si on ne le faisait pas, un adapteur ignorerait simplement les catégories
     * passées en paramètres.
     * 
     * Un adapteur retournera un 400 automatiquement si le support des 
catégories
     * est à false, sans avoir à ce soucier de ce support dans le dao associé à 
l'adapteur.
     * 
     * De plus, on en profite pour ajouter automatiquement les liens pour le 
paging.
     * 
     * @see #getHandlesCategories()
     */
    @Override
    protected void addFeedDetails(Feed feed, RequestContext request) throws 
ResponseContextException {
        // Y a-t-il au moins une categorie sur l'URL?
        
        if (request.getTarget().getParameter("category1") != null) {
            // Il y a des categories dans l'URI de la requete...
            
            if (!getHandlesCategories()) {
                // L'adapteur ne supporte pas les categories. On bypass le 
traitement normal.

                ServiceContext context = getServiceContext(request);
                
                
context.getGenericErrors().reject(BaseErrorCode.REST_REQUEST_CATEGORIES_NOT_SUPPORTED.toString(),
 
                        "Cet adapteur ne supporte pas les catégories.");
                checkForErrors(context, request);
            }
        }

        // TODO:RG Devrait utiliser un hook du genre afterGetEntries() plutôt
        //         que de réimplanter complètement addFeedDetails().
        // Ajouter automatiquement un lien next page pour le paging si le 
nombre d'entrées
        // dans le feed est un de plus que ce qui devait être retourné.
        // La méthode getServiceContext() a modifié cette valeur pour que 
l'adapteur
        // charge une entrée supplémentaire. Donc, si cette entrée est présente 
dans
        // le feed, cela indique qu'une prochaine page est disponible.
        
        feed.setUpdated(new Date());

        Iterable<T> entries = getEntries(request);

        ServiceContext context = getServiceContext(request);
        int inflatedMaxResult = context.getMaxResults();
        // On corrige l'info de paging dans le ServiceContext.
        context.setMaxResults(inflatedMaxResult - 1);
        boolean needNextPageLink = false;
        int collectionSize = 0;
        
        if (entries != null) {
            for (T entryObj : entries) {
                // Detection de la presence d'une prochaine page.
                
                ++collectionSize;
                if (collectionSize >= inflatedMaxResult) {
                    needNextPageLink = true;
                    // Ne pas ajouter cette dernière entrée.
                    break;
                }
                
                Entry e = feed.addEntry();

                IRI feedIri = new IRI(getFeedIriForEntry(entryObj, request));
                addEntryDetails(request, e, feedIri, entryObj);

                if (isMediaEntry(entryObj)) {
                    addMediaContent(feedIri, e, entryObj, request);
                } else {
                    addContent(e, entryObj, request);
                }
            }
        }
        
        int currentStartIndex = context.getStartIndex();
        
        // Ajout d'un lien premiere page?
        
        if (currentStartIndex > 1) {
            FeedPagingHelper.setFirst(feed, createPagingLink(request, 1));
        }
        
        // Ajout d'un lien page précédente?
        
        if (currentStartIndex > 1) {
            int newStartIndex = currentStartIndex - context.getMaxResults();
            if (newStartIndex < 1) {
                newStartIndex = 1;
            }
            FeedPagingHelper.setPrevious(feed, createPagingLink(request, 
newStartIndex));
        }

        // Ajout d'un lien page suivante?
        
        if (needNextPageLink) {
            int newStartIndex = currentStartIndex + context.getMaxResults();
            FeedPagingHelper.setNext(feed, createPagingLink(request, 
newStartIndex));
        }
        
        // Un lien derniere page n'est jamais ajouté étant trop couteux à 
calculer.
    }

    /**
     * Génère un lien de paging pour insertion dans le feed par l'appelant.
     * 
     * @param startIndex Le nouveau start-index.
     * @return Un lien absolue.
     */
    private String createPagingLink(RequestContext request, int startIndex) {
        String uri = request.getResolvedUri().toString();
        int separatorIndex = uri.indexOf("?");
        if (separatorIndex != -1) {
            uri = uri.substring(0, separatorIndex);
        }
        
        StringBuilder link = new StringBuilder(uri);
        
        
link.append("?").append(SearchConstants.START_INDEX).append("=").append(startIndex);
        
        for(String parameterName : request.getParameterNames()) {
            if (!parameterName.equals(SearchConstants.START_INDEX)) {
                String parameterValue = request.getParameter(parameterName);
                
link.append("&").append(parameterName).append("=").append(parameterValue);
            }
        }
        
        return link.toString();
    }
    

> Generating page links
> ---------------------
>
>                 Key: ABDERA-168
>                 URL: https://issues.apache.org/jira/browse/ABDERA-168
>             Project: Abdera
>          Issue Type: Improvement
>    Affects Versions: 0.4.0
>            Reporter: Remy Gendron
>            Priority: Minor
>
> Hello all,
> I'm adding glue code in my application to automatically generate and add 
> paging links to a returned feed.
> I have the necessary information to decide if a link should be there.
> I know how to compute the URI parameters to target another page.
> I know how to add a link to a feed with the FeedPagingHelper class.
> My question is: Is there an easy way provided by Abdera to generate the link 
> by altering the current request URI parameters? I need to pass thru all the 
> current parameters that define the feed content returned (eg 
> ?q=xxx&author=yyy&max-results=10) but alter the start-index parameter to 
> target the new page.
> I can do it manually by getting this information from the RequestContext and 
> building the new link. But I see a bunch of getLink() methods in the adapter 
> classes. Is there an easy (or recommended) way of generated new links based 
> on the current request that I haven't seen?
> Another question would be... do you make the paging links relative to the 
> base URI or do paging links need to be absolute as the examples in RFC5005 
> seem to indicate?
> Thank you,
> Rémy

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to