public <T extends CvObject> T getByLabel( Class<T> cvType, String label, boolean forceReload ) { if ( cvType == null ) { throw new NullPointerException( "To get a CV using its label, you need the CV type" ); } LabelKey labelKey = new LabelKey( label, cvType ); if ( !forceReload && cachedByLabel.containsKey( labelKey ) ) { if (log.isDebugEnabled()) log.debug("Getting by label: "+label+" ("+cvType.getSimpleName()+") - Hit"); return ( T ) cachedByLabel.get( labelKey ); } T cvObject = null; try { cvObject = getDaoFactory().getCvObjectDao( cvType ).getByShortLabel( cvType, label ); } catch (Exception e) { e.printStackTrace(); throw new IntactException("Exception getting cvType "+cvType.getSimpleName()+" with label: "+label, e); } if ( cvObject == null ) { if (log.isDebugEnabled()) log.debug("Getting by label: "+label+" ("+cvType.getSimpleName()+") - Missed"); return null; } if (log.isDebugEnabled()) log.debug("Getting by label: "+label+" ("+cvType.getSimpleName()+") - DB Hit"); putCv( cvObject ); return cvObject; }
/** * Updates a CvObject already present in the context * * @param cvObject The new value for the cvObject. Note that it must be equal to the existing one in order to be * updated */ public void updateCvObject( CvObject cvObject ) { if ( cvObject == null ) { throw new IllegalArgumentException( "The cvObject is null." ); } else if ( cvObject.getAc() == null ) { throw new IllegalArgumentException( "The cvObject does not have an ac." ); } else if ( cvObject.getShortLabel() == null ) { throw new IllegalArgumentException( "The cvObject does not have a shortlabel." ); } //Remove the old cv from the cachedByLabel map Iterator it = cachedByLabel.entrySet().iterator(); while ( it.hasNext() ) { Map.Entry<String, CvObject> pairs = ( Map.Entry ) it.next(); if ( pairs.getValue().getAc().equals( cvObject.getAc() ) ) { it.remove(); } } //Add the new one. cachedByLabel.put( new LabelKey( cvObject.getShortLabel(), cvObject.getClass() ), cvObject ); // Update the cvObject in cachedByAc (we don't need to remove it first as it's quite unlikely that the // ac changed whereas the shortlabel could change. cachedByAc.put( cvObject.getAc(), cvObject ); }
private void putCv( CvObject cv ) { cachedByAc.put( cv.getAc(), cv ); cachedByLabel.put( new LabelKey( cv.getShortLabel(), cv.getClass() ), cv ); }