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.log4j.Logger;
14
import org.hibernate.Session;
15

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

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

    
26

    
27
    private static final char HASH_SEPARATOR = '.';
28

    
29
    private static final String IN_PERSITENT_CONTEXT = "*";
30

    
31
    private final static Logger logger = Logger.getLogger(PersistentContextAnalyzer.class);
32

    
33
    private Session session;
34

    
35
    private boolean showHashCodes = false;
36

    
37
    /**
38
     * @param entity
39
     * @param session
40
     */
41
    public PersistentContextAnalyzer(CdmBase entity, Session session){
42
        this.session = session;
43
        this.entities.add(entity);
44
        update();
45
    }
46

    
47
    public PersistentContextAnalyzer(CdmBase entity){
48
        this(entity, null);
49
    }
50

    
51
    public PersistentContextAnalyzer(CdmEntityCache entityCache, Session session){
52
        this.session = session;
53
        this.entities.addAll(entityCache.getEntities());
54

    
55
    }
56

    
57

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

    
69
    @Override
70
    protected String analyzeMore(CdmBase bean, EntityKey entityKey, String flags, CdmBase mappedEntity) {
71

    
72
        boolean hashAdded = false;
73

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

    
87
    /**
88
     * @return the showHashCodes
89
     */
90
    public boolean isShowHashCodes() {
91
        return showHashCodes;
92
    }
93

    
94
    /**
95
     * @param showHashCodes the showHashCodes to set
96
     */
97
    public void setShowHashCodes(boolean showHashCodes) {
98
        boolean runUpdate = this.showHashCodes != showHashCodes;
99
        this.showHashCodes = showHashCodes;
100
        if(runUpdate){
101
            update();
102
        }
103
    }
104

    
105
}
    (1-1/1)