@SuppressWarnings("unchecked")
  @Override
  public <T> T findById(Object id, String collectionName) {
    CollectionMetaData cmd = cmdMap.get(collectionName);
    Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName);
    if((null == cmd) || null == collection) {
      throw new InvalidJsonDbApiUsageException("Collection by name '" + collectionName + "' not found. Create collection first.");
    }
    cmd.getCollectionLock().readLock().lock();
    try {
      Object obj = Util.deepCopy(collection.get(id));
      if(encrypted && cmd.hasSecret() && null != obj){
        CryptoUtil.decryptFields(obj, cmd, dbConfig.getCipher());
      }
      return (T) obj;
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
      logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e);
      throw new JsonDBException("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e);
    } finally {
      cmd.getCollectionLock().readLock().unlock();
    }
  }
@ SuppressWarnings ( "unchecked" ) @ Override public < T > T findById ( Object id , String collectionName ) { CollectionMetaData cmd = cmdMap . get ( collectionName ) ; Map < Object , T > collection = ( Map < Object , T > ) collectionsRef . get ( ) . get ( collectionName ) ; if ( ( null == cmd ) || null == collection ) { throw new InvalidJsonDbApiUsageException ( "Collection by name '" + collectionName + "' not found. Create collection first." ) ; } cmd . getCollectionLock ( ) . readLock ( ) . lock ( ) ; try { Object obj = Util . deepCopy ( collection . get ( id ) ) ; if ( encrypted && cmd . hasSecret ( ) && null != obj ) { CryptoUtil . decryptFields ( obj , cmd , dbConfig . getCipher ( ) ) ; } return ( T ) obj ; } catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) { logger . error ( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName , e ) ; throw new JsonDBException ( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName , e ) ; } finally { cmd . getCollectionLock ( ) . readLock ( ) . unlock ( ) ; } }
/* (non-Javadoc)
@see io.jsondb.JsonDBOperations#findById(java.lang.Object, java.lang.String)