001/**
002 * Copyright (C) 2010-2015 The Roslin Institute <contact andy.law@roslin.ed.ac.uk>
003 *
004 * This file is part of JEnsembl: a Java API to Ensembl data sources developed by the
005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of
006 * Veterinary Studies, University of Edinburgh.
007 *
008 * Project hosted at: http://jensembl.sourceforge.net
009 *
010 * This is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License (version 3) as published by
012 * the Free Software Foundation.
013 *
014 * This software is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * in this software distribution. If not, see: http://opensource.org/licenses/gpl-3.0.html
021 */
022package uk.ac.roslin.ensembl.datasourceaware;
023
024import java.io.Serializable;
025import java.util.Comparator;
026import uk.ac.roslin.ensembl.model.database.Registry;
027import uk.ac.roslin.ensembl.dao.factory.DAOFactory;
028import uk.ac.roslin.ensembl.model.IdentifiableObject;
029
030
031/**
032 * @author paterson
033 */
034public abstract class DAObject implements IdentifiableObject {
035
036
037    protected DAOFactory daoFactory = null;
038    protected String schemaVersion = null;
039    protected String dbVersion = null;
040    protected Registry registry = null;
041    protected Integer id = null;
042    private boolean lazyloadAllowed = true;
043
044    public DAObject() {
045
046    }
047
048    public DAObject(DAOFactory factory) {
049        this.setDaoFactory(factory);
050    }
051
052    public DAOFactory getDaoFactory() {
053        return daoFactory;
054    }
055
056    public void setDaoFactory(DAOFactory daoFactory) {
057        if (daoFactory != null) {
058            this.daoFactory = daoFactory;
059            this.setSchemaVersion(daoFactory.getEnsemblSchemaVersion());
060            this.setDBVersion(daoFactory.getDBVersion());
061            this.setRegistry(daoFactory.getRegistry());
062        }
063    }
064
065    @Override
066    public String getSchemaVersion() {
067            return schemaVersion;
068    }
069
070    private void setSchemaVersion(String eSchemaVersion) {
071        this.schemaVersion = eSchemaVersion;
072    }
073
074    @Override
075    public String getDBVersion() {
076            return dbVersion;
077    }
078
079    private void setDBVersion(String dbversion) {
080        this.dbVersion = dbversion;
081    }
082
083    public Registry getRegistry() {
084            return registry;
085    }
086
087    public void setRegistry(Registry datasource) {
088        this.registry = datasource;
089    }
090
091    @Override
092    public Integer getId() {
093        return id;
094    }
095
096    @Override
097    public void setId(Integer id) {
098        this.id = id;
099    }
100
101
102    @Override
103    public String getHashID() {
104        return
105                ((this.getDaoFactory()!=null) ? this.getDaoFactory().getDatabaseName() : "NODATABASE")
106
107                +"_"
108
109                +((this.getType()!=null) ? this.getType().toString() : "NOTYPE")
110
111                +"_"
112
113                +((this.getId()!=null) ? this.getId().toString() : "NOID");
114    }
115
116    public boolean isLazyloadAllowed() {
117        return lazyloadAllowed;
118    }
119
120    public void setLazyloadAllowed(boolean lazyloadAllowed) {
121        this.lazyloadAllowed = lazyloadAllowed;
122    }
123    
124    public static final class DAComparator implements Comparator<DAObject>, Serializable {
125
126        @Override
127        public int compare(DAObject o1, DAObject o2) {
128
129            if (o1 != null && o2 != null) {
130                    return ((DAObject)o1 ).getHashID().compareTo(((DAObject)o2).getHashID());
131                }
132
133            return 0;
134        }
135
136    }
137
138    public static final DAComparator daComparator = new DAComparator();
139
140}