KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > impl > SavedSearchFolderPatch


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.admin.patch.impl;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.alfresco.i18n.I18NUtil;
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.repo.admin.patch.AbstractPatch;
28 import org.alfresco.repo.importer.ImporterBootstrap;
29 import org.alfresco.service.cmr.admin.PatchException;
30 import org.alfresco.service.cmr.repository.ChildAssociationRef;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.service.cmr.repository.StoreRef;
34 import org.alfresco.service.cmr.search.SearchService;
35 import org.alfresco.service.namespace.NamespaceService;
36 import org.alfresco.service.namespace.QName;
37 import org.springframework.context.MessageSource;
38
39 /**
40  * Ensures that the <b>savedsearches</b> folder is present.
41  * <p>
42  * This uses the bootstrap importer to get the paths to look for. If not present,
43  * the required structures are created.
44  * <p>
45  * This class should be replaced with a more generic <code>ImporterPatch</code>
46  * that can do conditional importing into given locations.
47  * <p>
48  * JIRA: {@link http://www.alfresco.org/jira/browse/AR-342 AR-342}
49  *
50  * @author Derek Hulley
51  */

52 public class SavedSearchFolderPatch extends AbstractPatch
53 {
54     private static final String JavaDoc MSG_EXISTS = "patch.savedSearchesFolder.result.exists";
55     private static final String JavaDoc MSG_CREATED = "patch.savedSearchesFolder.result.created";
56     
57     public static final String JavaDoc PROPERTY_COMPANY_HOME_CHILDNAME = "spaces.company_home.childname";
58     public static final String JavaDoc PROPERTY_DICTIONARY_CHILDNAME = "spaces.dictionary.childname";
59     public static final String JavaDoc PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME = "spaces.savedsearches.childname";
60     private static final String JavaDoc PROPERTY_SAVED_SEARCHES_FOLDER_NAME = "spaces.savedsearches.name";
61     private static final String JavaDoc PROPERTY_SAVED_SEARCHES_FOLDER_DESCRIPTION = "spaces.savedsearches.description";
62     private static final String JavaDoc PROPERTY_ICON = "space-icon-default";
63     
64     private ImporterBootstrap importerBootstrap;
65     private NamespaceService namespaceService;
66     private SearchService searchService;
67     private NodeService nodeService;
68     private MessageSource messageSource;
69     
70     protected NodeRef dictionaryNodeRef;
71     protected Properties JavaDoc configuration;
72     protected NodeRef savedSearchesFolderNodeRef;
73     
74     public void setImporterBootstrap(ImporterBootstrap importerBootstrap)
75     {
76         this.importerBootstrap = importerBootstrap;
77     }
78
79     public void setNamespaceService(NamespaceService namespaceService)
80     {
81         this.namespaceService = namespaceService;
82     }
83     
84     public void setSearchService(SearchService searchService)
85     {
86         this.searchService = searchService;
87     }
88
89     public void setNodeService(NodeService nodeService)
90     {
91         this.nodeService = nodeService;
92     }
93
94     public void setMessageSource(MessageSource messageSource)
95     {
96         this.messageSource = messageSource;
97     }
98
99     /**
100      * Ensure that required common properties have been set
101      */

102     protected void checkCommonProperties() throws Exception JavaDoc
103     {
104         if (importerBootstrap == null)
105         {
106             throw new PatchException("'importerBootstrap' property has not been set");
107         }
108         else if (namespaceService == null)
109         {
110             throw new PatchException("'namespaceService' property has not been set");
111         }
112         else if (searchService == null)
113         {
114             throw new PatchException("'searchService' property has not been set");
115         }
116         else if (nodeService == null)
117         {
118             throw new PatchException("'nodeService' property has not been set");
119         }
120     }
121     
122     /**
123      * Extracts pertinent references and properties that are common to execution
124      * of this and derived patches.
125      */

126     protected void setUp() throws Exception JavaDoc
127     {
128         // get the node store that we must work against
129
StoreRef storeRef = importerBootstrap.getStoreRef();
130         if (storeRef == null)
131         {
132             throw new PatchException("Bootstrap store has not been set");
133         }
134         NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
135
136         this.configuration = importerBootstrap.getConfiguration();
137         // get the association names that form the path
138
String JavaDoc companyHomeChildName = configuration.getProperty(PROPERTY_COMPANY_HOME_CHILDNAME);
139         if (companyHomeChildName == null || companyHomeChildName.length() == 0)
140         {
141             throw new PatchException("Bootstrap property '" + PROPERTY_COMPANY_HOME_CHILDNAME + "' is not present");
142         }
143         String JavaDoc dictionaryChildName = configuration.getProperty(PROPERTY_DICTIONARY_CHILDNAME);
144         if (dictionaryChildName == null || dictionaryChildName.length() == 0)
145         {
146             throw new PatchException("Bootstrap property '" + PROPERTY_DICTIONARY_CHILDNAME + "' is not present");
147         }
148         String JavaDoc savedSearchesChildName = configuration.getProperty(PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME);
149         if (savedSearchesChildName == null || savedSearchesChildName.length() == 0)
150         {
151             throw new PatchException("Bootstrap property '" + PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME + "' is not present");
152         }
153         
154         // build the search string to get the dictionary node
155
StringBuilder JavaDoc sb = new StringBuilder JavaDoc(512);
156         sb.append("/").append(companyHomeChildName)
157           .append("/").append(dictionaryChildName);
158         String JavaDoc xpath = sb.toString();
159         // get the dictionary node
160
List JavaDoc<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, xpath, null, namespaceService, false);
161         if (nodeRefs.size() == 0)
162         {
163             throw new PatchException("XPath didn't return any results: \n" +
164                     " root: " + storeRootNodeRef + "\n" +
165                     " xpath: " + xpath);
166         }
167         else if (nodeRefs.size() > 1)
168         {
169             throw new PatchException("XPath returned too many results: \n" +
170                     " root: " + storeRootNodeRef + "\n" +
171                     " xpath: " + xpath + "\n" +
172                     " results: " + nodeRefs);
173         }
174         this.dictionaryNodeRef = nodeRefs.get(0);
175         
176         // Now we have the optional part. Check for the existence of the saved searches folder
177
xpath = savedSearchesChildName;
178         nodeRefs = searchService.selectNodes(dictionaryNodeRef, xpath, null, namespaceService, false);
179         if (nodeRefs.size() > 1)
180         {
181             throw new PatchException("XPath returned too many results: \n" +
182                     " dictionary node: " + dictionaryNodeRef + "\n" +
183                     " xpath: " + xpath + "\n" +
184                     " results: " + nodeRefs);
185         }
186         else if (nodeRefs.size() == 0)
187         {
188             // the node does not exist
189
this.savedSearchesFolderNodeRef = null;
190         }
191         else
192         {
193             // we have the saved searches folder noderef
194
this.savedSearchesFolderNodeRef = nodeRefs.get(0);
195         }
196     }
197     
198     @Override JavaDoc
199     protected String JavaDoc applyInternal() throws Exception JavaDoc
200     {
201         // properties must be set
202
checkCommonProperties();
203         if (messageSource == null)
204         {
205             throw new PatchException("'messageSource' property has not been set");
206         }
207         
208         // get useful values
209
setUp();
210         
211         String JavaDoc msg = null;
212         if (savedSearchesFolderNodeRef == null)
213         {
214             // create it
215
createFolder();
216             msg = I18NUtil.getMessage(MSG_CREATED, savedSearchesFolderNodeRef);
217         }
218         else
219         {
220             // it already exists
221
msg = I18NUtil.getMessage(MSG_EXISTS, savedSearchesFolderNodeRef);
222         }
223         // done
224
return msg;
225     }
226     
227     private void createFolder()
228     {
229         // get required properties
230
String JavaDoc savedSearchesChildName = configuration.getProperty(PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME);
231         if (savedSearchesChildName == null)
232         {
233             throw new PatchException("Bootstrap property '" + PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME + "' is not present");
234         }
235         
236         String JavaDoc savedSearchesName = messageSource.getMessage(
237                 PROPERTY_SAVED_SEARCHES_FOLDER_NAME,
238                 null,
239                 I18NUtil.getLocale());
240         if (savedSearchesName == null || savedSearchesName.length() == 0)
241         {
242             throw new PatchException("Bootstrap property '" + PROPERTY_SAVED_SEARCHES_FOLDER_NAME + "' is not present");
243         }
244
245         String JavaDoc savedSearchesDescription = messageSource.getMessage(
246                 PROPERTY_SAVED_SEARCHES_FOLDER_DESCRIPTION,
247                 null,
248                 I18NUtil.getLocale());
249         if (savedSearchesDescription == null || savedSearchesDescription.length() == 0)
250         {
251             throw new PatchException("Bootstrap property '" + PROPERTY_SAVED_SEARCHES_FOLDER_DESCRIPTION + "' is not present");
252         }
253
254         Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(7);
255         properties.put(ContentModel.PROP_NAME, savedSearchesName);
256         properties.put(ContentModel.PROP_TITLE, savedSearchesName);
257         properties.put(ContentModel.PROP_DESCRIPTION, savedSearchesDescription);
258         properties.put(ContentModel.PROP_ICON, PROPERTY_ICON);
259         // create the node
260
ChildAssociationRef childAssocRef = nodeService.createNode(
261                 dictionaryNodeRef,
262                 ContentModel.ASSOC_CONTAINS,
263                 QName.resolveToQName(namespaceService, savedSearchesChildName),
264                 ContentModel.TYPE_FOLDER,
265                 properties);
266         savedSearchesFolderNodeRef = childAssocRef.getChildRef();
267         // add the required aspects
268
nodeService.addAspect(savedSearchesFolderNodeRef, ContentModel.ASPECT_UIFACETS, null);
269         
270         // done
271
}
272 }
273
Popular Tags