KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > SavedSearchResultsMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.io.StringReader JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.service.ServiceRegistry;
24 import org.alfresco.service.cmr.repository.ContentReader;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.repository.TemplateNode;
27 import org.dom4j.Document;
28 import org.dom4j.Element;
29 import org.dom4j.io.SAXReader;
30
31 /**
32  * Provides functionality to load a saved search and execute it to return TemplateNode objects.
33  *
34  * @author Kevin Roast
35  */

36 public class SavedSearchResultsMap extends BaseSearchResultsMap
37 {
38     private static final String JavaDoc ELEMENT_QUERY = "query";
39     
40     /**
41      * Constructor
42      *
43      * @param parent The parent TemplateNode to execute searches from
44      * @param services The ServiceRegistry to use
45      */

46     public SavedSearchResultsMap(TemplateNode parent, ServiceRegistry services)
47     {
48         super(parent, services);
49     }
50
51     /**
52      * @see org.alfresco.repo.template.BaseTemplateMap#get(java.lang.Object)
53      */

54     public Object JavaDoc get(Object JavaDoc key)
55     {
56         String JavaDoc search = null;
57         
58         if (key != null && key.toString().length() != 0)
59         {
60             // read the Saved Search XML on the specified node - and get the Lucene search from it
61
try
62             {
63                 NodeRef ref = new NodeRef(key.toString());
64                 
65                 ContentReader content = services.getContentService().getReader(ref, ContentModel.PROP_CONTENT);
66                 if (content != null && content.exists())
67                 {
68                     // get the root element
69
SAXReader reader = new SAXReader();
70                     Document document = reader.read(new StringReader JavaDoc(content.getContentString()));
71                     Element rootElement = document.getRootElement();
72                     
73                     Element queryElement = rootElement.element(ELEMENT_QUERY);
74                     if (queryElement != null)
75                     {
76                         search = queryElement.getText();
77                     }
78                 }
79             }
80             catch (Throwable JavaDoc err)
81             {
82                 throw new AlfrescoRuntimeException("Failed to find or load saved Search: " + key, err);
83             }
84         }
85         
86         // execute the search
87
return query(search);
88     }
89 }
90
Popular Tags