Project

General

Profile

Download (8.36 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.config.IIdentifiableEntityServiceConfigurator;
23
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
24
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
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.model.term.DefinedTerm;
29
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
30
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
31
import eu.etaxonomy.cdm.persistence.query.MatchMode;
32
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
33

    
34

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

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

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

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

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

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

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

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

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

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

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

    
94
        return result;
95
    }
96

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

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

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

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

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

    
127

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

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

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

    
145
        }
146
        return result;
147
    }
148

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

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

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

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

    
176

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

    
193
    /**
194
     * {@inheritDoc}
195
     */
196
    @Override
197
    public List<UuidAndTitleCache<Reference>> getUuidAndTitleCacheForUUIDS(Set<UUID> uuids, ReferenceType refType ) {
198

    
199
        return dao.getUuidAndTitle(uuids, getInReferenceType(refType));
200

    
201
    }
202

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

    
210
    }
211

    
212
    @Override
213
    public List<Reference> findByTitleAndAbbrevTitle(IIdentifiableEntityServiceConfigurator<Reference> config){
214
        return dao.findByTitleAndAbbrevTitle(config.getClazz(),config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getCriteria(), config.getPageSize(), config.getPageNumber(), config.getOrderHints(), config.getPropertyPaths());
215
    }
216

    
217
}
(80-80/97)