Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.contexts;
2

    
3
import java.io.IOException;
4
import java.util.*;
5

    
6
import com.google.common.collect.Maps;
7
import com.ximpleware.AutoPilot;
8
import com.ximpleware.VTDException;
9
import com.ximpleware.VTDGen;
10
import com.ximpleware.VTDNav;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.dom4j.Document;
14
import org.dom4j.DocumentHelper;
15
import org.dom4j.Element;
16
import org.joda.time.DateTime;
17

    
18
public class BuildH2020FETTaxonomy {
19

    
20
	private static final Log log = LogFactory.getLog(BuildH2020FETTaxonomy.class);
21

    
22
	private Iterator<String> projects;
23
	private Map<String, Map<String, Map<String, List<Info>>>> dic;
24

    
25
	public BuildH2020FETTaxonomy() {
26
		dic = Maps.newHashMap();
27
	}
28

    
29
	public void setIterator(Iterator<String> projects) {
30
		this.projects = projects;
31
	}
32

    
33
	public String parseProjects() throws Exception {
34
		while (projects.hasNext())
35
			parse(projects.next());
36
		return getTaxonomy();
37
	}
38

    
39
	public String getTaxonomy() throws Exception {
40
		Document taxonomy = DocumentHelper.createDocument();
41
		Element root1 = taxonomy.addElement("RESOURCE_PROFILE");
42
		createHeader(root1);
43
		Element body = root1.addElement("BODY");
44
		Element configurationElement = body.addElement("CONFIGURATION");
45
		createContext(configurationElement);
46
		body.addElement("STATUS");
47
		body.addElement("SECURITY_PARAMETERS");
48
		return taxonomy.getDocument().asXML();
49

    
50
	}
51

    
52
	private void createContext(final Element configurationElement) {
53
		Element context = configurationElement.addElement("context");
54
		context.addAttribute("type", "community");
55
		context.addAttribute("id", "fet-h2020");
56
		context.addAttribute("label", "FET H2020");
57
		for (Map.Entry<String, Map<String, Map<String, List<Info>>>> entry : dic.entrySet()) {
58
			String type = entry.getKey();
59
			Element cat = context.addElement("category");
60
			String catid = "fet-h2020::" + type.toLowerCase();
61
			cat.addAttribute("id", catid);
62
			cat.addAttribute("label", "FET " + type);
63
			cat.addAttribute("claim", "false");
64
			int count = 0;
65
			for (Map.Entry<String, Map<String, List<Info>>> entryst : dic.get(type).entrySet()) {
66
				String subtype = entryst.getKey();
67
				for (Map.Entry<String, List<Info>> entryp : dic.get(type).get(subtype).entrySet()) {
68
					String opt = entryp.getKey();
69
					count++;
70
					Element subcat = cat.addElement("concept");
71
					String subcatid = catid + "::" + count;
72
					subcat.addAttribute("id", subcatid);
73
					Element pa = subcat.addElement("param");
74
					pa.addAttribute("name", "CALL_ID");
75
					List<Info> projectRows = dic.get(type).get(subtype).get(opt);
76
					if (!projectRows.isEmpty()) {
77
						for (Info row : projectRows) {
78
							createProjectConcept(row, subcat, subcatid);
79
						}
80
						subcat.addAttribute("label", projectRows.get(0).getCallname());
81
						subcat.addAttribute("claim", "false");
82
						pa.setText(projectRows.get(0).getCallID());
83
					} else throw new RuntimeException("The lis of projects for " + subcatid + " is empty. This exception should never be thrown.");
84
				}
85
			}
86
		}
87
	}
88

    
89
	private void createProjectConcept(Info row, Element father, String prefix) {
90
		String code = row.getCode();
91
		String acronym = row.getAcronym();
92
		String title = row.getTitle();
93
		String projcallid = row.getProjcallid();
94
		Element concept = father.addElement("concept");
95
		concept.addAttribute("id", prefix + "::" + code);
96
		concept.addAttribute("label", title);
97
		concept.addAttribute("claim", "true");
98
		Element p = concept.addElement("param");
99
		p.addAttribute("name", "CD_PROJ_ID");
100
		p.setText(code);
101
		p = concept.addElement("param");
102
		p.addAttribute("name", "CD_CALL_ID");
103
		p.setText(projcallid);
104
		p = concept.addElement("param");
105
		p.addAttribute("name", "CD_PROJECT_NUMBER");
106
		p.setText(code);
107
		p = concept.addElement("param");
108
		p.addAttribute("name", "CD_ACRONYM");
109
		p.setText(acronym);
110
		p = concept.addElement("param");
111
		p.addAttribute("name", "CD_FRAMEWORK");
112
		p.setText("H2020");
113
	}
114

    
115
	private void createHeader(final Element profile) {
116
		Element header = profile.addElement("HEADER");
117
		Element rs = header.addElement("RESOURCE_IDENTIFIER");
118
		rs.addAttribute("value", "");
119
		Element rt = header.addElement("RESOURCE_TYPE");
120
		rt.addAttribute("value", "ContextDSResourceType");
121
		Element rk = header.addElement("RESOURCE_KIND");
122
		rk.addAttribute("value", "ContextDSResources");
123
		Element ru = header.addElement("RESOURCE_URI");
124
		ru.addAttribute("value", "");
125
		Element daoc = header.addElement("DATE_OF_CREATION");
126
		daoc.addAttribute("value", DateTime.now().toString());
127
	}
128

    
129
	private boolean parse(String project) throws VTDException, IOException {
130
		boolean fet = false;
131
		String callid = "";
132
		String projectid = "";
133
		VTDGen vg = new VTDGen();
134
		vg.setDoc(project.getBytes("UTF-8"));
135
		vg.parse(false);
136
		VTDNav vn = vg.getNav();
137
		AutoPilot ap = new AutoPilot(vn);
138
		ap.selectXPath("//metadata/ROWS/ROW[@table=\"projects\"]");
139

    
140
		while (ap.evalXPath() != -1) {
141
			AutoPilot ap1 = new AutoPilot(vn);
142
			ap1.selectXPath("./FIELD[@name=\"call_identifier\"]");
143
			if (ap1.evalXPath() != -1)
144
				callid = vn.toNormalizedString(vn.getText());
145
			ap1.clearVariableExprs();
146
			vn.toElement(VTDNav.PARENT);
147
			ap1.selectXPath("./FIELD[@name=\"optional1\"]");
148
			if (ap1.evalXPath() != -1)
149
				projectid = vn.toNormalizedString(vn.getText());
150
			if (callid.contains("FET")) {
151
				String type;
152
				if (callid.contains("OPEN"))
153
					type = "OPEN";
154
				else if (callid.contains("PROACT"))
155
					type = "PROACT";
156
				else if (callid.contains("HPC"))
157
					type = "HPC";
158
				else
159
					type = "FLAG";
160
				vn.toElement(VTDNav.ROOT);
161
				fet = true;
162
				insert(type, callid, projectid, new Info(vn));
163
			}
164
		}
165
		return fet;
166
	}
167

    
168
	private void insert(String type, String callid, String projcallid, Info row) {
169
		Map<String, Map<String, List<Info>>> dopen;
170
		Map<String, List<Info>> entry;
171
		List<Info> projects;
172
		if (dic.containsKey(type)) {
173
			dopen = dic.get(type);
174
			if (dopen.containsKey(callid)) {
175
				entry = dopen.get(callid);
176
				if (entry.containsKey(projcallid)) {
177
					entry.get(projcallid).add(row);
178
				} else {
179
					projects = new ArrayList<>();
180
					projects.add(row);
181
					entry.put(projcallid, projects);
182
				}
183
			} else {
184
				projects = new ArrayList<>();
185
				entry = new HashMap<>();
186
				projects.add(row);
187
				entry.put(projcallid, projects);
188
				dopen.put(callid, entry);
189
			}
190
		} else {
191
			projects = new ArrayList<>();
192
			entry = new HashMap<>();
193
			dopen = new HashMap<>();
194
			projects.add(row);
195
			entry.put(projcallid, projects);
196
			dopen.put(callid, entry);
197
			dic.put(type, dopen);
198
		}
199
	}
200

    
201
	public void parseFETProject(int projects_number) throws VTDException, IOException {
202
		int parsed = 0;
203
		while (projects.hasNext() && parsed < projects_number) {
204
			if (parse(projects.next()))
205
				parsed++;
206
		}
207
		log.debug(parsed);
208
	}
209

    
210
	private class Info {
211

    
212
		VTDNav vn;
213
		private String code, acronym, title, call_identifier, projcallid, callname;
214

    
215
		public String getCode() {
216
			return code;
217
		}
218

    
219
		public String getAcronym() {
220
			return acronym;
221
		}
222

    
223
		public String getTitle() {
224
			return title;
225
		}
226

    
227
		public String getCallID() {
228
			return call_identifier;
229
		}
230

    
231
		public String getProjcallid() {
232
			return projcallid;
233
		}
234

    
235
		public String getCallname() {
236
			return callname;
237
		}
238

    
239
		public Info(VTDNav vn) {
240
			this.vn = vn;
241
			parse();
242
		}
243

    
244
		private void parse() {
245
			try {
246
				AutoPilot ap = new AutoPilot(vn);
247
				ap.selectXPath("//FIELD");
248
				while (ap.evalXPath() != -1) {
249
					String aname = vn.toNormalizedString(vn.getAttrVal("name"));
250
					String text = "";
251
					if (vn.getText() != -1)
252
						text = vn.toNormalizedString(vn.getText());
253
					if (aname.equals("code"))
254
						code = text;
255
					else if (aname.equals("acronym"))
256
						acronym = text;
257
					else if (aname.equals("title"))
258
						title = text;
259
					else if (aname.equals("call_identifier"))
260
						call_identifier = text;
261
					else if (aname.equals("optional1"))
262
						projcallid = text;
263
					else if (aname.equals("optional2"))
264
						callname = text;
265
				}
266
			} catch (Exception e) {
267
			}
268
		}
269

    
270
	}
271

    
272
}
(1-1/8)