Project

General

Profile

« Previous | Next » 

Revision 39076

reformatted code

View differences:

PublicationDAO.java
1 1
package eu.dnetlib.goldoa.service.dao;
2 2

  
3
import eu.dnetlib.goldoa.domain.*;
3
import eu.dnetlib.goldoa.domain.Affiliation;
4
import eu.dnetlib.goldoa.domain.Journal;
5
import eu.dnetlib.goldoa.domain.Organization;
6
import eu.dnetlib.goldoa.domain.Person;
7
import eu.dnetlib.goldoa.domain.Publication;
8
import eu.dnetlib.goldoa.domain.PublicationIdentifier;
9
import eu.dnetlib.goldoa.domain.Publisher;
4 10
import eu.dnetlib.goldoa.service.PersonManagerImpl;
5 11
import org.springframework.beans.factory.annotation.Autowired;
6 12
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
......
8 14
import org.springframework.jdbc.core.RowMapper;
9 15

  
10 16
import javax.sql.DataSource;
11
import java.sql.*;
17
import java.sql.PreparedStatement;
18
import java.sql.ResultSet;
19
import java.sql.SQLException;
20
import java.sql.Types;
12 21
import java.util.ArrayList;
13 22
import java.util.List;
14 23

  
15 24
public class PublicationDAO {
16 25

  
17
    @Autowired
18
    private DataSource dataSource;
26
	@Autowired
27
	private DataSource dataSource;
19 28

  
20
    private static final String UPDATE_PUBLICATION = "update publication set title=?, languages=?, subjects=?, doi=?, source=?, date=?, type=?, journal=?, publisher=?, repository=?, acceptancedate=? where id=?";
29
	private static final String UPDATE_PUBLICATION = "update publication set title=?, languages=?, subjects=?, doi=?, source=?, date=?, type=?, journal=?, publisher=?, repository=?, acceptancedate=? where id=?";
21 30

  
22
    private static final String INSERT_PUBLICATION = "insert into publication (title, languages, subjects, doi, source, date, type, journal, publisher, repository, acceptancedate, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
31
	private static final String INSERT_PUBLICATION = "insert into publication (title, languages, subjects, doi, source, date, type, journal, publisher, repository, acceptancedate, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
23 32

  
24
    private static final String GET_PUBLICATION =
25
            "select p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher, repository, acceptancedate, array_agg(distinct pa.affiliation) as authors, array_agg(distinct i.type || '||' || i.identifier) as identifiers\n" +
26
            "from publication p\n" +
27
            "left join publication_affiliation pa on pa.publication=p.id\n" +
28
            "left join publication_identifier i on i.publication = p.id\n" +
29
            "where p.id=?\n" +
30
            "group by p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher, repository, acceptancedate;";
33
	private static final String GET_PUBLICATION =
34
			"select p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher, repository, acceptancedate, array_agg(distinct pa.affiliation) as authors, array_agg(distinct i.type || '||' || i.identifier) as identifiers\n" +
35
					"from publication p\n" +
36
					"left join publication_affiliation pa on pa.publication=p.id\n" +
37
					"left join publication_identifier i on i.publication = p.id\n" +
38
					"where p.id=?\n" +
39
					"group by p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher, repository, acceptancedate;";
31 40

  
32
    private static final String INSERT_PUBLICATION_AFFILIATIONS =
33
            "with aff as (insert into affiliation (id, person, organization, startdate, enddate, department) values (?, ?, ?, ?, ?, ?) returning id as affid) insert into publication_affiliation (publication, affiliation) select ?, affid from aff";
41
	private static final String INSERT_PUBLICATION_AFFILIATIONS =
42
			"with aff as (insert into affiliation (id, person, organization, startdate, enddate, department) values (?, ?, ?, ?, ?, ?) returning id as affid) insert into publication_affiliation (publication, affiliation) select ?, affid from aff";
34 43

  
35
    private static final String UPDATE_PUBLICATION_AFFILIATIONS =
36
            "with aff as (select id from affiliation where person=? and organization=?) insert into publication_affiliation (publication, affiliation) select ?, aff.id from aff";
44
	private static final String UPDATE_PUBLICATION_AFFILIATIONS =
45
			"with aff as (select id from affiliation where person=? and organization=?) insert into publication_affiliation (publication, affiliation) select ?, aff.id from aff";
37 46

  
38
    private static final String LOAD_AFFILIATIONS = "select a.id, startdate, enddate, department, p.id || '||' || p.email || '||' || p.firstname || '||' || p.lastname || '||' || case when p.initials is null then '' else p.initials end as person, o.id || '||' || o.name as organisation\n" +
39
            "from affiliation a\n" +
40
            "left join person p on a.person=p.id\n" +
41
            "left join organisation o on o.id=a.organization\n" +
42
            "join publication_affiliation pa on pa.affiliation=a.id and pa.publication=?\n";
47
	private static final String LOAD_AFFILIATIONS = "select a.id, startdate, enddate, department, p.id || '||' || p.email || '||' || p.firstname || '||' || p.lastname || '||' || case when p.initials is null then '' else p.initials end as person, o.id || '||' || o.name as organisation\n" +
48
			"from affiliation a\n" +
49
			"left join person p on a.person=p.id\n" +
50
			"left join organisation o on o.id=a.organization\n" +
51
			"join publication_affiliation pa on pa.affiliation=a.id and pa.publication=?\n";
43 52

  
44
    public Publication getPublication(final String publicationId) {
45
        return new JdbcTemplate(dataSource).queryForObject(GET_PUBLICATION, new String[]{publicationId}, new int[]{Types.VARCHAR}, new RowMapper<Publication>() {
46
            @Override
47
            public Publication mapRow(ResultSet rs, int i) throws SQLException {
53
	public Publication getPublication(final String publicationId) {
54
		return new JdbcTemplate(dataSource).queryForObject(GET_PUBLICATION, new String[]{publicationId}, new int[]{Types.VARCHAR}, new RowMapper<Publication>() {
55
			@Override
56
			public Publication mapRow(ResultSet rs, int i) throws SQLException {
48 57

  
49
                Publication publication = new Publication();
58
				Publication publication = new Publication();
50 59

  
51
                publication.setId(rs.getString("id"));
52
                publication.setTitle(rs.getString("title"));
53
                publication.setLanguages(rs.getString("languages"));
54
                publication.setSubjects(rs.getString("subjects"));
55
                publication.setDoi(rs.getString("doi"));
56
                publication.setSource(rs.getString("source"));
57
                publication.setPublicationDate(rs.getDate("date"));
58
                publication.setAcceptanceDate(rs.getDate("acceptancedate"));
59
                publication.setType(Publication.Type.valueOf(rs.getString("type")));
60
                publication.setJournal(new Journal(rs.getString("journal")));
61
                publication.setPublisher(new Publisher(rs.getString("publisher")));
62
                publication.setRepository(rs.getString("repository"));
60
				publication.setId(rs.getString("id"));
61
				publication.setTitle(rs.getString("title"));
62
				publication.setLanguages(rs.getString("languages"));
63
				publication.setSubjects(rs.getString("subjects"));
64
				publication.setDoi(rs.getString("doi"));
65
				publication.setSource(rs.getString("source"));
66
				publication.setPublicationDate(rs.getDate("date"));
67
				publication.setAcceptanceDate(rs.getDate("acceptancedate"));
68
				publication.setType(Publication.Type.valueOf(rs.getString("type")));
69
				publication.setJournal(new Journal(rs.getString("journal")));
70
				publication.setPublisher(new Publisher(rs.getString("publisher")));
71
				publication.setRepository(rs.getString("repository"));
63 72

  
64
                publication.setAuthors(new ArrayList<Affiliation>());
65
                for (int aff: (Integer[]) rs.getArray("authors").getArray())
66
                    publication.getAuthors().add(new Affiliation(aff));
73
				publication.setAuthors(new ArrayList<Affiliation>());
74
				for (int aff : (Integer[]) rs.getArray("authors").getArray())
75
					publication.getAuthors().add(new Affiliation(aff));
67 76

  
68
                publication.setIdentifiers(new ArrayList<PublicationIdentifier>());
69
                for (String s: (String[]) rs.getArray("identifiers").getArray()) {
70
                    if (s != null) {
71
                        String[] parts = s.split("\\|\\|");
77
				publication.setIdentifiers(new ArrayList<PublicationIdentifier>());
78
				for (String s : (String[]) rs.getArray("identifiers").getArray()) {
79
					if (s != null) {
80
						String[] parts = s.split("\\|\\|");
72 81

  
73
                        publication.getIdentifiers().add(new PublicationIdentifier(parts[0], parts[1]));
74
                    }
75
                }
82
						publication.getIdentifiers().add(new PublicationIdentifier(parts[0], parts[1]));
83
					}
84
				}
76 85

  
77
                return publication;
78
            }
79
        });
80
    }
86
				return publication;
87
			}
88
		});
89
	}
81 90

  
82
    public Publication savePublication(Publication publication) {
83
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
84
        Object[] args = {publication.getTitle(), publication.getLanguages(), publication.getSubjects(), publication.getDoi(), publication.getSource(), publication.getPublicationDate(), publication.getType().toString(), publication.getJournal()!=null?publication.getJournal().getId():null, publication.getPublisher()!=null?publication.getPublisher().getId():null, publication.getRepository(), publication.getAcceptanceDate(), publication.getId()};
85
        int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR};
91
	public Publication savePublication(Publication publication) {
92
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
93
		Object[] args = {publication.getTitle(), publication.getLanguages(), publication.getSubjects(), publication.getDoi(), publication.getSource(), publication.getPublicationDate(), publication.getType().toString(), publication.getJournal() != null ? publication.getJournal().getId() : null, publication.getPublisher() != null ? publication.getPublisher().getId() : null, publication.getRepository(), publication.getAcceptanceDate(), publication.getId()};
94
		int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR};
86 95

  
87
        if (jdbcTemplate.update(UPDATE_PUBLICATION, args, types) == 0) {
88
            jdbcTemplate.update(INSERT_PUBLICATION, args, types);
89
        }
96
		if (jdbcTemplate.update(UPDATE_PUBLICATION, args, types) == 0) {
97
			jdbcTemplate.update(INSERT_PUBLICATION, args, types);
98
		}
90 99

  
91
        return publication;
92
    }
100
		return publication;
101
	}
93 102

  
94
    public void insertIdentifiers(final Publication publication) {
103
	public void insertIdentifiers(final Publication publication) {
95 104

  
96
        new JdbcTemplate(dataSource).batchUpdate("insert into publication_identifier (publication, type, identifier) values (?, ?, ?)", new BatchPreparedStatementSetter() {
97
            @Override
98
            public void setValues(PreparedStatement ps, int i) throws SQLException {
99
                PublicationIdentifier identifier = publication.getIdentifiers().get(i);
105
		new JdbcTemplate(dataSource).batchUpdate("insert into publication_identifier (publication, type, identifier) values (?, ?, ?)", new BatchPreparedStatementSetter() {
106
			@Override
107
			public void setValues(PreparedStatement ps, int i) throws SQLException {
108
				PublicationIdentifier identifier = publication.getIdentifiers().get(i);
100 109

  
101
                ps.setString(1, publication.getId());
102
                ps.setString(2, identifier.getType());
103
                ps.setString(3, identifier.getValue());
104
            }
110
				ps.setString(1, publication.getId());
111
				ps.setString(2, identifier.getType());
112
				ps.setString(3, identifier.getValue());
113
			}
105 114

  
106
            @Override
107
            public int getBatchSize() {
108
                return publication.getIdentifiers()!=null?publication.getIdentifiers().size():0;
109
            }
110
        });
111
    }
115
			@Override
116
			public int getBatchSize() {
117
				return publication.getIdentifiers() != null ? publication.getIdentifiers().size() : 0;
118
			}
119
		});
120
	}
112 121

  
113
    public void deleteAffiliations(Publication publication) {
114
        new JdbcTemplate(dataSource).update("delete from publication_affiliation where publication=?", new String[] {publication.getId()}, new int[] {Types.VARCHAR});
115
    }
122
	public void deleteAffiliations(Publication publication) {
123
		new JdbcTemplate(dataSource).update("delete from publication_affiliation where publication=?", new String[]{publication.getId()}, new int[]{Types.VARCHAR});
124
	}
116 125

  
117
    public void deleteIdentifiers(Publication publication) {
118
        new JdbcTemplate(dataSource).update("delete from publication_identifier where publication=?", new String[] {publication.getId()}, new int[] {Types.VARCHAR});
119
    }
126
	public void deleteIdentifiers(Publication publication) {
127
		new JdbcTemplate(dataSource).update("delete from publication_identifier where publication=?", new String[]{publication.getId()}, new int[]{Types.VARCHAR});
128
	}
120 129

  
121
    public void saveAffiliations(final Publication publication) {
122
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
130
	public void saveAffiliations(final Publication publication) {
131
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
123 132

  
124
        if (publication.getAuthors() != null) {
125
            for (Affiliation affiliation:publication.getAuthors()) {
126
                if (jdbcTemplate.update(UPDATE_PUBLICATION_AFFILIATIONS, new String[] {affiliation.getPerson().getId(), affiliation.getOrganization() != null? affiliation.getOrganization().getId(): null, publication.getId()}, new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}) == 0) {
127
                    jdbcTemplate.update(INSERT_PUBLICATION_AFFILIATIONS,
128
                            new Object[] {PersonManagerImpl.generateId(affiliation), affiliation.getPerson().getId(), affiliation.getOrganization() != null?affiliation.getOrganization().getId():null, affiliation.getStart(), affiliation.getEnd(), affiliation.getDepartment(), publication.getId()},
129
                            new int[] {Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
130
                }
131
            }
132
        }
133
    }
133
		if (publication.getAuthors() != null) {
134
			for (Affiliation affiliation : publication.getAuthors()) {
135
				if (jdbcTemplate.update(UPDATE_PUBLICATION_AFFILIATIONS, new String[]{affiliation.getPerson().getId(), affiliation.getOrganization() != null ? affiliation.getOrganization().getId() : null, publication.getId()}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}) == 0) {
136
					jdbcTemplate.update(INSERT_PUBLICATION_AFFILIATIONS,
137
							new Object[]{PersonManagerImpl.generateId(affiliation), affiliation.getPerson().getId(), affiliation.getOrganization() != null ? affiliation.getOrganization().getId() : null, affiliation.getStart(), affiliation.getEnd(), affiliation.getDepartment(), publication.getId()},
138
							new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
139
				}
140
			}
141
		}
142
	}
134 143

  
135
    public void loadAffiliations(Publication publication) {
136
        List<Affiliation> affiliations = new JdbcTemplate(dataSource).query(LOAD_AFFILIATIONS, new String[]{publication.getId()}, new int[]{Types.VARCHAR}, new RowMapper<Affiliation>() {
137
            @Override
138
            public Affiliation mapRow(ResultSet rs, int rowNum) throws SQLException {
139
                Affiliation aff = new Affiliation();
144
	public void loadAffiliations(Publication publication) {
145
		List<Affiliation> affiliations = new JdbcTemplate(dataSource).query(LOAD_AFFILIATIONS, new String[]{publication.getId()}, new int[]{Types.VARCHAR}, new RowMapper<Affiliation>() {
146
			@Override
147
			public Affiliation mapRow(ResultSet rs, int rowNum) throws SQLException {
148
				Affiliation aff = new Affiliation();
140 149

  
141
                aff.setId(rs.getInt("id"));
142
                aff.setDepartment(rs.getString("department"));
143
                aff.setStart(rs.getDate("startdate"));
144
                aff.setEnd(rs.getDate("enddate"));
150
				aff.setId(rs.getInt("id"));
151
				aff.setDepartment(rs.getString("department"));
152
				aff.setStart(rs.getDate("startdate"));
153
				aff.setEnd(rs.getDate("enddate"));
145 154

  
146
                aff.setPerson(this.parsePerson(rs.getString("person")));
147
                aff.setOrganization(this.parseOrganisation(rs.getString("organisation")));
155
				aff.setPerson(this.parsePerson(rs.getString("person")));
156
				aff.setOrganization(this.parseOrganisation(rs.getString("organisation")));
148 157

  
149
                return aff;
150
            }
158
				return aff;
159
			}
151 160

  
152
            private Organization parseOrganisation(String organisation) {
153
                if (organisation != null) {
154
                    Organization org = new Organization();
155
                    String[] parts = organisation.split("\\|\\|");
161
			private Organization parseOrganisation(String organisation) {
162
				if (organisation != null) {
163
					Organization org = new Organization();
164
					String[] parts = organisation.split("\\|\\|");
156 165

  
157
                    org.setId(parts[0]);
158
                    org.setName(parts[1]);
166
					org.setId(parts[0]);
167
					org.setName(parts[1]);
159 168

  
160
                    return org;
161
                } else
162
                    return null;
163
            }
169
					return org;
170
				} else
171
					return null;
172
			}
164 173

  
165
            private Person parsePerson(String pStr) {
166
                String[] parts = pStr.split("\\|\\|");
167
                Person person = new Person();
174
			private Person parsePerson(String pStr) {
175
				String[] parts = pStr.split("\\|\\|");
176
				Person person = new Person();
168 177

  
169
                person.setId(parts[0]);
170
                person.setEmail(parts[1]);
171
                person.setName(parts[2]);
172
                person.setLastname(parts[3]);
173
                if (parts.length > 4)
174
                    person.setInitials(parts[4]);
178
				person.setId(parts[0]);
179
				person.setEmail(parts[1]);
180
				person.setName(parts[2]);
181
				person.setLastname(parts[3]);
182
				if (parts.length > 4)
183
					person.setInitials(parts[4]);
175 184

  
176
                return person;
177
            }
178
        });
185
				return person;
186
			}
187
		});
179 188

  
180
        publication.setAuthors(affiliations);
181
    }
189
		publication.setAuthors(affiliations);
190
	}
182 191
}

Also available in: Unified diff