Project

General

Profile

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 static ProjectDetails fromJson(final String json) {
120
		return new Gson().fromJson(json, ProjectDetails.class);
121
	}
122

    
123
	public static ProjectDetails fromCSV(final String csv) throws IOException {
124
		ICsvBeanReader beanReader = null;
125
		try {
126
			beanReader = new CsvBeanReader(new StringReader(csv), CsvPreference.STANDARD_PREFERENCE);
127
			return beanReader.read(ProjectDetails.class, FIELDS, getProcessors(new StringCellProcessor() {
128
				@Override
129
				public Object execute(final Object value, final CsvContext context) {
130
					return new Gson().fromJson(value.toString(), List.class);
131
				}
132
			}));
133
		} finally {
134
			if (beanReader != null) {
135
				beanReader.close();
136
			}
137
		}
138
	}
139

    
140
	/**
141
	 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
142
	 * columns are read as null (hence the NotNull() for mandatory columns).
143
	 *
144
	 * @return the cell processors
145
	 */
146
	private static CellProcessor[] getProcessors(final CellProcessor fundingPathProcessor) {
147
		return new CellProcessor[] {
148
				new Optional(), // projectId
149
				new Optional(), // acronym
150
				new Optional(), // code
151
				new Optional(), // jsonextrainfo
152
				fundingPathProcessor
153
		};
154
	}
155

    
156
	public String asJson() {
157
		return new Gson().toJson(this) + '\n';
158
	}
159

    
160
	public String asCSV() throws IOException {
161
		final StringWriter sb = new StringWriter();
162
		try (ICsvBeanWriter beanWriter = new CsvBeanWriter(sb, CsvPreference.STANDARD_PREFERENCE)) {
163
			beanWriter.write(this, FIELDS, getProcessors(new StringCellProcessor() {
164
				@Override
165
				public Object execute(final Object value, final CsvContext context) {
166
					return new Gson().toJson(value);
167
				}
168
			}));
169
			beanWriter.flush();
170
		}
171

    
172
		return sb.toString();
173
	}
174
}
(2-2/3)