001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.datamodel;
024
025import java.sql.Connection;
026
027import dk.netarkivet.common.exceptions.ArgumentNotValid;
028import dk.netarkivet.common.exceptions.IllegalState;
029import dk.netarkivet.common.utils.DBUtils;
030
031/**
032 * Enum class defining the tables of the Harvester database and the required versions of the individual tables.
033 */
034public enum HarvesterDatabaseTables {
035
036    /** The table containing information about domains. */
037    DOMAINS {
038        static final String NAME = "domains";
039        static final int REQUIRED_VERSION = 3;
040
041        @Override
042        public int getRequiredVersion() {
043            return REQUIRED_VERSION;
044        }
045
046        @Override
047        public String getTablename() {
048            return NAME;
049        }
050    },
051    /** The table containing information about domain-configurations. */
052    CONFIGURATIONS {
053        static final String NAME = "configurations";
054        static final int REQUIRED_VERSION = 5;
055
056        @Override
057        public int getRequiredVersion() {
058            return REQUIRED_VERSION;
059        }
060
061        @Override
062        public String getTablename() {
063            return NAME;
064        }
065    },
066    /** The table containing information about seedlists. */
067    SEEDLISTS {
068        static final String NAME = "seedlists";
069        static final int REQUIRED_VERSION = 1;
070
071        @Override
072        public int getRequiredVersion() {
073            return REQUIRED_VERSION;
074        }
075
076        @Override
077        public String getTablename() {
078            return NAME;
079        }
080    },
081    /**
082     * The table containing information about passwords. Currently not used.
083     */
084    PASSWORDS {
085        static final String NAME = "passwords";
086        static final int REQUIRED_VERSION = 1;
087
088        @Override
089        public int getRequiredVersion() {
090            return REQUIRED_VERSION;
091        }
092
093        @Override
094        public String getTablename() {
095            return NAME;
096        }
097    },
098    /**
099     * The table containing information about ownerinfo. Currently not used.
100     */
101    OWNERINFO {
102        static final String NAME = "ownerinfo";
103        static final int REQUIRED_VERSION = 1;
104
105        @Override
106        public int getRequiredVersion() {
107            return REQUIRED_VERSION;
108        }
109
110        @Override
111        public String getTablename() {
112            return NAME;
113        }
114    },
115    /** The table containing information about history info. */
116    HISTORYINFO {
117        static final String NAME = "historyinfo";
118        static final int REQUIRED_VERSION = 2;
119
120        @Override
121        public int getRequiredVersion() {
122            return REQUIRED_VERSION;
123        }
124
125        @Override
126        public String getTablename() {
127            return NAME;
128        }
129    },
130    /**
131     * The table containing information about config passwords. Currently not used.
132     */
133    CONFIGPASSWORDS {
134        static final String NAME = "config_passwords";
135        static final int REQUIRED_VERSION = 1;
136
137        @Override
138        public int getRequiredVersion() {
139            return REQUIRED_VERSION;
140        }
141
142        @Override
143        public String getTablename() {
144            return NAME;
145        }
146    },
147    /** The table containing information about config seedlists. */
148    CONFIGSEEDLISTS {
149        static final String NAME = "config_seedlists";
150        static final int REQUIRED_VERSION = 1;
151
152        @Override
153        public int getRequiredVersion() {
154            return REQUIRED_VERSION;
155        }
156
157        @Override
158        public String getTablename() {
159            return NAME;
160        }
161    },
162    /** The table containing information about harvestdefinitions. */
163    HARVESTDEFINITIONS {
164        static final String NAME = "harvestdefinitions";
165        static final int REQUIRED_VERSION = 4;
166
167        @Override
168        public int getRequiredVersion() {
169            return REQUIRED_VERSION;
170        }
171
172        @Override
173        public String getTablename() {
174            return NAME;
175        }
176    },
177    /** The table containing information about partial harvests. */
178    PARTIALHARVESTS {
179        static final String NAME = "partialharvests";
180        static final int REQUIRED_VERSION = 1;
181
182        @Override
183        public int getRequiredVersion() {
184            return REQUIRED_VERSION;
185        }
186
187        @Override
188        public String getTablename() {
189            return NAME;
190        }
191    },
192    /** The table containing information about full harvests. */
193    FULLHARVESTS {
194        static final String NAME = "fullharvests";
195        static final int REQUIRED_VERSION = 5;
196
197        @Override
198        public int getRequiredVersion() {
199            return REQUIRED_VERSION;
200        }
201
202        @Override
203        public String getTablename() {
204            return NAME;
205        }
206    },
207    /** The table containing information about harvest configs */
208    HARVESTCONFIGS {
209        static final String NAME = "harvest_configs";
210        static final int REQUIRED_VERSION = 1;
211
212        @Override
213        public int getRequiredVersion() {
214            return REQUIRED_VERSION;
215        }
216
217        @Override
218        public String getTablename() {
219            return NAME;
220        }
221    },
222    /** The table containing information about harvest schedules. */
223    SCHEDULES {
224        static final String NAME = "schedules";
225        static final int REQUIRED_VERSION = 1;
226
227        @Override
228        public int getRequiredVersion() {
229            return REQUIRED_VERSION;
230        }
231
232        @Override
233        public String getTablename() {
234            return NAME;
235        }
236    },
237    /** The table containing information about Heritrix templates. */
238    ORDERTEMPLATES {
239        static final String NAME = "ordertemplates";
240        static final int REQUIRED_VERSION = 2;
241
242        @Override
243        public int getRequiredVersion() {
244            return REQUIRED_VERSION;
245        }
246
247        @Override
248        public String getTablename() {
249            return NAME;
250        }
251    },
252    /** The table containing information about jobs. */
253    JOBS {
254        static final String NAME = "jobs";
255        static final int REQUIRED_VERSION = 10;
256
257        @Override
258        public int getRequiredVersion() {
259            return REQUIRED_VERSION;
260        }
261
262        @Override
263        public String getTablename() {
264            return NAME;
265        }
266    },
267    /** The table containing information about jobconfigs. */
268    JOBCONFIGS {
269        static final String NAME = "job_configs";
270        static final int REQUIRED_VERSION = 1;
271
272        @Override
273        public int getRequiredVersion() {
274            return REQUIRED_VERSION;
275        }
276
277        @Override
278        public String getTablename() {
279            return NAME;
280        }
281    },
282    /** The table containing information about list of global crawlertraps. */
283    GLOBALCRAWLERTRAPLISTS {
284        static final String NAME = "global_crawler_trap_lists";
285        static final int REQUIRED_VERSION = 1;
286
287        @Override
288        public int getRequiredVersion() {
289            return REQUIRED_VERSION;
290        }
291
292        @Override
293        public String getTablename() {
294            return NAME;
295        }
296    },
297    /** The table containing information about global crawlertrap expressions. */
298    GLOBALCRAWLERTRAPEXPRESSIONS {
299        static final String NAME = "global_crawler_trap_expressions";
300        static final int REQUIRED_VERSION = 1;
301
302        @Override
303        public int getRequiredVersion() {
304            return REQUIRED_VERSION;
305        }
306
307        @Override
308        public String getTablename() {
309            return NAME;
310        }
311    },
312    /** The table containing information about running jobs history. */
313    RUNNINGJOBSHISTORY {
314        static final String NAME = "runningjobshistory";
315        static final int REQUIRED_VERSION = 2;
316
317        @Override
318        public int getRequiredVersion() {
319            return REQUIRED_VERSION;
320        }
321
322        @Override
323        public String getTablename() {
324            return NAME;
325        }
326    },
327    /** The table containing information about running jobs monitor. */
328    RUNNINGJOBSMONITOR {
329        static final String NAME = "runningjobsmonitor";
330        static final int REQUIRED_VERSION = 2;
331
332        @Override
333        public int getRequiredVersion() {
334            return REQUIRED_VERSION;
335        }
336
337        @Override
338        public String getTablename() {
339            return NAME;
340        }
341    },
342    /** The table containing information about frontier report monitor. */
343    FRONTIERREPORTMONITOR {
344        static final String NAME = "frontierreportmonitor";
345        static final int REQUIRED_VERSION = 1;
346
347        @Override
348        public int getRequiredVersion() {
349            return REQUIRED_VERSION;
350        }
351
352        @Override
353        public String getTablename() {
354            return NAME;
355        }
356    },
357    /** The table containing information about extended fields. */
358    EXTENDEDFIELD {
359        static final String NAME = "extendedfield";
360        static final int REQUIRED_VERSION = 2;
361
362        @Override
363        public int getRequiredVersion() {
364            return REQUIRED_VERSION;
365        }
366
367        @Override
368        public String getTablename() {
369            return NAME;
370        }
371    },
372    /** The table containing information about extended field values. */
373    EXTENDEDFIELDVALUE {
374        static final String NAME = "extendedfieldvalue";
375        static final int REQUIRED_VERSION = 2;
376
377        @Override
378        public int getRequiredVersion() {
379            return REQUIRED_VERSION;
380        }
381
382        @Override
383        public String getTablename() {
384            return NAME;
385        }
386    },
387    /** The table containing information about extended field types. */
388    EXTENDEDFIELDTYPE {
389        static final String NAME = "extendedfieldtype";
390        static final int REQUIRED_VERSION = 1;
391
392        @Override
393        public int getRequiredVersion() {
394            return REQUIRED_VERSION;
395        }
396
397        @Override
398        public String getTablename() {
399            return NAME;
400        }
401    },
402    /** Harvest channels. */
403    HARVESTCHANNELS {
404        static final String NAME = "harvestchannel";
405        static final int REQUIRED_VERSION = 1;
406
407        @Override
408        public int getRequiredVersion() {
409            return REQUIRED_VERSION;
410        }
411
412        @Override
413        public String getTablename() {
414            return NAME;
415        }
416    },
417    /** Harvest channels. */
418    EAVTYPEATTRIBUTE {
419        static final String NAME = "eav_type_attribute";
420        static final int REQUIRED_VERSION = 1;
421
422        @Override
423        public int getRequiredVersion() {
424            return REQUIRED_VERSION;
425        }
426
427        @Override
428        public String getTablename() {
429            return NAME;
430        }
431    },
432    /** Harvest channels. */
433    EAVATTRIBUTE {
434        static final String NAME = "eav_attribute";
435        static final int REQUIRED_VERSION = 1;
436
437        @Override
438        public int getRequiredVersion() {
439            return REQUIRED_VERSION;
440        }
441
442        @Override
443        public String getTablename() {
444            return NAME;
445        }
446    };
447
448    /** @return required version of table. */
449    public abstract int getRequiredVersion();
450
451    /** @return name of database table. */
452    public abstract String getTablename();
453
454    /**
455     * Check that a database table has the required version.
456     * <p>
457     * NB: the provided connection is not closed.
458     *
459     * @param connection connection to the database.
460     * @param table The table to check up against required version
461     * @throws IllegalState if the version isn't as required.
462     */
463    public static void checkVersion(Connection connection, HarvesterDatabaseTables table) {
464        ArgumentNotValid.checkNotNull(connection, "Connection connection");
465        ArgumentNotValid.checkNotNull(table, "HarvesterDatabaseTables table");
466
467        int actualVersion = DBUtils.getTableVersion(connection, table.getTablename());
468        if (actualVersion != table.getRequiredVersion()) {
469            String message = "Wrong table version for '" + table.getTablename() + "': " + "Should be "
470                    + table.getRequiredVersion() + ", but is " + actualVersion;
471            throw new IllegalState(message);
472        }
473    }
474
475}