[Yanel-commits] rev 31069 -
public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery
michi at wyona.com
michi at wyona.com
Fri Feb 1 01:39:08 CET 2008
Author: michi
Date: 2008-02-01 01:39:07 +0100 (Fri, 01 Feb 2008)
New Revision: 31069
Added:
public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ImageItemInTheGallery.java
public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ItemInTheRepositorySupport.java
Modified:
public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/GenericItemBase.java
public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/SimpleGallery.java
Log:
more stuff added
Modified: public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/GenericItemBase.java
===================================================================
--- public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/GenericItemBase.java 2008-02-01 00:00:02 UTC (rev 31068)
+++ public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/GenericItemBase.java 2008-02-01 00:39:07 UTC (rev 31069)
@@ -9,14 +9,14 @@
public abstract class GenericItemBase implements GenericItem {
protected static final Logger log = Logger.getLogger(GenericItemBase.class);
- private Properties p = new Properties();
+ protected Properties properties = new Properties();
public Object getProperty(String key) {
- return p.get(key);
+ return properties.get(key);
}
public void setProperty(String key, Object value) {
- p.put(key, value);
+ properties.put(key, value);
}
public final String getId() {
Added: public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ImageItemInTheGallery.java
===================================================================
--- public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ImageItemInTheGallery.java (rev 0)
+++ public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ImageItemInTheGallery.java 2008-02-01 00:39:07 UTC (rev 31069)
@@ -0,0 +1,97 @@
+package org.wyona.yanel.impl.resources.gallery;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import org.w3c.dom.Element;
+import org.wyona.commons.io.MimeTypeUtil;
+import org.wyona.commons.io.PathUtil;
+import org.wyona.yarep.core.Node;
+import org.wyona.yarep.core.Repository;
+import org.wyona.yarep.core.RepositoryException;
+
+public class ImageItemInTheGallery extends ItemInTheRepositorySupport implements ImageGalleryItem {
+ private class LinkedImageItemContent implements GalleryItemContent{
+ private String contentType = null;
+ private String link = null;
+
+ public LinkedImageItemContent(String contentType, String link){
+ this.contentType = contentType;
+ this.link = link;
+ }
+ public String getContentType() {
+ return contentType;
+ }
+
+ public boolean isLinkedContent() {
+ return true;
+ }
+ public String toString() {
+ return link;
+ }
+ }
+
+ public ImageItemInTheGallery(Repository repository, String collection)throws RepositoryException{
+ super(repository, collection);
+ }
+
+ protected void init() throws RepositoryException{
+ List/* <String> */imageTypes = new ArrayList/* <String> */();
+ imageTypes.add(ImageGalleryItem.BMP_TYPE);
+ imageTypes.add(ImageGalleryItem.JPEG_TYPE);
+ imageTypes.add(ImageGalleryItem.PNG_TYPE);
+ imageTypes.add(ImageGalleryItem.GIF_TYPE);
+
+ Node itemNode = getRepository().getNode(getPath());
+ Node[] imageData = itemNode.getNodes();
+ for (int j = 0; j < imageData.length; j++) {
+ // Detect mime type
+ String mimeType = imageData[j].getMimeType();
+ if (null == mimeType) {
+ mimeType = MimeTypeUtil.guessMimeType(PathUtil
+ .getSuffix(imageData[j].getName()));
+ }
+
+ if (imageData[j].isResource() && imageTypes.contains(mimeType)) {
+ content = new LinkedImageItemContent(mimeType, imageData[j].getPath());
+
+ // --- Set item-X properties available in the repository
+ Date d = new Date();
+
+ String id = itemNode.getUUID();
+ if (null == id) {
+ id = itemNode.getPath();
+ }
+ properties.setProperty(ID_KEY, id);
+
+ try {
+ d = new Date(getRepository().getNode(getPath())
+ .getLastModified());
+ } catch (Exception e) {
+ // Do nothing
+ }
+ properties.setProperty(UPDATED_KEY, XML_DATE_FORMAT.format(d));
+
+ properties.setProperty(PATH_KEY, itemNode.getPath());
+
+ // --- Set item properties from meta.xml
+ try {
+ Element meta = getMeta(getRepository().getNode(itemNode.getPath()).getNode(META_XML));
+ String title = getMetaValue(meta, TITLE_KEY);
+ if (title == null) {
+ title = imageData[j].getPath();
+ }
+ properties.setProperty(TITLE_KEY, title);
+ } catch (Exception e) {
+ log.debug(e, e);
+ properties.setProperty(TITLE_KEY, imageData[j].getPath());
+ }
+ }
+ }
+ }
+
+ public GalleryItemContent getContent() {
+ return content;
+ }
+}
Added: public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ItemInTheRepositorySupport.java
===================================================================
--- public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ItemInTheRepositorySupport.java (rev 0)
+++ public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/ItemInTheRepositorySupport.java 2008-02-01 00:39:07 UTC (rev 31069)
@@ -0,0 +1,108 @@
+package org.wyona.yanel.impl.resources.gallery;
+
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.wyona.yarep.core.NoSuchNodeException;
+import org.wyona.yarep.core.Node;
+import org.wyona.yarep.core.Repository;
+import org.wyona.yarep.core.RepositoryException;
+
+public abstract class ItemInTheRepositorySupport extends GenericItemBase{
+ public static final String META_XML = "meta.xml";
+
+ private Repository repository = null;
+ protected List/*<Item>*/ subitems = null;
+ protected GalleryItemContent content = null;
+
+ public ItemInTheRepositorySupport(Repository repository, String collection)throws RepositoryException{
+ this.repository = repository;
+ setCollection(collection);
+ init();
+ }
+
+ /**
+ * Does nothing
+ * */
+ protected ItemInTheRepositorySupport(){}
+
+ /**
+ * Write property to the repository (update meta.xml that must be already available)
+ * */
+ public void setProperty(String key, Object value) {
+ Element meta = null;
+ try {
+ meta = getMeta(getRepository().getNode(getPath()).getNode(META_XML));
+ } catch (Exception e) {
+ log.info(e, e);
+
+ // Failed to set the property
+ return;
+ }
+
+ if(key.equals(TITLE_KEY)){
+ NodeList nl = meta.getElementsByTagName(TITLE_KEY);
+ while(nl.getLength() > 0){
+ nl.item(0).getParentNode().removeChild(nl.item(0));
+ }
+ Element title = meta.getOwnerDocument().createElement(TITLE_KEY);
+ title.appendChild(meta.getOwnerDocument().createTextNode(String.valueOf(value)));
+ meta.appendChild(title);
+ }
+ // TODO: write other properties
+
+ try {
+ Transformer t = TransformerFactory.newInstance().newTransformer();
+ t.transform(new DOMSource(meta), new StreamResult(getRepository().getNode(getPath()).getNode(META_XML).getOutputStream()));
+ // Update the instance
+ super.setProperty(key, value);
+ } catch (Exception e) {
+ log.info(e, e);
+ }
+ }
+
+ public void setRepository(Repository repository){
+ this.repository = repository;
+ }
+
+ public Repository getRepository(){
+ return repository;
+ }
+
+ protected abstract void init() throws RepositoryException;
+
+ public void setCollection(String path) {
+ if(!path.endsWith("/")){
+ path += "/";
+ }
+ properties.setProperty(PATH_KEY, path);
+ }
+
+ protected final Element getMeta(Node metaXml) throws Exception{
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ Document d = dbf.newDocumentBuilder().parse(metaXml.getInputStream());
+ return d.getDocumentElement();
+ }
+
+ protected final String getMetaValue(Element meta, String tag) throws Exception{
+ String value = null;
+
+ NodeList nl = meta.getElementsByTagName(TITLE_KEY);
+ if(nl.getLength() > 0){
+ value = nl.item(0).getFirstChild().getNodeValue();
+ }
+ return value;
+ }
+}
Modified: public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/SimpleGallery.java
===================================================================
--- public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/SimpleGallery.java 2008-02-01 00:00:02 UTC (rev 31068)
+++ public/yanel/contributions/resources/gallery/src/java/org/wyona/yanel/impl/resources/gallery/SimpleGallery.java 2008-02-01 00:39:07 UTC (rev 31069)
@@ -53,44 +53,20 @@
* </pre>
* </p>
*/
-public class SimpleGallery extends GenericItemBase implements Gallery {
- public static final String META_XML = "meta.xml";
+public class SimpleGallery extends ItemInTheRepositorySupport implements Gallery {
private static final String ITEM_COLLECTION_PREFIX = "item-";
- private Repository repository = null;
- private List/*<Item>*/ imageItems = null;
-
/**
* Clients should initialize the object properly after calling this constructor.
* Repository and gallery collection must be set as well as loadRepository() must be performed.
* */
- public SimpleGallery(){
-
+ public SimpleGallery(Repository repository, String collection) throws RepositoryException{
+ super(repository, collection);
}
+
+ private SimpleGallery(){}
- public SimpleGallery(Repository repository, String galleryCollection){
- this.repository = repository;
- setCollection(galleryCollection);
-
- loadRepository();
- }
-
- public void setRepository(Repository repository) {
- this.repository = repository;
- }
-
- public Repository getRepository() {
- return repository;
- }
-
- public void setCollection(String path) {
- if(!path.endsWith("/")){
- path += "/";
- }
- setProperty(PATH_KEY, path);
- }
-
- public final void loadRepository(){
+ public final void init(){
initGallery();
initItems();
}
@@ -105,9 +81,9 @@
log.debug(e, e);
}
if(id == null)
- setProperty(ID_KEY, getPath());
+ properties.setProperty(ID_KEY, getPath());
else
- setProperty(ID_KEY, id);
+ properties.setProperty(ID_KEY, id);
Date d = new Date();
try {
@@ -115,28 +91,28 @@
} catch (Exception e) {
// Do nothing
}
- setProperty(UPDATED_KEY, d);
+ properties.setProperty(UPDATED_KEY, XML_DATE_FORMAT.format(d));
- setProperty(PATH_KEY, getPath());
+ properties.setProperty(PATH_KEY, getPath());
- //--- Set properties in available in meta.xml
+ //--- Set properties if available in meta.xml
try {
Element meta = getMeta(getRepository().getNode(getPath()).getNode(META_XML));
String title = getMetaValue(meta, TITLE_KEY);
if(title == null){
title = getPath();
}
- setProperty(TITLE_KEY, title);
+ properties.setProperty(TITLE_KEY, title);
} catch (Exception e) {
log.debug(e, e);
- setProperty(TITLE_KEY, getPath());
+ properties.setProperty(TITLE_KEY, getPath());
}
}
protected void initItems(){
- if(imageItems == null){
- imageItems = new ArrayList/*<Item>*/();
+ if(subitems == null){
+ subitems = new ArrayList/*<Item>*/();
}
List/*<String>*/ imageTypes = new ArrayList/*<String>*/();
@@ -162,98 +138,34 @@
// Don't care about the weired files
continue;
}else{
- Node [] imageData = children[i].getNodes();
- for (int j = 0; j < imageData.length; j++) {
- // Detect mime type
- String mimeType = imageData[j].getMimeType();
- if(null == mimeType){
- mimeType = MimeTypeUtil.guessMimeType(PathUtil.getSuffix(imageData[j].getName()));
- }
-
- if(imageData[j].isResource() && imageTypes.contains(mimeType)){
- LinkedImageGalleryItem item = new LinkedImageGalleryItem(mimeType, imageData[j].getPath());
-
- //--- Set item-X properties available in the repository
- Date d = new Date();
-
- String id = children[i].getUUID();
- if(null == id){
- id = children[i].getPath();
- }
- item.setProperty(ID_KEY, id);
-
- try {
- d = new Date(getRepository().getNode(getPath()).getLastModified());
- } catch (Exception e) {
- // Do nothing
- }
- item.setProperty(UPDATED_KEY, d);
-
- item.setProperty(PATH_KEY, children[i].getPath());
-
- //--- Set item properties from meta.xml
- try {
- Element meta = getMeta(getRepository().getNode(children[i].getPath()).getNode(META_XML));
- String title = getMetaValue(meta, TITLE_KEY);
- if(title == null){
- title = imageData[j].getPath();
- }
- item.setProperty(TITLE_KEY, title);
- } catch (Exception e) {
- log.debug(e, e);
- item.setProperty(TITLE_KEY, imageData[j].getPath());
- }
-
- imageItems.add(item);
- }
- }
+ subitems.add(new ImageItemInTheGallery(getRepository(), children[i].getPath()));
}
} catch (RepositoryException e) {
- log.debug(e, e);
+ log.debug("Could not load a gallery item", e);
}
}
}
- protected final Element getMeta(Node metaXml) throws Exception{
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- Document d = dbf.newDocumentBuilder().parse(metaXml.getInputStream());
- return d.getDocumentElement();
+ public final GalleryItem getItem(int index) throws IndexOutOfBoundsException {
+ return (GalleryItem)subitems.get(index);
}
- protected final String getMetaValue(Element meta, String tag) throws Exception{
- String value = null;
-
- NodeList nl = meta.getElementsByTagName(TITLE_KEY);
- if(nl.getLength() > 0){
- value = nl.item(0).getFirstChild().getNodeValue();
+ public GalleryItem getItem(String id){
+ for (Iterator i = subitems.iterator(); i.hasNext();) {
+ GalleryItem item = (GalleryItem) i.next();
+ if(item.getId().equals(id)){
+ return item;
+ }
}
- return value;
+ return null;
}
-
- public final GalleryItem getItem(int index) throws IndexOutOfBoundsException {
- if(imageItems == null){
- imageItems = new ArrayList/*<Item>*/();
- loadRepository();
- }
- return (GalleryItem)imageItems.get(index);
- }
public final int size() {
- if(imageItems == null){
- imageItems = new ArrayList/*<Item>*/();
- loadRepository();
- }
- return imageItems.size();
+ return subitems.size();
}
public final GalleryItem removeItem(String id) {
- if(imageItems == null){
- imageItems = new ArrayList/*<Item>*/();
- loadRepository();
- }
-
- for (Iterator i = imageItems.iterator(); i.hasNext();) {
+ for (Iterator i = subitems.iterator(); i.hasNext();) {
GalleryItem item = (GalleryItem) i.next();
if(id.equals(item.getId())){
return removeItem(item);
@@ -303,11 +215,6 @@
* @return - created [item-X] node, or null if failed to create one.
* */
public Node createItem(GenericItem item) {
- if(imageItems == null){
- imageItems = new ArrayList/*<Item>*/();
- loadRepository();
- }
-
Node itemNode = null;
try {
Node galleryNode = getRepository().getNode(getPath());
@@ -320,11 +227,6 @@
}
itemNode = galleryNode.addNode(itemNodeName, NodeType.COLLECTION);
- // Fix the system properties
- item.setProperty(ID_KEY, itemNode.getUUID());
- item.setProperty(PATH_KEY, itemNode.getPath());
- imageItems.add(item);
-
// Create meta of the item
Node metaXml = itemNode.addNode(META_XML, NodeType.RESOURCE);
metaXml.setMimeType("application/xml");
@@ -345,6 +247,10 @@
PrintWriter pw = new PrintWriter(new OutputStreamWriter(metaXml.getOutputStream()));
pw.write(sb.toString());
pw.flush();
+
+ // Fix the system properties
+ GalleryItem gi = new ImageItemInTheGallery(getRepository(), itemNode.getPath());
+ subitems.add(gi);
} catch (Exception e) {
log.warn("Could not create the item properly", e);
}
More information about the Yanel-commits
mailing list