You are viewing a plain text version of this content. The canonical link for it is here.
Posted to photark-commits@incubator.apache.org by lr...@apache.org on 2011/01/30 22:04:15 UTC

svn commit: r1065392 - in /incubator/photark/branches/photark-rest: photark-jcr/src/main/java/org/apache/photark/services/jcr/ photark/src/main/java/org/apache/photark/

Author: lresende
Date: Sun Jan 30 22:04:15 2011
New Revision: 1065392

URL: http://svn.apache.org/viewvc?rev=1065392&view=rev
Log:
Partial implementation for image related operations

Added:
    incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageList.java
      - copied, changed from r1065391, incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java
Modified:
    incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRGallery.java
    incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java
    incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Image.java
    incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageRef.java

Modified: incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRGallery.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRGallery.java?rev=1065392&r1=1065391&r2=1065392&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRGallery.java (original)
+++ incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRGallery.java Sun Jan 30 22:04:15 2011
@@ -36,6 +36,9 @@ import javax.jcr.Session;
 import org.apache.photark.Album;
 import org.apache.photark.AlbumList;
 import org.apache.photark.AlbumRef;
+import org.apache.photark.Image;
+import org.apache.photark.ImageList;
+import org.apache.photark.ImageRef;
 import org.apache.photark.services.GalleryService;
 import org.apache.photark.services.PhotarkRuntimeException;
 import org.oasisopen.sca.annotation.Init;
@@ -74,7 +77,7 @@ public class JCRGallery implements Galle
             while (albumNodes.hasNext()) {
                 Node albumNode = albumNodes.nextNode();
                 if(albumNode != null) {
-                    Album album = fromNode(albumNode);
+                    Album album = fromAlbumNode(albumNode);
                     albums.put(album.getName(), album);
                 }
             }
@@ -88,6 +91,8 @@ public class JCRGallery implements Galle
         }
     }
 
+    // ALBUM FUNCTIONS
+
     public AlbumList getAlbums() {
         AlbumList albumList = new AlbumList();
         //loop trough the entries and create a albumRef for each Album
@@ -221,7 +226,164 @@ public class JCRGallery implements Galle
         }
     }
 
-    private static Album fromNode(Node albumNode) throws RepositoryException {
+    // IMAGE FUNCTIONS
+
+    public ImageList getImages(String albumName) {
+        ImageList images = new ImageList();
+        Node albumNode = null;
+
+        try {
+            albumNode = getAlbumNode(repositoryManager, albumName);
+            NodeIterator imageNodes = albumNode.getNodes();
+
+            while (imageNodes.hasNext()) {
+                Node imageNode = imageNodes.nextNode();
+                if(imageNode != null) {
+                    Image image = fromImageNode(imageNode);
+                    images.getImages().add(ImageRef.createImageRef(image));
+                }
+            }
+        }catch(RepositoryException e) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Error retrieving images for album '" + albumName + "'" );
+            }
+            throw new PhotarkRuntimeException("Error retrieving images for album '" + albumName + "'" );
+        }
+
+        return images;
+    }
+
+    public Image getImage(String albumName, String imageId) {
+        if(imageId == null || imageId.isEmpty()) {
+            throw new InvalidParameterException("Invalid/Empty image id");
+        }
+
+        if(! albums.containsKey(imageId)) {
+            throw new InvalidParameterException("Image '" + imageId + "' not found");
+        }
+
+        return null;
+    }
+
+    public void addImage(String albumName, Image newImage) throws PhotarkRuntimeException {
+        if(newImage.getId() == null || newImage.getId().isEmpty()) {
+            throw new InvalidParameterException("Image has no name");
+        }
+
+        if(newImage.getName() == null || newImage.getName().isEmpty()) {
+            throw new InvalidParameterException("Image has no name");
+        }
+
+        try {
+            Node albumNode = getAlbumNode(repositoryManager, albumName);
+
+            if(albumNode.hasNode(newImage.getId())) {
+                throw new PhotarkRuntimeException("Error creating image. Image '" + newImage.getId() + "' already exists !");
+            }
+
+            Node imageNode = albumNode.addNode(newImage.getId());
+            if(imageNode != null) {
+                imageNode.setProperty("id", newImage.getId());
+                imageNode.setProperty("name", newImage.getName());
+                imageNode.setProperty("title", newImage.getTitle());
+                imageNode.setProperty("description", newImage.getDescription());
+                imageNode.setProperty("url", newImage.getLocation());
+                repositoryManager.getSession().save();
+            }
+
+        } catch(RepositoryException e) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Error creating new album node '" + newImage.getName() + "'" );
+            }
+            throw new PhotarkRuntimeException("Error creating new album node '" + newImage.getName() + "'" );
+        }
+    }
+
+    public void updateImage(String albumName, Image image) throws PhotarkRuntimeException {
+        /*
+        if(image.getName() == null || image.getName().isEmpty()) {
+            throw new InvalidParameterException("Album has no name");
+        }
+
+        if(! albums.containsKey(image.getName())) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Album '" + image.getName() + "' not found");
+            }
+            throw new InvalidParameterException("Album '" + image.getName() + "' not found");
+        }
+
+        try {
+            Node albumRootNode = getAlbumRoot(repositoryManager);
+
+            if(! albumRootNode.hasNode(image.getName())) {
+                if(logger.isLoggable(Level.SEVERE)) {
+                    logger.log(Level.SEVERE, "Album node '" + image.getName() + "' not found in the JCR repository");
+                }
+
+                throw new PhotarkRuntimeException("Album node '" + image.getName()  + "' not found in the JCR repository");
+            }
+
+            Node albumNode = albumRootNode.getNode(image.getName());
+            albumNode.setProperty("name", image.getName());
+            albumNode.setProperty("description", image.getDescription());
+            albumNode.setProperty("name", image.getLocation());
+            repositoryManager.getSession().save();
+
+            albums.put(image.getName(), image);
+
+        } catch(RepositoryException e) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Error updating album node '" + image.getName() + "'" );
+            }
+            throw new PhotarkRuntimeException("Error updating album node '" + image.getName() + "'" );
+        }
+        */
+
+    }
+
+    public void removeImage(String albumName, String imageId) throws PhotarkRuntimeException {
+        /*
+        if(imageId == null || imageId.isEmpty()) {
+            throw new InvalidParameterException("Invalid/Empty album name");
+        }
+
+
+        if(! albums.containsKey(imageId)) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Album '" + imageId + "' not found");
+            }
+            throw new InvalidParameterException("Album '" + imageId + "' not found");
+        }
+
+        try {
+            Node albumRootNode = getAlbumRoot(repositoryManager);
+
+            if(! albumRootNode.hasNode(imageId)) {
+                if(logger.isLoggable(Level.SEVERE)) {
+                    logger.log(Level.SEVERE, "Album node '" + imageId + "' not found in the JCR repository");
+                }
+
+                throw new PhotarkRuntimeException("Album node '" + imageId  + "' not found in the JCR repository");
+            }
+
+            Node albumNode = albumRootNode.getNode(imageId);
+            albumNode.remove();
+            repositoryManager.getSession().save();
+
+            albums.remove(imageId);
+
+        } catch(RepositoryException e) {
+            if(logger.isLoggable(Level.SEVERE)) {
+                logger.log(Level.SEVERE, "Error deleting album node '" + imageId + "'" );
+            }
+            throw new PhotarkRuntimeException("Error deleting album node '" + imageId + "'" );
+        }
+        */
+    }
+
+    // UTILITY FUNCTIONS
+
+    private static Album fromAlbumNode(Node albumNode) throws RepositoryException {
         Album album = new Album();
         album.setName(albumNode.getName());
         album.setDescription(albumNode.getProperty("description").getString());
@@ -230,6 +392,18 @@ public class JCRGallery implements Galle
     }
 
 
+    private static Image fromImageNode(Node imageNode) throws RepositoryException {
+        Image image = new Image();
+        image.setName(imageNode.getName());
+        image.setTitle(imageNode.getProperty("title").getString());
+        image.setDescription(imageNode.getProperty("description").getString());
+        image.setLocation(imageNode.getProperty("url").getString());
+        image.setThumbnailLocation(imageNode.getProperty("urlThumb").getString());
+
+        return image;
+    }
+
+
     private static Node getAlbumRoot(JCRRepositoryManager repositoryManager) throws PhotarkRuntimeException {
         Session session = null;
         Node root = null;
@@ -254,5 +428,32 @@ public class JCRGallery implements Galle
         return albumRoot;
     }
 
+    /**
+     * This method create new album node in case it does not exists in
+     * repository or return older album node otherwise.
+     *
+     * @param
+     * @return
+     * @throws RepositoryException
+     */
+    private static Node getAlbumNode(JCRRepositoryManager repositoryManager, String name) throws PhotarkRuntimeException {
+        Session session = null;
+        Node root = null;
+        Node albumNode = null;
+        try {
+            session = repositoryManager.getSession();
+            root = session.getRootNode();
+            if (root.hasNode(name)) {
+                albumNode = root.getNode(name);
+            }
+        } catch (LoginException e) {
+            logger.log(Level.SEVERE, "Error loging in to photark repository :" + e.getMessage(), e);
+            throw new PhotarkRuntimeException("Error loging in to photark repository  :" + e.getMessage(), e);
+        } catch (RepositoryException e) {
+            logger.log(Level.SEVERE, "Error retrieving albums from the repository :" + e.getMessage(), e);
+            throw new PhotarkRuntimeException("Error retrieving albums from the repository :" + e.getMessage(), e);
+        }
 
+        return albumNode;
+    }
 }

Modified: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java?rev=1065392&r1=1065391&r2=1065392&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java (original)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java Sun Jan 30 22:04:15 2011
@@ -6,15 +6,15 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.photark;
@@ -24,11 +24,11 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- * Aggregate a list of album references, 
+ * Aggregate a list of album references,
  * useful to produce the proper json/xml output
  * for gallery set of albums, without the full
  * details of the album model object itself
- * 
+ *
  * @version $Rev$ $Date$
  */
 public class AlbumList implements Serializable {

Modified: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Image.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Image.java?rev=1065392&r1=1065391&r2=1065392&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Image.java (original)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Image.java Sun Jan 30 22:04:15 2011
@@ -38,6 +38,7 @@ public class Image implements Serializab
     private String id;
     private String name;
     private String title;
+    private String description;
     private Date datePosted;
 
     private String url;
@@ -129,6 +130,14 @@ public class Image implements Serializab
         this.title = title;
     }
 
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
     /**
      * Get date when image was posted
      *

Copied: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageList.java (from r1065391, incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java)
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageList.java?p2=incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageList.java&p1=incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java&r1=1065391&r2=1065392&rev=1065392&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/AlbumList.java (original)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageList.java Sun Jan 30 22:04:15 2011
@@ -6,15 +6,15 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.photark;
@@ -23,25 +23,16 @@ import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
-/**
- * Aggregate a list of album references, 
- * useful to produce the proper json/xml output
- * for gallery set of albums, without the full
- * details of the album model object itself
- * 
- * @version $Rev$ $Date$
- */
-public class AlbumList implements Serializable {
-
-    private static final long serialVersionUID = 2578709033197276816L;
+public class ImageList implements Serializable {
+    private static final long serialVersionUID = -2629958440102925366L;
 
-    private List<AlbumRef> albumRefs = new ArrayList<AlbumRef>();
+    private List<ImageRef> imageRefs = new ArrayList<ImageRef>();
 
     /**
      * Retrieve a list of albums
      * @return the list of album references
      */
-    public List<AlbumRef> getAlbums() {
-        return this.albumRefs;
+    public List<ImageRef> getImages() {
+        return this.imageRefs;
     }
 }

Modified: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageRef.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageRef.java?rev=1065392&r1=1065391&r2=1065392&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageRef.java (original)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/ImageRef.java Sun Jan 30 22:04:15 2011
@@ -6,20 +6,24 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
 package org.apache.photark;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
 
 /**
  * Summary info for Images
@@ -29,29 +33,8 @@ public class ImageRef implements Seriali
 
     private static final long serialVersionUID = -3663988501067415961L;
 
-    private String name;
     private String title;
-
-    private String url;
-    private String urlThumb;
-
-    private String imageRef;
-
-    /**
-     * Get image name
-     * @return image name
-     */
-    public String getName() {
-        return name;
-    }
-
-    /**
-     * Set image name
-     * @param name image name
-     */
-    public void setName(String name) {
-        this.name = name;
-    }
+    private List<Link> links = new ArrayList<Link>();
 
     /**
      * Get image title
@@ -70,50 +53,32 @@ public class ImageRef implements Seriali
     }
 
     /**
-     * Get image location
-     * @return image location
+     * Get cover image reference
+     * @return the cover image url reference
      */
-    public String getLocation() {
-        return url;
+    @XmlElement(name="link", namespace="http://www.w3.org/2005/Atom")
+    public List<Link> getLinks() {
+        return this.links;
     }
 
-    /**
-     * Set image location
-     * @param location image location
-     */
-    public void setLocation(String location) {
-        this.url = location;
-    }
 
-    /**
-     * Get thumbnail image location
-     * @return thumbnail image location
-     */
-    public String getThumbnailLocation() {
-        return urlThumb;
+    @Override
+    public String toString() {
+        return "ImageRef [title=" + title + ", links=" + links + "]";
     }
 
     /**
-     * Set thumbnail image location
-     * @param thumbnailLocation thumbnail image location
+     * Utility method to create an ImageRef from an Image
+     * @param album
+     * @return
      */
-    public void setThumbnailLocation(String thumbnailLocation) {
-        this.urlThumb = thumbnailLocation;
-    }
+    public static ImageRef createImageRef(Image image) {
+        ImageRef imageRef = new ImageRef();
 
-    /**
-     * Get image reference
-     * @return image reference
-     */
-    public String getImageRef() {
-        return imageRef;
-    }
+        imageRef.setTitle(image.getTitle());
+        imageRef.getLinks().add(Link.to("thumbnail", image.getThumbnailLocation()));
+        imageRef.getLinks().add(Link.to("url", image.getLocation()));
 
-    /**
-     * Set image reference
-     * @param imageRef image reference
-     */
-    public void setImageRef(String imageRef) {
-        this.imageRef = imageRef;
+        return imageRef;
     }
 }