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;
private TaxonNameBase taxonNameBase;
private List<INomenclaturalReference> duplicateReferences;
+ private List<INomenclaturalReference> duplicateInReferences;
private List<TaxonNameBase> duplicateNames;
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();
}
}
+ /**
+ * 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();
}
/**
+ * 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(){
}
/**
+ * 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(){
- /**
- * @param parsedName
+ /**
+ * @param name The name to resolve duplicates for.
*/
private void resolveDuplicates(NonViralName name) {
resolveDuplicateNames(name);
resolveDuplicateReferences(name);
+ if(duplicateInReferences != null)
+ resolveDuplicateInReferences(name);
}
-
- /**
- * @param composite
+
+
+ /**
+ * @param name The name to resolve duplicates for.
*/
private void resolveDuplicateNames(NonViralName name) {
}
}
- /**
- * @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) {
}
}
-
+ /**
+ * 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);
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);
}
}
/**
- * @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) {
}
try{
- return CdmStore.getCommonService().findMatching(authorTeam, null);
+ return CdmStore.getCommonService().findMatching(authorTeam, MatchStrategyConfigurator.TeamOrPersonMatchStrategy());
}catch (MatchException e) {
logger.error("Error finding matching authors", e);
}
}
/**
- * @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);
}
/**
- * @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;