Project

General

Profile

Download (7.85 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.common.DefinedTerm;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.model.reference.ReferenceType;
27
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
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 void updateTitleCache(Class<? extends Reference> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Reference> cacheStrategy, IProgressMonitor monitor) {
50
        if (clazz == null){
51
            clazz = Reference.class;
52
        }
53
        super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
54
    }
55

    
56

    
57
    @Override
58
    protected void setOtherCachesNull(Reference ref) {
59
        if (! ref.isProtectedAbbrevTitleCache()){
60
            ref.setAbbrevTitleCache(null, false);
61
        }
62
    }
63

    
64

    
65
    @Override
66
    @Autowired
67
    protected void setDao(IReferenceDao dao) {
68
        this.dao = dao;
69
    }
70

    
71
    @Override
72
    public List<UuidAndTitleCache<Reference>> getUuidAndTitle() {
73

    
74
        return dao.getUuidAndTitle();
75
    }
76

    
77
    @Override
78
    public List<Reference> getAllReferencesForPublishing(){
79
        return dao.getAllNotNomenclaturalReferencesForPublishing();
80
    }
81

    
82
    @Override
83
    public List<Reference> getAllNomenclaturalReferences() {
84

    
85
        return dao.getAllNomenclaturalReferences();
86
    }
87

    
88
    @Override
89
    public List<TaxonBase> listCoveredTaxa(Reference reference, boolean includeSubordinateReferences, List<String> propertyPaths) {
90

    
91
        List<TaxonBase> taxonList = dao.listCoveredTaxa(reference, includeSubordinateReferences, null, propertyPaths);
92

    
93
        return taxonList;
94
    }
95

    
96
    @Override
97
    public DeleteResult delete(Reference reference) {
98
        //check whether the reference is used somewhere
99
        DeleteResult result = isDeletable(reference.getUuid(), null);
100

    
101
        if (result.isOk()){
102
            dao.delete(reference);
103
            result.addDeletedObject(reference);
104
        }
105

    
106
        return result;
107
    }
108

    
109
    @Override
110
    @Transactional(readOnly=false)
111
    public DeleteResult delete(UUID referenceUuid) {
112
        return delete(dao.load(referenceUuid));
113
    }
114

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

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

    
127
    @Override
128
    public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCacheForAuthor(Integer limit, String pattern, ReferenceType type) {
129
        return dao.getUuidAndAbbrevTitleCacheForAuthor(limit, pattern, null);
130
    }
131

    
132
    @Override
133
    public List<UuidAndTitleCache<Reference>> getUuidAndTitleCache(Integer limit, String pattern, ReferenceType type) {
134
        ReferenceType inReferenceType = null;
135
        inReferenceType = getInReferenceType(type);
136
        return dao.getUuidAndTitleCache(limit, pattern, inReferenceType);
137
    }
138

    
139

    
140
    @Transactional(readOnly = true)
141
    @Override
142
    public List<IdentifiedEntityDTO<Reference>> listByIdentifierAbbrev(
143
            String identifier, DefinedTerm identifierType, MatchMode matchmode,
144
            Integer limit) {
145

    
146
        long numberOfResults = dao.countByIdentifier(Reference.class, identifier, identifierType, matchmode);
147
        List<Object[]> daoResults = new ArrayList<Object[]>();
148
        if(numberOfResults > 0) { // no point checking again
149
            daoResults = dao.findByIdentifierAbbrev( identifier, identifierType,
150
                    matchmode,  limit);
151
        }
152

    
153
        List<IdentifiedEntityDTO<Reference>> result = new ArrayList<>();
154
        for (Object[] daoObj : daoResults){
155
            result.add(new IdentifiedEntityDTO<Reference>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3],(String)daoObj[4]));
156

    
157
        }
158
        return result;
159
    }
160

    
161
    @Transactional(readOnly = true)
162
    @Override
163
    public List<IdentifiedEntityDTO<Reference>> listByIdentifierAndTitleCacheAbbrev(
164
            String identifier, DefinedTerm identifierType, MatchMode matchmode,
165
            Integer limit) {
166

    
167
        long numberOfResults = dao.countByIdentifier(Reference.class, identifier, identifierType, matchmode);
168
        long numberOfResultsTitle = dao.countByTitle(identifier);
169
        List<Object[]> daoResults = new ArrayList<Object[]>();
170
        List<UuidAndTitleCache<Reference>> daoResultsTitle = new ArrayList();
171
        if(numberOfResults > 0) { // no point checking again
172
            daoResults = dao.findByIdentifierAbbrev( identifier, identifierType,
173
                    matchmode,  limit);
174
        }
175
        daoResultsTitle = dao.getUuidAndAbbrevTitleCache(100, identifier, null);
176

    
177
        List<IdentifiedEntityDTO<Reference>> result = new ArrayList<>();
178
        for (Object[] daoObj : daoResults){
179
            result.add(new IdentifiedEntityDTO<Reference>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3],(String)daoObj[4]));
180

    
181
        }
182
        for (UuidAndTitleCache<Reference> uuidAndTitleCache: daoResultsTitle){
183
            result.add(new IdentifiedEntityDTO<>(null, null, uuidAndTitleCache.getUuid(), uuidAndTitleCache.getTitleCache(), uuidAndTitleCache.getAbbrevTitleCache()));
184
        }
185
        return result;
186
    }
187

    
188

    
189
    private ReferenceType getInReferenceType(ReferenceType type){
190
        ReferenceType inReferenceType = null;
191
        if (type.equals(ReferenceType.Article)){
192
            inReferenceType = ReferenceType.Journal;
193
        } else if (type.equals(ReferenceType.BookSection)){
194
            inReferenceType = ReferenceType.Book;
195
        } else if (type.equals(ReferenceType.InProceedings) ){
196
            inReferenceType = ReferenceType.Proceedings;
197
        } else if (type.equals(ReferenceType.Book) || type.equals(ReferenceType.Proceedings)){
198
            inReferenceType = ReferenceType.PrintSeries;
199
        } else if (type.equals(ReferenceType.Generic)){
200
            inReferenceType = ReferenceType.Generic;
201
        }
202
        return inReferenceType;
203
    }
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
    @Override
209
    public List<UuidAndTitleCache<Reference>> getUuidAndTitleCacheForUUIDS(Set<UUID> uuids) {
210
        return dao.getUuidAndTitle(uuids);
211
    }
212

    
213
}
(89-89/103)