001package org.kuali.ole.docstore.utility;
002
003import java.io.File;
004import java.io.IOException;
005import java.net.URISyntaxException;
006import java.text.SimpleDateFormat;
007import java.util.Date;
008
009import org.apache.commons.io.FileUtils;
010import org.apache.commons.lang.StringUtils;
011import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil;
012import org.kuali.ole.docstore.indexer.solr.IndexerService;
013import org.kuali.ole.docstore.util.DocStoreEnvUtil;
014import org.kuali.ole.repository.DeleteManager;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 *
020 */
021public class DocumentStoreMaintenance {
022    DeleteManager deleteManager = new DeleteManager();
023    IndexerService indexerService = null;
024    DocStoreEnvUtil docStoreEnvUtil = new DocStoreEnvUtil();
025    DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
026    private static Logger logger = LoggerFactory.getLogger(DocumentStoreMaintenance.class);
027    private static final String PREPARE_FOR_NEW_BUILD = "prepareForNewBuild";
028    private static final String CLEANUP_DATA = "cleanupData";
029    private static final String RESET_DOCSTORE = "resetDocstore";
030
031    /**
032     * Used to rename the folder / file with the current timestamp.
033     *
034     * @param filePath
035     * @param timeStamp
036     */
037    private void renameFolder(String filePath, String timeStamp) {
038        File srcFile = new File(filePath);
039        File destFile = new File(filePath + "-" + timeStamp);
040        boolean success = srcFile.renameTo(destFile);
041        if (success) {
042            logger.info("Renamed " + filePath + " as " + destFile.getAbsolutePath());
043        } else {
044            logger.info("Failed to rename " + filePath + " as " + destFile.getAbsolutePath());
045        }
046
047    }
048
049    /**
050     * Used to rename the %ole.docstore.home/repository folder with the current timestamp.
051     *
052     * @param timeStamp
053     */
054//    private void deleteDocStoreData(String timeStamp) {
055//        String docStoreDataFolder = docStoreEnvUtil.getJackrabbitFolderPath();
056//        renameFolder(docStoreDataFolder, timeStamp);
057//    }
058
059    /**
060     * Used to rename the %ole.discovery.home%/solr-data folder with the current timestamp.
061     *
062     * @param timeStamp
063     */
064    private void deleteDiscoveryData(String timeStamp) {
065        String discoveryDateFilePath = discoveryEnvUtil.getSolrDataFolderPath();
066        renameFolder(discoveryDateFilePath, timeStamp);
067    }
068
069    /**
070     * Used to rename the %ole.docstore.home/Properties folder with the current timestamp.
071     *
072     * @param timeStamp
073     */
074    private void deleteDocstoreProperties(String timeStamp) {
075        String docStorePropertiesFilePath = docStoreEnvUtil.getDocStorePropertiesFolderPath();
076        renameFolder(docStorePropertiesFilePath, timeStamp);
077    }
078
079    /**
080     * Used to rename the %ole.discovery.home%/solr-config folder with the current timestamp.
081     *
082     * @param timeStamp
083     */
084    private void deleteDiscoveryConfig(String timeStamp) {
085        String discoceryConfigFilePath = discoveryEnvUtil.getSolrConfigFolderPath();
086        renameFolder(discoceryConfigFilePath, timeStamp);
087    }
088
089    /**
090     * Used to rename the %ole.docstore.home folder with the current timestamp.
091     *
092     * @param timeStamp
093     */
094    private void deleteDocstore(String timeStamp) {
095        String docStoreRootFilePath = docStoreEnvUtil.getRootFolderPath();
096        renameFolder(docStoreRootFilePath, timeStamp);
097    }
098
099    /**
100     * Used to rename the %ole.discovery.home folder with the current timestamp.
101     *
102     * @param timeStamp
103     */
104    private void deleteDiscovery(String timeStamp) {
105        String discoveryRootFilePath = discoveryEnvUtil.getRootFolderPath();
106        renameFolder(discoveryRootFilePath, timeStamp);
107    }
108
109    /**
110     * This method will execute if the argument is prepareForNewBuild in the maintenance.command file. In this process
111     * %ole.docstore.home%/properties and %ole.discovery.home/solr-config will renamed with the current timestamp at the time of
112     * initialization.
113     *
114     * @param timeStamp
115     */
116
117    private void prepareForNewBuild(String timeStamp) {
118        deleteDocstoreProperties(timeStamp);
119        deleteDiscoveryConfig(timeStamp);
120
121    }
122
123    /**
124     * This method will execute if the argument is resetDocStore in the maintenance.command file. In this process
125     * %ole.docstore.home%,%ole.docstore.home%/jackrabbit,%ole.discovery.home and %ole.discovery.home/solr-data will renamed with the
126     * current timestamp at the time of initialization.
127     *
128     * @param timeStamp
129     */
130    private void resetDocStore(String timeStamp) {
131//        deleteDocStoreData(timeStamp);
132        deleteDiscoveryData(timeStamp);
133        deleteDiscovery(timeStamp);
134        deleteDocstore(timeStamp);
135
136    }
137
138    /**
139     * This method is called at the time of docstore initialization and operations are performed based on the command mentioned in
140     * maintenance.command file
141     *
142     * @param docStoreHome
143     * @param discoveryHome
144     * @throws IOException
145     * @throws URISyntaxException
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                // cleanupData();
178            } else if (maintenanceAction.equalsIgnoreCase(RESET_DOCSTORE)) {
179                resetDocStore(timeStamp);
180            }
181            if (maintenanceFile != null) {
182                maintenanceFile.delete();
183            }
184
185        }
186    }
187}