ArchiveThread.java [tools/GetWEPSArchives/src/getwepsarchives] Revision: 25d34dc3fd197e11bc5c953aacbbd2b0e3b982af  Date: Wed Nov 20 15:40:48 MST 2019
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package getwepsarchives;

import archives.MongoArchive;
import csip.ServiceException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.ModelArchive;
import models.WEPSModelArchive;
import models.WEPSModelArchive.WEPSMetaData;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class ArchiveThread extends Thread {

    protected String suid;
    protected CountDownLatch latch;
    protected String dbLocation;
    protected MongoArchive mongoArchive;
    protected ArrayList<ModelArchive> tArchive;
    protected ModelArchive model;
    protected String errorMessage;
    protected WEPSMetaData wepsMetaData;
    ConcurrentHashMap<String, WEPSMetaData> metaDataResults;

    public ArchiveThread(String _suid, String _dbLocation, ConcurrentHashMap<String, WEPSMetaData> _metaDataResults, CountDownLatch _latch) throws Exception {
        suid = _suid;
        latch = _latch;
        dbLocation = _dbLocation;
        metaDataResults = _metaDataResults;
        if (dbLocation.contains("mongodb://")) {
            mongoArchive = new MongoArchive(dbLocation);
        }
    }

    @Override
    public void run() {

        try {
            tArchive = mongoArchive.getArchivesByFilter("_id", suid, 1);
        } catch (Exception ex) {
            errorMessage += "#\nCannot get that suid, " + suid + ", from the archive:  " + ex.getMessage() + "\n\n" + ex.toString();
        }

        if (tArchive.isEmpty()) {
            errorMessage += "#\nCould not find a WEPS model in the archive for that suid and mongodb location.";
        } else {

            model = tArchive.get(0);

            if (((WEPSModelArchive) model).badModelRun()) {
                errorMessage += "#\nFound a WEPS model in the archive, however the model was invalid: " + ((WEPSModelArchive) model).badModelMessage();
            } else {
                ((WEPSModelArchive) model).calcWEPSMetaData();

                try {
                    mongoArchive.shutdown();
                } catch (Exception ex) {
                    errorMessage += "#\nCould not shutdown mongo archive at: " + dbLocation + "\n\n" + ex.toString();
                }

                wepsMetaData = ((WEPSModelArchive) model).getWEPSMetaData();
                if ((null != wepsMetaData) && (null != metaDataResults)) {
                    metaDataResults.put(suid, wepsMetaData);
                }
            }
        }
        
        if ( (null != errorMessage) && !errorMessage.isEmpty()) {
            System.err.println(suid + ": " + errorMessage);
        }

        latch.countDown();

    }

}