Project

General

Profile

« Previous | Next » 

Revision 38999

Added by Marek Horst about 9 years ago

#1302 merging 20150518_new_funding_model branch back to the trunk

View differences:

modules/icm-iis-import/trunk/src/test/java/eu/dnetlib/iis/importer/converter/ProjectConverterTest.java
25 25
		List<String> fundingTreeList = Collections.singletonList(strWriter.toString());
26 26
		String fundingClass = ProjectConverter.extractFundingClass(fundingTreeList);
27 27
		assertNotNull(fundingClass);
28
		assertEquals("FP7", fundingClass);
28
		assertEquals("WT::WT", fundingClass);
29 29
		
30 30
	}
31 31
}
modules/icm-iis-import/trunk/src/test/resources/eu/dnetlib/iis/importer/converter/fundingclass_example.json
1
{
2
	"funding_level_2":
3
		{
4
			"class":"ec:program", "
5
			id":"corda_______::FP7::SP3::PEOPLE", 
6
			"description":"Marie-Curie Actions", 
7
			"name":"PEOPLE", 
8
			"parent":
9
				{
10
					"funding_level_1":
11
						{
12
							"class":"ec:specificprogram", 
13
							"id":"corda_______::FP7::SP3", 
14
							"description":"SP3-People", 
15
							"name":"SP3", 
16
							"parent":
17
								{
18
									"funding_level_0":
19
										{
20
											"class":"ec:frameworkprogram", 
21
											"id":"corda_______::FP7", 
22
											"description":"SEVENTH FRAMEWORK PROGRAMME", 
23
											"name":"FP7", 
24
											"parent":{}
25
										}
26
								}
27
						}
28
				}
29
		}
30
}
1
<fundingtree>
2
    <funder>
3
        <id>wt__________::WT</id>
4
        <shortname>WT</shortname>
5
        <name>Wellcome Trust</name>
6
        <jurisdiction/>
7
    </funder>
8
    <funding_level_1>
9
        <id>wt__________::WT::Non-Stream Activity - GM</id>
10
        <description>Non-Stream Activity - GM</description>
11
        <name>Non-Stream Activity - GM</name>
12
        <class>wt:fundingStream</class>
13
        <parent>
14
            <funding_level_0>
15
                <id>wt__________::WT</id>
16
                <description>Wellcome Trust Funding Stream</description>
17
                <name>WT</name>
18
                <parent/>
19
                <class>wt:fundingStream</class>
20
            </funding_level_0>
21
        </parent>
22
    </funding_level_1>
23
</fundingtree>
modules/icm-iis-import/trunk/src/main/java/eu/dnetlib/iis/importer/database/project/DatabaseServiceBasedProjectImporter.java
103 103
//			reading sql query content
104 104
			StringWriter writer = new StringWriter();
105 105
			IOUtils.copy(this.getClass().getClassLoader()
106
					.getResourceAsStream("eu/dnetlib/iis/importer/database/project/sql/read_project_details.sql"),
107
//					updated model
108
//	                .getResourceAsStream("eu/dnetlib/iis/importer/database/project/sql/read_project_details_v2.sql"), 
106
	                .getResourceAsStream("eu/dnetlib/iis/importer/database/project/sql/read_project_details_v2.sql"), 
109 107
	                writer, "utf-8");
110 108
			
111 109
			W3CEndpointReference eprResult = databaseService.searchSQL(
modules/icm-iis-import/trunk/src/main/java/eu/dnetlib/iis/importer/converter/ProjectConverter.java
170 170
	 * @throws IOException 
171 171
	 */
172 172
	public static String extractFundingClass(List<String> fundingTreeList) throws IOException {
173
		return extractFundingClassFromJSON(fundingTreeList);
174
//		return extractFundingClassFromXML(fundingTreeList);
173
		return extractFundingClassFromXML(fundingTreeList);
175 174
	}
176 175
	
177
	/**
178
	 * Extracts funding class from funding tree defined as JSON.
179
	 * @param fundingTreeJsonList
180
	 * @return extracted funding class
181
	 * @throws IOException 
182
	 */
183
	public static String extractFundingClassFromJSON(List<String> fundingTreeJsonList) throws IOException {
184
		if (fundingTreeJsonList!=null && !fundingTreeJsonList.isEmpty()) {
185
			for (String currentFundingTreeJson : fundingTreeJsonList) {
186
				if (currentFundingTreeJson!=null && !currentFundingTreeJson.isEmpty()) {
187
					try {
188
						JSONParser parser = new JSONParser();
189
						JSONObject topLevelParent = getTopLevelParent(
190
								(JSONObject) parser.parse(currentFundingTreeJson));
191
						if (topLevelParent!=null) {
192
							@SuppressWarnings("unchecked")
193
							Collection<JSONObject> topLevelParentValues = topLevelParent.values();
194
							for (JSONObject currentValue : topLevelParentValues) {
195
								Object currentName = currentValue.get(ELEM_FUNDING_TREE_NAME);
196
								if (currentName!=null) {
197
									return currentName.toString();
198
								}
199
							}
200
//							fallback
201
							return null;
202
						} else {
203
							return null;
204
						}
205
						
206
					} catch (ParseException e) {
207
						throw new IOException("unable to parse funding tree: " + 
208
					currentFundingTreeJson, e);
209
					}	
210
				}
211
			}		
212
//			fallback
213
			return null;
214
		} else {
215
			return null;
216
		}
217
	}
218
	
219
	private static JSONObject getTopLevelParent(JSONObject parent) {
220
		if (parent!=null) {
221
			@SuppressWarnings("unchecked")
222
			Collection<JSONObject> values = parent.values();
223
			for (JSONObject value : values) {
224
				JSONObject newParent = (JSONObject) value.get(ELEM_FUNDING_TREE_PARENT);
225
				if (newParent!=null && !newParent.isEmpty()) {
226
					return getTopLevelParent(newParent);
227
				}	
228
			}
229
//			fallback
230
			return parent;
231
		} else {
232
			return null;
233
		}
234
		
235
	}
236
	
237 176
}

Also available in: Unified diff