Hello,

This is what I think you need to do in order to sort by a field. The main 
problem is that there are some Strings hardcoded in several classes, so even 
when you define the farm and the query model it keeps calling the default 
listing, there's probably a more efficent way to do it, but this is what I got:

1. Implement a new Farm (Copy the existent code and modify as needed):

import static org.jboss.seam.ScopeType.SESSION;

import java.io.Serializable;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.PagedDocumentsProvider;
import org.nuxeo.ecm.core.api.SortInfo;
import org.nuxeo.ecm.core.search.api.client.querymodel.QueryModel;
import org.nuxeo.ecm.platform.ui.web.api.ResultsProviderFarm;
import org.nuxeo.ecm.webapp.base.InputController;
import org.nuxeo.ecm.webapp.pagination.ResultsProvidersCache;
import org.nuxeo.ecm.webapp.querymodel.QueryModelActions;


@Name("issuedInvoiceSearchFarm")
@Scope(SESSION)
public class InvoiceSearchFarmBean extends InputController implements
ResultsProviderFarm, Serializable {

        private static final long serialVersionUID = 1L;

        // Result providers
        // public static final String CHILDREN_BY_SEARCH =
        // "CURRENT_DOC_CHILDREN_BY_SEARCH";

        private static final String SORTED_ISSUED_INVOICE_QUERY = 
"SORTED_ISSUED_INVOICE_QUERY";
        
        private static final String FILTER_SCHEMA_NAME = "browsing_filters";

        private static final String FILTER_FIELD_NAME_PARENT_ID = 
"query_parentId";

        @In(create = true)
        private transient QueryModelActions queryModelActions;

        @In(create = true)
        private transient ResultsProvidersCache resultsProvidersCache;

        @In(create = true, required = false)
        private transient CoreSession documentManager;

        public PagedDocumentsProvider getResultsProvider(String name)
        throws ClientException {
                return getResultsProvider(name, null);
        }

        public PagedDocumentsProvider getResultsProvider(String name,
                        SortInfo sortInfo) throws ClientException {
                final DocumentModel currentDoc = 
navigationContext.getCurrentDocument();
                
                if (SORTED_ISSUED_INVOICE_QUERY.equals(name)) {
                        PagedDocumentsProvider provider = 
getChildrenResultsProviderQMPattern(name, currentDoc, sortInfo);
                        provider.setName(name);
                        return provider;
                } else {
                        throw new ClientException("Unknown provider: " + name);
                }
        }

        /**
         * Usable with a queryModel that defines a pattern NXQL.
         */
        private PagedDocumentsProvider getChildrenResultsProviderQMPattern(
                        String queryModelName, DocumentModel parent, SortInfo 
sortInfo) throws ClientException {

                final String parentId = parent.getId();
                Object[] params = { parentId };
                return getResultsProvider(queryModelName, params, sortInfo);
        }

        private PagedDocumentsProvider getResultsProvider(String qmName,
                        Object[] params, SortInfo sortInfo) throws 
ClientException {
                QueryModel qm = queryModelActions.get(qmName);
                return qm.getResultsProvider(documentManager, params, sortInfo);
        }

        /**
         * Usable with a queryModel that defines a WhereClause with predicates.
         */
        protected PagedDocumentsProvider getChildrenResultsProviderQMPred(
                        String queryModelName, DocumentModel currentDoc)
        throws ClientException {
                QueryModel qm = queryModelActions.get(queryModelName);
                if (qm == null) {
                        throw new ClientException("no QueryModel registered 
under name: "
                                        + queryModelName);
                }

                // Invalidation code. TODO Would be better to listen to an event
                String currentRef = currentDoc.getRef().toString();
                if (!currentRef.equals(qm.getProperty(FILTER_SCHEMA_NAME,
                                FILTER_FIELD_NAME_PARENT_ID))) {
                        qm.setProperty(FILTER_SCHEMA_NAME, 
FILTER_FIELD_NAME_PARENT_ID,
                                        currentRef);
                        resultsProvidersCache.invalidate(queryModelName);
                }

                return resultsProvidersCache.get(queryModelName);
        }

}


2. Define a new results provider:

<resultsProvider name="SORTED_ISSUED_INVOICE_QUERY" 
farm="issuedInvoiceSearchFarm"/>


3. Define a new query model:

    <queryModel name="SORTED_ISSUED_INVOICE_QUERY">
      <pattern>
        SELECT * FROM Document
                WHERE ecm:parentId = ? AND ecm:isCheckedInVersion = 0 
                AND ecm:mixinType != 'HiddenInNavigation' 
                AND ecm:currentLifeCycleState != 'deleted'
      </pattern>
      <sortable value="true" defaultSortColumn="Invoice:document_number"
        defaultSortAscending="true" />
      <max>10</max>
    </queryModel>
    
    
 4. Define a custom DocumentActionsBean and add the following method:
 
     @Factory(value = "currentIssuedInvoiceChildrenSelectModel", scope = EVENT)
    public SelectDataModel getIssuedInvoiceChildrenSelectModel() throws 
ClientException {
        // XXX : this proves that this method is called too many times
        // log.debug("Getter children select model");
        CustomNavigationContextBean 
customNavigationContext=(CustomNavigationContextBean)navigationContext;

        //nuevo metodo que recibe por parametro la ordenacion a usar. El metodo 
sin parametros tiene a fuego la busqueda por defecto
        DocumentModelList documents = 
customNavigationContext.getCurrentDocumentChildrenPage(SORTED_ISSUED_INVOICE_QUERY);
        List<DocumentModel> selectedDocuments = 
documentsListsManager.getWorkingList(
                DocumentsListsManager.CURRENT_DOCUMENT_SELECTION);
        SelectDataModel model = new SelectDataModelImpl(CHILDREN_DOCUMENT_LIST,
                documents, selectedDocuments);
        model.addSelectModelListener(this);
        // XXX AT: see if cache is useful
        // cacheUpdateNotifier.addCacheListener(model);
        return model;
    }
    
 
 
 5. Define a custom NavigationContextBean and add the following method:
 
     public DocumentModelList getCurrentDocumentChildrenPage(String name)
    throws ClientException {
        final String logPrefix = "<getCurrentDocumentChildrenPage> ";

        if (documentManager == null) {
                log.error(logPrefix + "documentManager not initialized");
                return new DocumentModelListImpl();
        }

        try {
                ResultsProvidersCache resultsProvidersCache = 
(ResultsProvidersCache) Component.getInstance("resultsProvidersCache");
                //resultsProvidersCache.invalidate(name);
                resultsProvider = resultsProvidersCache.get(name);

                currentDocumentChildren = resultsProvider.getCurrentPage();
        } catch (Throwable t) {
                throw ClientException.wrap(t);
        }
        return currentDocumentChildren;
    }   
 
 
 6. Modify /nuxeo.war/incl/tabs/document_content.xhtml adding a choose element 
and including the new folder type:
 
        <c:choose>
                <c:when test="${currentDocument.type=='IssuedInvoiceFolder'}">
                        <nxu:set var="provider"
                                
value="#{resultsProvidersCache.get('SORTED_ISSUED_INVOICE_QUERY')}"
                                cache="true">
                                <nxu:set var="layoutName" 
value="#{currentListingLayoutName}"
                                        cache="true">

                                        <ui:decorate 
template="/incl/documents_layout_table.xhtml">
                                                <ui:param name="documents" 
value="#{currentIssuedInvoiceChildrenSelectModel}" />
                                                <ui:param name="listName" 
value="CURRENT_SELECTION" />
                                                <ui:param name="layoutName" 
value="#{layoutName}" />
                                                <ui:define name="buttons">
                                                        <ui:include 
src="/incl/clipboard.xhtml" />
                                                </ui:define>
                                        </ui:decorate>

                                </nxu:set>
                        </nxu:set>
                </c:when>
                //other options
        </c:choose>
 
I couldn't make it work by extending the navigation and documentactions beans, 
so I had to copy the full class, change the bean name and add the new methods.

I think this is all you need, but maybe I forgot something, so if you need more 
help tell me (or send me a PM, and I'll answer you directly in spanish)
--
Posted by "aldago" at Nuxeo Discussions <http://nuxeo.org/discussions>
View the complete thread: 
<http://www.nuxeo.org/discussions/thread.jspa?threadID=3942#12336>
_______________________________________________
ECM mailing list
[email protected]
http://lists.nuxeo.com/mailman/listinfo/ecm
To unsubscribe, go to http://lists.nuxeo.com/mailman/options/ecm

Reply via email to