+++ /dev/null
-/**
- *
- */
-package eu.etaxonomy.taxeditor.editor.name;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
-import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
-import eu.etaxonomy.cdm.model.name.NonViralName;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
-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.parser.ParserUtil;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * @author n.hoffmann
- *
- */
-public class ParseHandler{
- private static final Logger logger = Logger.getLogger(ParseHandler.class);
-
- private TaxonNameBase taxonNameBase;
-
- private List<ReferenceBase> duplicateReferences;
-
- private List<TaxonNameBase> duplicateNames;
-
- private List<TeamOrPersonBase> duplicateCombinationAuthorTeams;
- private List<TeamOrPersonBase> duplicateExCombinationAuthorTeams;
- private List<TeamOrPersonBase> duplicateBasionymAuthorTeams;
- private List<TeamOrPersonBase> duplicateExBasionymAuthorTeams;
-
- private NonViralNameParserImpl nonViralNameParser;
-
- private NonViralName name;
-
- private Control textWidget;
-
- private ParseHandler(Control textWidget, TaxonNameBase name){
-
- nonViralNameParser = NonViralNameParserImpl.NewInstance();
-
- if(textWidget != null){
- this.textWidget = textWidget;
- checkControlHasText();
- }else{
- throw new IllegalArgumentException("text widget must not be null");
- }
-
- if(name == null){
- this.name = nonViralNameParser.getNonViralNameInstance("", PreferencesUtil.getPreferredNomenclaturalCode());
- }else{
- this.name = (NonViralName) HibernateProxyHelper.deproxy(name);
- }
- }
-
- public static ParseHandler NewInstance(Control textWidget, TaxonNameBase name){
- return new ParseHandler(textWidget, name);
-
- }
-
- private void checkControlHasText(){
- Class clazz = textWidget.getClass();
-
- try {
- clazz.getDeclaredMethod("getText", null);
- } catch (SecurityException e) {
- } catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("Given composite does not have a getText method");
- }
- }
-
- /**
- *
- * @param unparsedNameString
- * @param rank
- * @return
- */
- public NonViralName parse(){
-
-
- String unparsedNameString = "";
- try {
- Method getText;
- getText = textWidget.getClass().getDeclaredMethod("getText", null);
- unparsedNameString = (String) getText.invoke(textWidget, null);
- } catch (Exception e) {
- // we should never get here
- logger.error("Error trying to invoke getText method");
- }
-
-
- nonViralNameParser.parseReferencedName(name, unparsedNameString,
- name.getRank(), true);
-
- if (name.hasProblem()) {
- name.setFullTitleCache(unparsedNameString);
- }
-
- return name;
- }
-
- /**
- *
- * @param unparsedNameString
- * @param rank
- * @return
- */
- public NonViralName parseAndResolveDuplicates(){
-
- parse();
-
- findMatches(name);
-
- resolveDuplicates(name);
-
- return name;
- }
-
-
-
-
- /**
- * @param parsedName
- */
- private void resolveDuplicates(NonViralName name) {
- resolveDuplicateNames(name);
-
- resolveAllDuplicateAuthors(name);
-
- resolveDuplicateReferences(name);
-
- }
-
- /**
- * @param composite
- */
- private void resolveDuplicateNames(NonViralName name) {
-
- if (duplicateNames.size() == 1){
- name = (NonViralName) duplicateNames.iterator().next();
- }else if(duplicateNames.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name = (NonViralName) duplicateNames.iterator().next();
- }
- }
-
- /**
- * @param composite
- */
- 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());
- }else if(duplicateReferences.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name.setNomenclaturalReference(duplicateReferences.iterator().next());
- }
- }
-
-
-
- /**
- * @param composite
- */
- private void resolveAllDuplicateAuthors(NonViralName name) {
-
- if(duplicateCombinationAuthorTeams.size() == 1){
- name.setCombinationAuthorTeam(duplicateCombinationAuthorTeams.iterator().next());
- }else if(duplicateCombinationAuthorTeams.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name.setCombinationAuthorTeam(duplicateCombinationAuthorTeams.iterator().next());
- }
-
- if(duplicateExCombinationAuthorTeams.size() == 1){
- name.setExCombinationAuthorTeam(duplicateExCombinationAuthorTeams.iterator().next());
- }else if(duplicateExCombinationAuthorTeams.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name.setExCombinationAuthorTeam(duplicateExCombinationAuthorTeams.iterator().next());
- }
-
- if(duplicateBasionymAuthorTeams.size() == 1){
- name.setBasionymAuthorTeam(duplicateBasionymAuthorTeams.iterator().next());
- }else if(duplicateBasionymAuthorTeams.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name.setBasionymAuthorTeam(duplicateBasionymAuthorTeams.iterator().next());
- }
-
- if(duplicateExBasionymAuthorTeams.size() == 1){
- name.setExBasionymAuthorTeam(duplicateExBasionymAuthorTeams.iterator().next());
- }else if(duplicateExBasionymAuthorTeams.size() > 1){
- // FIXME TODO resolve multiple duplications. Use first match for a start
- name.setExBasionymAuthorTeam(duplicateExBasionymAuthorTeams.iterator().next());
- }
- }
-
-
- private void findMatches(NonViralName name){
-
- duplicateNames = findMatchingLatinNames(name);
-
- duplicateCombinationAuthorTeams = findMatchingAuthors((TeamOrPersonBase) name.getCombinationAuthorTeam());
- duplicateExCombinationAuthorTeams = findMatchingAuthors((TeamOrPersonBase) name.getExCombinationAuthorTeam());
- duplicateBasionymAuthorTeams = findMatchingAuthors((TeamOrPersonBase) name.getBasionymAuthorTeam());
- duplicateExBasionymAuthorTeams = findMatchingAuthors((TeamOrPersonBase) name.getExBasionymAuthorTeam());
-
- duplicateReferences = findMatchingNomenclaturalReference(name.getNomenclaturalReference());
- }
-
- /**
- * @param name
- */
- private List<ReferenceBase> findMatchingNomenclaturalReference(ReferenceBase referenceBase) {
- if(referenceBase == null) return new ArrayList<ReferenceBase>();
- try{
- return CdmStore.getCommonService().findMatching(referenceBase, null);
- }catch (MatchException e) {
- logger.error("Error finding matching references", e);
- }
- return null;
- }
-
- /**
- * @param name
- */
- private List<TeamOrPersonBase> findMatchingAuthors(TeamOrPersonBase authorTeam) {
-
- if(authorTeam == null){
- return new ArrayList<TeamOrPersonBase>();
- }
-
- try{
- return CdmStore.getCommonService().findMatching(authorTeam, null);
- }catch (MatchException e) {
- logger.error("Error finding matching authors", e);
- }
- return null;
- }
-
- /**
- * @param name
- */
- 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);
-
- } catch (MatchException e) {
- logger.error("Error finding matching names", e);
- }
- return null;
- }
-
- /**
- * @return the textWidget
- */
- public Control getTextWidget() {
- return textWidget;
- }
-
- /**
- * @param textWidget the textWidget to set
- */
- public void setTextWidget(Control textWidget) {
- this.textWidget = textWidget;
- checkControlHasText();
- }
-}