Project

General

Profile

Revision 1f1f7ea1

ID1f1f7ea103177a09f739fe1808def0261f1ea424
Parent af51bfe5
Child cb29fec9

Added by Andreas Müller 6 months ago

ref #6581 , fix #9004 update script for TaxonNode.source and some fixes

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IdentifiableSource.java
121 121

  
122 122
	@Override
123 123
    public boolean isEmpty(){
124
	    //nothing to do
125
	    return super.isEmpty();
124
	    return this.isEmpty(false);
126 125
	}
127 126

  
127
	@Override
128
    public boolean isEmpty(boolean excludeType){
129
        //nothing to do
130
	    return super.isEmpty(excludeType);
131
	}
128 132
//*********************************** CLONE *********************************************************/
129 133

  
130 134

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/ReferencedEntityBase.java
111 111
    protected boolean isEmpty(){
112 112
       return super.isEmpty()
113 113
            && this.getCitation() == null
114
            && this.getCitationMicroReference() == null
115
            && isBlank(this.getOriginalNameString())
116
           ;
114
            && isBlank(this.getCitationMicroReference())
115
            && isBlank(this.getOriginalNameString());
117 116
    }
118 117

  
119 118
//****************** CLONE ************************************************/
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/DescriptionElementSource.java
168 168

  
169 169
    @Override
170 170
    public boolean isEmpty(){
171
       return super.isEmpty()
172
            && this.nameUsedInSource != null
173
           ;
171
       return this.isEmpty(false);
172
    }
173

  
174
    @Override
175
    public boolean isEmpty(boolean excludeType){
176
        return super.isEmpty(excludeType)
177
            && this.nameUsedInSource == null;
174 178
    }
175 179

  
176 180
//*********************************** CLONE *********************************************************/
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/reference/OriginalSourceBase.java
233 233

  
234 234
// **************** EMPTY ************************/
235 235

  
236
	@Override
236
    @Override
237 237
    public boolean isEmpty(){
238
        return isEmpty(false);
239
    }
240

  
241
    public boolean isEmpty(boolean excludeType){
238 242
	   return super.isEmpty()
239 243
	        && isBlank(this.getIdInSource())
240 244
	        && isBlank(this.getIdNamespace())
241 245
	        && this.links.isEmpty()
242
	        && this.cdmSource != null
243
	        && this.type == null
246
	        && this.cdmSource == null
247
	        && (excludeType || this.type == null)
244 248
           ;
245 249
	}
246 250

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNode.java
342 342
    }
343 343

  
344 344
    private void checkNullSource() {
345
        if (this.source != null && this.source.isEmpty()){
345
        if (this.source != null && this.source.isEmpty(true)){
346 346
            this.source = null;
347 347
        }
348 348
    }
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/SchemaUpdaterStepBase.java
101 101
		return langId;
102 102
	}
103 103

  
104
    /**
105
     * Returns the smallest next free id, if includeAudit is <code>true</code> the audit table is also considered in computation
106
     * @throws NumberFormatException
107
     * @throws SQLException
108
     */
109
    protected int getMaxId1(ICdmDataSource datasource, String tableName, boolean includeAudit, IProgressMonitor monitor, CaseType caseType,
110
            SchemaUpdateResult result) throws SQLException {
111
        String sql = "SELECT max(id) FROM " +caseType.transformTo(tableName);
112
        Integer maxId = Integer.valueOf(datasource.getSingleValue(sql).toString());
113
        if (maxId == null){
114
            maxId = 0;
115
        }
116
        Integer maxIdAud = -1;
117
        if(includeAudit){
118
            sql = "SELECT max(id) FROM " +caseType.transformTo(tableName + "_AUD");
119
            maxIdAud = Integer.valueOf(datasource.getSingleValue(sql).toString());
120
            if (maxIdAud == null){
121
                maxIdAud = 0;
122
            }
123
        }
124
        return Math.max(maxId, maxIdAud) + 1;
125
    }
126

  
104 127
	@Override
105 128
	public List<ISchemaUpdaterStep> getInnerSteps(){
106 129
		return new ArrayList<>();
......
110 133
	public boolean isIgnoreErrors() {
111 134
		return ignoreErrors;
112 135
	}
113

  
114

  
115 136
	@Override
116 137
	public void setIgnoreErrors(boolean ignoreErrors) {
117 138
		this.ignoreErrors = ignoreErrors;
......
127 148
		return DateTime.now().toString("YYYY-MM-dd HH:mm:ss");
128 149
	}
129 150

  
151
    protected String nullSafeParam(String param) {
152
        return param == null ? "NULL" : "'" + param + "'";
153
    }
154

  
130 155
	@Override
131 156
	public String toString(){
132 157
		if (StringUtils.isNotBlank(stepName)){
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v512_515/Reference2SourceMover.java
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.database.update.v512_515;
10

  
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.UUID;
16

  
17
import org.apache.log4j.Logger;
18

  
19
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
20
import eu.etaxonomy.cdm.database.ICdmDataSource;
21
import eu.etaxonomy.cdm.database.update.CaseType;
22
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
24
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
25

  
26
/**
27
 * @author a.mueller
28
 * @since 12.06.2020
29
 */
30
public class Reference2SourceMover  extends SchemaUpdaterStepBase {
31

  
32

  
33
    @SuppressWarnings("unused")
34
    private static final Logger logger = Logger.getLogger(Reference2SourceMover.class);
35

  
36
    private static final String stepName = "Move Reference 2 Source";
37

  
38
    private final String tableName;
39
    private final String citationsIdAttr;
40
    private final String detailAttr;
41
    private final String sourceAttr;
42

  
43
    public static final Reference2SourceMover NewInstance(List<ISchemaUpdaterStep> stepList, String tableName,
44
            String citationsIdAttr, String detailAttr, String sourceAttr){
45
        Reference2SourceMover result = new Reference2SourceMover(stepList, tableName, citationsIdAttr, detailAttr, sourceAttr);
46

  
47
        return result;
48
    }
49

  
50
    protected Reference2SourceMover(List<ISchemaUpdaterStep> stepList, String tableName,
51
            String citationsIdAttr, String detailAttr, String sourceAttr) {
52
        super(stepList, stepName);
53
        this.tableName = tableName;
54
        this.citationsIdAttr = citationsIdAttr;
55
        this.detailAttr = detailAttr;
56
        this.sourceAttr = sourceAttr;
57
    }
58

  
59
    @Override
60
    public List<ISchemaUpdaterStep> getInnerSteps() {
61
        List<ISchemaUpdaterStep> result = new ArrayList<>();
62

  
63
        return result;
64
    }
65

  
66
    @Override
67
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
68
            SchemaUpdateResult result) throws SQLException {
69

  
70
        boolean includeAudit = true;
71
        int id = getMaxId1(datasource, "OriginalSourceBase", includeAudit, monitor, caseType, result);
72

  
73
        String sql = "SELECT * "
74
                + " FROM "+caseType.transformTo(tableName)+" t "
75
                + " WHERE t."+this.citationsIdAttr+" IS NOT NULL OR t."+this.detailAttr+" IS NOT NULL ";
76
        System.out.println(sql);
77

  
78
        ResultSet rs = datasource.executeQuery(sql);
79
        while(rs.next()){
80
            int tnId = rs.getInt("id");
81
            Integer citationId = nullSafeInt(rs, citationsIdAttr);
82
            Integer createdById = nullSafeInt(rs, "createdBy_id");
83
            String detail = rs.getString(detailAttr);
84

  
85
            sql = "INSERT INTO @@OriginalSourceBase@@ (DTYPE, sourceType, uuid, id, citation_id, citationMicroReference, createdBy_id, created)"
86
               + " VALUES ('IdentifiableSource', 'PTS','"+UUID.randomUUID()+"'," + id + ", " + citationId + "," + nullSafeParam(detail) + "," + createdById + ",'" + this.getNowString() + "')";
87
            System.out.println(sql);
88
            datasource.executeUpdate(caseType.replaceTableNames(sql));
89

  
90
            sql = "UPDATE " + caseType.transformTo(tableName)
91
                    + " SET " + sourceAttr + " = " + id
92
                    + " WHERE id = " + tnId;
93
            id++;
94

  
95
            System.out.println(sql);
96

  
97
            datasource.executeUpdate(caseType.replaceTableNames(sql));
98

  
99
        }
100
    }
101

  
102
    protected Integer nullSafeInt(ResultSet rs, String columnName) throws SQLException {
103
        Object intObject = rs.getObject(columnName);
104
        if (intObject == null){
105
            return null;
106
        }else{
107
            return Integer.valueOf(intObject.toString());
108
        }
109
    }
110

  
111

  
112
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v512_515/SchemaUpdater_5151_5152.java
69 69
        referencedTable = "OriginalSourceBase";
70 70
        ColumnAdder.NewIntegerInstance(stepList, stepName, tableName, newColumnName, INCLUDE_AUDIT, !NOT_NULL, referencedTable);
71 71

  
72
        Reference2SourceMover.NewInstance(stepList, tableName, "referenceForParentChildRelation_id", "microReferenceForParentChildRelation", "source_id");
73

  
72 74
        //6581
73 75
        stepName = "Add source to Classification";
74 76
        tableName = "Classification";

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)