1 17 package org.alfresco.repo.admin.patch.impl; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Properties ; 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 52 public class SavedSearchFolderPatch extends AbstractPatch 53 { 54 private static final String MSG_EXISTS = "patch.savedSearchesFolder.result.exists"; 55 private static final String MSG_CREATED = "patch.savedSearchesFolder.result.created"; 56 57 public static final String PROPERTY_COMPANY_HOME_CHILDNAME = "spaces.company_home.childname"; 58 public static final String PROPERTY_DICTIONARY_CHILDNAME = "spaces.dictionary.childname"; 59 public static final String PROPERTY_SAVED_SEARCHES_FOLDER_CHILDNAME = "spaces.savedsearches.childname"; 60 private static final String PROPERTY_SAVED_SEARCHES_FOLDER_NAME = "spaces.savedsearches.name"; 61 private static final String PROPERTY_SAVED_SEARCHES_FOLDER_DESCRIPTION = "spaces.savedsearches.description"; 62 private static final String 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 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 102 protected void checkCommonProperties() throws Exception 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 126 protected void setUp() throws Exception 127 { 128 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 String 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 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 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 StringBuilder sb = new StringBuilder (512); 156 sb.append("/").append(companyHomeChildName) 157 .append("/").append(dictionaryChildName); 158 String xpath = sb.toString(); 159 List <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 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 this.savedSearchesFolderNodeRef = null; 190 } 191 else 192 { 193 this.savedSearchesFolderNodeRef = nodeRefs.get(0); 195 } 196 } 197 198 @Override 199 protected String applyInternal() throws Exception 200 { 201 checkCommonProperties(); 203 if (messageSource == null) 204 { 205 throw new PatchException("'messageSource' property has not been set"); 206 } 207 208 setUp(); 210 211 String msg = null; 212 if (savedSearchesFolderNodeRef == null) 213 { 214 createFolder(); 216 msg = I18NUtil.getMessage(MSG_CREATED, savedSearchesFolderNodeRef); 217 } 218 else 219 { 220 msg = I18NUtil.getMessage(MSG_EXISTS, savedSearchesFolderNodeRef); 222 } 223 return msg; 225 } 226 227 private void createFolder() 228 { 229 String 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 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 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 <QName, Serializable > properties = new HashMap <QName, Serializable >(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 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 nodeService.addAspect(savedSearchesFolderNodeRef, ContentModel.ASPECT_UIFACETS, null); 269 270 } 272 } 273 | Popular Tags |