AnsweredAssumed Answered

Custom search dialog

Question asked by oltchuva on Oct 3, 2008
Latest reply on Jun 15, 2011 by flykarthick
I needed a custom view to display search results. I couldn't find how to apply template to search results using web client, so I implemented a dialog that takes search terms as parameter and displays results using custom jsp

Below are details of the implementation in case someone is trying to solve a similar problem. I removed my application specific sorting and data formatting (the reason I needed custom view in the first place), but you should get an idea =)

I also have a couple of questions; they are in in blue font

I packaged dialog as described in Packaging_And_Deploying_Extensions by placing files in the root of deployed Alfresco web application in <alfresco_home>\tomcat\webapps\alfresco

> WEB-INF
   > lib
      > custom-dialog.jar
         > alfresco
            > extension
               > web-client-config-custom.xml
         > META-INF
            > faces-config.xml
            > MANIFEST.MF
         > edu
            > fccc
               > sample
                  > SearchClinicalDocumentsDialog.class
                  > SearchClinicalDocumentsDialog$Node.class
> jsp
   > extension
      > search-clinical-documents.jsp
Q: Does anyone know how to work with Multiple web-client-config-custom.xml files

web-client-config-custom.xml

<alfresco-config>

         <!– Add custom dialog –>
    <config>
      <actions>
      <!– Launch Clinical Documents Search Dialog –>
          <action id="search_clinical_documents">
             <label>Search Clinical Documents</label>
             <image>/images/icons/view_web_project.gif</image>
             <action>dialog:searchClinicalDocuments</action>
          </action>
          <action-group id="navigator_actions">
             <action idref="search_clinical_documents"/>
          </action-group>
      </actions>
   </config>
   
    <config>   
       <dialogs>
          <dialog name="searchClinicalDocuments"
                   page = "/jsp/extension/search-clinical-documents.jsp"
                   managed-bean="SearchClinicalDocumentsDialog"
                   icon="/images/icons/submit_large.gif"
                   title="Search for Clinical Documents"
                   description="Returns list of clinical documents">
            <buttons>
                  <button id="searchClinicalDocuments_new"
                        label="New search"
                          action="#{DialogManager.bean.newSearch}"
                          disabled="#{DialogManager.bean.newSearchDisabled}"

                          />
               </buttons>
          </dialog>
       </dialogs>
    </config>   
</alfresco-config>
Q:  I used action-group "navigator_actions" to add link to the dialog, but i actually would like to add it to search drop down list under Advanced Search option. Is there an action-group for that?

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>

   <managed-bean>
      <managed-bean-name>SearchClinicalDocumentsDialog</managed-bean-name>
      <managed-bean-class>edu.fccc.sample.ClinicalDocumentsDialog</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
         <property-name>searchService</property-name>
         <value>#{SearchService}</value>
      </managed-property>
   </managed-bean>

</faces-config>

SearchClinicalDocumentsDialog.java
 package edu.fccc.sample;

import javax.faces.context.FacesContext;
import org.alfresco.web.bean.dialog.BaseDialogBean;

import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.web.app.servlet.DownloadContentServlet;


public class SearchClinicalDocumentsDialog extends BaseDialogBean{
     
   private String searchTerms;
   private boolean searchFinished;
   private java.util.List<Node> sortedNodes;
   
   
   public void init (java.util.Map  parameters){
         super.init(parameters);
         searchFinished = false;
      }

      protected String finishImpl(FacesContext context, String outcome) throws Exception
      {
         startSearch();
         searchFinished = true;
         return "do nothing";
      }

      public boolean getFinishButtonDisabled()
      {
        return searchFinished;
      }

      public boolean getNewSearchDisabled(){
         return !searchFinished;
      }

      public String getFinishButtonLabel()
      {
         return "Search";
      }
     
      public String getCancelButtonLabel()
      {
         return "Finish";
      }
     
   //main method: performs search and stores result to sortedNodes
   public void startSearch(){

      sortedNodes = new java.util.ArrayList<Node>();
      
      SearchParameters searchParameters = new SearchParameters();
      searchParameters.addStore(new StoreRef("workspace", "SpacesStore"));
      searchParameters.setLanguage(this.getSearchService().LANGUAGE_LUCENE);
      //I only searched for documents with certain custom aspect; modify query to fit your model
String aspect= " +ASPECT:\"{custom.model}ClinicalReport\"";
      String text = " +TEXT:\""+ searchTerms.trim() +"\"";
      
      searchParameters.setQuery(aspect + text);
      
      ResultSet reports = this.getSearchService().query(searchParameters);
      for (ResultSetRow row: reports){
         sortedNodes.add(new Node(row));
      }
   }
   
// called when New Search button clicked – resets dialog
   public void newSearch(){
      init(new java.util.HashMap());
   }
   

         public String getSearchTerms() {
         return searchTerms;
      }

      public void setSearchTerms(String searchTerms) {
         this.searchTerms = searchTerms;
      }

      public java.util.List<Node> getSortedNodes() {
         return sortedNodes;
      }

      public void setSortedNodes(java.util.List<Node> sortedNodes) {
         this.sortedNodes = sortedNodes;
      }

      public boolean isSearchFinished() {
         return searchFinished;
      }
      public void setSearchFinished(boolean searchFinished) {
         this.searchFinished = searchFinished;
      }


   public class Node{
      String name;
      String url;
      
      public Node (ResultSetRow row){
         try {
            name = row.getValue(QName.createQName("{http://www.alfresco.org/model/content/1.0}name")).toString();
         }catch (Exception e){}

         try {
            url = DownloadContentServlet.generateBrowserURL(row.getNodeRef(), name);
         }catch (Exception e){}
//add more code

      }
      
      public String getName() {
         return name;
      }
      public void setName(String name) {
         this.name = name;
      }
      public String getUrl() {
         return url;
      }
      public void setUrl(String url) {
         this.url = url;
      }
      
   }

}
Q:  I perform search in finishImpl method because it already wrapped in transaction. When I tried to search from method triggered by additional dialog button, Alfresco threw an exception. SO I used finish method instead, but this way the flow becomes quirky (finish is not a finish and additional button resets dialog by calling init). It’d be nice to know how to do it more elegantly 

search-clinical-documents.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="/WEB-INF/alfresco.tld" prefix="a" %>
<%@ taglib uri="/WEB-INF/repo.tld" prefix="r" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>                             

<h:panelGroup rendered="#{DialogManager.bean.searchFinished == false }">
   <h:outputText value="Enter serach terms then click \"Search\":  "/>
   <h:inputText size="100" value="#{DialogManager.bean.searchTerms}" />
</h:panelGroup>
      


<h:panelGroup rendered="#{DialogManager.bean.searchFinished == true }">
   <h:outputText value="Serach terms: "/>
   <h:outputText value="#{DialogManager.bean.searchTerms}" style="font-weight:bold"/>
   <f:verbatim><BR><BR></f:verbatim>
   <h:dataTable var="node" value="#{DialogManager.bean.sortedNodes}"    border="1"  cellpadding="10" style="vertical-align:top; "> 
      <h:column>    
         <h:outputLink   value="/alfresco/#{node.url}" target="_blank">
            <h:outputText value="#{node.name}" />   
         </h:outputLink>
      </h:column>    
   </h:dataTable>
</h:panelGroup>
That’s it.

Olga

Outcomes