Project

General

Profile

« Previous | Next » 

Revision 62318

tidied up the Converter

View differences:

modules/uoa-repository-manager-service/branches/new-datasource-model/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java
72 72
    @Value("${services.provide.usageStatisticsNumbersBaseURL}")
73 73
    private String usageStatisticsNumbersBaseURL;
74 74

  
75
    private Converter converter;
75 76

  
76 77
    private static final Map<String, List<String>> dataSourceClass = new HashMap<>();
77 78
    private static final Map<String, String> invertedDataSourceClass = new HashMap<>();
......
91 92
                                 AuthoritiesUpdater authoritiesUpdater,
92 93
                                 VocabularyLoader vocabularyLoader,
93 94
                                 RestTemplate restTemplate,
95
                                 Converter converter,
94 96
                                 @Lazy EmailUtils emailUtils,
95 97
                                 @Lazy ValidatorService validatorService,
96 98
                                 @Lazy PiWikService piWikService) {
......
100 102
        this.authoritiesUpdater = authoritiesUpdater;
101 103
        this.vocabularyLoader = vocabularyLoader;
102 104
        this.piWikService = piWikService;
105
        this.converter = converter;
103 106
        this.emailUtils = emailUtils;
104 107
        this.validatorService = validatorService;
105 108
        this.restTemplate = restTemplate;
......
177 180
            requestFilter.setId(repoId);
178 181
            String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
179 182

  
180
            repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs)));
183
            repos.addAll(converter.toRepositoryList(new JSONObject(rs)));
181 184
        }
182 185

  
183 186
        for (Repository r : repos)
......
363 366
        if (jsonArray.length() == 0)
364 367
            throw new ResourceNotFoundException();
365 368

  
366
        repo = Converter.jsonToRepositorySnippetObject(jsonArray.getJSONObject(0));
369
        repo = converter.toRepositorySnippet(jsonArray.getJSONObject(0));
367 370
        return repo;
368 371
    }
369 372

  
......
382 385
        if (jsonArray.length() == 0)
383 386
            throw new ResourceNotFoundException();
384 387

  
385
        repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
388
        repo = converter.toRepository(jsonArray.getJSONObject(0));
386 389
        return updateRepositoryInfo(repo);
387 390
    }
388 391

  
......
396 399
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
397 400
        JSONArray aggregationInfo = new JSONObject(rs).getJSONArray("aggregationInfo");
398 401

  
399
        List<AggregationDetails> aggregationHistory = new ArrayList<>(Converter.getAggregationHistoryFromJson(aggregationInfo));
402
        List<AggregationDetails> aggregationHistory = new ArrayList<>(converter.toAggregationHistory(aggregationInfo));
400 403

  
401 404
        return aggregationHistory;
402 405
    }
......
441 444
        requestFilter.setOfficialname(name);
442 445

  
443 446
        String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
444
        List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
447
        List<Repository> repos = converter.toRepositoryList(new JSONObject(rs));
445 448
        for (Repository r : repos)
446 449
            updateRepositoryInfo(r);
447 450
        return repos;
......
457 460
                .build().expand(id).encode();
458 461

  
459 462
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
460
        return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
463
        return converter.toRepositoryInterfaceList(new JSONObject(rs));
461 464

  
462 465
    }
463 466

  
......
526 529
                .build()
527 530
                .encode();
528 531

  
529
        String json_repository = Converter.repositoryObjectToJson(repository);
532
        String json_repository = converter.toJson(repository);
530 533
        LOGGER.debug("JSON to add(update) -> " + json_repository);
531 534

  
532 535
        HttpEntity<String> httpEntity = new HttpEntity<>(json_repository, httpHeaders);
......
552 555
                .build()
553 556
                .encode();
554 557

  
555
        String json_repository = Converter.repositoryObjectToJson(repository);
558
        String json_repository = converter.toJson(repository);
556 559

  
557 560
        LOGGER.debug("JSON to update -> " + json_repository);
558 561

  
......
585 588
                .fromHttpUrl(baseAddress + "/ds/add/")
586 589
                .build()
587 590
                .encode();
588
        String json_repository = Converter.repositoryObjectToJson(repository);
591
        String json_repository = converter.toJson(repository);
589 592
        HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
590 593
        ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
591 594

  
......
619 622
                                                      String comment, RepositoryInterface repositoryInterface) throws Exception {
620 623
        Repository e = this.getRepositoryById(repoId);
621 624
        repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype);
622
        String json_interface = Converter.repositoryInterfaceObjectToJson(e, repositoryInterface);
625
        String json_interface = converter.toJson(e, repositoryInterface);
623 626

  
624 627
        UriComponents uriComponents = UriComponentsBuilder
625 628
                .fromHttpUrl(baseAddress + "/ds/api/add/")
......
725 728
    @Override
726 729
    public List<String> getDnetCountries() {
727 730
        LOGGER.debug("Getting dnet-countries!");
728
        return Converter.readFile("countries.txt");
731
        return converter.readFile("countries.txt");
729 732
    }
730 733

  
731 734
    @Override
732 735
    public List<String> getTypologies() {
733
        return Converter.readFile("typologies.txt");
736
        return converter.readFile("typologies.txt");
734 737
    }
735 738

  
736 739
    @Override
737 740
    public List<Timezone> getTimezones() {
738
        List<String> timezones = Converter.readFile("timezones.txt");
739
        return Converter.toTimezones(timezones);
741
        List<String> timezones = converter.readFile("timezones.txt");
742
        return converter.toTimezones(timezones);
740 743
    }
741 744

  
742 745
    @Override
modules/uoa-repository-manager-service/branches/new-datasource-model/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java
27 27

  
28 28
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
29 29

  
30
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
30
    public Repository toRepository(JSONObject repositoryObject) throws JSONException {
31 31

  
32 32
        Repository repository = new Repository();
33 33

  
......
63 63
        repository.setTimezone(timezone != null ? timezone : 0.0);
64 64
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
65 65
        repository.setOdLanguages(datasource.get("languages").toString());
66
        repository.setDateOfValidation(convertStringToDate(datasource.get("dateofvalidation").toString()));
66
        repository.setDateOfValidation(toDate(datasource.get("dateofvalidation").toString()));
67 67

  
68 68
        /*  typology -> platform
69 69
         *  datasource class -> typology */
......
81 81
        repository.setDatasourceClass(datasource.get("typology").toString());
82 82
//        <--
83 83

  
84
        repository.setDateOfCollection(convertStringToDate(datasource.get("dateofcollection").toString()));
84
        repository.setDateOfCollection(toDate(datasource.get("dateofcollection").toString()));
85 85
        repository.setActivationId(datasource.get("activationId").toString());
86 86

  
87 87
        repository.setDescription(datasource.get("description").toString());
......
117 117
        repository.setConsentTermsOfUseDate(null);
118 118
        repository.setLastConsentTermsOfUseDate(null);
119 119
        try {
120
            repository.setConsentTermsOfUseDate(convertStringToDate(datasource.get("consentTermsOfUseDate").toString()));
121
            repository.setLastConsentTermsOfUseDate(convertStringToDate(datasource.get("lastConsentTermsOfUseDate").toString()));
120
            repository.setConsentTermsOfUseDate(toDate(datasource.get("consentTermsOfUseDate").toString()));
121
            repository.setLastConsentTermsOfUseDate(toDate(datasource.get("lastConsentTermsOfUseDate").toString()));
122 122
        } catch (JSONException e) {
123
            LOGGER.info("Error setting consentTermsOfUseDate date and lastConsentTermsOfUseDate", e);
123
            LOGGER.error("Error setting consentTermsOfUseDate date and lastConsentTermsOfUseDate", e);
124 124
        }
125 125
        repository.setFullTextDownload(convertStringToBoolean(datasource.get("fullTextDownload").toString()));
126 126

  
......
129 129
        return repository;
130 130
    }
131 131

  
132
    public static Boolean convertStringToBoolean(String value) {
133
        return value.equals("null") ? null : Boolean.valueOf(value);
134
    }
132
    public List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {
135 133

  
136
    public static Date convertStringToDate(String date) {
137

  
138
        if (Objects.equals(date, "null"))
139
            return null;
140

  
141
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
142
        try {
143
            return formatter.parse(date);
144
        } catch (ParseException e) {
145
            LOGGER.error(e);
146
        }
147
        return null;
148
    }
149

  
150
    public static String convertDateToString(Date date) {
151

  
152
        if (Objects.equals(date, null))
153
            return null;
154

  
155
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
156
        return formatter.format(date);
157
    }
158

  
159
    public static Double toDouble(String number) {
160
        if (Objects.equals(number, "null"))
161
            return 0.0;
162
        else
163
            return Double.valueOf(number);
164
    }
165

  
166
    public static List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {
167

  
168 134
        List<RepositorySnippet> resultSet = new ArrayList<>();
169 135
        JSONArray rs = json.getJSONArray("datasourceInfo");
170 136
        for (int i = 0; i < rs.length(); i++)
171
            resultSet.add(jsonToRepositorySnippetObject(rs.getJSONObject(i)));
137
            resultSet.add(toRepositorySnippet(rs.getJSONObject(i)));
172 138
        return resultSet;
173 139
    }
174 140

  
175
    public static RepositorySnippet jsonToRepositorySnippetObject(JSONObject repositorySnippetObject) throws JSONException {
141
    public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) throws JSONException {
176 142

  
177 143

  
178 144
        RepositorySnippet repositorySnippet = new RepositorySnippet();
......
197 163

  
198 164
        repositorySnippet.setConsentTermsOfUse(repositorySnippetObject.get("consenttermsofuse").toString());
199 165
        repositorySnippet.setFullTextDownload(repositorySnippetObject.get("fulltextdownload").toString());
200
        repositorySnippet.setConsentTermsOfUseDate(convertStringToDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
166
        repositorySnippet.setConsentTermsOfUseDate(toDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
201 167

  
202 168
        return repositorySnippet;
203 169

  
204 170
    }
205 171

  
206
    public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {
172
    public List<Repository> toRepositoryList(JSONObject json) throws JSONException {
207 173

  
208 174
        List<Repository> resultSet = new ArrayList<>();
209 175
        JSONArray rs = json.getJSONArray("datasourceInfo");
210 176
        for (int i = 0; i < rs.length(); i++)
211
            resultSet.add(jsonToRepositoryObject(rs.getJSONObject(i)));
177
            resultSet.add(toRepository(rs.getJSONObject(i)));
212 178
        return resultSet;
213 179
    }
214 180

  
215
    public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException {
181
    public List<RepositoryInterface> toRepositoryInterfaceList(JSONObject json) throws JSONException {
216 182

  
217 183
        List<RepositoryInterface> resultSet = new ArrayList<>();
218 184
        JSONArray rs = json.getJSONArray("api");
219 185
        for (int i = 0; i < rs.length(); i++)
220
            resultSet.add(jsonToRepositoryInterfaceObject(rs.getJSONObject(i)));
186
            resultSet.add(toRepositoryInterface(rs.getJSONObject(i)));
221 187
        return resultSet;
222 188
    }
223 189

  
224
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
190
    public RepositoryInterface toRepositoryInterface(JSONObject repositoryInterfaceObject) throws JSONException {
225 191

  
226 192
        RepositoryInterface repositoryInterface = new RepositoryInterface();
227 193

  
......
265 231
        return repositoryInterface;
266 232
    }
267 233

  
268
    public static String repositoryObjectToJson(Repository repository) throws JSONException, JsonProcessingException {
234
    public String toJson(Repository repository) throws JSONException, JsonProcessingException {
269 235

  
270 236
        HashMap<String, Object> repositoryMap = new HashMap<>();
271 237
        ObjectMapper mapper = new ObjectMapper();
......
284 250
        repositoryMap.put("namespaceprefix", repository.getNamespacePrefix() != null ? repository.getNamespacePrefix() : "");
285 251
        repositoryMap.put("languages", repository.getOdLanguages() != null ? repository.getOdLanguages() : "");
286 252

  
287
        repositoryMap.put("dateofcollection", repository.getDateOfCollection() != null ? convertDateToString(repository.getDateOfCollection()) : "");
253
        repositoryMap.put("dateofcollection", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
288 254

  
289 255
        /*
290 256
         * typology -> platform
......
293 259
//        repositoryMap.put("eoscDatasourceType", repository.getDatasourceClass()); // TODO: enable in future release
294 260
        repositoryMap.put("platform", repository.getTypology());
295 261

  
296
        repositoryMap.put("dateofvalidation", repository.getDateOfCollection() != null ? convertDateToString(repository.getDateOfCollection()) : "");
262
        repositoryMap.put("dateofvalidation", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
297 263
        repositoryMap.put("activationId", repository.getActivationId() != null ? repository.getActivationId() : "");
298 264

  
299 265
        repositoryMap.put("description", repository.getDescription());
......
338 304
        repositoryMap.put("subjects", "");
339 305
        repositoryMap.put("consentTermsOfUse", repository.getConsentTermsOfUse());
340 306
        repositoryMap.put("fullTextDownload", repository.getFullTextDownload());
341
        repositoryMap.put("consentTermsOfUseDate", convertDateToString(repository.getConsentTermsOfUseDate()));
342
        repositoryMap.put("lastConsentTermsOfUseDate", convertDateToString(repository.getLastConsentTermsOfUseDate()));
307
        repositoryMap.put("consentTermsOfUseDate", toString(repository.getConsentTermsOfUseDate()));
308
        repositoryMap.put("lastConsentTermsOfUseDate", toString(repository.getLastConsentTermsOfUseDate()));
343 309

  
344 310
        return mapper.writeValueAsString(repositoryMap);
345 311
    }
346 312

  
347
    public static String repositoryInterfaceObjectToJson(Repository repository, RepositoryInterface repositoryInterface) throws JSONException {
313
    public String toJson(Repository repository, RepositoryInterface repositoryInterface) throws JSONException {
348 314

  
349 315
        JSONObject jsonObject = new JSONObject();
350 316

  
......
384 350
        return jsonObject.toString();
385 351
    }
386 352

  
387
    public static ArrayList<String> readFile(String filename) {
353
    public ArrayList<String> readFile(String filename) {
388 354
        String line;
389 355
        ArrayList<String> list = new ArrayList<String>();
390 356
        try {
......
402 368
        return list;
403 369
    }
404 370

  
405
    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONArray aggregationInfo) throws JSONException {
371
    public List<AggregationDetails> toAggregationHistory(JSONArray aggregationInfo) throws JSONException {
406 372
        List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
407 373

  
408 374
        for (int i = 0; i < aggregationInfo.length(); i++)
409
            aggregationDetailsList.add(jsonToAggregationDetails(aggregationInfo.getJSONObject(i)));
375
            aggregationDetailsList.add(toAggregationDetails(aggregationInfo.getJSONObject(i)));
410 376

  
411 377
        return aggregationDetailsList;
412 378
    }
413 379

  
414
    private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {
380
    public List<Timezone> toTimezones(List<String> timezones) {
415 381

  
416
        AggregationDetails aggregationDetails = new AggregationDetails();
417

  
418
        if (aggregationObject.has("collectionMode"))
419
            aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
420
        if (aggregationObject.has("indexedVersion"))
421
            aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
422

  
423
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
424
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
425
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
426

  
427
        return aggregationDetails;
428
    }
429

  
430
    public static List<Timezone> toTimezones(List<String> timezones) {
431

  
432 382
        List<Timezone> tmz = new ArrayList<>();
433 383
        for (String t : timezones) {
434 384
            String[] s = t.split("\t");
......
437 387
        return tmz;
438 388
    }
439 389

  
440
    private static String getOpenaireId(String repositoryId) {
390
    private String getOpenaireId(String repositoryId) {
441 391
        if (repositoryId != null && repositoryId.contains("::"))
442 392
            return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
443 393
        return null;
444 394
    }
445 395

  
396
    private Boolean convertStringToBoolean(String value) {
397
        return value.equals("null") ? null : Boolean.valueOf(value);
398
    }
399

  
400
    private Date toDate(String date) {
401

  
402
        if (Objects.equals(date, "null"))
403
            return null;
404

  
405
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
406
        try {
407
            return formatter.parse(date);
408
        } catch (ParseException e) {
409
            LOGGER.error(e);
410
        }
411
        return null;
412
    }
413

  
414
    private String toString(Date date) {
415

  
416
        if (Objects.equals(date, null))
417
            return null;
418

  
419
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
420
        return formatter.format(date);
421
    }
422

  
423
    private Double toDouble(String number) {
424
        if (Objects.equals(number, "null"))
425
            return 0.0;
426
        else
427
            return Double.valueOf(number);
428
    }
429

  
430
    private AggregationDetails toAggregationDetails(JSONObject aggregationObject) throws JSONException {
431

  
432
        AggregationDetails aggregationDetails = new AggregationDetails();
433

  
434
        if (aggregationObject.has("collectionMode"))
435
            aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
436
        if (aggregationObject.has("indexedVersion"))
437
            aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
438

  
439
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
440
        aggregationDetails.setDate(toDate(aggregationObject.get("date").toString()));
441
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
442

  
443
        return aggregationDetails;
444
    }
446 445
}

Also available in: Unified diff