Project

General

Profile

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

    
3
import eu.dnetlib.goldoa.domain.*;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
6
import org.springframework.jdbc.core.JdbcTemplate;
7
import org.springframework.jdbc.core.RowMapper;
8

    
9
import javax.sql.DataSource;
10
import java.sql.*;
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
/**
15
 * Created by antleb on 3/19/15.
16
 */
17
public class PublicationDAO {
18

    
19
    @Autowired
20
    private DataSource dataSource;
21

    
22
    private static final String UPDATE_PUBLICATION = "update publication set title=?, languages=?, subjects=?, doi=?, source=?, date=?, type=?, journal=?, publisher=?, repository=? where id=?";
23

    
24
    private static final String INSERT_PUBLICATION = "insert into publication (title, languages, subjects, doi, source, date, type, journal, publisher, repository, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
25

    
26
    private static final String GET_PUBLICATION =
27
            "select p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher, repository, array_agg(distinct pa.affiliation) as authors, array_agg(distinct i.type || '||' || i.identifier) as identifiers\n" +
28
            "from publication p\n" +
29
            "left join publication_affiliation pa on pa.publication=p.id\n" +
30
            "left join publication_identifier i on i.publication = p.id\n" +
31
            "group by p.id, title, languages, subjects, doi, source, date, p.type, journal, publisher;";
32

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

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

    
39
    private static final String LOAD_AFFILIATIONS = "select a.id, startdate, enddate, department, 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" +
40
            "from affiliation a\n" +
41
            "left join person p on a.person=p.email\n" +
42
            "left join organisation o on o.id=a.organization\n" +
43
            "join publication_affiliation pa on pa.affiliation=a.id and pa.publication=?\n";
44

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

    
50
                Publication publication = new Publication();
51

    
52
                publication.setId(rs.getString("id"));
53
                publication.setTitle(rs.getString("title"));
54
                publication.setLanguages(rs.getString("languages"));
55
                publication.setSubjects(rs.getString("subjects"));
56
                publication.setDoi(rs.getString("doi"));
57
                publication.setSource(rs.getString("source"));
58
                publication.setPublicationDate(rs.getDate("date"));
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"));
63

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

    
68
                publication.setIdentifiers(new ArrayList<PublicationIdentifier>());
69
                for (String s: (String[]) rs.getArray("identifiers").getArray()) {
70
                    String[] parts = s.split("\\|\\|");
71

    
72
                    publication.getIdentifiers().add(new PublicationIdentifier(parts[0], parts[1]));
73
                }
74

    
75
                return publication;
76
            }
77
        });
78
    }
79

    
80
    public Publication savePublication(Publication publication) {
81
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
82
        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.getId()};
83
        int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
84

    
85
        if (jdbcTemplate.update(UPDATE_PUBLICATION, args, types) == 0) {
86
            jdbcTemplate.update(INSERT_PUBLICATION, args, types);
87
        }
88

    
89
        return publication;
90
    }
91

    
92
    public void insertIdentifiers(final Publication publication) {
93

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

    
99
                ps.setString(1, publication.getId());
100
                ps.setString(2, identifier.getType());
101
                ps.setString(3, identifier.getValue());
102
            }
103

    
104
            @Override
105
            public int getBatchSize() {
106
                return publication.getIdentifiers()!=null?publication.getIdentifiers().size():0;
107
            }
108
        });
109
    }
110

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

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

    
119
    public void saveAffiliations(final Publication publication) {
120
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
121

    
122
        if (publication.getAuthors() != null) {
123
            for (Affiliation affiliation:publication.getAuthors()) {
124
                if (jdbcTemplate.update(UPDATE_PUBLICATION_AFFILIATIONS, new String[] {affiliation.getPerson().getEmail(), affiliation.getOrganization().getId(), publication.getId()}, new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}) == 0) {
125
                    jdbcTemplate.update(INSERT_PUBLICATION_AFFILIATIONS,
126
                            new Object[] {affiliation.getPerson().getEmail(), affiliation.getOrganization().getId(), affiliation.getStart()!=null?affiliation.getStart():null, affiliation.getEnd()!=null?affiliation.getEnd():null, affiliation.getDepartment(), publication.getId()},
127
                            new int[] {Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR});
128
                }
129
            }
130
        }
131
    }
132

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

    
139
                aff.setId(rs.getInt("id"));
140
                aff.setDepartment(rs.getString("department"));
141
                aff.setStart(rs.getDate("startdate"));
142
                aff.setEnd(rs.getDate("enddate"));
143

    
144
                aff.setPerson(this.parsePerson(rs.getString("person")));
145
                aff.setOrganization(this.parseOrganisation(rs.getString("organisation")));
146

    
147
                return aff;
148
            }
149

    
150
            private Organization parseOrganisation(String organisation) {
151
                Organization org = new Organization();
152
                String[] parts = organisation.split("\\|\\|");
153

    
154
                org.setId(parts[0]);
155
                org.setName(parts[1]);
156

    
157
                return org;
158
            }
159

    
160
            private Person parsePerson(String pStr) {
161
                String[] parts = pStr.split("\\|\\|");
162
                Person person = new Person();
163

    
164
                person.setEmail(parts[0]);
165
                person.setName(parts[1]);
166
                person.setLastname(parts[2]);
167
                person.setInitials(parts[3]);
168

    
169
                return person;
170
            }
171
        });
172

    
173
        publication.setAuthors(affiliations);
174
    }
175

    
176
    public DataSource getDataSource() {
177
        return dataSource;
178
    }
179

    
180
    public void setDataSource(DataSource dataSource) {
181
        this.dataSource = dataSource;
182
    }
183
}
(5-5/6)