1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.batch.service.impl;
17
18 import java.io.File;
19 import java.util.List;
20
21 import org.apache.commons.io.filefilter.AndFileFilter;
22 import org.apache.commons.io.filefilter.IOFileFilter;
23 import org.apache.commons.io.filefilter.OrFileFilter;
24 import org.kuali.ole.sys.FileUtil;
25 import org.kuali.ole.sys.batch.FilePurgeCustomAge;
26 import org.kuali.ole.sys.batch.FilePurgeDirectoryWalker;
27 import org.kuali.ole.sys.batch.FilePurgeStep;
28 import org.kuali.ole.sys.batch.MaxAgePurgeFileFilter;
29 import org.kuali.ole.sys.batch.NotAmongDirectoriesFileFilter;
30 import org.kuali.ole.sys.batch.service.FilePurgeService;
31 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
32
33
34
35
36 public class FilePurgeServiceImpl implements FilePurgeService {
37 protected org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FilePurgeServiceImpl.class);
38 private ParameterService parameterService;
39
40 protected static final String DAYS_BEFORE_PURGE_PARAMETER_SUFFIX = "_NUMBER_OF_DAYS_OLD";
41 protected static final String DAYS_BEFORE_PURGE_PARAMETER_PREFIX = "DEFAULT";
42
43
44
45
46
47 public void purgeFiles(String directory, List<FilePurgeCustomAge> customAges) {
48
49 FileUtil.createDirectory(directory);
50
51 purgeCustomAgeFiles(directory, customAges);
52 purgeDefaultFiles(directory, customAges);
53 }
54
55
56
57
58
59
60 protected void purgeCustomAgeFiles(String directory, List<FilePurgeCustomAge> customAges) {
61
62 if (customAges != null && customAges.size() > 0) {
63 FilePurgeDirectoryWalker directoryWalker = getCustomAgesDirectoryWalker(customAges);
64 List<File> filesToPurge = directoryWalker.getFilesToPurge(directory);
65 for (File fileToPurge : filesToPurge) {
66 LOG.info("Purging file "+fileToPurge.getPath());
67 fileToPurge.delete();
68 }
69 }
70 }
71
72
73
74
75
76
77 protected void purgeDefaultFiles(String directory, List<FilePurgeCustomAge> customAges) {
78
79 FilePurgeDirectoryWalker directoryWalker = getDefaultDirectoryWalker(customAges);
80 List<File> filesToPurge = directoryWalker.getFilesToPurge(directory);
81 for (File fileToPurge : filesToPurge) {
82 LOG.info("Purging file "+fileToPurge.getPath());
83 fileToPurge.delete();
84 }
85 }
86
87
88
89
90
91
92 protected FilePurgeDirectoryWalker getCustomAgesDirectoryWalker(List<FilePurgeCustomAge> customAges) {
93 OrFileFilter fileFilter = new OrFileFilter();
94 for (FilePurgeCustomAge customAge : customAges) {
95 fileFilter.addFileFilter(customAge.getFileFilter());
96 }
97 return new FilePurgeDirectoryWalker(fileFilter);
98 }
99
100
101
102
103
104
105 protected FilePurgeDirectoryWalker getDefaultDirectoryWalker(List<FilePurgeCustomAge> customAges) {
106 IOFileFilter ageFileFilter = buildDefaultAgeFileFilter();
107 if (customAges != null && customAges.size() > 0) {
108 AndFileFilter andFileFilter = new AndFileFilter();
109 andFileFilter.addFileFilter(ageFileFilter);
110 andFileFilter.addFileFilter(buildAnyDirectoryButCustomAgeDirectoryFileFilter(customAges));
111 return new FilePurgeDirectoryWalker(andFileFilter);
112 } else {
113 return new FilePurgeDirectoryWalker(ageFileFilter);
114 }
115 }
116
117
118
119
120
121
122 protected IOFileFilter buildAnyDirectoryButCustomAgeDirectoryFileFilter(List<FilePurgeCustomAge> customAges) {
123 NotAmongDirectoriesFileFilter skipDirectoriesFileFilter = new NotAmongDirectoriesFileFilter(customAges);
124 return skipDirectoriesFileFilter;
125 }
126
127
128
129
130 public int getDaysBeforePurgeForCustomAge(FilePurgeCustomAge customAge) {
131 final String parameterName = customAge.getParameterPrefix()+getDaysBeforePurgeSuffix();
132 return retrieveDaysBeforePurgeParameterValue(parameterName);
133 }
134
135
136
137
138 protected String getDaysBeforePurgeSuffix() {
139 return FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_SUFFIX;
140 }
141
142
143
144
145 public int getStandardDaysBeforePurge() {
146 final String parameterName = getStandardDaysBeforePurgeParameterName();
147 return retrieveDaysBeforePurgeParameterValue(parameterName);
148 }
149
150
151
152
153 protected String getStandardDaysBeforePurgeParameterName() {
154 return FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_PREFIX+FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_SUFFIX;
155 }
156
157
158
159
160
161
162 protected int retrieveDaysBeforePurgeParameterValue(String parameterName) {
163 final String parameterValue = getParameterService().getParameterValueAsString(FilePurgeStep.class, parameterName);
164 Integer parameterValueAsInteger = null;
165 parameterValueAsInteger = new Integer(parameterValue);
166 return (parameterValueAsInteger == null ? Integer.MAX_VALUE : parameterValueAsInteger.intValue());
167 }
168
169
170
171
172
173 protected IOFileFilter buildDefaultAgeFileFilter() {
174 return new MaxAgePurgeFileFilter();
175 }
176
177
178
179
180
181 public ParameterService getParameterService() {
182 return parameterService;
183 }
184
185
186
187
188
189 public void setParameterService(ParameterService parameterService) {
190 this.parameterService = parameterService;
191 }
192 }