PHOTARK-9 - Adding other files I missed from Avdhesh Yadav patch

git-svn-id: https://svn.apache.org/repos/asf/incubator/photark/trunk@898834 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/GalleryImpl.java b/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/GalleryImpl.java
new file mode 100644
index 0000000..13ede26
--- /dev/null
+++ b/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/GalleryImpl.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+package org.apache.photark.services.gallery.jcr;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.photark.services.album.Album;
+import org.apache.photark.services.album.jcr.AlbumImpl;
+import org.apache.photark.services.gallery.AbsGalleryImpl;
+import org.apache.photark.services.gallery.Gallery;
+import org.oasisopen.sca.annotation.Init;
+
+public class GalleryImpl extends AbsGalleryImpl implements Gallery{
+    
+    public GalleryImpl() {
+    	
+    }
+    
+    public GalleryImpl(String name) {
+        super(name);
+    }
+    
+    @Init
+    public void init() {
+    	System.out.println(">>> Initializing JCR Gallery");
+        try {
+            URL galleryURL = this.getClass().getClassLoader().getResource(name);
+            if(galleryURL == null) {
+                // Accomodate for J2EE classpath that starts in WEB-INF\classes
+                galleryURL = this.getClass().getClassLoader().getResource("../../" + name);
+            }
+
+            if(galleryURL != null) {
+                File album = new File(galleryURL.toURI());
+                if (album.isDirectory() && album.exists()) {
+                    File[] albums = album.listFiles();
+                    for(File albumFile : albums) {
+                        if(! albumFile.getName().startsWith(".")) {
+                            if(albumFile.isDirectory() && albumFile.exists()) {
+                                Album newAlbum = AlbumImpl.createAlbum(albumFile.getName());
+                                newAlbum.setName(albumFile.getName());
+                                ((AlbumImpl)newAlbum).setGallery(name);
+                                this.albums.add(newAlbum);
+                            }                                
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            // FIXME: ignore for now
+            e.printStackTrace();
+        }
+        
+        initialized = true;
+    }
+}
\ No newline at end of file
diff --git a/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/JCRSession.java b/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/JCRSession.java
new file mode 100644
index 0000000..d58e353
--- /dev/null
+++ b/photark-jcr/src/main/java/org/apache/photark/services/gallery/jcr/JCRSession.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+package org.apache.photark.services.gallery.jcr;
+
+import java.io.IOException;
+
+import javax.jcr.LoginException;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import org.apache.jackrabbit.core.TransientRepository;
+
+public class JCRSession {
+	
+	private static Session session;
+	
+	private static void initSession(){
+		try {
+			Repository repository = new TransientRepository();
+			session = repository.login(new SimpleCredentials("username",
+					"password".toCharArray()));
+		} catch (LoginException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (RepositoryException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	public synchronized static Session getSession(){
+		if(session == null){
+			initSession();
+		}
+		return session;
+	}
+}
\ No newline at end of file
diff --git a/photark-webapp/src/main/resources/photo-gallery-jcr.composite b/photark-webapp/src/main/resources/photo-gallery-jcr.composite
new file mode 100644
index 0000000..a3f4845
--- /dev/null
+++ b/photark-webapp/src/main/resources/photo-gallery-jcr.composite
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.    
+-->
+<composite	xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
+		xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
+		targetNamespace="http://org.apache.photoark"
+		name="photo-gallery-jcr">
+  
+    <component name="Gallery">
+   		<implementation.java class="org.apache.photark.services.gallery.jcr.GalleryImpl"/>
+   		<property name="name">gallery</property>
+   		<service name="Gallery">
+   			<interface.java interface="org.apache.photark.services.gallery.Gallery"/>
+   			<tuscany:binding.jsonrpc uri="/Gallery"/>
+   		</service>
+    </component>
+    
+</composite>
diff --git a/photark/src/main/java/org/apache/photark/services/gallery/AbsGalleryImpl.java b/photark/src/main/java/org/apache/photark/services/gallery/AbsGalleryImpl.java
new file mode 100644
index 0000000..0029aa8
--- /dev/null
+++ b/photark/src/main/java/org/apache/photark/services/gallery/AbsGalleryImpl.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+package org.apache.photark.services.gallery;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.photark.services.album.Album;
+import org.oasisopen.sca.annotation.Property;
+
+public abstract class AbsGalleryImpl implements Gallery {
+	
+	protected String name;
+	private String location;
+	protected boolean initialized;
+	protected List<Album> albums = new ArrayList<Album>();
+
+	public AbsGalleryImpl() {
+
+	}
+
+	public AbsGalleryImpl(String name) {
+		this.name = name;
+	}
+
+	public abstract void init(); 
+
+	public String getName() {
+		return name;
+	}
+
+	@Property
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public void addAlbum(Album album) {
+
+	}
+
+	public Album[] getAlbums() {
+		if(! initialized) {
+			init();
+		}
+
+		Album[] albumArray = new Album[albums.size()];
+		albums.toArray(albumArray);
+		return albumArray;
+	}
+
+	public String getAlbumCover(String albumName) {
+		Album albumLookup = getAlbum(albumName);
+
+		if (albumLookup != null) {
+			return albumLookup.getPictures()[0];
+		} else {
+			//FIXME: return proper not found exception
+			return null;             
+		}
+	}
+
+	public String[] getAlbumPictures(String albumName) {
+		Album albumLookup = getAlbum(albumName);
+
+		if (albumLookup != null) {
+			return albumLookup.getPictures();
+		} else {
+			//FIXME: return proper not found exception
+			return new String[]{};             
+		}
+	}
+
+	private Album getAlbum(String albumName) {
+		Album albumLookup = null;
+		for(Album album : albums) {
+			if(album.getName().equalsIgnoreCase(albumName)) {
+				albumLookup = album;
+				break;
+			}
+		}
+
+		return albumLookup;
+	}
+
+	private String getLocation() {
+		return location;
+	}
+}