/*
 * NIST HL7 Web Service
 * Repository.java Oct 23, 2007
 *
 * This code was produced by the National Institute of Standards and
 * Technology (NIST). See the "nist.disclaimer" file given in the distribution
 * for information on the use and redistribution of this software.
 */

package gov.nist.hl7.ws.repository;

import org.apache.log4j.Logger;

import gov.nist.hl7.core.MalformedProfileException;
import gov.nist.hl7.core.profile.Profile;
import gov.nist.hl7.core.validation.message.TableProfileDocument;

import gov.nist.hl7.ws.JdbcRepositoryDao;
//import gov.nist.hl7.ws.Logger;

/**
 * @author Roch Bertucat (NIST)
 * See interface for more details
 */
public class Repository implements RepositoryInterface {

    private static Logger log=Logger.getLogger(Repository.class);
    private JdbcRepositoryDao rDao;
    private String exception = "";

    public String bind(String profileOID, String resourceOID) throws Exception {
        String ret = "";
        try {
            ret = rDao.bind(profileOID, resourceOID);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            //log.error(e);
            throw e;
        }
        return ret;
    }

    public boolean bindAnotherResource(String resourceOID, String handleOID) throws Exception {
        boolean ret = false;
        try {
            ret = rDao.bindAnotherResource(resourceOID, handleOID);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    /**
     * @param bindWithOID
     * @throws Exception
     */
    public boolean bindWithOID(String profileOID, String resourceOID, String handleOID) throws Exception {
        boolean ret = false;
        try {
            ret = rDao.bind(profileOID, resourceOID, handleOID);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    public String[] getBindings(String handleOID) throws Exception {
        String[] bindings = null;
        try {
            bindings = rDao.getBindings(handleOID);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return bindings;
    }

    /**
     * @param loadProfileWithOID
     * @throws Exception
     */
    public boolean loadProfileWithOID(String xmlProfile, String oid) throws Exception {
        boolean ret = false;
        try {
            Profile p = new Profile("noId", xmlProfile);
            //  is that OK? hopefully
            ret = rDao.loadProfile(p, oid);
            //Logger.info("Repository - User loads a profileWithOID: " + oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    /**
     * @param loadResourceWithOID
     * @throws Exception
     */
    public boolean loadResourceWithOID(String xmlResource, String oid) throws Exception {
        boolean ret = false;
        try {
            //  is that OK? hopefully
            TableProfileDocument td = TableProfileDocument.Factory.parse(xmlResource);
            ret = rDao.loadResource(td, oid);

            //Logger.info("Repository - User loads a resourceWithOID: " + oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    /**
     * @param queryRepositoryd
     * @throws Exception
     */
    public boolean queryRepository(String oid) throws Exception {
        boolean ret = false;
        try {
            String pName;
            pName = rDao.getFile(oid, "profile");
            if (!pName.equals("")) {
                ret = true;
            }

            //Logger.info("Repository - User queries the rep with oid: " + oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    /**
     * @param getProfile
     * @throws Exception
     */
    public String getProfile(String oid) throws Exception {
        String ret = "";
        try {
            Profile p = rDao.getProfile(oid);
            ret = p.getDocument().toString();
            //Logger.info("Repository - User gets a profile, whose oid is: " + oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            //log.error(e);
            //throw e;
        }
        return ret;
    }

    /**
     * @param getResource
     * @throws Exception
     */
    public String getResource(String oid) throws Exception {
        String ret = "";
        try {
            ret = rDao.getResource(oid).toString();
            //Logger.info("Repository - User gets a resource, whose oid is: "+ oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return ret;
    }

    /**
     * @param loadProfile
     * @throws MalformedProfileException
     */
    public String loadProfile(String xmlProfile) throws MalformedProfileException {
        String oid = "";
        try {
            Profile p = new Profile("noId", xmlProfile);
            //  is that OK? hopefully
            oid = rDao.loadProfile(p);
           // Logger.info("Repository - User loads a profile, whose generated oid is: " + oid);
        } catch (MalformedProfileException e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return oid;
    }

    /**
     * Auto generated method signature
     * @param loadResource
     * @throws Exception
     */
    public String loadResource(String xmlResource) throws Exception {
        String oid = "";
        try {
            oid = rDao.loadResource(TableProfileDocument.Factory.parse(xmlResource));
            //Logger.info("Repository - User loads a resource, whose generated oid is: "+ oid);
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return oid;
    }

    /**
     * Auto generated method signature
     * @throws Exception
     */
    public String[] queryRepositoryForProfiles() throws Exception {
        // testing
        String[] profiles = null;
        try {
            profiles = rDao.listFiles("profile");
           // Logger.info("Repository - User queries the rep for profiles");
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return profiles;
    }

    /**
     * Auto generated method signature
     * @throws Exception
     */
    public String[] queryRepositoryForResources() throws Exception {
        String[] resources = null;
        try {
            resources = rDao.listFiles("resource");
           // Logger.info("Repository - User queries the rep for resources");
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return resources;
    }

    public String[] queryRepositoryForHandles() throws Exception {
        String[] handles = null;
        try {
            handles = rDao.listHandles();
            //Logger.info("Repository - User queries the rep for profiles");
        } catch (Exception e) {
            exception += e.getMessage() + "\n";
            log.error(e);
            throw e;
        }
        return handles;
    }

    public String getLastExceptionMessage() {
        String lastException = exception;
        exception = "";
        //Logger.info("Repository - User gets the last exception message");
        return lastException;
    }

    public void setJdbcRepositoryDao(JdbcRepositoryDao inRDao) {
        this.rDao = inRDao;
    }
}
