Package org.photovault.dbhelper

Examples of org.photovault.dbhelper.ODMGXAWrapper


     are actually deleted. This is sometimes useful for e.g. cleaning up a test
     environment but production code should use
     {@link #delete( boolean deleteExternalInstances )} instead.
     */
    public void delete() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        Database db = ODMG.getODMGDatabase();
       
        // First delete all instances
        for ( int i = 0; i < instances.size(); i++ ) {
            ImageInstance f = (ImageInstance) instances.get( i );
            f.delete();
        }
       
        // Then delete the photo from all folders it belongs to
        if ( folders != null ) {
            Object[] foldersArray = folders.toArray();
            for ( int n = 0; n < foldersArray.length; n++ ) {
                ((PhotoFolder)foldersArray[n]).removePhoto( this );
            }
        }
       
        // Then delete the PhotoInfo itself
        db.deletePersistent( this );
        txw.commit();
    }
View Full Code Here


     @param id id of the folder to retrieve
     @return The given folder or <code>null</code> if not found
    */
    public static PhotoFolder getFolderById( int id ) {
        PhotoFolder f = null;
        ODMGXAWrapper txw = new ODMGXAWrapper();
        Implementation odmg = ODMG.getODMGImplementation();
       
        List folders = null;
        boolean mustCommit = false;
        try {
            OQLQuery query = odmg.newOQLQuery();
            query.create( "select folders from " + PhotoFolder.class.getName()
                        + " where folderId = " + id );
            folders = (List) query.execute();
        } catch ( Exception e ) {
            txw.abort();
            return null;
        }
        f = (PhotoFolder) folders.get( 0 );
        // If a new transaction was created, commit it
        txw.commit();
        return f;
    }
View Full Code Here

     volumes
    
     @throws PhotovaultException if some instances of the photo cannot be deleted
     */
    public void delete( boolean deleteExternalInstances ) throws PhotovaultException {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        Database db = ODMG.getODMGDatabase();

        // First delete all instances
        Vector deletedInstances = new Vector();
        Vector notDeletedInstances = new Vector();
        for ( int i = 0; i < instances.size(); i++ ) {
            ImageInstance f = (ImageInstance) instances.get( i );
            if ( f.delete( deleteExternalInstances ) ) {
                deletedInstances.add( f );
            } else {
                notDeletedInstances.add( f );
            }
        }
       
        // Remove all instances we were able to delete
        for ( int i = 0 ; i < deletedInstances.size(); i++ ) {
            instances.remove( deletedInstances.elementAt( i ) );
        }
       
        if ( notDeletedInstances.size() > 0 ) {
            txw.commit();
            throw new PhotovaultException( "Unable to delete some instances of the photo" );
        }

        /*
         All instances were succesfully deleted, so we can delete metadata as well.
         First, delete the photo from all folders it belongs to
         */
        if ( folders != null ) {
            Object[] foldersArray = folders.toArray();
            for ( int n = 0; n < foldersArray.length; n++ ) {
                ((PhotoFolder)foldersArray[n]).removePhoto( this );
            }
        }
       
        // Then delete the PhotoInfo object itself
        db.deletePersistent( this );
        txw.commit();       
    }
View Full Code Here

    /**
       Deletes this object from the persistent repository
    */
    public void delete() {
  ODMGXAWrapper txw = new ODMGXAWrapper();
  // Find the current transaction or create a new one
  boolean mustCommit = false;

  // First make sure that this object is deleted from its parent's subfolders list (if it has a parent)
  setParentFolder( null );

  // Then notify all photos belonging to this folder
  if ( photos != null ) {
      Iterator photoIter = photos.iterator();
      while ( photoIter.hasNext() ) {
    PhotoInfo photo = (PhotoInfo) photoIter.next();
    photo.removedFromFolder( this );
      }
  }
  Database db = ODMG.getODMGDatabase();
  db.deletePersistent( this );
 
  txw.commit();
    }
View Full Code Here

   
    /**
     Returns the uid of the object
     */
    public int getUid() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.READ );
        txw.commit();
        return uid;
    }
View Full Code Here

    /**
     Adds a new image instance for this photo
     @param i The new instance
     */
    public void addInstance( ImageInstance i ) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        txw.lock( i, Transaction.WRITE );
        instances.add( i );
        i.setPhotoUiduid );
        if ( i.getInstanceType() == ImageInstance.INSTANCE_TYPE_ORIGINAL ) {
            origInstanceHash = i.getHash();
        }
        txw.commit();
       
    }
View Full Code Here

     @param instanceType Type of the instance - original, modified or thumbnail.
     @return The new instance
     @see ImageInstance class documentation for details.
     */
    public ImageInstance addInstance( VolumeBase volume, File instanceFile, int instanceType ) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        // Vector origInstances = getInstances();
        ImageInstance instance = ImageInstance.create( volume, instanceFile, this, instanceType );
        instances.add( instance );
       
        // If this is the first instance or we are adding original image we need to invalidate
        // thumbnail
        if ( instances.size() == 1 || instanceType == ImageInstance.INSTANCE_TYPE_ORIGINAL ) {
            invalidateThumbnail();
        }
       
        if ( instanceType == ImageInstance.INSTANCE_TYPE_ORIGINAL ) {
            // Store the hash code of original (even if this original instance is later deleted
            // we can identify later that another file is an instance of this photo)
            origInstanceHash = instance.getHash();
            // If an original instance is added notify listeners since some of
            // them may be displaying the default thumbnail
            modified();
        }
        txw.commit();
        return instance;
    }
View Full Code Here

        txw.commit();
        return instance;
    }
   
    public void removeInstance( int instanceNum throws IndexOutOfBoundsException {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        ImageInstance instance = null;
        try {
            instance = (ImageInstance) getInstances().get(instanceNum );
        } catch ( IndexOutOfBoundsException e ) {
            txw.abort();
            throw e;
        }
        txw.lock( this, Transaction.WRITE );
        txw.lock( instance, Transaction.WRITE );
        instances.remove( instance );
        instance.delete();
        txw.commit();
    }
View Full Code Here

        }
        log.debug( "Deleting " + purgeList.size() + " instances" );
        Iterator iter = purgeList.iterator();
        while ( iter.hasNext() ) {
            ImageInstance i = (ImageInstance) iter.next();
            ODMGXAWrapper txw = new ODMGXAWrapper();
            txw.lock( this, Transaction.WRITE );
            txw.lock( i, Transaction.WRITE );
            instances.remove( i );
            i.delete();
            txw.commit();
        }
        log.debug( "exit: purgeInvalidInstances" );       
    }
View Full Code Here

     @param createpreview, if <code>true</code> create also a preview image.
     */
    protected void createThumbnail( VolumeBase volume, boolean createPreview ) {
       
        log.debug( "Creating thumbnail for " + uid );
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
       
        // Maximum size of the thumbnail
        int maxThumbWidth = 100;
        int maxThumbHeight = 100;
        checkCropBounds();
       
        /*
         Determine the minimum size for the instance used for thumbnail creation
         to get decent image quality.
         The cropped portion of the image must be roughly the same
         resolution as the intended thumbnail.
         */
        double cropWidth = cropMaxX - cropMinX;
        cropWidth = ( cropWidth > 0.000001 ) ? cropWidth : 1.0;
        double cropHeight = cropMaxY - cropMinY;
        cropHeight = ( cropHeight > 0.000001 ) ? cropHeight : 1.0;
        int minInstanceWidth = (int)(((double)maxThumbWidth)/cropWidth);
        int minInstanceHeight = (int)(((double)maxThumbHeight)/cropHeight);
        int minInstanceSide = Math.max( minInstanceWidth, minInstanceHeight );
       
       
        // Find the original image to use as a staring point
        EnumSet<ImageOperations> allowedOps = EnumSet.allOf( ImageOperations.class );           
        if ( createPreview ) {
            // We need to create also the preview image, so we need original.
            allowedOps = EnumSet.noneOf( ImageOperations.class );
            minInstanceWidth = 1024;
            minInstanceHeight = 1024;
        }
       
        ImageInstance original = this.getPreferredInstance( EnumSet.noneOf( ImageOperations.class ),
                allowedOps, minInstanceWidth, minInstanceHeight );
       
        if ( original == null ) {
            // If there are no uncorrupted instances, no thumbnail can be created
            log.warn( "Error - no original image was found!!!" );
            txw.commit();
            return;
        }
        txw.lock( original, Transaction.READ );
        log.debug( "Found original, reading it..." );
       
        /*
         We try to ensure that the thumbnail is actually from the original image
         by comparing aspect ratio of it to original. This is not a perfect check
         but it will usually catch the most typical errors (like having a the original
         rotated by RAW conversion SW but still the original EXIF thumbnail.
         */
        double origAspect = this.getAspect(
                original.getWidth(),
                original.getHeight(), 1.0 );
        double aspectAccuracy = 0.01;
       
        // First, check if there is a thumbnail in image header
        RenderedImage origImage = null;
       
        // Read the image
        RenderedImage thumbImage = null;
        RenderedImage previewImage = null;
       
        try {
            File imageFile = original.getImageFile();
            PhotovaultImageFactory imgFactory = new PhotovaultImageFactory();
            PhotovaultImage img = imgFactory.create( imageFile, false, false );
            if ( channelMap != null ) {
                img.setColorAdjustment( channelMap );
            }
            if ( img instanceof RawImage ) {
                RawImage ri = (RawImage) img;
                ri.setRawSettings( rawSettings );
            }
            if ( createPreview ) {
                // Calculate preview image size
                int previewWidth = img.getWidth();
                int previewHeight = img.getHeight();
                while ( previewWidth > 2048 || previewHeight > 2048 ) {
                    previewWidth >>= 1;
                    previewHeight >>=1;
                }
                previewImage = img.getRenderedImage( previewWidth, previewHeight, false );
            }
            img.setCropBounds( this.getCropBounds() );
            img.setRotation( prefRotation - original.getRotated() );
            thumbImage = img.getRenderedImage( maxThumbWidth, maxThumbHeight, true );
        } catch ( Exception e ) {
            log.warn( "Error reading image: " + e.getMessage() );
            // TODO: If we aborted here due to image writing problem we would have
            // problems later with non-existing transaction. We should really
            // rethink the error handling logic in the whole function. Anyway, we
            // haven't changed anything yet so we can safely commit the tx.
            txw.commit();
            return;
        }
        log.debug( "Done, finding name" );
       
        // Find where to store the file in the target volume
        File thumbnailFile = volume.getInstanceName( this, "jpg" );
        log.debug( "name = " + thumbnailFile.getName() );
       
        try {
            saveInstance( thumbnailFile, thumbImage );
            if ( thumbImage instanceof PlanarImage ) {
                ((PlanarImage)thumbImage).dispose();
                System.gc();
            }
        } catch (PhotovaultException ex) {
            log.error( "error writing thumbnail for " + original.getImageFile().getAbsolutePath() +
                    ": " + ex.getMessage() );
            // TODO: If we abort here due to image writing problem we will have
            // problems later with non-existing transaction. We should really
            // rethink the error handling login in the whole function. Anyway, we
            // haven't changed anything yet so we can safely commit the tx.
            txw.commit();
            return;
        }
       
        // add the created instance to this persistent object
        ImageInstance thumbInstance = addInstance( volume, thumbnailFile,
                ImageInstance.INSTANCE_TYPE_THUMBNAIL );
        thumbInstance.setRotated( prefRotation -original.getRotated() );
        thumbInstance.setCropBounds( getCropBounds() );
        thumbInstance.setColorChannelMapping( channelMap );
        thumbInstance.setRawSettings( rawSettings );
        log.debug( "Loading thumbnail..." );
       
        thumbnail = Thumbnail.createThumbnail( this, thumbnailFile );
        oldThumbnail = null;
        log.debug( "Thumbnail loaded" );
       
        if ( createPreview ) {
            File previewFile = volume.getInstanceName( this, "jpg" );
            try {
                saveInstance( previewFile, previewImage );
                if ( previewImage instanceof PlanarImage ) {
                    ((PlanarImage)previewImage).dispose();
                    System.gc();
                }
            } catch (PhotovaultException ex) {
                log.error( "error writing preview for " + original.getImageFile().getAbsolutePath() +
                        ": " + ex.getMessage() );
                // TODO: If we abort here due to image writing problem we will have
                // problems later with non-existing transaction. We should really
                // rethink the error handling login in the whole function. Anyway, we
                // haven't changed anything yet so we can safely commit the tx.
                txw.commit();
                return;
            }
            ImageInstance previewInstance = addInstance( volume, previewFile,
                    ImageInstance.INSTANCE_TYPE_MODIFIED );
            previewInstance.setColorChannelMapping( channelMap );
            previewInstance.setRawSettings( rawSettings );
        }
        txw.commit();
    }
View Full Code Here

TOP

Related Classes of org.photovault.dbhelper.ODMGXAWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.