2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.database
.update
.v523_525
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
17 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
18 import eu
.etaxonomy
.cdm
.database
.update
.CaseType
;
19 import eu
.etaxonomy
.cdm
.database
.update
.ISchemaUpdaterStep
;
20 import eu
.etaxonomy
.cdm
.database
.update
.SchemaUpdateResult
;
21 import eu
.etaxonomy
.cdm
.database
.update
.SchemaUpdaterStepBase
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
24 import eu
.etaxonomy
.cdm
.strategy
.cache
.agent
.PersonDefaultCacheStrategy
;
25 import eu
.etaxonomy
.cdm
.strategy
.cache
.agent
.TeamDefaultCacheStrategy
;
32 public class CollectorTitleUpdater
extends SchemaUpdaterStepBase
{
34 private static final String step
= "Update collector title";
36 public static CollectorTitleUpdater
NewInstance (List
<ISchemaUpdaterStep
> stepList
){
37 return new CollectorTitleUpdater(stepList
);
40 protected CollectorTitleUpdater(List
<ISchemaUpdaterStep
> stepList
) {
41 super(stepList
, step
);
44 public List
<ISchemaUpdaterStep
> getInnerSteps() {
45 List
<ISchemaUpdaterStep
> result
= new ArrayList
<>();
51 public void invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
,
52 SchemaUpdateResult result
) throws SQLException
{
54 //set collectorCache = titleCache for all persons and teams
55 String sql
= "UPDATE @@AgentBase@@ SET collectorTitleCache = titleCache WHERE DTYPE = 'Person' OR DTYPE = 'Team'";
56 datasource
.executeUpdate(caseType
.replaceTableNames(sql
));
58 //to remove changes previously made in SchemaUpdater_5230_5250 before handlePerson() was adapted
59 sql
= "UPDATE @@AgentBase@@ SET collectorTitle = NULL WHERE DTYPE = 'Person'";
60 datasource
.executeUpdate(caseType
.replaceTableNames(sql
));
62 //for teams being part of gathering event
63 sql
= " SELECT ab.* FROM @@AgentBase@@ ab WHERE id IN (SELECT actor_id FROM @@GatheringEvent@@) ";
64 ResultSet rs
= datasource
.executeQuery(caseType
.replaceTableNames(sql
));
66 String dtype
= rs
.getString("DTYPE");
67 int id
= rs
.getInt("id");
68 if ("Team".equalsIgnoreCase(dtype
)){
69 boolean protectedTitleCache
= rs
.getBoolean("protectedTitleCache");
70 if (protectedTitleCache
){
71 //set protectedCollector = true if protectedTitleCache = true
72 sql
= "UPDATE @@AgentBase@@ SET protectedCollectorTitleCache = "+getBoolean(true, datasource
)+" WHERE id = " + id
;
73 datasource
.executeUpdate(caseType
.replaceTableNames(sql
));
75 //for each team member handle like persons below
76 sql
= "SELECT p.* FROM @@AgentBase_AgentBase@@ MN INNER JOIN @@AgentBase@@ p ON p.id = MN.teamMembers_id WHERE MN.team_ID = " + id
+ " ORDER BY sortIndex ";
77 ResultSet rs2
= datasource
.executeQuery(caseType
.replaceTableNames(sql
));
78 Team team
= Team
.NewInstance();
80 Person member
= handlePerson(rs2
, datasource
, caseType
);
81 team
.addTeamMember(member
);
84 String collectorTitleCache
= TeamDefaultCacheStrategy
.INSTANCE().getCollectorTitleCache(team
);
85 sql
= " UPDATE @@AgentBase@@ SET collectorTitleCache = '" + collectorTitleCache
+ "' WHERE id = " + id
;
86 datasource
.executeUpdate(caseType
.replaceTableNames(sql
));
88 }else if ("Person".equalsIgnoreCase(dtype
)){
89 //for each person in gathering event
90 handlePerson(rs
, datasource
, caseType
);
95 private Person
handlePerson(ResultSet rs
, ICdmDataSource datasource
, CaseType caseType
) throws SQLException
{
97 int id
= rs
.getInt("id");
98 String familyName
= rs
.getString("familyName");
99 String initials
= rs
.getString("initials");
100 String givenName
= rs
.getString("givenName");
101 String titleCache
= rs
.getString("titleCache");
102 String nomenclaturalTitle
= rs
.getString("nomenclaturalTitle");
103 boolean protectedTitleCache
= rs
.getBoolean("protectedTitleCache");
105 Person person
= Person
.NewInstance(nomenclaturalTitle
, familyName
, initials
, givenName
);
106 person
.setTitleCache(titleCache
, protectedTitleCache
);
107 String collectorTitle
= PersonDefaultCacheStrategy
.INSTANCE().getCollectorTitleCache(person
);
109 String sql
= "UPDATE @@AgentBase@@ SET collectorTitleCache = '"+collectorTitle
+"', collectorTitle = '"+collectorTitle
+"' WHERE id = " + id
;
110 datasource
.executeUpdate(caseType
.replaceTableNames(sql
));