Project

General

Profile

Download (7.65 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Service;
20
import org.springframework.transaction.annotation.Transactional;
21

    
22
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
23
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.reference.ReferenceType;
26
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27
import eu.etaxonomy.cdm.model.term.DefinedTerm;
28
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
29
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
30
import eu.etaxonomy.cdm.persistence.query.MatchMode;
31
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
32

    
33

    
34
@Service
35
@Transactional(readOnly = true)
36
public class ReferenceServiceImpl extends IdentifiableServiceBase<Reference,IReferenceDao> implements IReferenceService {
37

    
38
    static Logger logger = Logger.getLogger(ReferenceServiceImpl.class);
39

    
40
    /**
41
     * Constructor
42
     */
43
    public ReferenceServiceImpl(){
44
        if (logger.isDebugEnabled()) { logger.debug("Load ReferenceService Bean"); }
45
    }
46

    
47
    @Override
48
    @Transactional(readOnly = false)
49
    public UpdateResult updateCaches(Class<? extends Reference> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Reference> cacheStrategy, IProgressMonitor monitor) {
50
        if (clazz == null){
51
            clazz = Reference.class;
52
        }
53
        return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
54
    }
55

    
56
    @Override
57
    @Autowired
58
    protected void setDao(IReferenceDao dao) {
59
        this.dao = dao;
60
    }
61

    
62
    @Override
63
    public List<UuidAndTitleCache<Reference>> getUuidAndTitle() {
64
        return dao.getUuidAndTitle();
65
    }
66

    
67
    @Override
68
    public List<Reference> getAllReferencesForPublishing(){
69
        return dao.getAllNotNomenclaturalReferencesForPublishing();
70
    }
71

    
72
    @Override
73
    public List<Reference> getAllNomenclaturalReferences() {
74
        return dao.getAllNomenclaturalReferences();
75
    }
76

    
77
    @Override
78
    public List<TaxonBase> listCoveredTaxa(Reference reference, boolean includeSubordinateReferences, List<String> propertyPaths) {
79
        List<TaxonBase> taxonList = dao.listCoveredTaxa(reference, includeSubordinateReferences, null, propertyPaths);
80
        return taxonList;
81
    }
82

    
83
    @Override
84
    public DeleteResult delete(Reference reference) {
85
        //check whether the reference is used somewhere
86
        DeleteResult result = isDeletable(reference.getUuid(), null);
87

    
88
        if (result.isOk()){
89
            dao.delete(reference);
90
            result.addDeletedObject(reference);
91
        }
92

    
93
        return result;
94
    }
95

    
96
    @Override
97
    @Transactional(readOnly=false)
98
    public DeleteResult delete(UUID referenceUuid) {
99
        return delete(dao.load(referenceUuid));
100
    }
101

    
102
    @Override
103
    public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCache(Integer limit, String pattern) {
104
        return dao.getUuidAndAbbrevTitleCache(limit, pattern, null);
105
    }
106

    
107
    @Override
108
    public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCache(Integer limit, String pattern, ReferenceType type) {
109
        ReferenceType inReferenceType = null;
110
        inReferenceType = getInReferenceType(type);
111
        return dao.getUuidAndAbbrevTitleCache(limit, pattern, inReferenceType);
112
    }
113

    
114
    @Override
115
    public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCacheForAuthor(Integer limit, String pattern, ReferenceType type) {
116
        return dao.getUuidAndAbbrevTitleCacheForAuthor(limit, pattern, null);
117
    }
118

    
119
    @Override
120
    public List<UuidAndTitleCache<Reference>> getUuidAndTitleCache(Integer limit, String pattern, ReferenceType type) {
121
        ReferenceType inReferenceType = null;
122
        inReferenceType = getInReferenceType(type);
123
        return dao.getUuidAndTitleCache(limit, pattern, inReferenceType);
124
    }
125

    
126

    
127
    @Transactional(readOnly = true)
128
    @Override
129
    public List<IdentifiedEntityDTO<Reference>> listByIdentifierAbbrev(
130
            String identifier, DefinedTerm identifierType, MatchMode matchmode,
131
            Integer limit) {
132

    
133
        long numberOfResults = dao.countByIdentifier(Reference.class, identifier, identifierType, matchmode);
134
        List<Object[]> daoResults = new ArrayList<Object[]>();
135
        if(numberOfResults > 0) { // no point checking again
136
            daoResults = dao.findByIdentifierAbbrev( identifier, identifierType,
137
                    matchmode,  limit);
138
        }
139

    
140
        List<IdentifiedEntityDTO<Reference>> result = new ArrayList<>();
141
        for (Object[] daoObj : daoResults){
142
            result.add(new IdentifiedEntityDTO<Reference>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3],(String)daoObj[4]));
143

    
144
        }
145
        return result;
146
    }
147

    
148
    @Transactional(readOnly = true)
149
    @Override
150
    public List<IdentifiedEntityDTO<Reference>> listByIdentifierAndTitleCacheAbbrev(
151
            String identifier, DefinedTerm identifierType, MatchMode matchmode,
152
            Integer limit) {
153

    
154
        long numberOfResults = dao.countByIdentifier(Reference.class, identifier, identifierType, matchmode);
155
        long numberOfResultsTitle = dao.countByTitle(identifier);
156
        List<Object[]> daoResults = new ArrayList<>();
157
        List<UuidAndTitleCache<Reference>> daoResultsTitle = new ArrayList();
158
        if(numberOfResults > 0) { // no point checking again
159
            daoResults = dao.findByIdentifierAbbrev( identifier, identifierType,
160
                    matchmode,  limit);
161
        }
162
        daoResultsTitle = dao.getUuidAndAbbrevTitleCache(100, identifier, null);
163

    
164
        List<IdentifiedEntityDTO<Reference>> result = new ArrayList<>();
165
        for (Object[] daoObj : daoResults){
166
            result.add(new IdentifiedEntityDTO<Reference>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3],(String)daoObj[4]));
167

    
168
        }
169
        for (UuidAndTitleCache<Reference> uuidAndTitleCache: daoResultsTitle){
170
            result.add(new IdentifiedEntityDTO<>(null, null, uuidAndTitleCache.getUuid(), uuidAndTitleCache.getTitleCache(), uuidAndTitleCache.getAbbrevTitleCache()));
171
        }
172
        return result;
173
    }
174

    
175

    
176
    private ReferenceType getInReferenceType(ReferenceType type){
177
        ReferenceType inReferenceType = null;
178
        if (type.equals(ReferenceType.Article)){
179
            inReferenceType = ReferenceType.Journal;
180
        } else if (type.equals(ReferenceType.BookSection)){
181
            inReferenceType = ReferenceType.Book;
182
        } else if (type.equals(ReferenceType.InProceedings) ){
183
            inReferenceType = ReferenceType.Proceedings;
184
        } else if (type.equals(ReferenceType.Book) || type.equals(ReferenceType.Proceedings)){
185
            inReferenceType = ReferenceType.PrintSeries;
186
        } else if (type.equals(ReferenceType.Generic)){
187
            inReferenceType = ReferenceType.Generic;
188
        }
189
        return inReferenceType;
190
    }
191

    
192
    /**
193
     * {@inheritDoc}
194
     */
195
    @Override
196
    public List<UuidAndTitleCache<Reference>> getUuidAndTitleCacheForUUIDS(Set<UUID> uuids) {
197
        return dao.getUuidAndTitle(uuids);
198
    }
199

    
200
}
(89-89/103)