2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.debug
;
11 import java
.io
.PrintStream
;
13 import org
.apache
.logging
.log4j
.LogManager
;
14 import org
.apache
.logging
.log4j
.Logger
;
15 import org
.hibernate
.Session
;
17 import eu
.etaxonomy
.cdm
.cache
.CdmEntityCache
;
18 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 * @author a.kohlbecker
24 public class PersistentContextAnalyzer
extends CdmEntityCache
{
26 @SuppressWarnings("unused")
27 private static final Logger logger
= LogManager
.getLogger();
29 private static final char HASH_SEPARATOR
= '.';
31 private static final String IN_PERSITENT_CONTEXT
= "*";
33 private Session session
;
35 private boolean showHashCodes
= false;
37 public PersistentContextAnalyzer(CdmBase entity
, Session session
){
38 this.session
= session
;
39 this.entities
.add(entity
);
43 public PersistentContextAnalyzer(CdmBase entity
){
47 public PersistentContextAnalyzer(CdmEntityCache entityCache
, Session session
){
48 this.session
= session
;
49 this.entities
.addAll(entityCache
.getEntities());
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");
66 protected String
analyzeMore(CdmBase bean
, EntityKey entityKey
, String flags
, CdmBase mappedEntity
) {
68 boolean hashAdded
= false;
70 if(mappedEntity
!= null && mappedEntity
!= bean
) {
71 flags
+= bean
.hashCode();
74 if(session
!= null && session
.contains(bean
)){
75 flags
+= IN_PERSITENT_CONTEXT
;
77 if(showHashCodes
&& ! hashAdded
){
78 flags
+= HASH_SEPARATOR
+ bean
.hashCode();
84 * @return the showHashCodes
86 public boolean isShowHashCodes() {
91 * @param showHashCodes the showHashCodes to set
93 public void setShowHashCodes(boolean showHashCodes
) {
94 boolean runUpdate
= this.showHashCodes
!= showHashCodes
;
95 this.showHashCodes
= showHashCodes
;