Project

General

Profile

« Previous | Next » 

Revision 62318

tidied up the Converter

View differences:

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