001    /*
002     * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia;
024    
025    import java.sql.SQLException;
026    import java.sql.Timestamp;
027    import java.util.ArrayList;
028    import java.util.Collection;
029    import java.util.Iterator;
030    
031    import org.mesopotamia.sql.MesopotamiaEngine;
032    import org.mesopotamia.sql.ScanImpl;
033    
034    import biz.hammurapi.sql.IdentityGenerator;
035    import biz.hammurapi.sql.IdentityRetriever;
036    import biz.hammurapi.sql.SQLProcessor;
037    import biz.hammurapi.sql.Transaction;
038    
039    /**
040     * @author Pavel Vlasov
041     * @revision $Revision: 1.2 $
042     */
043    public class Repository {
044            private RepositoryFactory factory;
045            private org.mesopotamia.sql.Repository dbData;
046            
047            /**
048             * Repository is instantiated only by RepositoryFactory
049             * @param factory
050             * @param language
051             * @param languageVersion
052             * @param name
053             */
054            Repository(RepositoryFactory factory, org.mesopotamia.sql.Repository dbData) {
055                    this.factory=factory;
056                    this.dbData=dbData;
057            }
058            
059            public RepositoryFactory getFactory() {
060                    return factory;
061            }
062    
063            public String getDescription() {
064                    return dbData.getDescription();
065            }
066    
067            public int getId() {
068                    return dbData.getId();
069            }
070    
071            public String getName() {
072                    return dbData.getName();
073            }
074            
075            public Collection<Scan> getScans() throws MesopotamiaException {
076                    Collection<Scan> ret = new ArrayList<Scan>();
077                    Iterator<org.mesopotamia.sql.Scan> it = factory.getEngine().getScanByRepository(getId()).iterator();
078                    while (it.hasNext()) {
079                            org.mesopotamia.sql.Scan sd = it.next();
080                            ret.add(factory.getScan(sd.getId()));
081                    }
082                    return ret;
083            }
084            
085            public Scan createScan(
086                            SourceIterator sourceIterator, 
087                            LanguageSelector selector, 
088                            String description, 
089                            String checkSumName, 
090                            Object environment, 
091                            ScanLoadListener listener) throws MesopotamiaException {
092                    try {
093                            final ScanImpl si=new ScanImpl(true);
094                            si.setDescription(description);
095                            si.setScanDate(new Timestamp(System.currentTimeMillis()));
096                            si.setRepository(dbData.getId());
097                            factory.getProcessor().executeTransaction(new Transaction() {
098            
099                                    public boolean execute(SQLProcessor processor) throws SQLException {                                    
100                                            if (factory.getIdentityManager() instanceof IdentityGenerator) {
101                                                    si.setId(((IdentityGenerator) factory.getIdentityManager()).generate(processor.getConnection(), "REPOSITORY"));
102                                            }
103                                            new MesopotamiaEngine(processor).insertScan(si);
104                                            if (factory.getIdentityManager() instanceof IdentityRetriever) {
105                                                    si.setId(((IdentityRetriever) factory.getIdentityManager()).retrieve(processor.getConnection()));
106                                            }
107                                            return true;
108                                    }
109                                    
110                            });
111                            
112                            Scan scan = new Scan(this, si, sourceIterator, selector, checkSumName, environment, listener);
113                            factory.addScan(scan);
114                            return scan;
115                    } catch (SQLException e) {
116                            throw new MesopotamiaException("Cannot create repository", e);
117                    }
118            }
119            
120            /**
121             * Removes all scans and repository from the database.
122             * @throws MesopotamiaException 
123             */
124            public void delete() throws MesopotamiaException {
125                    try {
126                            MesopotamiaEngine engine = factory.getEngine();
127                            engine.deleteSourceUnitByRepository(getId()); // To avoid potential cascading conflict with Scan. Cascades to source_unit_scan
128                            engine.deleteRepository(dbData.getId()); // Cascades to scan
129                    } catch (SQLException e) {
130                            throw new MesopotamiaException("Cannot delete repository", e);
131                    }
132            }
133    }