refactor Xper adapter, also done in last commit on xper2
authorAndreas Müller <a.mueller@bgbm.org>
Mon, 11 Apr 2011 10:41:44 +0000 (10:41 +0000)
committerAndreas Müller <a.mueller@bgbm.org>
Mon, 11 Apr 2011 10:41:44 +0000 (10:41 +0000)
XperCdmIntegration/src/main/java/eu/etaxonomy/cdm/io/xper/AdaptaterCdmXper.java
XperCdmIntegration/src/test/java/eu/etaxonomy/cdm/io/xper/TestAdapterCdmXper.java

index ed4ebac533dee45f6ab6d695b525beac0266e169..8a81c537577c60c421f39abd53239f8e399534b7 100644 (file)
@@ -11,6 +11,7 @@ import org.apache.commons.lang.StringUtils;
 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.api.service.ITermService;\r
 import eu.etaxonomy.cdm.api.service.IVocabularyService;\r
 import eu.etaxonomy.cdm.api.service.pager.Pager;\r
@@ -45,12 +46,25 @@ import fr_jussieu_snv_lis.utils.Utils;
 public class AdaptaterCdmXper implements ICdmAdapter{\r
        private static final Logger logger = Logger.getLogger(AdaptaterCdmXper.class);\r
        \r
-       TransactionStatus tx = Xper.getCdmApplicationController().startTransaction();\r
+       TransactionStatus tx;\r
+\r
+       private CdmApplicationController cdmApplicationController;\r
        \r
-       public AdaptaterCdmXper() {\r
-               \r
+       public AdaptaterCdmXper(CdmApplicationController appCtr) {\r
+               setCdmApplicationController(appCtr);\r
        }\r
        \r
+\r
+       public void setCdmApplicationController(CdmApplicationController appCtr) {\r
+               this.cdmApplicationController = appCtr;\r
+                tx = cdmApplicationController.startTransaction();\r
+       }\r
+\r
+\r
+       public CdmApplicationController getCdmApplicationController() {\r
+               return cdmApplicationController;\r
+       }\r
+\r
        \r
        public void load(){\r
                loadFeatures();\r
@@ -65,14 +79,14 @@ public class AdaptaterCdmXper implements ICdmAdapter{
 //             UUID featureTreeUUID = UUID.fromString("43ab1efd-fa15-419a-8cd6-05477e4b37bc");\r
                List<String> featureTreeInit = Arrays.asList(new String[]{"root.children.feature.representations"});\r
                \r
-               TransactionStatus tx = Xper.getCdmApplicationController().startTransaction();\r
-               FeatureTree featureTree = Xper.getCdmApplicationController().getFeatureTreeService().load(featureTreeUUID, featureTreeInit);\r
+               TransactionStatus tx = cdmApplicationController.startTransaction();\r
+               FeatureTree featureTree =cdmApplicationController.getFeatureTreeService().load(featureTreeUUID, featureTreeInit);\r
                if (featureTree != null) {\r
                        loadFeatureNode(featureTree.getRoot(), -1);\r
                }else{\r
                        logger.warn("Feature tree " + featureTreeUUID.toString() + " not found");\r
                }\r
-               Xper.getCdmApplicationController().commitTransaction(tx);\r
+               cdmApplicationController.commitTransaction(tx);\r
        }\r
        \r
        /**\r
@@ -125,8 +139,8 @@ public class AdaptaterCdmXper implements ICdmAdapter{
        \r
        // Load all the taxa and 1 description\r
        public void loadTaxaAndDescription() {\r
-               TransactionStatus tx = Xper.getCdmApplicationController().startTransaction();\r
-               List<TaxonBase> taxonList = Xper.getCdmApplicationController().getTaxonService().list(Taxon.class , null, null, null, null);\r
+               TransactionStatus tx = cdmApplicationController.startTransaction();\r
+               List<TaxonBase> taxonList = cdmApplicationController.getTaxonService().list(Taxon.class , null, null, null, null);\r
                for(TaxonBase taxonBase : taxonList){\r
                        if (Utils.currentBase != null) {\r
                                Individual individual = new Individual(taxonBase.getName().toString());\r
@@ -145,13 +159,13 @@ public class AdaptaterCdmXper implements ICdmAdapter{
                                Utils.currentBase.addIndividual(individual);\r
                        }\r
                }\r
-               Xper.getCdmApplicationController().commitTransaction(tx);\r
+               cdmApplicationController.commitTransaction(tx);\r
        }\r
 \r
        // Load the first taxonDescription\r
        public void loadDescription(Individual individual, TaxonBase taxonBase) {\r
                \r
-               Pager<TaxonDescription> taxonDescriptionPager = Xper.getCdmApplicationController().getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));\r
+               Pager<TaxonDescription> taxonDescriptionPager = cdmApplicationController.getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));\r
                List<TaxonDescription> taxonDescriptionList = taxonDescriptionPager.getRecords();\r
                TaxonDescription taxonDescription = taxonDescriptionList.get(0);\r
                Set<DescriptionElementBase> DescriptionElementBaseList = taxonDescription.getElements();\r
@@ -215,25 +229,25 @@ public class AdaptaterCdmXper implements ICdmAdapter{
        // Create a workingSet if not exist\r
        public void createWorkingSet(){\r
                \r
-               if(Xper.getCdmApplicationController().getWorkingSetService().list(WorkingSet.class, null, null, null, null).size() <= 0){\r
+               if(cdmApplicationController.getWorkingSetService().list(WorkingSet.class, null, null, null, null).size() <= 0){\r
                        WorkingSet ws = WorkingSet.NewInstance();\r
                        \r
                        UUID featureTreeUUID = UUID.fromString("47eda782-89c7-4c69-9295-e4052ebe16c6");\r
                        List<String> featureTreeInit = Arrays.asList(new String[]{"root.children.feature.representations"});\r
                        \r
-                       FeatureTree featureTree = Xper.getCdmApplicationController().getFeatureTreeService().load(featureTreeUUID, featureTreeInit);\r
+                       FeatureTree featureTree = cdmApplicationController.getFeatureTreeService().load(featureTreeUUID, featureTreeInit);\r
                        ws.setDescriptiveSystem(featureTree);\r
                        \r
-                       List<TaxonBase> taxonList = Xper.getCdmApplicationController().getTaxonService().list(Taxon.class , null, null, null, null);\r
+                       List<TaxonBase> taxonList = cdmApplicationController.getTaxonService().list(Taxon.class , null, null, null, null);\r
                        for(TaxonBase taxonBase : taxonList){\r
-                               Pager<TaxonDescription> taxonDescriptionPager = Xper.getCdmApplicationController().getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));\r
+                               Pager<TaxonDescription> taxonDescriptionPager = cdmApplicationController.getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));\r
                                List<TaxonDescription> taxonDescriptionList = taxonDescriptionPager.getRecords();\r
                                TaxonDescription taxonDescription = taxonDescriptionList.get(0);\r
                                ws.addDescription(taxonDescription);\r
                                System.out.println(taxonDescription.getUuid());\r
                        }\r
                        \r
-                       Xper.getCdmApplicationController().getWorkingSetService().save(ws);\r
+                       cdmApplicationController.getWorkingSetService().save(ws);\r
                }\r
        }\r
 \r
@@ -253,7 +267,7 @@ public class AdaptaterCdmXper implements ICdmAdapter{
         * @param vars\r
         */\r
        private void saveFeatures(List<Variable> vars) {\r
-               tx = Xper.getCdmApplicationController().startTransaction();\r
+               tx = cdmApplicationController.startTransaction();\r
                for (Variable variable : vars){\r
                        Feature feature = getFeature(variable);\r
                        if (Utils.numType.equals(variable.getType())){\r
@@ -264,7 +278,7 @@ public class AdaptaterCdmXper implements ICdmAdapter{
                                logger.warn("variable type undefined");\r
                        }\r
                }\r
-               Xper.getCdmApplicationController().commitTransaction(tx);\r
+               cdmApplicationController.commitTransaction(tx);\r
        }\r
 \r
 \r
@@ -274,15 +288,15 @@ public class AdaptaterCdmXper implements ICdmAdapter{
         */\r
        private Feature getFeature(Variable variable) {\r
                UUID uuid = variable.getUuid();\r
-               ITermService termService = Xper.getCdmApplicationController().getTermService();\r
+               ITermService termService = cdmApplicationController.getTermService();\r
                DefinedTermBase<?> term = termService.find(uuid);\r
                Feature feature = CdmBase.deproxy(term, Feature.class);\r
                return feature;\r
        }\r
 \r
        private void saveCategoricalFeature(Variable variable, Feature feature) {\r
-               ITermService termService = Xper.getCdmApplicationController().getTermService();\r
-               IVocabularyService vocService = Xper.getCdmApplicationController().getVocabularyService();\r
+               ITermService termService = cdmApplicationController.getTermService();\r
+               IVocabularyService vocService = cdmApplicationController.getVocabularyService();\r
                if (feature == null){\r
                        saveNewFeature(variable, termService, vocService);\r
                }else{\r
@@ -422,8 +436,8 @@ public class AdaptaterCdmXper implements ICdmAdapter{
 \r
 \r
        private void saveNumericalFeature(Variable variable, Feature feature) {\r
-               ITermService termService = Xper.getCdmApplicationController().getTermService();\r
-               IVocabularyService vocService = Xper.getCdmApplicationController().getVocabularyService();\r
+               ITermService termService = cdmApplicationController.getTermService();\r
+               IVocabularyService vocService = cdmApplicationController.getVocabularyService();\r
                String variableUnit = variable.getUnit();\r
                Set<MeasurementUnit> units = feature.getRecommendedMeasurementUnits();\r
                //preliminary\r
@@ -478,4 +492,8 @@ public class AdaptaterCdmXper implements ICdmAdapter{
                return null;\r
        }\r
 \r
+\r
+\r
+\r
+\r
 }\r
index 6fa48dcc6db9401880080e99cba2f2500d0d007b..5739b1af56ea823823e7fa36416e3fee4347a56a 100644 (file)
@@ -13,43 +13,50 @@ import fr_jussieu_snv_lis.utils.Utils;
 \r
 public class TestAdapterCdmXper {\r
        \r
+       AdaptaterCdmXper adapterCdmXper;\r
        \r
-       \r
-       public TestAdapterCdmXper(){\r
-               \r
+       /**\r
+        * \r
+        */\r
+       private void startApplications() {\r
                DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;\r
                ICdmDataSource datasource = CdmDestinations.cdm_test_local_xper();\r
-\r
-               final CdmApplicationController appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation);\r
+               System.out.println("cdm start");\r
+               CdmApplicationController appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation);\r
+               System.out.println("cdm started :::");\r
                \r
+               adapterCdmXper = new AdaptaterCdmXper(appCtr);\r
                \r
                Thread t = new Thread() {\r
                        public void run() {\r
-                               new Xper(appCtr);\r
+                               new Xper(adapterCdmXper);\r
                        }\r
                };\r
+               System.out.println("xper2 start");\r
                t.start();\r
+               while(!Utils.xperReady){\r
+                       //TODO wait\r
+               }\r
+               System.out.println("xper2 started :::");\r
        }\r
        \r
-       public static void xperloadDataFromCdm(){\r
+       public void xperloadDataFromCdm(){\r
+               System.out.println("start load data");\r
+               // display a loading gif\r
+               Utils.displayLoadingGif(true);\r
+               \r
+               \r
                // create a new empty base and load data from CDM\r
-               if(Xper.getCdmApplicationController() != null){\r
+               if(Utils.cdmAdapter != null){\r
                        // create a new base\r
                        Xper.getMainframe().newBase("baseTest");\r
                        // specify that the current base is not new (needed to be able to add images)\r
                        Utils.isNewBase = false;\r
-                       // use the current directory as working directory for Xper2\r
-                       XPDisplay.getControler().getBase().setPathName(System.getProperty("user.dir") + Utils.sep);\r
-                       // create a _thumbnail directory to store thumbnails\r
-                       new File(System.getProperty("user.dir") + Utils.sep + "images" + Utils.sep + "_thumbnails").mkdirs();\r
                        // delete the variable create by default and update the frame\r
                        XPDisplay.getControler().getBase().deleteVariable(XPDisplay.getControler().getBase().getVariableAt(0));\r
                        XPDisplay.displayNbVariable();\r
                        XPDisplay.getControler().displayJifVarTree();\r
                        \r
-                       AdaptaterCdmXper adapterCdmXper = new AdaptaterCdmXper();\r
-                       Utils.cdmAdapter = adapterCdmXper;\r
-                       \r
                        if (Utils.currentBase != null) {\r
 //                             adaptaterCdmXper.createWorkingSet();\r
                                adapterCdmXper.load();\r
@@ -58,33 +65,60 @@ public class TestAdapterCdmXper {
                                XPDisplay.getControler().displayJifVarTree();\r
                        }\r
                }\r
+               // undisplay a loading gif\r
+               Utils.displayLoadingGif(false);\r
+               System.out.println("data loaded :::");\r
        }\r
 \r
        /**\r
-        * @param args\r
+        * \r
         */\r
-       public static void main(String[] args) {\r
-               System.out.println("cdm start");\r
-               new TestAdapterCdmXper();\r
-               System.out.println("cdm started :::");\r
-               \r
-               while(!Utils.xperReady){\r
-                       //TODO wait\r
-               }\r
-               System.out.println("start load data");\r
-               // display a loading gif\r
-               Utils.displayLoadingGif(true);\r
-               // load the data from CDM\r
-               \r
-               xperloadDataFromCdm();\r
-               // undisplay a loading gif\r
-               Utils.displayLoadingGif(false);\r
-               System.out.println("stop load data");\r
+       private void createThumbnailDirectory() {\r
+               // create a _thumbnail directory to store thumbnails\r
+               new File(System.getProperty("user.dir") + Utils.sep + "images" + Utils.sep + "_thumbnails").mkdirs();\r
+       }\r
+       \r
+       /**\r
+        * \r
+        */\r
+       private void generateThumbnails() {\r
                System.out.println("start generate thumbnails");\r
                // generate all thumbnails (a loading gif is automatically displayed\r
                Utils.generateThumbnailsFromURLImage(XPDisplay.getControler().getBase().getAllResources());\r
                System.out.println("stop generate thumbnails");\r
+       }\r
+       \r
+\r
+       private void startPartialCdm() {\r
+               // TODO Auto-generated method stub\r
+               \r
+       }\r
+\r
+       /**\r
+        * @param args\r
+        */\r
+       public static void main(String[] args) {\r
+               System.out.println("start test");\r
+               //start CDM and Xper\r
+               TestAdapterCdmXper testAdapter = new TestAdapterCdmXper();\r
+               testAdapter.startApplications();\r
+               testAdapter.createThumbnailDirectory();\r
+               if (args.length >= 1 && "-p".equals(args[0]) ){\r
+                       testAdapter.startPartialCdm();\r
+               }else{\r
+                       // load the data from CDM\r
+                       testAdapter.xperloadDataFromCdm();\r
+                       // use the current directory as working directory for Xper2\r
+                       XPDisplay.getControler().getBase().setPathName(System.getProperty("user.dir") + Utils.sep);\r
+                       \r
+                       testAdapter.generateThumbnails();\r
+               }\r
+\r
+\r
                \r
        }\r
 \r
+\r
+\r
+\r
 }\r