Project

General

Profile

« Previous | Next » 

Revision 49879

merged newAPI branch

View differences:

RepositoryServiceImpl.java
11 11
import eu.dnetlib.repo.manager.client.services.RepositoryService;
12 12
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
13 13
import eu.dnetlib.repo.manager.server.utils.LocalVocabularies;
14
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
14 15
import eu.dnetlib.repo.manager.shared.*;
15 16
import eu.dnetlib.repos.RepoApi;
16 17
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
......
18 19
import org.apache.commons.lang.StringEscapeUtils;
19 20
import org.apache.commons.lang.WordUtils;
20 21
import org.apache.log4j.Logger;
22
import org.json.JSONException;
21 23
import org.springframework.beans.factory.annotation.Autowired;
22 24
import org.springframework.beans.factory.annotation.Value;
23 25
import org.springframework.core.ParameterizedTypeReference;
......
34 36
import javax.annotation.PostConstruct;
35 37
import java.io.IOException;
36 38
import java.io.UnsupportedEncodingException;
37
import java.net.URL;
38 39
import java.net.URLEncoder;
39 40
import java.text.Normalizer;
40 41
import java.util.*;
41 42
import java.util.concurrent.ConcurrentHashMap;
43
import java.net.URL;
42 44

  
43 45
/**
44 46
 * Created by nikonas on 12/8/15.
......
101 103
            "</noscript>\n" +
102 104
            "<!— End Piwik Code —>");
103 105

  
106

  
107
    @Autowired
108
    private RepositoryApi repositoryApi;
109

  
110

  
111

  
104 112
    @PostConstruct
105 113
    public void init() {
106 114
        this.loadVocabularies();
......
114 122
            LOGGER.debug("Getting repositories of country: " + country + " with type: " + mode + " and includeUnknownCountries: " + includeUnknownCountries);
115 123

  
116 124
            Tuple<List<Repository>, List<Repository>> retTuple = new Tuple<List<Repository>, List<Repository>>();
117
            List<Repository> reposList = this.repoAPI.getRepositoriesPerCountry(mode).get(country);
125
//            List<Repository> reposList = this.repoAPI.getRepositoriesPerCountry(mode).get(country);
126

  
127
            List<Repository> reposList = repositoryApi.getRepositoriesByCountry(country,mode);
118 128
            if (reposList == null) {
119 129
                retTuple.setFirst(new ArrayList<Repository>());
120 130
//                if (!includeUnknownCountries) {
......
155 165

  
156 166
    @Override
157 167
    public DatasourcesCollection getRepositoriesOfUser(String userEmail, boolean includeShared, boolean includeByOthers) throws RepositoryServiceException {
168

  
169
        DatasourcesCollection retDatasources = new DatasourcesCollection();
158 170
        try {
159
            LOGGER.debug("Getting repositories of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers);
160
            DatasourcesCollection retDatasources = new DatasourcesCollection();
171
            LOGGER.debug("Getting repositories of user: " + userEmail + " . IncludeShared: "
172
                    + includeShared + " . IncludeByOthers: " + includeByOthers);
173
            int page = 1;
174
            String size = "10";
175
            List<Repository> resultSet = repositoryApi.getRepositoriesOfUser(userEmail,String.valueOf(page),size);
176
            while(resultSet.size() > 0 ){
177
                retDatasources.getDatasourcesOfUser().addAll(resultSet);
178
                page++;
179
                resultSet = repositoryApi.getRepositoriesOfUser(userEmail,String.valueOf(page),size);
180
            }
161 181

  
162
            retDatasources.setDatasourcesOfUser(this.repoAPI.getRepositoriesOfUser(userEmail, false));
163
            //geting Piwik Info
164
            for(Repository repository: retDatasources.getDatasourcesOfUser())
165
                repository.setPiwikInfo(this.getPiwikSiteForRepository(repository.getId()));
166

  
167
            if (includeShared) {
182
/*
183
	if (includeShared) {
168 184
                //TODO create dao to save-get shared datasourcesIDs
169 185
                List<String> sharedDatasourceIds = new ArrayList<String>();
170 186
                retDatasources.setSharedDatasources(this.repoAPI.getReposByIds(sharedDatasourceIds));
......
179 195
                for(Repository repository: retDatasources.getDatasourcesOfOthers())
180 196
                    repository.setPiwikInfo(this.getPiwikSiteForRepository(repository.getId()));
181 197
            }
182

  
183
            return retDatasources;
184

  
185
        } catch (Exception e) {
198
*/
199
        } catch (JSONException e) {
186 200
            LOGGER.error("Error while getting repositories of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers, e);
187
            if (e instanceof RepositoryServiceException) {
188
                throw (RepositoryServiceException) e;
189
            } else {
190
                emailUtils.reportException(e);
191
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
192
            }
201
            emailUtils.reportException(e);
202
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
193 203
        }
204
        return retDatasources;
194 205
    }
195 206

  
196 207
    @Override
......
199 210
            LOGGER.debug("Getting repositories(urls) of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers);
200 211
            List<String> retRepos = new ArrayList<String>();
201 212

  
202
            retRepos.addAll(this.repoAPI.getUrlsOfRepos(userEmail, false));
203

  
213
            int page = 1;
214
            String size = "10";
215
            List<String> resultSet = repositoryApi.getUrlsOfUserRepos(userEmail,String.valueOf(page),size);
216
            while(resultSet.size() > 0 ){
217
                retRepos.addAll(resultSet);
218
                page++;
219
                resultSet = repositoryApi.getUrlsOfUserRepos(userEmail,String.valueOf(page),size);
220
            }
204 221
            return retRepos;
205 222

  
206 223
        } catch (Exception e) {
207 224
            LOGGER.error("Error while getting repositories(urls) of user: " + userEmail + " . IncludeShared: " + includeShared + " . IncludeByOthers: " + includeByOthers, e);
208
            if (e instanceof RepositoryServiceException) {
209
                throw (RepositoryServiceException) e;
210
            } else {
211
                emailUtils.reportException(e);
212
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
213
            }
225
            emailUtils.reportException(e);
226
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
214 227
        }
215 228
    }
216 229

  
......
218 231
    public Repository getRepository(String repoId) throws RepositoryServiceException {
219 232
        try {
220 233
            LOGGER.debug("Getting repository with id: " + repoId);
234
            Repository repo = repositoryApi.getRepositoryById(repoId);
221 235

  
222
            Repository repo = this.repoAPI.getRepository(repoId);
223 236
            if (repo != null) {
224 237
                for (RepositoryInterface iFace : repo.getInterfaces()) {
225 238
                    if (!iFace.getContentDescription().equals("file::hybrid") && iFace.getAccessProtocol().equalsIgnoreCase("oai")) {
......
262 275
                LOGGER.debug("using \"dnet:countries\" vocabulary");
263 276
                countries.addAll(this.getVocabulary("dnet:countries").getEnglishNames());
264 277
            }
265

  
278
//            countries.addAll(repositoryApi.getDnetCountries());
266 279
            for (String country : countries) {
267 280
                countriesMap.put(country, WordUtils.capitalizeFully(country));
268 281
            }
......
289 302
    public List<Timezone> getTimezones() throws RepositoryServiceException {
290 303
        try {
291 304
            LOGGER.debug("Getting timezones from file");
305
//            return repositoryApi.getTimezones();
292 306
            return LocalVocabularies.timezones;
293 307
        } catch (Exception e) {
294 308
            LOGGER.error("Error while getting timezones from file", e);
......
302 316
    public List<String> getTypologies() throws RepositoryServiceException {
303 317
        try {
304 318
            LOGGER.debug("Getting typologies from file");
319
           // return repositoryApi.getTypologies();
305 320
            return LocalVocabularies.typologies;
306 321
        } catch (Exception e) {
307 322
            LOGGER.error("Error while getting typologies from file", e);
......
312 327

  
313 328
    @Override
314 329
    public Map<String, String> getDatasourceClasses(String mode) throws RepositoryServiceException {
315
        try {
316
            LOGGER.debug("Getting datasource classes for mode: " + mode);
317
            Map<String, String> retMap = new HashMap<String, String>();
318

  
319
            for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
320
                if (mode.equalsIgnoreCase("aggregator")) {
321
                    if (entry.getKey().contains("aggregator"))
322
                        retMap.put(entry.getKey(), entry.getValue());
323
                } else if (mode.equalsIgnoreCase("journal")) {
324
                    if (entry.getKey().contains("journal"))
325
                        retMap.put(entry.getKey(), entry.getValue());
326
                } else if (mode.equalsIgnoreCase("opendoar")) {
327
                    if (entry.getKey().contains("pubsrepository"))
328
                        retMap.put(entry.getKey(), entry.getValue());
329
                } else if (mode.equalsIgnoreCase("re3data")) {
330
                    if (entry.getKey().contains("datarepository"))
331
                        retMap.put(entry.getKey(), entry.getValue());
332
                }
333
            }
334
            return retMap;
335

  
336
        } catch (Exception e) {
337
            LOGGER.error("Error while getting datasource classes for mode: " + mode, e);
338
            emailUtils.reportException(e);
339
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
340
        }
330
        return repositoryApi.getDatasourceClasses(mode);
341 331
    }
342 332

  
343 333
    @Override
344 334
    public Map<String, String> getCompatibilityClasses(String mode) throws RepositoryServiceException {
345
        try {
346
            LOGGER.debug("Getting compatibility classes for mode: " + mode);
347
            Map<String, String> retMap = new HashMap<String, String>();
348

  
349
            Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
350
            boolean foundData = false;
351
            for (Map.Entry<String, String> entry : compatibilityClasses.entrySet()) {
352
                if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
353
                    return compatibilityClasses;
354
                else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
355
                    if (entry.getKey().matches("^openaire[1-9].0_data$")) {
356
                        retMap.put(entry.getKey(), entry.getValue());
357
                        foundData = true;
358
                    }
359
                } else {
360
                    if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver"))
361
                        retMap.put(entry.getKey(), entry.getValue());
362
                }
363
            }
364

  
365
            //TODO TO BE REMOVED WHEN VOCABULARIES ARE UPDATED
366
            if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA) && !foundData)
367
                retMap.put("openaire2.0_data", "OpenAIRE Data (funded, referenced datasets)");
368

  
369
            return retMap;
370

  
371
        } catch (Exception e) {
372
            LOGGER.error("Error while getting compatibility classes for mode: " + mode, e);
373
            emailUtils.reportException(e);
374
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
375
        }
335
        return repositoryApi.getCompatibilityClasses(mode);
376 336
    }
377 337

  
378 338
    @Override
......
380 340

  
381 341
        try {
382 342
            LOGGER.debug("Storing repository with name: " + repo.getOfficialName());
383

  
384 343
            List<RepositoryInterface> interfacesToRegister = new ArrayList<RepositoryInterface>();
385 344

  
386 345
            //TODO update map
......
410 369
                        repo.setNamespacePrefix(repo.getNamespacePrefix().concat("_"));
411 370
                }
412 371
            }
413

  
414
            this.repoAPI.storeRepository(repo, mode, interfacesToRegister);
415

  
416
        } catch (Exception e) {
372
            repositoryApi.addRepository(repo);
373
        } catch (JSONException e) {
417 374
            LOGGER.error("Error while Storing repository with name: " + repo.getOfficialName(), e);
418
            if (e instanceof RepositoryServiceException) {
419
                throw (RepositoryServiceException) e;
420
            } else {
421
                emailUtils.reportException(e);
422
                throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
423
            }
375
	    emailUtils.reportException(e);
376
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
424 377
        }
425 378
    }
426 379

  
......
428 381
    public void updateRepositoryInformation(Repository repo) throws RepositoryServiceException {
429 382
        try {
430 383
            LOGGER.debug("Updating information of repo: " + repo.getOfficialName());
431
            this.repoAPI.updateRepositoryInformation(repo);
432 384

  
385
	    //TODO SOS, check old API
386

  
387
            //this.repoAPI.updateRepositoryInformation(repo);
388
            repositoryApi.updateEnglishName(repo.getId(),repo.getEnglishName());
389
            repositoryApi.updateLatitude(repo.getId(), String.valueOf(repo.getLatitude()));
390
            repositoryApi.updateLongitude(repo.getId(), String.valueOf(repo.getLongitude()));
391
            repositoryApi.updateOfficialName(repo.getId(),repo.getOfficialName());
433 392
        } catch (Exception e) {
434 393
            LOGGER.error("Error while updating information of repo: " + repo.getOfficialName(), e);
435 394
            if (e instanceof RepositoryServiceException) {
......
466 425
        try {
467 426
            LOGGER.debug("inserting interface with id: " + iFace.getId());
468 427
            RepositoryInterface retIface = null;
469
            retIface = this.repoAPI.insertRepositoryInterfaceWithoutChecks(repoId, iFace, datatype);
428
            //retIface = this.repoAPI.insertRepositoryInterfaceWithoutChecks(repoId, iFace, datatype);
429

  
430
            Repository e = this.getRepository(repoId);
431
            iFace.setContentDescription("metadata");
432
            iFace.setCompliance("UNKNOWN");
433
            if(e.getDatasourceClass() == null) {
434
                if(datatype.equalsIgnoreCase("journal")) {
435
                    iFace.setTypology("pubsrepository::journal");
436
                } else if(datatype.equalsIgnoreCase("aggregator")) {
437
                    iFace.setTypology("aggregator::pubsrepository::unknown");
438
                } else {
439
                    iFace.setTypology("pubsrepository::unknown");
440
                }
441
            } else {
442
                iFace.setTypology(e.getDatasourceClass());
443
            }
444

  
445
            if(datatype.equals("re3data")) {
446
                iFace.setAccessFormat("oai_datacite");
447
            } else {
448
                iFace.setAccessFormat("oai_dc");
449
            }
450

  
451
            iFace.setAccessProtocol("oai");
452
            iFace.setRemovable(true);
453
            iFace.setMetadataIdentifierPath("//*[local-name()=\'header\']/*[local-name()=\'identifier\']");
454
            iFace.setId("api_________::" + repoId + "::" + UUID.randomUUID().toString().substring(0, 8));
455
            if(iFace.getAccessSet().isEmpty()) {
456
                LOGGER.debug("set is empty: " + iFace.getAccessSet());
457
                iFace.removeAccessSet();
458
            }
459

  
460
            retIface = repositoryApi.addRepositoryInterface(iFace);
470 461
            retIface.setComplianceName(this.getComplianceName(retIface.getCompliance()));
462

  
471 463
            return retIface;
472 464

  
473 465
        } catch (Exception e) {
......
492 484
    public void deleteInterfaces(String repoId, List<RepositoryInterface> iFaces, String datatype) throws RepositoryServiceException {
493 485
        try {
494 486
            LOGGER.debug("deleting interfaces of repo: " + repoId);
495
            this.repoAPI.deleteRepositoryInterfacesWithoutChecks(repoId, iFaces, datatype);
487
            //this.repoAPI.deleteRepositoryInterfacesWithoutChecks(repoId, iFaces, datatype);
496 488

  
489
            for(RepositoryInterface iFace : iFaces) {
490
                LOGGER.info("deleting repository interface with url/set/id: " + iFace.getBaseUrl() + "/"
491
                        + iFace.getAccessSet() + "/" + iFace.getId());
492
                repositoryApi.deleteRepositoryInterface(iFace.getId());
493
            }
494

  
497 495
        } catch (Exception e) {
498 496
            LOGGER.error("deleting interfaces of repo: " + repoId, e);
499 497
            if (e instanceof RepositoryServiceException) {
......
578 576
    @Override
579 577
    public PiwikInfo getPiwikSiteForRepository(String repoId) throws RepositoryServiceException {
580 578
        try {
579
            LOGGER.debug("Repo id -> " + repoId);
581 580
            return this.piwikDAO.getPiwikSiteForRepo(repoId);
582 581
        } catch (EmptyResultDataAccessException e) {
583 582
            return null;

Also available in: Unified diff