Project

General

Profile

« Previous | Next » 

Revision 5e97d775

Added by Andreas Kohlbecker over 5 years ago

solving multiple representation probelms when adding source references to MediaSpecimen

  • extending PersistentContextAnalyzer
  • EntityCache support for more than one base entities

View differences:

src/main/java/eu/etaxonomy/cdm/cache/CdmEntityCache.java
44 44

  
45 45
    private static final String COPY_ENTITY = "!";
46 46

  
47
    private CdmBase entity;
47
    private Set<CdmBase> entities = new HashSet<>();
48 48

  
49 49
    private Map<EntityKey, CdmBase> entityyMap = new HashMap<>();
50 50

  
......
57 57
    private Set<Object> objectsSeen = new HashSet<>();
58 58

  
59 59
    public CdmEntityCache(CdmBase entity){
60
        this.entity = entity;
60
        this.entities.add(entity);
61 61
        update();
62 62
    }
63 63

  
......
69 69
        objectsSeen.clear();
70 70
        copyEntitiyKeys.clear();
71 71

  
72
        String propertyPath = "";
73

  
74
        analyzeEntity(entity, propertyPath);
72
        for(CdmBase entity : entities){
73
        analyzeEntity(entity, "");
74
        }
75 75

  
76 76
        return copyEntitiyKeys.isEmpty();
77 77
    }
......
269 269
     */
270 270
    @Override
271 271
    public <CDM extends CdmBase> CDM find(CDM value) {
272
        EntityKey entityKey = new EntityKey(HibernateProxyHelper.deproxy(value));
273
        return (CDM) entityyMap.get(entityKey);
272
        if(value != null){
273
            EntityKey entityKey = new EntityKey(HibernateProxyHelper.deproxy(value));
274
            return (CDM) entityyMap.get(entityKey);
275
        }
276
        return null;
274 277
    }
275 278

  
276 279
    /**
......
282 285
        return (CDM) entityyMap.get(entityKey);
283 286
    }
284 287

  
288
    /**
289
     * {@inheritDoc}
290
     */
291
    @Override
292
    public <CDM extends CdmBase> void add(CDM value) {
293
        entities.add(value);
294
        analyzeEntity(value, "");
295
    }
296

  
285 297

  
286 298
}
src/main/java/eu/etaxonomy/cdm/cache/EntityCache.java
25 25

  
26 26
    public boolean update();
27 27

  
28
    public <CDM extends CdmBase> void add(CDM value);
29

  
28 30
    /**
29 31
     * @param type
30 32
     * @param id
src/main/java/eu/etaxonomy/cdm/debug/PersistentContextAnalyzer.java
40 40
 */
41 41
public class PersistentContextAnalyzer {
42 42

  
43
    /**
44
     *
45
     */
46
    private static final char HASH_SEPARATOR = '.';
47

  
43 48
    /**
44 49
     *
45 50
     */
......
66 71

  
67 72
    private Set<Object> objectsSeen = new HashSet<>();
68 73

  
74
    private boolean showHashCodes = false;
75

  
69 76

  
70 77
    public PersistentContextAnalyzer(CdmBase entity, Session session){
71 78
        this.session = session;
......
107 114
     */
108 115
    protected void printLegend(PrintStream printStream) {
109 116
        printStream.println("PersistentContextAnalyzer legend: ");
110
        printStream.println("    - '*': entity mapped in persistent context");
117
        printStream.println("    - '.{objectHash}': unique copy entity, followed by object hash (only shown when showHashCodes is enabled)");
111 118
        printStream.println("    - '!{objectHash}': detected copy entity, followed by object hash");
119
        printStream.println("    - '*': entity mapped in persistent context");
112 120
    }
113 121

  
114 122
    /**
......
122 130
        String flags = "";
123 131
        CdmBase mappedEntity = entityyMap.put(entityKey, bean);
124 132

  
133
        boolean hashAdded = false;
134

  
125 135
        if(session != null && session.contains(bean)){
126 136
            flags += IN_PERSITENT_CONTEXT;
127 137
        }
128 138
        if(mappedEntity != null && mappedEntity != bean) {
129 139
            copyEntitiyKeys.add(entityKey);
130 140
            flags += COPY_ENTITY + bean.hashCode();
141
            hashAdded = true;
142
        }
143
        if(showHashCodes && ! hashAdded){
144
            flags += HASH_SEPARATOR + bean.hashCode();
131 145
        }
132 146
        if(!flags.isEmpty()){
133 147
            propertyPath += "(" + flags + ")";
......
207 221
        }
208 222
    }
209 223

  
224
    /**
225
     * @return the showHashCodes
226
     */
227
    public boolean isShowHashCodes() {
228
        return showHashCodes;
229
    }
230

  
231
    /**
232
     * @param showHashCodes the showHashCodes to set
233
     */
234
    public void setShowHashCodes(boolean showHashCodes) {
235
        boolean runUpdate = this.showHashCodes != showHashCodes;
236
        this.showHashCodes = showHashCodes;
237
        if(runUpdate){
238
            update();
239
        }
240
    }
241

  
210 242
    class EntityKey {
211 243

  
212 244
        Class type;
src/main/java/eu/etaxonomy/cdm/service/SpecimenTypeDesignationWorkingSetServiceImpl.java
22 22
import org.springframework.transaction.annotation.Transactional;
23 23

  
24 24
import eu.etaxonomy.cdm.api.application.CdmRepository;
25
import eu.etaxonomy.cdm.debug.PersistentContextAnalyzer;
25 26
import eu.etaxonomy.cdm.model.common.VersionableEntity;
26 27
import eu.etaxonomy.cdm.model.name.Registration;
27 28
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
......
77 78
        FieldUnit newfieldUnit = FieldUnit.NewInstance();
78 79
        Registration reg = repo.getRegistrationService().load(registrationId, RegistrationWorkingSetService.REGISTRATION_INIT_STRATEGY);
79 80
        TaxonName typifiedName = repo.getNameService().load(typifiedNameId, TAXON_NAME_INIT_STRATEGY);
80
        Reference citation = repo.getReferenceService().load(registrationId, Arrays.asList("$"));
81
        Reference citation = repo.getReferenceService().load(publicationId, Arrays.asList("$"));
81 82
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto = new SpecimenTypeDesignationWorkingSetDTO<Registration>(reg, newfieldUnit, citation, typifiedName);
82 83
        return workingSetDto;
83 84
    }
......
157 158

  
158 159
            Session session = repo.getSession();
159 160

  
161
            PersistentContextAnalyzer regAnalyzer = new PersistentContextAnalyzer(dto.getOwner(), session);
162
            regAnalyzer.printEntityGraph(System.out);
163
            regAnalyzer.printCopyEntities(System.out);
164

  
160 165
            session.merge(dto.getOwner());
161 166

  
167

  
162 168
            session.flush();
163 169
        }
164 170

  
src/main/java/eu/etaxonomy/cdm/vaadin/event/ToOneRelatedEntityReloader.java
43 43
    @Override
44 44
    public void valueChange(ValueChangeEvent event) {
45 45

  
46

  
46 47
        CDM value = (CDM)event.getProperty().getValue();
48
        if(value == null) {
49
            return;
50
        }
47 51
        value = HibernateProxyHelper.deproxy(value);
48 52

  
49 53
        EntityCache cache = cachingPresenter.getCache();
50 54
        if(cache != null){
51 55
            cache.update();
52 56
            CDM cachedEntity = cache.find(value);
53
            if(cachedEntity != null && cachedEntity != value){
57
            if(cachedEntity == null){
58
                cache.add(value);
59
            } else if(cachedEntity != value){
54 60
                toOneRelatedEntityField.removeValueChangeListener(this);
55 61
                toOneRelatedEntityField.setValue(null); // reset to trick equals check in vaadin
56 62
                toOneRelatedEntityField.setValue(cachedEntity);
src/main/java/eu/etaxonomy/cdm/vaadin/model/registration/SpecimenTypeDesignationWorkingSetDTO.java
57 57
        super();
58 58
        this.owner = owner;
59 59
        this.baseEntity = baseEntity;
60
        if(citation == null){
61
            throw new NullPointerException("citation must not be null");
62
        }
63
        if(typifiedName == null){
64
            throw new NullPointerException("typifiedName must not be null");
65
        }
60 66
        this.citation = citation;
61 67
        this.typifiedName = typifiedName;
62 68
        if(baseEntity instanceof FieldUnit){
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/SpecimenTypeDesignationDTORow.java
82 82

  
83 83
        mediaSpecimenReference.setEnabled(publishedImageType);
84 84
        mediaSpecimenReferenceDetail.setEnabled(publishedImageType);
85
        mediaUri.setEnabled(unPublishedImageType);
85
        mediaUri.setEnabled(publishedImageType || unPublishedImageType);
86 86

  
87 87
    }
88 88
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/SpecimenTypeDesignationWorkingsetEditorPresenter.java
154 154
                        );
155 155
                row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>());
156 156
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection));
157
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(), SpecimenTypeDesignationWorkingsetEditorPresenter.this));
157
                row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(),
158
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
158 159

  
159 160
                row.mediaSpecimenReference.loadFrom(
160 161
                        referencePagingProvider,
......
164 165

  
165 166
                row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
166 167
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference));
168
                row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(row.mediaSpecimenReference.getSelect(),
169
                        SpecimenTypeDesignationWorkingsetEditorPresenter.this));
167 170

  
168 171
                getView().applyDefaultComponentStyle(row.components());
169 172

  

Also available in: Unified diff