KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.alfresco.i18n.I18NUtil;
24 import org.alfresco.repo.admin.patch.AbstractPatch;
25 import org.alfresco.repo.importer.ImporterBootstrap;
26 import org.alfresco.service.cmr.admin.PatchException;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.repository.StoreRef;
30 import org.alfresco.service.cmr.search.SearchService;
31 import org.alfresco.service.namespace.NamespaceService;
32
33 /**
34  * Generic patch that uses existing {@link org.alfresco.repo.importer.ImporterBootstrap importers}
35  * to import snippets into the system. These snippets would otherwise have been bootstrapped by
36  * a clean install.
37  * <p>
38  * By providing this class with a bootstrap view and an importer, it can check whether the path
39  * exists and perform the import if it doesn't.
40  *
41  * @author Derek Hulley
42  */

43 public class GenericBootstrapPatch extends AbstractPatch
44 {
45     private static final String JavaDoc MSG_EXISTS = "patch.genericBootstrap.result.exists";
46     private static final String JavaDoc MSG_CREATED = "patch.genericBootstrap.result.created";
47     private static final String JavaDoc ERR_MULTIPLE_FOUND = "patch.genericBootstrap.err.multiple_found";
48     
49     private NamespaceService namespaceService;
50     private NodeService nodeService;
51     private SearchService searchService;
52     private ImporterBootstrap importerBootstrap;
53     private String JavaDoc checkPath;
54     private Properties JavaDoc bootstrapView;
55
56     public void setNamespaceService(NamespaceService namespaceService)
57     {
58         this.namespaceService = namespaceService;
59     }
60     
61     public void setNodeService(NodeService nodeService)
62     {
63         this.nodeService = nodeService;
64     }
65
66     public void setSearchService(SearchService searchService)
67     {
68         this.searchService = searchService;
69     }
70
71     /**
72      * @param importerBootstrap the bootstrap bean that performs the user store bootstrap
73      */

74     public void setImporterBootstrap(ImporterBootstrap importerBootstrap)
75     {
76         this.importerBootstrap = importerBootstrap;
77     }
78
79     /**
80      * Set the XPath statement that must be executed to check whether the import data is
81      * already present or not.
82      *
83      * @param checkPath an XPath statement
84      */

85     public void setCheckPath(String JavaDoc checkPath)
86     {
87         this.checkPath = checkPath;
88     }
89
90     /**
91      * @see ImporterBootstrap#setBootstrapViews(List)
92      *
93      * @param bootstrapView the bootstrap location
94      */

95     public void setBootstrapView(Properties JavaDoc bootstrapView)
96     {
97         this.bootstrapView = bootstrapView;
98     }
99
100     @Override JavaDoc
101     protected void checkProperties()
102     {
103         checkPropertyNotNull(namespaceService, "blah");
104         checkPropertyNotNull(nodeService, "nodeService");
105         checkPropertyNotNull(searchService, "searchService");
106         checkPropertyNotNull(importerBootstrap, "importerBootstrap");
107         checkPropertyNotNull(checkPath, "checkPath");
108         checkPropertyNotNull(bootstrapView, "bootstrapView");
109         // fulfil contract of override
110
super.checkProperties();
111     }
112
113     @Override JavaDoc
114     protected String JavaDoc applyInternal() throws Exception JavaDoc
115     {
116         StoreRef storeRef = importerBootstrap.getStoreRef();
117         NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
118         List JavaDoc<NodeRef> results = searchService.selectNodes(
119                 rootNodeRef,
120                 checkPath,
121                 null,
122                 namespaceService,
123                 false);
124         if (results.size() > 1)
125         {
126             throw new PatchException(ERR_MULTIPLE_FOUND, checkPath);
127         }
128         else if (results.size() == 1)
129         {
130             // nothing to do - it exsists
131
return I18NUtil.getMessage(MSG_EXISTS, checkPath);
132             
133         }
134         String JavaDoc path = bootstrapView.getProperty("path");
135         List JavaDoc<Properties JavaDoc> bootstrapViews = Collections.singletonList(bootstrapView);
136         // modify the bootstrapper
137
importerBootstrap.setBootstrapViews(bootstrapViews);
138         importerBootstrap.setUseExistingStore(true); // allow import into existing store
139

140         importerBootstrap.bootstrap();
141         // done
142
return I18NUtil.getMessage(MSG_CREATED, path, rootNodeRef);
143     }
144 }
145
Popular Tags