001/*
002 * Copyright 2012 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.docstore.service;
017
018import org.apache.camel.CamelContext;
019import org.kuali.ole.docstore.process.BulkIngestDocStoreRequestBuilder;
020import org.kuali.ole.docstore.process.BulkLoadHandler;
021import org.kuali.ole.docstore.process.DocStoreCamelContext;
022import org.kuali.ole.docstore.process.ProcessParameters;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.util.HashSet;
027import java.util.Set;
028
029/**
030 * Class BulkIngest Process Handler Service.
031 *
032 * @author Rajesh Chowdary K
033 * @version 0.8
034 * @created Aug 8, 2012
035 */
036public class BulkIngestProcessHandlerService {
037
038    private Logger logger = LoggerFactory.getLogger(BulkIngestProcessHandlerService.class);
039    private static Set<String> processMonitor = new HashSet<String>();
040    private BulkLoadHandler loadHandler = BulkLoadHandler.getInstance();
041
042    /**
043     * Method to start Bulk Ingest Process for DocStore Request Format.
044     *
045     * @throws Exception
046     */
047    public void startBulkIngestForDocStoreRequestFormat(String folder) throws Exception {
048        loadHandler.loadBulk(folder, ProcessParameters.BULK_DEFAULT_USER, ProcessParameters.BULK_DEFUALT_ACTION);
049    }
050
051    /**
052     * Method to start Bulk Ingest Process For Standard XML Format.
053     *
054     * @param folder
055     * @param category
056     * @param type
057     * @param format
058     * @throws Exception
059     */
060    public void startBulkIngestForStandardXMLFormat(String folder, String category, String type, String format,
061                                                    String bulkIngestUploadDir) throws Exception {
062        String cmd = category + "/" + type + "/" + format + "@" + folder;
063        try {
064            logger.info("Bulk Ingest Process for " + cmd + " \t\t: STARTING...");
065            if (processMonitor.contains(cmd)) {
066                throw new Exception("Process Already Running : " + cmd);
067            }
068            CamelContext camelContext = DocStoreCamelContext.getInstance().getCamelContext();
069            BulkIngestDocStoreRequestBuilder builder = new BulkIngestDocStoreRequestBuilder(folder,
070                    ProcessParameters.BULK_DEFAULT_USER,
071                    ProcessParameters.BULK_DEFUALT_ACTION,
072                    camelContext, category,
073                    type, format,
074                    bulkIngestUploadDir);
075            camelContext.addRoutes(builder);
076            camelContext.start();
077            processMonitor.add(cmd);
078            logger.info("Bulk Ingest Process for " + cmd + " \t\t: STARTED");
079        } catch (Exception e) {
080            logger.error("Unable to Start Bulk Ingest Process for " + cmd + ".\n Cuase: " + e.getMessage(), e);
081            throw new Exception("Unable to Start Bulk Ingest Process for " + cmd + ".\n Cuase: " + e.getMessage(), e);
082        }
083
084    }
085
086    public BulkLoadHandler getLoadHandler() {
087        return loadHandler;
088    }
089
090    public void setLoadHandler(BulkLoadHandler loadHandler) {
091        this.loadHandler = loadHandler;
092    }
093}