/ecat-checklistbank/src/test/java/org/gbif/manualtests/CreateDefaultDb.java

http://gbif-ecat.googlecode.com/ · Java · 132 lines · 89 code · 20 blank · 23 comment · 2 complexity · 54a1538973c2d5f5578a23627310ecf6 MD5 · raw file

  1. /***************************************************************************
  2. * Copyright 2010 Global Biodiversity Information Facility Secretariat
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. ***************************************************************************/
  16. package org.gbif.manualtests;
  17. import org.gbif.checklistbank.Constants;
  18. import org.gbif.checklistbank.TestConstants;
  19. import org.gbif.checklistbank.guice.BasicModule;
  20. import org.gbif.checklistbank.guice.ConfigModuleMaven;
  21. import org.gbif.checklistbank.model.Checklist;
  22. import org.gbif.checklistbank.model.voc.ChecklistType;
  23. import org.gbif.checklistbank.service.ChecklistImportService;
  24. import org.gbif.checklistbank.service.ChecklistService;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import com.google.inject.Guice;
  28. import com.google.inject.Injector;
  29. /**
  30. * This class creates a new default test clb database to be dumped into a sql
  31. * file for testing. Dump the db into a sql file with:
  32. * pg_dump -U postgres -n public -i --inserts --no-owner clb-test > defaultdb.sql
  33. *
  34. * @author markus
  35. */
  36. public class CreateDefaultDb {
  37. private ChecklistImportService importService;
  38. private ChecklistService checklistService;
  39. public CreateDefaultDb(ChecklistImportService importService, ChecklistService checklistService) {
  40. super();
  41. this.importService = importService;
  42. this.checklistService = checklistService;
  43. }
  44. public static void main(String[] args) {
  45. Injector injector = Guice.createInjector(new ConfigModuleMaven(), new BasicModule(false));
  46. CreateDefaultDb db = new CreateDefaultDb(injector.getInstance(ChecklistImportService.class),
  47. injector.getInstance(ChecklistService.class));
  48. System.out.println("\n*** START POPULATING THE DEFAULT DATABASE ***");
  49. db.initDefaultDb();
  50. //db.initNubTestDb();
  51. System.out.println("\n\n\n*** DEFAULT DATABASE CREATED ***");
  52. System.out
  53. .println("\n dump db with:\n pg_dump -U postgres -n public -i --inserts --no-owner clb-test > defaultdb.sql");
  54. }
  55. private List<Checklist> checklists() {
  56. List<Checklist> lists = new ArrayList<Checklist>();
  57. Checklist c = new Checklist();
  58. c.setId(TestConstants.checklistId1Denormed);
  59. c.setNubPriority(c.getId());
  60. c.setResourceKey(TestConstants.resourceKey1Denormed);
  61. c.setType(ChecklistType.inventory);
  62. checklistService.insert(c);
  63. lists.add(checklistService.updateChecklistWithArchiveMetadata(c));
  64. c = new Checklist();
  65. c.setId(TestConstants.checklistId7Plants);
  66. c.setNubPriority(c.getId());
  67. c.setResourceKey(TestConstants.resourceKey7Plants);
  68. c.setType(ChecklistType.taxonomic);
  69. checklistService.insert(c);
  70. lists.add(checklistService.updateChecklistWithArchiveMetadata(c));
  71. c = new Checklist();
  72. c.setId(TestConstants.checklistId9Mammals);
  73. c.setNubPriority(c.getId());
  74. c.setResourceKey(TestConstants.resourceKey9Mammals);
  75. c.setType(ChecklistType.taxonomic);
  76. checklistService.insert(c);
  77. lists.add(checklistService.updateChecklistWithArchiveMetadata(c));
  78. return lists;
  79. }
  80. protected void initDefaultDb() {
  81. importService.initDatabase();
  82. for (Checklist c : checklists()) {
  83. importService.importChecklist(c, true);
  84. }
  85. }
  86. protected void initNubTestDb() {
  87. importService.initDatabase();
  88. for (Checklist c : nubChecklists()) {
  89. importService.importChecklist(c, false);
  90. }
  91. }
  92. private List<Checklist> nubChecklists() {
  93. List<Checklist> lists = new ArrayList<Checklist>();
  94. lists.add(checklistService.get(Constants.COL_CHECKLIST_ID));
  95. lists.add(checklistService.get(Constants.HOMONYM_CHECKLIST_ID));
  96. Checklist c = new Checklist();
  97. c.setType(ChecklistType.nomenclator);
  98. c.setTitle("IPNI");
  99. c.setDownloadUri("http://ecat-dev.gbif.org/repository/protected/nomenclators/ipni.zip");
  100. c.setNubPriority(10);
  101. checklistService.insert(c);
  102. lists.add(checklistService.updateChecklistWithArchiveMetadata(c));
  103. c = new Checklist();
  104. c.setType(ChecklistType.nomenclator);
  105. c.setTitle("Index Fungorum");
  106. c.setDownloadUri("http://ecat-dev.gbif.org/repository/protected/nomenclators/index_fungorum.zip");
  107. c.setNubPriority(9);
  108. checklistService.insert(c);
  109. lists.add(checklistService.updateChecklistWithArchiveMetadata(c));
  110. return lists;
  111. }
  112. }