Project

General

Profile

« Previous | Next » 

Revision cac97126

Added by Andreas Müller over 5 years ago

ref #3560 implement loadTreeBranchToTaxon (pathTo) for unpublished

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/exception/UnpublishedException.java
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.exception;
10

  
11
/**
12
 * An Exception shown if data is unpublished and therefore under the given
13
 * circumstances not readable.
14
 *
15
 * @author a.mueller
16
 * @since 07.06.2018
17
 */
18
public class UnpublishedException extends Exception {
19

  
20
    private static final long serialVersionUID = 1901079330887825007L;
21

  
22
    /**
23
     *
24
     */
25
    public UnpublishedException() {
26
        super();
27
    }
28

  
29
    /**
30
     * @param message
31
     * @param cause
32
     */
33
    public UnpublishedException(String message, Throwable cause) {
34
        super(message, cause);
35
    }
36

  
37
    /**
38
     * @param message
39
     */
40
    public UnpublishedException(String message) {
41
        super(message);
42
    }
43

  
44
    /**
45
     * @param cause
46
     */
47
    public UnpublishedException(Throwable cause) {
48
        super(cause);
49
    }
50

  
51
}
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/ClassificationPortalListController.java
9 9
package eu.etaxonomy.cdm.remote.controller;
10 10

  
11 11
import java.io.IOException;
12
import java.util.ArrayList;
12 13
import java.util.Arrays;
13 14
import java.util.List;
14 15
import java.util.UUID;
......
28 29
import eu.etaxonomy.cdm.api.service.IClassificationService;
29 30
import eu.etaxonomy.cdm.api.service.ITaxonService;
30 31
import eu.etaxonomy.cdm.api.service.ITermService;
32
import eu.etaxonomy.cdm.exception.UnpublishedException;
31 33
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
32 34
import eu.etaxonomy.cdm.model.name.Rank;
33 35
import eu.etaxonomy.cdm.model.taxon.Classification;
......
236 238
            HttpServletResponse response) {
237 239
        logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
238 240

  
241
        boolean includeUnpublished = NO_UNPUBLISHED;
242

  
239 243
        Classification tree = service.find(treeUuid);
240 244
        Rank rank = findRank(rankUuid);
241 245
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
242 246

  
243
        return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);
247
        try {
248
            return service.loadTreeBranchToTaxon(taxon, tree, rank, includeUnpublished, NODE_INIT_STRATEGY);
249
        } catch (UnpublishedException e) {
250
            return new ArrayList<>();
251
        }
244 252
    }
245 253

  
246 254
    /**
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/dto/NameCatalogueController.java
48 48
import eu.etaxonomy.cdm.api.service.search.DocumentSearchResult;
49 49
import eu.etaxonomy.cdm.api.service.search.LuceneParseException;
50 50
import eu.etaxonomy.cdm.common.DocUtils;
51
import eu.etaxonomy.cdm.exception.UnpublishedException;
51 52
import eu.etaxonomy.cdm.hibernate.search.AcceptedTaxonBridge;
52 53
import eu.etaxonomy.cdm.io.dwca.in.DwcaImportTransformer;
53 54
import eu.etaxonomy.cdm.model.common.CdmBase;
......
802 803
            @RequestParam(value = "classification", required = false, defaultValue = CLASSIFICATION_DEFAULT) String classificationType,
803 804
            @RequestParam(value = "include", required = false, defaultValue = "") String[] includes,
804 805
            HttpServletRequest request, HttpServletResponse response) throws IOException {
806

  
807
        boolean includeUnpublished = NO_UNPUBLISHED;
808

  
805 809
        ModelAndView mv = new ModelAndView();
806 810
        List<RemoteResponse> tiList = new ArrayList<>();
807 811
        // loop through each name uuid
......
821 825
                    Taxon taxon = (Taxon) tb;
822 826
                    // build classification map
823 827
                    boolean includeUuids = Arrays.asList(includes).contains(INCLUDE_CLUUIDS);
824
                    Map<String,Map> classificationMap = getClassification(taxon, classificationType, includeUuids);
828
                    Map<String,Map> classificationMap = getClassification(taxon, classificationType, includeUuids, includeUnpublished);
825 829

  
826 830
                    logger.info("taxon uuid " + taxon.getUuid().toString() + " original hash code : " + System.identityHashCode(taxon) + ", name class " + taxon.getName().getClass().getName());
827 831
                    // update taxon information object with taxon related data
......
1139 1143
        List<RemoteResponse> ansList = new ArrayList<RemoteResponse>();
1140 1144
        logger.info("doGetAcceptedNameSearch()");
1141 1145

  
1146
        boolean includeUnpublished = NO_UNPUBLISHED;
1147

  
1142 1148
        // if search type is not known then return error
1143 1149
        if (!searchType.equals(NAME_SEARCH) && !searchType.equals(TITLE_SEARCH)) {
1144 1150
            ErrorResponse er = new ErrorResponse();
......
1187 1193
                            Taxon accTaxon = synonym.getAcceptedTaxon();
1188 1194
                            if (accTaxon != null) {
1189 1195
                                INonViralName accNvn = CdmBase.deproxy(accTaxon.getName());
1190
                                Map<String, Map> classificationMap = getClassification(accTaxon, CLASSIFICATION_DEFAULT, false);
1196
                                Map<String, Map> classificationMap = getClassification(accTaxon, CLASSIFICATION_DEFAULT, false, includeUnpublished);
1191 1197
                                ans.addToResponseList(accNvn.getNameCache(),accNvn.getAuthorshipCache(), accNvn.getRank().getTitleCache(), classificationMap);
1192 1198
                            }
1193 1199
                        } else {
......
1205 1211
                                }
1206 1212
                            }
1207 1213
                            if(isConceptRelationship) {
1208
                                Map classificationMap = getClassification(taxon, CLASSIFICATION_DEFAULT, false);
1214
                                Map classificationMap = getClassification(taxon, CLASSIFICATION_DEFAULT, false, includeUnpublished);
1209 1215
                                ans.addToResponseList(nvn.getNameCache(), nvn.getAuthorshipCache(),nvn.getRank().getTitleCache(), classificationMap);
1210 1216
                            }
1211 1217

  
......
1364 1370
    /**
1365 1371
     * Build classification map.
1366 1372
     */
1367
    private Map<String, Map> getClassification(Taxon taxon, String classificationType, boolean includeUuids) {
1373
    private Map<String, Map> getClassification(Taxon taxon, String classificationType, boolean includeUuids, boolean includeUnpublished) {
1368 1374
        // Using TreeMap is important, because we need the sorting of the classification keys
1369 1375
        // in the map to be stable.
1370
        TreeMap<String, Map> sourceClassificationMap = buildClassificationMap(taxon, includeUuids);
1376
        TreeMap<String, Map> sourceClassificationMap = buildClassificationMap(taxon, includeUuids, includeUnpublished);
1371 1377

  
1372 1378
        // if classification key is 'default' then return the default element of the map
1373 1379
        if(classificationType.equals(CLASSIFICATION_DEFAULT) && !sourceClassificationMap.isEmpty()) {
......
1381 1387
        } else if(classificationType.equals(CLASSIFICATION_ALL)) {
1382 1388
            return sourceClassificationMap;
1383 1389
        } else {
1384
            return new TreeMap<String,Map>();
1390
            return new TreeMap<>();
1385 1391
        }
1386 1392
    }
1387 1393

  
1388 1394
    /**
1389 1395
     * Build classification map.
1390 1396
     */
1391
    private TreeMap<String, Map> buildClassificationMap(Taxon taxon, boolean includeUuid) {
1397
    private TreeMap<String, Map> buildClassificationMap(Taxon taxon, boolean includeUuid, boolean includeUnpublished) {
1392 1398
        // Using TreeMap is important, because we need the sorting of the classification keys
1393 1399
        // in the map to be stable.
1394
        TreeMap<String, Map> sourceClassificationMap = new TreeMap<String, Map>();
1400
        TreeMap<String, Map> sourceClassificationMap = new TreeMap<>();
1395 1401
        Set<TaxonNode> taxonNodes = taxon.getTaxonNodes();
1396 1402
        //loop through taxon nodes and build classification map for each classification key
1397 1403
        for (TaxonNode tn : taxonNodes) {
1398
            Map<String, Object> classificationMap = new LinkedHashMap<String, Object>();
1399
            List<TaxonNode> tnList = classificationService.loadTreeBranchToTaxon(taxon,
1400
                    tn.getClassification(), null, TAXON_NODE_INIT_STRATEGY);
1401
            for (TaxonNode classificationtn : tnList) {
1402
                if(includeUuid) {
1403
                    // creating map object with <name, uuid> elements
1404
                    Map<String, String> clMap = new HashMap<String, String>();
1405
                    clMap.put("name",classificationtn.getTaxon().getName().getTitleCache());
1406
                    clMap.put("uuid",classificationtn.getTaxon().getUuid().toString());
1407
                    classificationMap.put(classificationtn.getTaxon().getName().getRank().getTitleCache(), clMap);
1408
                } else {
1409
                    classificationMap.put(classificationtn.getTaxon().getName().getRank().getTitleCache(),
1410
                            classificationtn.getTaxon().getName().getTitleCache());
1404
            Map<String, Object> classificationMap = new LinkedHashMap<>();
1405
            try {
1406
                List<TaxonNode> tnList = classificationService.loadTreeBranchToTaxon(taxon,
1407
                        tn.getClassification(), null, includeUnpublished, TAXON_NODE_INIT_STRATEGY);
1408

  
1409
                for (TaxonNode classificationtn : tnList) {
1410
                    if(includeUuid) {
1411
                        // creating map object with <name, uuid> elements
1412
                        Map<String, String> clMap = new HashMap<>();
1413
                        clMap.put("name",classificationtn.getTaxon().getName().getTitleCache());
1414
                        clMap.put("uuid",classificationtn.getTaxon().getUuid().toString());
1415
                        classificationMap.put(classificationtn.getTaxon().getName().getRank().getTitleCache(), clMap);
1416
                    } else {
1417
                        classificationMap.put(classificationtn.getTaxon().getName().getRank().getTitleCache(),
1418
                                classificationtn.getTaxon().getName().getTitleCache());
1419
                    }
1411 1420
                }
1421
            } catch (UnpublishedException e) {
1422
                //classificationMap stays empty for this node
1412 1423
            }
1413 1424
            String cname = removeInternalWhitespace(tn.getClassification().getTitleCache());
1414 1425
            logger.info("Building classification map " + cname);
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ClassificationServiceImpl.java
43 43
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
44 44
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
45 45
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
46
import eu.etaxonomy.cdm.exception.UnpublishedException;
46 47
import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
47 48
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
48 49
import eu.etaxonomy.cdm.model.common.CdmBase;
......
236 237

  
237 238
    }
238 239

  
240

  
239 241
    /**
240
     * @implements {@link IClassificationService#loadTreeBranch(TaxonNode, Rank, List)
241
     * @see eu.etaxonomy.cdm.api.service.ITaxonService#loadTreeBranchTo(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
242
     * FIXME Candidate for harmonization
243
     * move to classification service
242
     * {@inheritDoc}
244 243
     */
245 244
    @Override
246
    public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank, List<String> propertyPaths){
245
    public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank,
246
            boolean includeUnpublished, List<String> propertyPaths) throws UnpublishedException{
247 247

  
248 248
        TaxonNode thisNode = taxonNodeDao.load(taxonNode.getUuid(), propertyPaths);
249 249
        if(baseRank != null){
250 250
            baseRank = (Rank) termDao.load(baseRank.getUuid());
251 251
        }
252
        List<TaxonNode> pathToRoot = new ArrayList<TaxonNode>();
252
        if (!includeUnpublished && thisNode.getTaxon() != null && thisNode.getTaxon().isPublish()){
253
            throw new UnpublishedException("Final taxon in tree branch is unpublished.");
254
        }
255

  
256
        List<TaxonNode> pathToRoot = new ArrayList<>();
253 257
        pathToRoot.add(thisNode);
254 258

  
255 259
        while(!thisNode.isTopmostNode()){
256 260
            //TODO why do we need to deproxy here?
257 261
            //     without this thisNode.getParent() will return NULL in
258 262
            //     some cases (environment dependend?) even if the parent exits
259
            TaxonNode parentNode = CdmBase.deproxy(thisNode, TaxonNode.class).getParent();
263
            TaxonNode parentNode = CdmBase.deproxy(thisNode).getParent();
260 264

  
261 265
            if(parentNode == null){
262
                throw new NullPointerException("taxonNode " + thisNode + " must have a parent since it is not top most");
266
                throw new NullPointerException("Taxon node " + thisNode + " must have a parent since it is not top most");
263 267
            }
264 268
            if(parentNode.getTaxon() == null){
265
                throw new NullPointerException("The taxon associated with taxonNode " + parentNode + " is NULL");
269
                throw new NullPointerException("The taxon associated with taxon node " + parentNode + " is NULL");
270
            }
271
            if(!includeUnpublished && !parentNode.getTaxon().isPublish()){
272
                throw new UnpublishedException("Some taxon in tree branch is unpublished.");
266 273
            }
267 274
            if(parentNode.getTaxon().getName() == null){
268 275
                throw new NullPointerException("The name of the taxon associated with taxonNode " + parentNode + " is NULL");
269 276
            }
270 277

  
271
            Rank parentNodeRank = parentNode.getTaxon().getName() == null ? null : parentNode.getTaxon().getName().getRank();
278
            Rank parentNodeRank = (parentNode.getTaxon().getName() == null) ? null : parentNode.getTaxon().getName().getRank();
272 279
            // stop if the next parent is higher than the baseRank
273 280
            if(baseRank != null && parentNodeRank != null && baseRank.isLower(parentNodeRank)){
274 281
                break;
......
286 293
    }
287 294

  
288 295
    @Override
289
    public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank, List<String> propertyPaths){
290
        Classification tree = dao.load(classification.getUuid());
291
        taxon = (Taxon) taxonDao.load(taxon.getUuid());
292
        TaxonNode node = tree.getNode(taxon);
296
    public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank,
297
            boolean includeUnpublished, List<String> propertyPaths) throws UnpublishedException{
298

  
299
        UUID nodeUuid = getTaxonNodeUuidByTaxonUuid(classification.getUuid(), taxon.getUuid());
300
        TaxonNode node = taxonNodeService.find(nodeUuid);
293 301
        if(node == null){
294 302
            logger.warn("The specified taxon is not found in the given tree.");
295 303
            return null;
296 304
        }
297
        return loadTreeBranch(node, baseRank, propertyPaths);
305
        return loadTreeBranch(node, baseRank, includeUnpublished, propertyPaths);
298 306
    }
299 307

  
300 308

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IClassificationService.java
19 19
import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
20 20
import eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO;
21 21
import eu.etaxonomy.cdm.api.service.pager.Pager;
22
import eu.etaxonomy.cdm.exception.UnpublishedException;
22 23
import eu.etaxonomy.cdm.model.common.MarkerType;
23 24
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
24 25
import eu.etaxonomy.cdm.model.name.Rank;
......
148 149
     * @param propertyPaths
149 150
     *            the initialization strategy for the returned TaxonNode
150 151
     *            instances.
152
     * @param includeUnpublished
153
     *            if <code>true</code> no {@link UnpublishedException}
154
     *            is thrown if any of the taxa in the branch are unpublished
151 155
     * @return the path of nodes from the <b>base node</b> to the node of the
152
     *         specified taxon.
156
     *            specified taxon.
157
     * @throws UnpublishedException
158
     *            if any of the taxa in the path is unpublished an {@link UnpublishedException} is thrown.
153 159
     */
154
    public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank, List<String> propertyPaths);
160
    public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank, boolean includeUnpublished,
161
            List<String> propertyPaths) throws UnpublishedException;
155 162

  
156 163
    /**
157 164
     * Although this method seems to be a redundant alternative to {@link #loadChildNodesOfTaxonNode(TaxonNode, List)} it is an important
......
166 173
     *            Nodes of this rank or in case this rank does not exist in the
167 174
     *            current branch the next lower rank is taken as as root node for
168 175
     *            this rank henceforth called the <b>base node</b>.
176
     * @param includeUnpublished
177
     *            if <code>true</code> no {@link UnpublishedException}
178
     *            is thrown if any of the taxa in the branch are unpublished
169 179
     * @param propertyPaths
170 180
     *            the initialization strategy for the returned TaxonNode
171 181
     *            instances.
172 182
     * @return the path of nodes from the <b>base node</b> to the node of the specified
173
     *         taxon.
183
     *            taxon.
184
     * @throws UnpublishedException
185
     *            if any of the taxa in the path is unpublished an {@link UnpublishedException} is thrown
174 186
     */
175
    public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank, List<String> propertyPaths);
187
    public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank,
188
            boolean includeUnpublished, List<String> propertyPaths) throws UnpublishedException;
176 189

  
177 190
    public List<TaxonNode> listChildNodesOfTaxon(UUID taxonUuid, UUID classificationUuid, boolean includeUnpublished,
178 191
            Integer pageSize, Integer pageIndex, List<String> propertyPaths);

Also available in: Unified diff