Did some code cleanup. Removed obsolete and deprecated classes.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / parser / ParseHandler.java
index 6c29ebdad4030ef3290d4ee8070659fbe033e642..e3747375d2a6738c9a466681cdd3e9183becd2ea 100644 (file)
@@ -16,10 +16,7 @@ import eu.etaxonomy.cdm.model.name.NonViralName;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
-import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
-import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.MatchException;
-import eu.etaxonomy.cdm.strategy.match.MatchMode;
 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -34,6 +31,7 @@ public class ParseHandler{
        private TaxonNameBase taxonNameBase;
        
        private List<INomenclaturalReference> duplicateReferences;
+       private List<INomenclaturalReference> duplicateInReferences;
 
        private List<TaxonNameBase> duplicateNames;
 
@@ -42,16 +40,27 @@ public class ParseHandler{
        private List<TeamOrPersonBase> duplicateBasionymAuthorTeams;
        private List<TeamOrPersonBase> duplicateExBasionymAuthorTeams;
 
-       private NonViralNameParserImpl nonViralNameParser;
+       private static NonViralNameParserImpl nonViralNameParser = NonViralNameParserImpl.NewInstance();
 
+       /**
+        * The name that should get parsed
+        */
        private NonViralName name;
 
+       /**
+        * The widget, the name got entered in
+        */
        private Control textWidget;
+
+       private boolean doResolveInReferences;
        
+       /**
+        * Creates a new instance 
+        * 
+        * @param textWidget
+        * @param name
+        */
        private ParseHandler(Control textWidget, TaxonNameBase name){
-                               
-               nonViralNameParser = NonViralNameParserImpl.NewInstance();
-               
                if(textWidget != null){
                        this.textWidget = textWidget;
                        checkControlHasText();
@@ -66,11 +75,53 @@ public class ParseHandler{
                }
        }
        
+       /**
+        * Factory method to create a new instance of the this class
+        * 
+        * @param textWidget    The text widget where the NonViralName may be entered into
+        * @param name                  An initial NonViralName or null when creating a new name 
+        * @return                              An instance of this class 
+        */
        public static ParseHandler NewInstance(Control textWidget, TaxonNameBase name){
                return new ParseHandler(textWidget, name);
                
        }
        
+       /**
+        * Parses a given string and returns a <code>TaxonNameBase</code> instance with the 
+        * reuslts of the parsing.
+        * 
+        * This method should be used to quickly create a new name from a string.
+        * Wherever the string will be parsed again in subsequent editing, an instance 
+        * of <code>ParseHandler</code> should be attached to the text widget.
+        * 
+        * @param unparsedNameString
+        * @return
+        */
+       public static TaxonNameBase quickParse(String unparsedNameString){
+               TaxonNameBase name = nonViralNameParser.parseReferencedName(unparsedNameString,
+                               PreferencesUtil.getPreferredNomenclaturalCode(), null);
+
+               if (name.hasProblem()) {
+                       name.setFullTitleCache(unparsedNameString);
+               }
+               
+               return name;
+       }
+       
+       /**
+        * Creates an empty <code>TaxonNameBase</code> instance with the nomenclatural code
+        * currently set in preferences.
+        * 
+        * @return
+        */
+       public static TaxonNameBase createEmptyName(){
+               return nonViralNameParser.getNonViralNameInstance("", PreferencesUtil.getPreferredNomenclaturalCode());
+       }
+       
+       /**
+        * Check if the given control has a getText() method
+        */
        private void checkControlHasText(){
                Class clazz = textWidget.getClass();
                
@@ -83,10 +134,10 @@ public class ParseHandler{
        }
        
        /**
+        * Parses the string that was entered into the text widget and returns a
+        * NonViralName object that resulted from the parsing process. 
         * 
-        * @param unparsedNameString
-        * @param rank
-        * @return
+        * @return      The parsed NonViralName object
         */
        public NonViralName parse(){
                
@@ -113,10 +164,15 @@ public class ParseHandler{
        }
        
        /**
+        * Parses the string that was entered into the text widget and returns a
+        * NonViralName object that resulted from the parsing process. 
         * 
-        * @param unparsedNameString
-        * @param rank
-        * @return
+        * The atomized fields (scientific name, author and reference) will be matched
+        * against the database to find possible duplicates. If duplicates were found 
+        * the respective parts of the NonViralName will be replaced with the found
+        * objects. 
+        *
+        * @return      The parsed NonViralName object
         */
        public NonViralName parseAndResolveDuplicates(){
                
@@ -132,8 +188,8 @@ public class ParseHandler{
        
        
        
-       /**
-        * @param parsedName
+       /** 
+        * @param name The name to resolve duplicates for.
         */
        private void resolveDuplicates(NonViralName name) {
                resolveDuplicateNames(name);
@@ -142,10 +198,13 @@ public class ParseHandler{
                                
                resolveDuplicateReferences(name);
                
+               if(duplicateInReferences != null)
+                       resolveDuplicateInReferences(name);
        }
-       
-       /**
-        * @param composite
+
+
+       /** 
+        * @param name The name to resolve duplicates for.
         */
        private void resolveDuplicateNames(NonViralName name) {
                                
@@ -157,23 +216,38 @@ public class ParseHandler{
                }
        }
 
-       /**
-        * @param composite
+       /** 
+        * @param name The name to resolve duplicates for.
         */
        private void resolveDuplicateReferences(NonViralName name) {
                if(duplicateReferences.size() == 1){
                        // exactly one match. We assume that the user wants this reference
-                       name.setNomenclaturalReference(duplicateReferences.iterator().next());
+                       INomenclaturalReference duplicate = duplicateReferences.iterator().next();      
+                       name.setNomenclaturalReference(duplicate);
                }else if(duplicateReferences.size() > 1){
                        // FIXME TODO resolve multiple duplications. Use first match for a start
-                       name.setNomenclaturalReference(duplicateReferences.iterator().next());
-               }
+                       INomenclaturalReference duplicate = duplicateReferences.iterator().next();
+                       name.setNomenclaturalReference(duplicate);
+               }               
        }
 
+       /** 
+        * @param name The name to resolve duplicates for.
+        */
+       private void resolveDuplicateInReferences(NonViralName name) {
+               ReferenceBase reference = (ReferenceBase) name.getNomenclaturalReference();
+               if(duplicateInReferences.size() == 1){
+                       // exactly one match. We assume that the user wants this reference
+                       reference.setInReference((ReferenceBase) duplicateInReferences.iterator().next());
+               }else if(duplicateInReferences.size() > 1){
+                       // FIXME TODO resolve multiple duplications. Use first match for a start
+                       reference.setInReference((ReferenceBase) duplicateInReferences.iterator().next());
+               }
+       }
+       
 
-
-       /**
-        * @param composite
+       /** 
+        * @param name The name to resolve duplicates for.
         */
        private void resolveAllDuplicateAuthors(NonViralName name) {
                
@@ -206,7 +280,12 @@ public class ParseHandler{
                }
        }
        
-
+       /** 
+        * Splits a NonViralName into its parts and calls methods to find matches for these
+        * parts in the database.
+        * 
+        * @param name  The NonViralName to find matches for.
+        */
        private void findMatches(NonViralName name){
                
                duplicateNames = findMatchingLatinNames(name);
@@ -220,20 +299,24 @@ public class ParseHandler{
                duplicateReferences = findMatchingNomenclaturalReference(nomenclaturalReference);
                
                // check if the reference has an inreference and also check if the inReference already exists
-//             // FIXME implement this once the transition to new reference model is complete (cdmlib 3.2) 
-//             if(nomenclaturalReference.getInReference()){
-//                     duplicateInReferences = findMatchingNomenclaturalReference(referenceBase.getInReference());
-//             }
-               
+               // FIXME implement this once the transition to new reference model is complete (cdmlib 3.2) 
+               if(nomenclaturalReference != null){
+                       ReferenceBase inReference = ((ReferenceBase)nomenclaturalReference).getInReference();
+                       if(inReference != null){
+                               doResolveInReferences = true;
+                               duplicateInReferences = findMatchingNomenclaturalReference(inReference);
+                       }
+               }
        }
        
        /**
-        * @param name
+        * @param nomenclaturalReference        The NomenclaturalReference to find matches for.
+        * @return      A <code>List</code> of possibly matching NomenclaturalReference's.
         */
        private List<INomenclaturalReference> findMatchingNomenclaturalReference(INomenclaturalReference nomenclaturalReference) {
                if(nomenclaturalReference == null) return new ArrayList<INomenclaturalReference>();
                try{
-                       return CdmStore.getCommonService().findMatching(nomenclaturalReference, null);
+                       return CdmStore.getCommonService().findMatching(nomenclaturalReference, MatchStrategyConfigurator.ReferenceMatchStrategy());
                }catch (MatchException e) {
                        logger.error("Error finding matching references", e);
                }
@@ -241,7 +324,8 @@ public class ParseHandler{
        }
 
        /**
-        * @param name
+        * @param authorTeam    The TeamOrPersonBase to find matches for.
+        * @return      A <code>List</code> of possibly matching TeamOrPersonBase's.
         */
        private List<TeamOrPersonBase> findMatchingAuthors(TeamOrPersonBase authorTeam) {
 
@@ -250,7 +334,7 @@ public class ParseHandler{
                }
                
                try{
-                       return CdmStore.getCommonService().findMatching(authorTeam, null);
+                       return CdmStore.getCommonService().findMatching(authorTeam, MatchStrategyConfigurator.TeamOrPersonMatchStrategy());
                }catch (MatchException e) {
                        logger.error("Error finding matching authors", e);
                }
@@ -258,19 +342,13 @@ public class ParseHandler{
        }
 
        /**
-        * @param name
+        * @param taxonNameBase The TaxonNameBase to find matches for.
+        * @return      A <code>List</code> of possibly matching TaxonNameBase's.
         */
        private List<TaxonNameBase> findMatchingLatinNames(TaxonNameBase taxonNameBase) {
 
                try {
-                       IMatchStrategy strategy = DefaultMatchStrategy.NewInstance(NonViralName.class);
-                       strategy.setMatchMode("nomenclaturalReference", MatchMode.IGNORE);
-                       strategy.setMatchMode("combinationAuthorTeam", MatchMode.IGNORE);
-                       strategy.setMatchMode("exCombinationAuthorTeam", MatchMode.IGNORE);
-                       strategy.setMatchMode("basionymAuthorTeam", MatchMode.IGNORE);
-                       strategy.setMatchMode("exBasionymAuthorTeam", MatchMode.IGNORE);
-                       
-                       return CdmStore.getCommonService().findMatching(taxonNameBase, strategy);
+                       return CdmStore.getCommonService().findMatching(taxonNameBase, MatchStrategyConfigurator.NonViralNameMatchStrategy());
                        
                } catch (MatchException e) {
                        logger.error("Error finding matching names", e);
@@ -279,14 +357,14 @@ public class ParseHandler{
        }
 
        /**
-        * @return the textWidget
+        * @return The Control this ParseHandler is attached to.
         */
        public Control getTextWidget() {
                return textWidget;
        }
 
        /**
-        * @param textWidget the textWidget to set
+        * @param textWidget The textWidget to set
         */
        public void setTextWidget(Control textWidget) {
                this.textWidget = textWidget;