1 package org.kuali.ole.docstore.utility;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URISyntaxException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8
9 import org.apache.commons.io.FileUtils;
10 import org.apache.commons.lang.StringUtils;
11 import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil;
12 import org.kuali.ole.docstore.indexer.solr.IndexerService;
13 import org.kuali.ole.docstore.util.DocStoreEnvUtil;
14 import org.kuali.ole.repository.DeleteManager;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18
19
20
21 public class DocumentStoreMaintenance {
22 DeleteManager deleteManager = new DeleteManager();
23 IndexerService indexerService = null;
24 DocStoreEnvUtil docStoreEnvUtil = new DocStoreEnvUtil();
25 DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
26 private static Logger logger = LoggerFactory.getLogger(DocumentStoreMaintenance.class);
27 private static final String PREPARE_FOR_NEW_BUILD = "prepareForNewBuild";
28 private static final String CLEANUP_DATA = "cleanupData";
29 private static final String RESET_DOCSTORE = "resetDocstore";
30
31
32
33
34
35
36
37 private void renameFolder(String filePath, String timeStamp) {
38 File srcFile = new File(filePath);
39 File destFile = new File(filePath + "-" + timeStamp);
40 boolean success = srcFile.renameTo(destFile);
41 if (success) {
42 logger.info("Renamed " + filePath + " as " + destFile.getAbsolutePath());
43 } else {
44 logger.info("Failed to rename " + filePath + " as " + destFile.getAbsolutePath());
45 }
46
47 }
48
49
50
51
52
53
54 private void deleteDocStoreData(String timeStamp) {
55 String docStoreDataFolder = docStoreEnvUtil.getJackrabbitFolderPath();
56 renameFolder(docStoreDataFolder, timeStamp);
57 }
58
59
60
61
62
63
64 private void deleteDiscoveryData(String timeStamp) {
65 String discoveryDateFilePath = discoveryEnvUtil.getSolrDataFolderPath();
66 renameFolder(discoveryDateFilePath, timeStamp);
67 }
68
69
70
71
72
73
74 private void deleteDocstoreProperties(String timeStamp) {
75 String docStorePropertiesFilePath = docStoreEnvUtil.getDocStorePropertiesFolderPath();
76 renameFolder(docStorePropertiesFilePath, timeStamp);
77 }
78
79
80
81
82
83
84 private void deleteDiscoveryConfig(String timeStamp) {
85 String discoceryConfigFilePath = discoveryEnvUtil.getSolrConfigFolderPath();
86 renameFolder(discoceryConfigFilePath, timeStamp);
87 }
88
89
90
91
92
93
94 private void deleteDocstore(String timeStamp) {
95 String docStoreRootFilePath = docStoreEnvUtil.getRootFolderPath();
96 renameFolder(docStoreRootFilePath, timeStamp);
97 }
98
99
100
101
102
103
104 private void deleteDiscovery(String timeStamp) {
105 String discoveryRootFilePath = discoveryEnvUtil.getRootFolderPath();
106 renameFolder(discoveryRootFilePath, timeStamp);
107 }
108
109
110
111
112
113
114
115
116
117 private void prepareForNewBuild(String timeStamp) {
118 deleteDocstoreProperties(timeStamp);
119 deleteDiscoveryConfig(timeStamp);
120
121 }
122
123
124
125
126
127
128
129
130 private void resetDocStore(String timeStamp) {
131 deleteDocStoreData(timeStamp);
132 deleteDiscoveryData(timeStamp);
133 deleteDiscovery(timeStamp);
134 deleteDocstore(timeStamp);
135
136 }
137
138
139
140
141
142
143
144
145
146
147 public void docStoreMaintenance(String docStoreHome, String discoveryHome) throws IOException {
148 docStoreEnvUtil.initPathValues(docStoreHome);
149 discoveryEnvUtil.initPathValues(discoveryHome);
150 String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
151 String maintenanceFilePath = null;
152 String maintenanceFileName = null;
153 File maintenanceFile = null;
154 File folder = new File(docStoreHome);
155 File[] listOfFiles = folder.listFiles();
156 String maintenanceAction = null;
157 if (listOfFiles != null) {
158 for (int i = 0; i < listOfFiles.length; i++) {
159 if (listOfFiles[i].isFile()) {
160 maintenanceFileName = listOfFiles[i].getName();
161 if (maintenanceFileName.equalsIgnoreCase("maintenance.command")) {
162 maintenanceFilePath = listOfFiles[i].getAbsolutePath();
163 maintenanceFile = new File(maintenanceFilePath);
164 maintenanceAction = FileUtils.readFileToString(maintenanceFile);
165 logger.info("maintenanceAction-->" + maintenanceAction);
166 break;
167 }
168 }
169 }
170 }
171
172 if (!StringUtils.isBlank(maintenanceAction)) {
173 if (maintenanceAction.equalsIgnoreCase(PREPARE_FOR_NEW_BUILD)) {
174 prepareForNewBuild(timeStamp);
175
176 } else if (maintenanceAction.equalsIgnoreCase(CLEANUP_DATA)) {
177
178 } else if (maintenanceAction.equalsIgnoreCase(RESET_DOCSTORE)) {
179 resetDocStore(timeStamp);
180 }
181 if (maintenanceFile != null) {
182 maintenanceFile.delete();
183 }
184
185 }
186 }
187 }