PalmaeFeatureTreeUpdater
[cdmlib.git] / app-import / src / main / java / eu / etaxonomy / cdm / app / wp6 / palmae / PalmaeFeatureTreeUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.app.wp6.palmae;
12
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
20 import eu.etaxonomy.cdm.app.common.CdmDestinations;
21 import eu.etaxonomy.cdm.database.DbSchemaValidation;
22 import eu.etaxonomy.cdm.database.ICdmDataSource;
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.description.FeatureNode;
25 import eu.etaxonomy.cdm.model.description.FeatureTree;
26
27 /**
28 * @author a.mueller
29 * @created 01.10.2009
30 * @version 1.0
31 */
32 public class PalmaeFeatureTreeUpdater {
33 private static final Logger logger = Logger.getLogger(PalmaeFeatureTreeUpdater.class);
34
35 static final ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();
36
37
38 private String relationships = "relationships";
39 private String taxonomicAccounts = "taxonomic accounts";
40 private String fossilRecord = "fossil record";
41
42 public boolean updateMissingFeatures(ICdmDataSource dataSource) {
43 try{
44 int count = 0;
45 UUID featureTreeUuid = PalmaeActivator.featureTreeUuid;
46 CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
47
48 TransactionStatus tx = cdmApp.startTransaction();
49
50 FeatureTree tree = cdmApp.getDescriptionService().getFeatureTreeByUuid(featureTreeUuid);
51 FeatureNode root = tree.getRoot();
52
53 List<Feature> featureList = cdmApp.getDescriptionService().getFeaturesAll();
54 for (Feature feature : featureList){
55 String label = feature.getLabel();
56 if (relationships.equals(label)){
57 FeatureNode newNode = FeatureNode.NewInstance(feature);
58 root.addChild(newNode);
59 count++;
60 }else if(taxonomicAccounts.equals(label)){
61 FeatureNode newNode = FeatureNode.NewInstance(feature);
62 root.addChild(newNode);
63 count++;
64 }else if(fossilRecord.equals(label)){
65 FeatureNode newNode = FeatureNode.NewInstance(feature);
66 root.addChild(newNode);
67 count++;
68 }
69 }
70 cdmApp.commitTransaction(tx);
71 if (count != 3){
72 logger.warn("Did not find 3 additional features but " + count);
73 return false;
74 }
75 logger.info("Feature tree updated!");
76 return true;
77 } catch (Exception e) {
78 e.printStackTrace();
79 logger.error("ERROR in feature tree update");
80 return false;
81 }
82
83 }
84
85
86 /**
87 * @param args
88 */
89 public static void main(String[] args) {
90 PalmaeFeatureTreeUpdater updater = new PalmaeFeatureTreeUpdater();
91 try {
92 updater.updateMissingFeatures(cdmDestination);
93 } catch (Exception e) {
94 e.printStackTrace();
95 logger.error("ERROR in feature tree update");
96 }
97 }
98 }