Project

General

Profile

Download (3.09 KB) Statistics
| Branch: | Tag: | Revision:
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.persistence.dao.initializer;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.stream.Collectors;
15

    
16
import org.apache.commons.lang3.StringUtils;
17

    
18
/**
19
 * @author a.kohlbecker
20
 * @since Nov 6, 2018
21
 *
22
 */
23
public class EntityInitStrategy {
24

    
25
    private static final String DOT = ".";
26

    
27
    List<String> propertyPaths = new ArrayList<>();
28

    
29
    public EntityInitStrategy(){
30

    
31
    }
32

    
33

    
34
    public EntityInitStrategy(List<String> propertyPaths){
35
        if(propertyPaths != null){
36
            this.propertyPaths.addAll(propertyPaths);
37
        }
38
    }
39

    
40
    public EntityInitStrategy(String ... propertyPaths){
41
        if(propertyPaths != null){
42
            this.propertyPaths.addAll(Arrays.asList(propertyPaths));
43
        }
44
    }
45

    
46
    /**
47
     * Extends the property base bath by all property definitions in the
48
     * <code>extensions</code> and adds the resulting property path to the
49
     * EntityInitStrategy.
50
     * <p>
51
     * Potential duplicate property paths de-duplicated.
52
     *
53
     * @param basePath
54
     *            can be NUll or empty to just append the extensions to the init
55
     *            strategies.
56
     * @param extensions
57
     * @param basePathIsCollection
58
     */
59
    public EntityInitStrategy extend(String basePath, EntityInitStrategy extensions, boolean basePathIsCollection) {
60
        return extend(basePath, extensions.getPropertyPaths(), basePathIsCollection);
61
    }
62

    
63
    /**
64
     * Extends the property base bath by all property definitions in the
65
     * <code>extensions</code> and adds the resulting property path to the
66
     * EntityInitStrategy.
67
     * <p>
68
     * Potential duplicate property paths de-duplicated.
69
     *
70
     * @param basePath
71
     *            can be NUll or empty to just add the extensions to the init
72
     *            strategies.
73
     * @param extensions
74
     * @param basePathIsCollection
75
     */
76
    public EntityInitStrategy extend(String basePath, List<String> extensions, boolean basePathIsCollection){
77
        for(String appendix : extensions){
78
            if(basePathIsCollection && (appendix.startsWith("$") || appendix.startsWith("*"))){
79
                // need to suppress wildcards, see AdvancedBeanInitializer.initializeNodeWildcard()
80
                continue;
81
            }
82
            if(!StringUtils.isEmpty(basePath)){
83
                propertyPaths.add(basePath + DOT + appendix);
84
                propertyPaths.remove(basePath);
85
            } else {
86
                propertyPaths.add(appendix);
87
            }
88
        }
89
        propertyPaths = propertyPaths.stream().distinct().collect(Collectors.toList());
90
        return this;
91
    }
92

    
93

    
94
    /**
95
     * @return
96
     */
97
    public List<String> getPropertyPaths() {
98
        return propertyPaths;
99
    }
100

    
101
    @Override
102
    public EntityInitStrategy clone() {
103
        return new EntityInitStrategy(this.propertyPaths);
104

    
105
    }
106
}
(6-6/15)