+// $Id$\r
+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy \r
+* http://www.e-taxonomy.eu\r
+* \r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
+\r
+package eu.etaxonomy.cdm.app.wp6.palmae;\r
+\r
+import java.util.List;\r
+import java.util.UUID;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.springframework.transaction.TransactionStatus;\r
+\r
+import eu.etaxonomy.cdm.api.application.CdmApplicationController;\r
+import eu.etaxonomy.cdm.app.common.CdmDestinations;\r
+import eu.etaxonomy.cdm.database.DbSchemaValidation;\r
+import eu.etaxonomy.cdm.database.ICdmDataSource;\r
+import eu.etaxonomy.cdm.model.description.Feature;\r
+import eu.etaxonomy.cdm.model.description.FeatureNode;\r
+import eu.etaxonomy.cdm.model.description.FeatureTree;\r
+\r
+/**\r
+ * @author a.mueller\r
+ * @created 01.10.2009\r
+ * @version 1.0\r
+ */\r
+public class PalmaeFeatureTreeUpdater {\r
+ private static final Logger logger = Logger.getLogger(PalmaeFeatureTreeUpdater.class);\r
+\r
+ static final ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();\r
+ \r
+ \r
+ private String relationships = "relationships";\r
+ private String taxonomicAccounts = "taxonomic accounts";\r
+ private String fossilRecord = "fossil record";\r
+ \r
+ public boolean updateMissingFeatures(ICdmDataSource dataSource) {\r
+ try{\r
+ int count = 0;\r
+ UUID featureTreeUuid = PalmaeActivator.featureTreeUuid;\r
+ CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);\r
+ \r
+ TransactionStatus tx = cdmApp.startTransaction();\r
+ \r
+ FeatureTree tree = cdmApp.getDescriptionService().getFeatureTreeByUuid(featureTreeUuid);\r
+ FeatureNode root = tree.getRoot();\r
+ \r
+ List<Feature> featureList = cdmApp.getDescriptionService().getFeaturesAll();\r
+ for (Feature feature : featureList){\r
+ String label = feature.getLabel();\r
+ if (relationships.equals(label)){\r
+ FeatureNode newNode = FeatureNode.NewInstance(feature);\r
+ root.addChild(newNode);\r
+ count++;\r
+ }else if(taxonomicAccounts.equals(label)){\r
+ FeatureNode newNode = FeatureNode.NewInstance(feature);\r
+ root.addChild(newNode);\r
+ count++;\r
+ }else if(fossilRecord.equals(label)){\r
+ FeatureNode newNode = FeatureNode.NewInstance(feature);\r
+ root.addChild(newNode);\r
+ count++;\r
+ }\r
+ }\r
+ cdmApp.commitTransaction(tx);\r
+ if (count != 3){\r
+ logger.warn("Did not find 3 additional features but " + count);\r
+ return false;\r
+ }\r
+ logger.info("Feature tree updated!");\r
+ return true;\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ logger.error("ERROR in feature tree update");\r
+ return false;\r
+ }\r
+ \r
+ }\r
+ \r
+ \r
+ /**\r
+ * @param args\r
+ */\r
+ public static void main(String[] args) {\r
+ PalmaeFeatureTreeUpdater updater = new PalmaeFeatureTreeUpdater();\r
+ try {\r
+ updater.updateMissingFeatures(cdmDestination);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ logger.error("ERROR in feature tree update");\r
+ }\r
+ }\r
+}\r