Project

General

Profile

Revision d81a4119

IDd81a4119ce27cde95d4d991ba8c164be0e2da0bd
Parent f073606c
Child 075bc33f

Added by Patrick Plitzner almost 4 years ago

fix #6001 Clean up multiple sourceNotLinked() methods

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206Import.java
42 42
import eu.etaxonomy.cdm.model.common.CdmBase;
43 43
import eu.etaxonomy.cdm.model.common.DefinedTerm;
44 44
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
45
import eu.etaxonomy.cdm.model.common.ISourceable;
45 46
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
46 47
import eu.etaxonomy.cdm.model.common.Language;
47 48
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
......
830 831
        }
831 832
    }
832 833

  
834
<<<<<<< e12074844fd16df32275a14151d604461a15a064
833 835

  
834 836

  
835 837

  
836 838

  
837 839

  
840
=======
841
    /**
842
     * Look if the Institution does already exist
843
     * @param institutionCode: a string with the institutioncode
844
     * @param config : the configurator
845
     * @return the Institution (existing or new)
846
     */
847
    private Institution getInstitution(String institutionCode, Abcd206ImportState state) {
848
        Abcd206ImportConfigurator config = state.getConfig();
849
        Institution institution=null;
850
        List<Institution> institutions;
851
        try {
852
            institutions = getAgentService().list(Institution.class, null, null, null, null);
853
        } catch (Exception e) {
854
            institutions = new ArrayList<Institution>();
855
            logger.warn(e);
856
        }
857
        if (institutions.size() > 0 && config.isReUseExistingMetadata()) {
858
            for (Institution institut:institutions){
859
                try{
860
                    if (institut.getCode().equalsIgnoreCase(institutionCode)) {
861
                        institution=institut;
862
                    }
863
                }catch(Exception e){logger.warn("no institution code in the db");}
864
            }
865
        }
866
        if(DEBUG) {
867
            if(institution !=null) {
868
                logger.info("getinstitution " + institution.toString());
869
            }
870
        }
871
        if (institution == null){
872
            // create institution
873
            institution = Institution.NewInstance();
874
            institution.setCode(institutionCode);
875
            institution.setTitleCache(institutionCode, true);
876
        }
877
        save(institution, state);
878
        return institution;
879
    }
880

  
881
    /**
882
     * Look if the Collection does already exist
883
     * @param collectionCode
884
     * @param collectionCode: a string
885
     * @param config : the configurator
886
     * @return the Collection (existing or new)
887
     */
888
    private Collection getCollection(Institution institution, String collectionCode, Abcd206ImportState state) {
889
        Abcd206ImportConfigurator config = state.getConfig();
890
        Collection collection = null;
891
        List<Collection> collections;
892
        try {
893
            collections = getCollectionService().list(Collection.class, null, null, null, null);
894
        } catch (Exception e) {
895
            collections = new ArrayList<Collection>();
896
        }
897
        if (collections.size() > 0 && config.isReUseExistingMetadata()) {
898
            for (Collection coll:collections){
899
                if (coll.getCode() != null && coll.getInstitute() != null
900
                        && coll.getCode().equalsIgnoreCase(collectionCode) && coll.getInstitute().equals(institution)) {
901
                    collection = coll;
902
                    break;
903
                }
904
            }
905
        }
906

  
907
        if(collection == null){
908
            collection =Collection.NewInstance();
909
            collection.setCode(collectionCode);
910
            collection.setInstitute(institution);
911
            collection.setTitleCache(collectionCode);
912
        }
913
        save(collection, state);
914
        return collection;
915
    }
916

  
917

  
918
    /**
919
     * join DeterminationEvent to the Taxon Object
920
     * @param state : the ABCD import state
921
     * @param taxon: the current Taxon
922
     * @param preferredFlag :if the current name is preferred
923
     * @param derivedFacade : the derived Unit Facade
924
     */
925
    @SuppressWarnings("rawtypes")
926
    private void linkDeterminationEvent(Abcd206ImportState state, Taxon taxon, boolean preferredFlag,  DerivedUnitFacade derivedFacade) {
927
        Abcd206ImportConfigurator config = state.getConfig();
928
        if(DEBUG){
929
            logger.info("start linkdetermination with taxon:" + taxon.getUuid()+", "+taxon);
930
        }
931

  
932
        DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
933
        determinationEvent.setTaxonName(taxon.getName());
934
        determinationEvent.setPreferredFlag(preferredFlag);
935

  
936
        determinationEvent.setIdentifiedUnit(state.getDerivedUnitBase());
937
        state.getDerivedUnitBase().addDetermination(determinationEvent);
938

  
939
        if(DEBUG){
940
            logger.info("NB TYPES INFO: "+ state.getDataHolder().statusList.size());
941
        }
942
        for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus : state.getDataHolder().statusList) {
943
            if (specimenTypeDesignationstatus != null) {
944
                if(DEBUG){
945
                    logger.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus);
946
                }
947

  
948
                ICdmApplicationConfiguration cdmAppController = config.getCdmAppController();
949
                if(cdmAppController == null){
950
                    cdmAppController = this;
951
                }
952
                specimenTypeDesignationstatus = (SpecimenTypeDesignationStatus) cdmAppController.getTermService().find(specimenTypeDesignationstatus.getUuid());
953
                //Designation
954
                TaxonNameBase<?,?> name = taxon.getName();
955
                SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
956

  
957
                designation.setTypeStatus(specimenTypeDesignationstatus);
958
                designation.setTypeSpecimen(state.getDerivedUnitBase());
959
                name.addTypeDesignation(designation, true);
960
            }
961
        }
962

  
963
        for (String[] fullReference : state.getDataHolder().referenceList) {
964

  
965

  
966
            String strReference=fullReference[0];
967
            String citationDetail = fullReference[1];
968
            String citationURL = fullReference[2];
969
            List<Reference> references = getReferenceService().listByTitle(Reference.class, "strReference", MatchMode.EXACT, null, null, null, null, null);
970

  
971
            if (!references.isEmpty()){
972
                Reference reference = null;
973
                for (Reference refe: references) {
974
                    if (refe.getTitleCache().equalsIgnoreCase(strReference)) {
975
                        reference =refe;
976
                        break;
977
                    }
978
                }
979
                if (reference ==null){
980
                    reference = ReferenceFactory.newGeneric();
981
                    reference.setTitleCache(strReference, true);
982
                    save(reference, state);
983
                }
984
                determinationEvent.addReference(reference);
985
            }
986
        }
987
        save(state.getDerivedUnitBase(), state);
988

  
989
        if (config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag) {
990
            //do not add IndividualsAssociation to non-preferred taxa
991
            if(DEBUG){
992
                logger.info("isDoCreateIndividualsAssociations");
993
            }
994

  
995
            makeIndividualsAssociation(state, taxon, determinationEvent);
996

  
997
            save(state.getDerivedUnitBase(), state);
998
        }
999
    }
1000

  
1001
    /**
1002
     * create and link each association (specimen, observation..) to the accepted taxon
1003
     * @param state : the ABCD import state
1004
     * @param taxon: the current Taxon
1005
     * @param determinationEvent:the determinationevent
1006
     */
1007
    private void makeIndividualsAssociation(Abcd206ImportState state, Taxon taxon, DeterminationEvent determinationEvent) {
1008
        Abcd206ImportConfigurator config = state.getConfig();
1009
        SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
1010

  
1011
        if (DEBUG) {
1012
            logger.info("MAKE INDIVIDUALS ASSOCIATION");
1013
        }
1014

  
1015
        TaxonDescription taxonDescription = null;
1016
        Set<TaxonDescription> descriptions= taxon.getDescriptions();
1017
        if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1018
            if(!state.isDescriptionGroupSet()){
1019
                taxonDescription = sui.askForDescriptionGroup(descriptions);
1020
                state.setDescriptionGroup(taxonDescription);
1021
                state.setDescriptionGroupSet(true);
1022
            }else{
1023
                taxonDescription=state.getDescriptionGroup();
1024
            }
1025
        } else {
1026
            for (TaxonDescription description : descriptions){
1027
                Set<IdentifiableSource> sources =  new HashSet<>();
1028
                sources.addAll(description.getTaxon().getSources());
1029
                sources.addAll(description.getSources());
1030
                for (IdentifiableSource source:sources){
1031
                    if(state.getRef().equals(source.getCitation())) {
1032
                        taxonDescription = description;
1033
                    }
1034
                }
1035
            }
1036
        }
1037
        if (taxonDescription == null){
1038
            taxonDescription = TaxonDescription.NewInstance(taxon, false);
1039
            if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1040
                taxonDescription.addSource(OriginalSourceType.Import, null, null, state.getRef(), null);
1041
            }
1042
            state.setDescriptionGroup(taxonDescription);
1043
            taxon.addDescription(taxonDescription);
1044
        }
1045

  
1046
        //PREPARE REFERENCE QUESTIONS
1047

  
1048
        Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1049

  
1050
        List<IdentifiableSource> issTmp = new ArrayList<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1051
        List<DescriptionElementSource> issTmp2 = new ArrayList<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1052

  
1053
        Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
1054
        if(issTmp2!=null) {
1055
            osbSet.addAll(issTmp2);
1056
        }
1057
        if(issTmp!=null) {
1058
            osbSet.addAll(issTmp);
1059
        }
1060

  
1061

  
1062
        addToSourceMap(sourceMap, osbSet);
1063

  
1064
        if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1065
            List<OriginalSourceBase<?>> res = null;
1066
            if(!state.isDescriptionSourcesSet()){
1067
                res = sui.askForSource(sourceMap, "the description group ("+taxon+")",
1068
                        "The current reference is "+state.getRef().getTitleCache(),getReferenceService(), state.getDataHolder().docSources);
1069
                state.setDescriptionRefs(res);
1070
                state.setDescriptionSourcesSet(true);
1071
            }
1072
            else{
1073
                res=state.getDescriptionRefs();
1074
            }
1075
            if(res !=null) {
1076
                for (OriginalSourceBase<?> sour:res){
1077
                    if(sour.isInstanceOf(IdentifiableSource.class)){
1078
                        try {
1079
                            if(sourceNotLinkedToElement(taxonDescription,sour)) {
1080
                                taxonDescription.addSource((IdentifiableSource)sour.clone());
1081
                            }
1082
                        } catch (CloneNotSupportedException e) {
1083
                            logger.warn("no cloning?");
1084
                        }
1085
                    }else{
1086
                        if(sourceNotLinkedToElement(taxonDescription,sour)) {
1087
                            taxonDescription.addSource(OriginalSourceType.Import,null, null, sour.getCitation(),sour.getCitationMicroReference());
1088
                        }
1089
                    }
1090
                }
1091
            }
1092
        }
1093
        else {
1094
            if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1095
                taxonDescription.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1096
            }
1097
        }
1098
        state.setDescriptionGroup(taxonDescription);
1099

  
1100
        IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
1101
        Feature feature = makeFeature(state.getDerivedUnitBase());
1102
        indAssociation.setAssociatedSpecimenOrObservation(state.getDerivedUnitBase());
1103
        indAssociation.setFeature(feature);
1104

  
1105
        if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1106
            sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1107

  
1108
            issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
1109
            issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1110

  
1111
            osbSet = new HashSet<OriginalSourceBase>();
1112
            if(issTmp2!=null) {
1113
                osbSet.addAll(issTmp2);
1114
            }
1115
            if(issTmp!=null) {
1116
                osbSet.addAll(issTmp);
1117
            }
1118

  
1119

  
1120
            addToSourceMap(sourceMap, osbSet);
1121

  
1122
            List<OriginalSourceBase<?>> sources =null;
1123
            if(!state.isAssociationSourcesSet()) {
1124
                sources = sui.askForSource(sourceMap,  "descriptive element (association) ",taxon.toString(),
1125
                        getReferenceService(),state.getDataHolder().docSources);
1126
                state.setAssociationRefs(sources);
1127
                state.setAssociationSourcesSet(true);
1128
            }
1129
            else{
1130
                sources=state.getAssociationRefs();
1131
            }
1132
            if(sources !=null) {
1133
                for (OriginalSourceBase<?> source: sources) {
1134
                    if(source !=null) {
1135
                        if(source.isInstanceOf(DescriptionElementSource.class)){
1136
                            try {
1137
                                if(sourceNotLinkedToElement(indAssociation,source)) {
1138
                                    indAssociation.addSource((DescriptionElementSource)source.clone());
1139
                                }
1140
                            } catch (CloneNotSupportedException e) {
1141
                                logger.warn("clone forbidden?");
1142
                            }
1143
                        }else{
1144
                            if(sourceNotLinkedToElement(indAssociation,source)) {
1145
                                indAssociation.addSource(OriginalSourceType.Import,null, null, source.getCitation(),source.getCitationMicroReference());
1146
                            }
1147
                            try {
1148
                                if(sourceNotLinkedToElement(state.getDerivedUnitBase(), source)) {
1149
                                    state.getDerivedUnitBase().addSource((IdentifiableSource) source.clone());
1150
                                }
1151
                            } catch (CloneNotSupportedException e) {
1152
                                // TODO Auto-generated catch block
1153
                                e.printStackTrace();
1154
                            }
1155
                        }
1156

  
1157
                    }
1158
                }
1159
            }
1160
        }else {
1161
            if(sourceNotLinkedToElement(indAssociation,state.getRef(),null)) {
1162
                indAssociation.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1163
            }
1164
            if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
1165
                state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1166
            }
1167
            for (Reference citation : determinationEvent.getReferences()) {
1168
                if(sourceNotLinkedToElement(indAssociation,citation,null))
1169
                {
1170
                    indAssociation.addSource(DescriptionElementSource.NewInstance(OriginalSourceType.Import, null, null, citation, null));
1171
                }
1172
                if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
1173
                    state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1174
                }
1175
            }
1176
        }
1177

  
1178
        taxonDescription.addElement(indAssociation);
1179

  
1180
        save(taxonDescription, state);
1181
        save(taxon, state);
1182
        state.getReport().addDerivate(state.getDerivedUnitBase(), config);
1183
        state.getReport().addIndividualAssociation(taxon, state.getDataHolder().unitID, state.getDerivedUnitBase());
1184
    }
1185

  
1186
    private <T extends OriginalSourceBase<?>> boolean  sourceNotLinkedToElement(ISourceable<T> sourcable, Reference reference, String microReference) {
1187
        Set<T> linkedSources = sourcable.getSources();
1188
        for (T is:linkedSources){
1189
            Reference unitReference = is.getCitation();
1190
            String unitMicroReference = is.getCitationMicroReference();
1191

  
1192
            boolean refMatch=false;
1193
            boolean microMatch=false;
1194

  
1195
            try{
1196
                if (unitReference==null && reference==null) {
1197
                    refMatch=true;
1198
                }
1199
                if (unitReference!=null && reference!=null) {
1200
                    if (unitReference.getTitleCache().equalsIgnoreCase(reference.getTitleCache())) {
1201
                        refMatch=true;
1202
                    }
1203
                }
1204
            }catch(Exception e){}
1205

  
1206
            try{
1207
                if (unitMicroReference==null && microReference==null) {
1208
                    microMatch=true;
1209
                }
1210
                if(unitMicroReference!=null && microReference!=null) {
1211
                    if(unitMicroReference.equalsIgnoreCase(microReference)) {
1212
                        microMatch=true;
1213
                    }
1214
                }
1215
            }
1216
            catch(Exception e){}
1217

  
1218
            if (microMatch && refMatch) {
1219
                return false;
1220
            }
1221
        }
1222
        return true;
1223
    }
1224

  
1225
    private <T extends OriginalSourceBase<?>> boolean sourceNotLinkedToElement(ISourceable<T> sourcable, OriginalSourceBase<?> source) {
1226
        return sourceNotLinkedToElement(sourcable, source.getCitation(), source.getCitationMicroReference());
1227
    }
1228
>>>>>>> fix #6001 Clean up multiple sourceNotLinked() methods
838 1229

  
839 1230

  
840 1231

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)