1 17 package org.alfresco.repo.content.cleanup; 18 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 import org.alfresco.error.AlfrescoRuntimeException; 26 import org.alfresco.repo.content.ContentStore; 27 import org.alfresco.repo.node.db.NodeDaoService; 28 import org.alfresco.repo.transaction.TransactionUtil; 29 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork; 30 import org.alfresco.service.cmr.dictionary.DictionaryService; 31 import org.alfresco.service.cmr.repository.ContentData; 32 import org.alfresco.service.cmr.repository.ContentReader; 33 import org.alfresco.service.transaction.TransactionService; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 45 public class ContentStoreCleaner 46 { 47 private static Log logger = LogFactory.getLog(ContentStoreCleaner.class); 48 49 private DictionaryService dictionaryService; 50 private NodeDaoService nodeDaoService; 51 private TransactionService transactionService; 52 private List <ContentStore> stores; 53 private List <ContentStoreCleanerListener> listeners; 54 private int protectDays; 55 56 public ContentStoreCleaner() 57 { 58 this.stores = new ArrayList <ContentStore>(0); 59 this.listeners = new ArrayList <ContentStoreCleanerListener>(0); 60 this.protectDays = 7; 61 } 62 63 66 public void setDictionaryService(DictionaryService dictionaryService) 67 { 68 this.dictionaryService = dictionaryService; 69 } 70 71 74 public void setNodeDaoService(NodeDaoService nodeDaoService) 75 { 76 this.nodeDaoService = nodeDaoService; 77 } 78 79 82 public void setTransactionService(TransactionService transactionService) 83 { 84 this.transactionService = transactionService; 85 } 86 87 90 public void setStores(List <ContentStore> stores) 91 { 92 this.stores = stores; 93 } 94 95 98 public void setListeners(List <ContentStoreCleanerListener> listeners) 99 { 100 this.listeners = listeners; 101 } 102 103 109 public void setProtectDays(int protectDays) 110 { 111 this.protectDays = protectDays; 112 } 113 114 117 private void checkProperties() 118 { 119 if (dictionaryService == null) 120 { 121 throw new AlfrescoRuntimeException("Property 'dictionaryService' not set"); 122 } 123 if (nodeDaoService == null) 124 { 125 throw new AlfrescoRuntimeException("Property 'nodeDaoService' not set"); 126 } 127 if (transactionService == null) 128 { 129 throw new AlfrescoRuntimeException("Property 'transactionService' not set"); 130 } 131 if (stores == null || stores.size() == 0) 132 { 133 throw new AlfrescoRuntimeException("Property 'stores' not set"); 134 } 135 if (listeners == null) 136 { 137 throw new AlfrescoRuntimeException("Property 'listeners' not set"); 138 } 139 140 if (protectDays < 0) 142 { 143 throw new AlfrescoRuntimeException("Property 'protectDays' must be 0 or greater (0 is not recommended)"); 144 } 145 else if (protectDays == 0) 146 { 147 logger.warn( 148 "Property 'protectDays' is set to 0. " + 149 "It is possible that in-transaction content will be deleted."); 150 } 151 } 152 153 private Set <String > getValidUrls() 154 { 155 TransactionWork<List <String >> getUrlsWork = new TransactionWork<List <String >>() 157 { 158 public List <String > doWork() throws Exception 159 { 160 return nodeDaoService.getContentDataStrings(); 161 }; 162 }; 163 List <String > contentDataStrings = TransactionUtil.executeInUserTransaction( 165 transactionService, 166 getUrlsWork, 167 true); 168 169 Set <String > validUrls = new HashSet <String >(contentDataStrings.size()); 171 for (String contentDataString : contentDataStrings) 173 { 174 ContentData contentData = ContentData.createContentProperty(contentDataString); 175 if (contentData.getContentUrl() != null) 176 { 177 validUrls.add(contentData.getContentUrl()); 179 } 180 } 181 if (logger.isDebugEnabled()) 183 { 184 logger.debug("Found " + validUrls.size() + " valid URLs in metadata"); 185 } 186 return validUrls; 187 } 188 189 public void execute() 190 { 191 checkProperties(); 192 Set <String > validUrls = getValidUrls(); 193 for (ContentStore store : stores) 195 { 196 clean(validUrls, store); 197 } 198 } 199 200 private void clean(Set <String > validUrls, ContentStore store) 201 { 202 Date checkAllBeforeDate = new Date (System.currentTimeMillis() - (long) protectDays * 3600L * 1000L * 24L); 203 Set <String > storeUrls = store.getUrls(null, checkAllBeforeDate); 205 storeUrls.removeAll(validUrls); 207 for (String url : storeUrls) 209 { 210 ContentReader sourceReader = store.getReader(url); 211 for (ContentStoreCleanerListener listener : listeners) 213 { 214 ContentReader listenerReader = sourceReader.getReader(); 216 listener.beforeDelete(listenerReader); 218 } 219 store.delete(url); 221 222 if (logger.isDebugEnabled()) 223 { 224 logger.debug("Removed URL from store: \n" + 225 " Store: " + store + "\n" + 226 " URL: " + url); 227 } 228 } 229 } 230 } 231 | Popular Tags |