1
|
package eu.dnetlib.openaire.exporter.model.project;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.io.StringWriter;
|
6
|
import java.util.Arrays;
|
7
|
import java.util.List;
|
8
|
import javax.persistence.*;
|
9
|
|
10
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
11
|
import com.google.gson.Gson;
|
12
|
import io.swagger.annotations.ApiModel;
|
13
|
import org.hibernate.annotations.Type;
|
14
|
import org.supercsv.cellprocessor.Optional;
|
15
|
import org.supercsv.cellprocessor.ift.CellProcessor;
|
16
|
import org.supercsv.cellprocessor.ift.StringCellProcessor;
|
17
|
import org.supercsv.io.CsvBeanReader;
|
18
|
import org.supercsv.io.CsvBeanWriter;
|
19
|
import org.supercsv.io.ICsvBeanReader;
|
20
|
import org.supercsv.io.ICsvBeanWriter;
|
21
|
import org.supercsv.prefs.CsvPreference;
|
22
|
import org.supercsv.util.CsvContext;
|
23
|
|
24
|
/**
|
25
|
* Created by claudio on 04/07/2017.
|
26
|
*/
|
27
|
@Entity
|
28
|
@Table(name = "project_details")
|
29
|
@ApiModel(value = "Project details model", description = "provides project details")
|
30
|
public class ProjectDetails {
|
31
|
|
32
|
@Transient
|
33
|
@JsonIgnore
|
34
|
private transient static final String[] FIELDS = Arrays.stream(ProjectDetails.class.getDeclaredFields())
|
35
|
.map(f -> f.getName())
|
36
|
.filter(s -> !s.equals("FIELDS"))
|
37
|
.filter(s -> !s.startsWith("optional"))
|
38
|
.toArray(String[]::new);
|
39
|
|
40
|
@Id
|
41
|
@Column(name = "projectid")
|
42
|
private String projectId;
|
43
|
private String acronym;
|
44
|
private String code;
|
45
|
|
46
|
@Transient
|
47
|
@JsonIgnore
|
48
|
private transient String optional1;
|
49
|
|
50
|
@Transient
|
51
|
@JsonIgnore
|
52
|
private transient String optional2;
|
53
|
|
54
|
private String jsonextrainfo;
|
55
|
|
56
|
@Type(type = "eu.dnetlib.openaire.exporter.model.GenericArrayUserType")
|
57
|
@Column(name = "fundingpath", columnDefinition = "text[]")
|
58
|
private String[] fundingPath;
|
59
|
|
60
|
public ProjectDetails() {
|
61
|
}
|
62
|
|
63
|
public String getProjectId() {
|
64
|
return projectId;
|
65
|
}
|
66
|
|
67
|
public void setProjectId(final String projectId) {
|
68
|
this.projectId = projectId;
|
69
|
}
|
70
|
|
71
|
public String getAcronym() {
|
72
|
return acronym;
|
73
|
}
|
74
|
|
75
|
public void setAcronym(final String acronym) {
|
76
|
this.acronym = acronym;
|
77
|
}
|
78
|
|
79
|
public String getCode() {
|
80
|
return code;
|
81
|
}
|
82
|
|
83
|
public void setCode(final String code) {
|
84
|
this.code = code;
|
85
|
}
|
86
|
|
87
|
public String getOptional1() {
|
88
|
return optional1;
|
89
|
}
|
90
|
|
91
|
public void setOptional1(final String optional1) {
|
92
|
this.optional1 = optional1;
|
93
|
}
|
94
|
|
95
|
public String getOptional2() {
|
96
|
return optional2;
|
97
|
}
|
98
|
|
99
|
public void setOptional2(final String optional2) {
|
100
|
this.optional2 = optional2;
|
101
|
}
|
102
|
|
103
|
public String getJsonextrainfo() {
|
104
|
return jsonextrainfo;
|
105
|
}
|
106
|
|
107
|
public void setJsonextrainfo(final String jsonextrainfo) {
|
108
|
this.jsonextrainfo = jsonextrainfo;
|
109
|
}
|
110
|
|
111
|
public String[] getFundingPath() {
|
112
|
return fundingPath;
|
113
|
}
|
114
|
|
115
|
public void setFundingPath(final String[] fundingPath) {
|
116
|
this.fundingPath = fundingPath;
|
117
|
}
|
118
|
|
119
|
public void setFundingPath(final List<String> fundingPath) {
|
120
|
if(fundingPath != null && !fundingPath.isEmpty()) {
|
121
|
this.fundingPath = fundingPath.toArray(new String[fundingPath.size()]);
|
122
|
}
|
123
|
}
|
124
|
|
125
|
public static ProjectDetails fromJson(final String json) {
|
126
|
return new Gson().fromJson(json, ProjectDetails.class);
|
127
|
}
|
128
|
|
129
|
public static ProjectDetails fromCSV(final String csv) throws IOException {
|
130
|
ICsvBeanReader beanReader = null;
|
131
|
try {
|
132
|
beanReader = new CsvBeanReader(new StringReader(csv), CsvPreference.STANDARD_PREFERENCE);
|
133
|
return beanReader.read(ProjectDetails.class, FIELDS, getProcessors(new StringCellProcessor() {
|
134
|
@Override
|
135
|
public Object execute(final Object value, final CsvContext context) {
|
136
|
return new Gson().fromJson(value.toString(), List.class);
|
137
|
}
|
138
|
}));
|
139
|
} finally {
|
140
|
if (beanReader != null) {
|
141
|
beanReader.close();
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
|
148
|
* columns are read as null (hence the NotNull() for mandatory columns).
|
149
|
*
|
150
|
* @return the cell processors
|
151
|
*/
|
152
|
private static CellProcessor[] getProcessors(final CellProcessor fundingPathProcessor) {
|
153
|
return new CellProcessor[] {
|
154
|
new Optional(), // projectId
|
155
|
new Optional(), // acronym
|
156
|
new Optional(), // code
|
157
|
new Optional(), // jsonextrainfo
|
158
|
fundingPathProcessor
|
159
|
};
|
160
|
}
|
161
|
|
162
|
public String asJson() {
|
163
|
return new Gson().toJson(this) + '\n';
|
164
|
}
|
165
|
|
166
|
public String asCSV() throws IOException {
|
167
|
final StringWriter sb = new StringWriter();
|
168
|
try (ICsvBeanWriter beanWriter = new CsvBeanWriter(sb, CsvPreference.STANDARD_PREFERENCE)) {
|
169
|
beanWriter.write(this, FIELDS, getProcessors(new StringCellProcessor() {
|
170
|
@Override
|
171
|
public Object execute(final Object value, final CsvContext context) {
|
172
|
return new Gson().toJson(value);
|
173
|
}
|
174
|
}));
|
175
|
beanWriter.flush();
|
176
|
}
|
177
|
|
178
|
return sb.toString();
|
179
|
}
|
180
|
}
|