Project

General

Profile

Download (2.78 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.debug;
10

    
11
import java.io.PrintStream;
12

    
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.hibernate.Session;
16

    
17
import eu.etaxonomy.cdm.cache.CdmEntityCache;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @since 08.11.2017
23
 */
24
public class PersistentContextAnalyzer extends CdmEntityCache {
25

    
26
    @SuppressWarnings("unused")
27
    private final static Logger logger = LogManager.getLogger();
28

    
29
    private static final char HASH_SEPARATOR = '.';
30

    
31
    private static final String IN_PERSITENT_CONTEXT = "*";
32

    
33
    private Session session;
34

    
35
    private boolean showHashCodes = false;
36

    
37
    public PersistentContextAnalyzer(CdmBase entity, Session session){
38
        this.session = session;
39
        this.entities.add(entity);
40
        update();
41
    }
42

    
43
    public PersistentContextAnalyzer(CdmBase entity){
44
        this(entity, null);
45
    }
46

    
47
    public PersistentContextAnalyzer(CdmEntityCache entityCache, Session session){
48
        this.session = session;
49
        this.entities.addAll(entityCache.getEntities());
50

    
51
    }
52

    
53

    
54
    /**
55
     * @param printStream
56
     */
57
    @Override
58
    protected void printLegend(PrintStream printStream) {
59
        printStream.println("PersistentContextAnalyzer legend: ");
60
        printStream.println("    - '.{objectHash}': unique copy entity, followed by object hash (only shown when showHashCodes is enabled)");
61
        printStream.println("    - '!{objectHash}': detected copy entity, followed by object hash");
62
        printStream.println("    - '*': entity mapped in persistent context");
63
    }
64

    
65
    @Override
66
    protected String analyzeMore(CdmBase bean, EntityKey entityKey, String flags, CdmBase mappedEntity) {
67

    
68
        boolean hashAdded = false;
69

    
70
        if(mappedEntity != null && mappedEntity != bean) {
71
            flags += bean.hashCode();
72
            hashAdded = true;
73
        }
74
        if(session != null && session.contains(bean)){
75
            flags += IN_PERSITENT_CONTEXT;
76
        }
77
        if(showHashCodes && ! hashAdded){
78
            flags += HASH_SEPARATOR + bean.hashCode();
79
        }
80
        return flags;
81
    }
82

    
83
    /**
84
     * @return the showHashCodes
85
     */
86
    public boolean isShowHashCodes() {
87
        return showHashCodes;
88
    }
89

    
90
    /**
91
     * @param showHashCodes the showHashCodes to set
92
     */
93
    public void setShowHashCodes(boolean showHashCodes) {
94
        boolean runUpdate = this.showHashCodes != showHashCodes;
95
        this.showHashCodes = showHashCodes;
96
        if(runUpdate){
97
            update();
98
        }
99
    }
100

    
101
}
    (1-1/1)