Project

General

Profile

« Previous | Next » 

Revision 7319c528

Added by Patrick Plitzner over 6 years ago

Refactor name-author mapping

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportAuthor.java
13 13
import java.sql.SQLException;
14 14
import java.util.HashMap;
15 15
import java.util.Map;
16
import java.util.UUID;
17 16

  
18 17
import org.apache.log4j.Logger;
19 18
import org.springframework.stereotype.Component;
20 19

  
21 20
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23 21
import eu.etaxonomy.cdm.io.common.DbImportBase;
24 22
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25 23
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
......
64 62

  
65 63
    @Override
66 64
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
67
        super.doInvoke(state);
65
        makeAuthors(state, "AUTOR_KOMB");
66
        makeAuthors(state, "AUTOR_BASI");
68 67
    }
69 68

  
70 69

  
71 70
    @Override
72 71
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
73
        ResultSet rs = partitioner.getResultSet();
74
        Map<String, TeamOrPersonBase> teamsOrPersonToSave = new HashMap<String, TeamOrPersonBase>();
75
        try {
76
            while (rs.next()){
77
                makeSingleAuthor(state, rs, teamsOrPersonToSave);
78

  
79
            }
80
        } catch (SQLException e) {
81
            e.printStackTrace();
82
        }
83

  
84
//        getAgentService().saveOrUpdate(teamsOrPersonToSave.values());
85 72
        return true;
86 73
    }
87 74

  
88
    private void makeSingleAuthor(RedListGefaesspflanzenImportState state, ResultSet rs, Map<String, TeamOrPersonBase> teamsOrPersonToSave)
89
            throws SQLException {
90
        long id = rs.getLong("NAMNR");
91
        String authorName = rs.getString("AUTOR");
92
        String authorBasiName = rs.getString("AUTOR_BASI");
93
        String authorKombName = rs.getString("AUTOR_KOMB");
94

  
95
        //check null values
96
        if(CdmUtils.isBlank(authorName) && CdmUtils.isBlank(authorBasiName) && CdmUtils.isBlank(authorKombName)){
97
            logger.info("NAMNR "+id+": No author found!");
98
            return;
99
        }
100
        TeamOrPersonBase authorKomb = null;
101
        TeamOrPersonBase authorBasi = null;
102

  
103
        authorKomb = importPerson(state, teamsOrPersonToSave, id, authorKombName);
104
        if(authorKomb!=null){
105
            state.getAuthorKombMap().put(id, authorKomb.getUuid());
106
        }
107
        authorBasi = importPerson(state, teamsOrPersonToSave, id, authorBasiName);
108
        if(authorKomb!=null){
109
            state.getAuthorBasiMap().put(id, authorBasi.getUuid());
110
        }
111

  
112
        //check if missapplied name
113
        if(authorName.equals("auct.")){
114

  
115
        }
116
        //check if pro parte synonym
117
        else if(authorName.equals("p .p.")){
118

  
119
        }
120
        else if(authorBasi==null && authorKomb==null){
121
            logger.warn("NAMNR "+id+": Author not atomised in authorKomb and authorBasi. Author: "+authorName);
122
            TeamOrPersonBase team = importPerson(state, teamsOrPersonToSave, id, authorName);
123
            state.getAuthorKombMap().put(id, team.getUuid());
124
        }
125
//        //check author column consistency
126
//        String authorCheckString = "";
127
//        if(!CdmUtils.isBlank(authorKombName)){
128
//            authorCheckString = "("+authorBasiName+")"+" "+authorKombName;
129
//        }
130
//        else{
131
//            authorCheckString = authorBasiName;
132
//        }
133
//        boolean isAuthorStringCorrect = false;
134
//        if(authorName.startsWith(authorCheckString)){
135
//            isAuthorStringCorrect = true;
136
//        }
137
//        if(!isAuthorStringCorrect){
138
//            String errorString = "NAMNR "+id+": Author string not consistent! Is \""+authorName+"\" Should start with \""+authorCheckString+"\"";
139
//            logger.error(errorString);
140
//        }
141

  
142
    }
143

  
144
    private TeamOrPersonBase importPerson(RedListGefaesspflanzenImportState state, Map<String, TeamOrPersonBase> teamsOrPersonToSave,
145
            long id, String agentName) {
146
        TeamOrPersonBase agent = null;
147
        //check if agent already exists
148
        TeamOrPersonBase notYetPersistedAgent = teamsOrPersonToSave.get(agentName);
149
        UUID existingAgentUuid = state.getAuthorKombMap().get(agentName);
150
        if(notYetPersistedAgent!=null){
151
            agent = notYetPersistedAgent;
152
        }
153
        else if(existingAgentUuid!=null){
154
            agent = HibernateProxyHelper.deproxy(getAgentService().load(existingAgentUuid), TeamOrPersonBase.class);
155
        }
156
        else if(!CdmUtils.isBlank(agentName)){
157
            //check if it is a team
158
            if(agentName.contains("&")){
159
                agent = Team.NewInstance();
160
                String[] split = agentName.split("&");
161
                for (int i = 0; i < split.length; i++) {
162
                    ((Team) agent).addTeamMember(Person.NewTitledInstance(split[i].trim()));
75
    private void makeAuthors(RedListGefaesspflanzenImportState state, String columnName) {
76

  
77
        //--- combination authors ---
78
        String query = "select distinct "+columnName+" from V_TAXATLAS_D20_EXPORT t"
79
                + " WHERE TRIM(t."+columnName+") <>''";
80

  
81
        ResultSet rs = state.getConfig().getSource().getResultSet(query);
82

  
83
        try{
84
            while(rs.next()){
85
                String authorName = rs.getString(columnName);
86
                TeamOrPersonBase teamOrPerson = null;
87
                if(!CdmUtils.isBlank(authorName)){
88
                    //check if it is a team
89
                    if(authorName.contains("&")){
90
                        teamOrPerson = Team.NewInstance();
91
                        String[] split = authorName.split("&");
92
                        for (int i = 0; i < split.length; i++) {
93
                            ((Team) teamOrPerson).addTeamMember(Person.NewTitledInstance(split[i].trim()));
94
                        }
95
                    }
96
                    else{
97
                        teamOrPerson = Person.NewTitledInstance(authorName);
98
                    }
99
                    getAgentService().saveOrUpdate(teamOrPerson);
100
                    state.getAuthorMap().put(authorName, teamOrPerson.getUuid());
163 101
                }
164 102
            }
165
            else{
166
                agent = Person.NewTitledInstance(agentName);
167
            }
168
            teamsOrPersonToSave.put(agentName, agent);
169
//            ImportHelper.setOriginalSource(agent, state.getTransactionalSourceReference(), id, namespace);
103
        } catch (SQLException e) {
104
            e.printStackTrace();
170 105
        }
171
        return agent;
172 106
    }
173 107

  
174 108
    @Override
app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java
36 36
    @Override
37 37
    protected void makeIoClassList() {
38 38
        ioClassList = new Class[]{
39
//                RedListGefaesspflanzenImportAuthor.class,
39
                RedListGefaesspflanzenImportAuthor.class,
40 40
                RedListGefaesspflanzenImportNames.class,
41 41
//                RedListGefaesspflanzenImportTaxa.class,
42 42
        };
app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java
20 20
import org.springframework.stereotype.Component;
21 21

  
22 22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23 24
import eu.etaxonomy.cdm.io.common.DbImportBase;
24 25
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25 26
import eu.etaxonomy.cdm.io.common.ImportHelper;
26 27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
27 28
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
29 30
import eu.etaxonomy.cdm.model.common.CdmBase;
30 31
import eu.etaxonomy.cdm.model.name.BotanicalName;
31 32
import eu.etaxonomy.cdm.model.name.Rank;
......
47 48

  
48 49
    private static final String pluralString = "names";
49 50

  
50
    private static final String TAXON_NAMESPACE = "name";
51
    private static final String AUTHOR_KOMB_NAMESPACE = "author_komb";
52
    private static final String AUTHOR_BASI_NAMESPACE = "author_basi";
51
    private static final String NAME_NAMESPACE = "name";
53 52

  
54 53
    public RedListGefaesspflanzenImportNames() {
55 54
        super(tableName, pluralString);
......
80 79
    @Override
81 80
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
82 81
        ResultSet rs = partitioner.getResultSet();
83
        Set<TaxonNameBase> taxaToSave = new HashSet<TaxonNameBase>();
82
        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
84 83
        try {
85 84
            while (rs.next()){
86
                makeSingleName(state, rs, taxaToSave);
85
                makeSingleName(state, rs, namesToSave);
87 86

  
88 87
            }
89 88
        } catch (SQLException e) {
90 89
            e.printStackTrace();
91 90
        }
92 91

  
93
        getNameService().saveOrUpdate(taxaToSave);
92
        getNameService().saveOrUpdate(namesToSave);
94 93
        return true;
95 94
    }
96 95

  
......
104 103
        String ep3String = rs.getString("EPI3");
105 104
        String nomZusatzString = rs.getString("NOM_ZUSATZ");
106 105
        String zusatzString = rs.getString("ZUSATZ");
106
        String authorKombString = rs.getString("AUTOR_KOMB");
107
        String authorBasiString = rs.getString("AUTOR_BASI");
107 108

  
108 109
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
109 110
            logger.error("NAMNR: "+id+" No name found!");
......
120 121
        }
121 122

  
122 123
        //add author
123
        AgentBase authorKomb = getAgentService().load(state.getAuthorKombMap().get(id));
124
//        name.setCombinationAuthorship(authorKomb);
124
        TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
125
        name.setCombinationAuthorship(authorKomb);
125 126

  
126 127
        //id
127
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
128
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, NAME_NAMESPACE);
129

  
130
        namesToSave.add(name);
128 131
    }
129 132

  
130 133
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportState.java
28 28
	@SuppressWarnings("unused")
29 29
	private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportState.class);
30 30

  
31
	private Map<Long, UUID> authorKombMap = new HashMap<Long, UUID>();
32
	private Map<Long, UUID> authorBasiMap = new HashMap<Long, UUID>();
31
	private final Map<String, UUID> authorMap = new HashMap<String, UUID>();
33 32

  
34 33
    protected RedListGefaesspflanzenImportState(RedListGefaesspflanzenImportConfigurator config) {
35 34
        super(config);
36 35
    }
37 36

  
38
    public Map<Long, UUID> getAuthorKombMap() {
39
        return authorKombMap;
40
    }
41

  
42
    public Map<Long, UUID> getAuthorBasiMap() {
43
        return authorBasiMap;
37
    public Map<String, UUID> getAuthorMap() {
38
        return authorMap;
44 39
    }
45 40

  
46 41
}

Also available in: Unified diff