[iCal4j-commit] ical4j-connector/src/main/java/net/fortuna/ical4j/connector/jcr AbstractJcrObjectC
Brought to you by:
fortuna
From: Ben F. <fo...@us...> - 2009-01-23 07:59:53
|
Update of /cvsroot/ical4j/ical4j-connector/src/main/java/net/fortuna/ical4j/connector/jcr In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv17202/src/main/java/net/fortuna/ical4j/connector/jcr Modified Files: JcrCalendarCollection.java JcrCalendar.java JcrCalendarStore.java Added Files: AbstractJcrObjectCollection.java AbstractJcrObjectStore.java Log Message: Added abstract jcr implementations Index: JcrCalendar.java =================================================================== RCS file: /cvsroot/ical4j/ical4j-connector/src/main/java/net/fortuna/ical4j/connector/jcr/JcrCalendar.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** JcrCalendar.java 18 Jan 2009 06:52:40 -0000 1.1 --- JcrCalendar.java 23 Jan 2009 07:59:49 -0000 1.2 *************** *** 107,111 **** file = new JcrFile(); ! file.setName("calendarData"); file.setDataProvider(new JcrDataProviderImpl(TYPE.BYTES, calendar.toString().getBytes())); file.setMimeType(MediaType.ICALENDAR_2_0.getContentType()); --- 107,111 ---- file = new JcrFile(); ! file.setName("data"); file.setDataProvider(new JcrDataProviderImpl(TYPE.BYTES, calendar.toString().getBytes())); file.setMimeType(MediaType.ICALENDAR_2_0.getContentType()); --- NEW FILE: AbstractJcrObjectCollection.java --- /* * Copyright (c) 2009, Ben Fortuna * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * o Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * o Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * o Neither the name of Ben Fortuna nor the names of any other contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package net.fortuna.ical4j.connector.jcr; import net.fortuna.ical4j.connector.ObjectCollection; import org.jcrom.AbstractJcrEntity; import org.jcrom.annotations.JcrProperty; /** * * * @author Ben * * Created on: 23/01/2009 * * $Id: AbstractJcrObjectCollection.java,v 1.1 2009/01/23 07:59:49 fortuna Exp $ */ public abstract class AbstractJcrObjectCollection extends AbstractJcrEntity implements ObjectCollection { /** * */ private static final long serialVersionUID = -7943312823296190389L; @JcrProperty private String description; @JcrProperty private String displayName; private AbstractJcrObjectStore<? extends AbstractJcrObjectCollection> store; /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectCollection#getDescription() */ @Override public String getDescription() { return description; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectCollection#getDisplayName() */ @Override public String getDisplayName() { return displayName; } /** * @param description the description to set */ public final void setDescription(String description) { this.description = description; } /** * @param displayName the displayName to set */ public final void setDisplayName(String displayName) { this.displayName = displayName; } /** * @return the store */ public final AbstractJcrObjectStore<? extends AbstractJcrObjectCollection> getStore() { return store; } /** * @param store the store to set */ public final void setStore( AbstractJcrObjectStore<? extends AbstractJcrObjectCollection> store) { this.store = store; } } Index: JcrCalendarStore.java =================================================================== RCS file: /cvsroot/ical4j/ical4j-connector/src/main/java/net/fortuna/ical4j/connector/jcr/JcrCalendarStore.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** JcrCalendarStore.java 18 Jan 2009 08:25:55 -0000 1.3 --- JcrCalendarStore.java 23 Jan 2009 07:59:49 -0000 1.4 *************** *** 33,48 **** package net.fortuna.ical4j.connector.jcr; - import javax.jcr.LoginException; - import javax.jcr.Node; import javax.jcr.PathNotFoundException; import javax.jcr.Repository; import javax.jcr.RepositoryException; - import javax.jcr.Session; - import javax.jcr.SimpleCredentials; import net.fortuna.ical4j.connector.CalendarStore; import net.fortuna.ical4j.connector.ObjectNotFoundException; import net.fortuna.ical4j.connector.ObjectStoreException; - import net.fortuna.ical4j.model.Calendar; import org.jcrom.Jcrom; --- 33,43 ---- *************** *** 56,68 **** * */ ! public class JcrCalendarStore implements CalendarStore<JcrCalendarCollection> { ! ! private Repository repository; ! ! private String path; ! ! private Session session; ! ! private Jcrom jcrom; /** --- 51,56 ---- * */ ! public class JcrCalendarStore extends AbstractJcrObjectStore<JcrCalendarCollection> ! implements CalendarStore<JcrCalendarCollection> { /** *************** *** 70,201 **** */ public JcrCalendarStore(Jcrom jcrom, Repository repository, String path) { ! this.repository = repository; ! this.path = path; - // jcrom = new Jcrom(true, true); - this.jcrom = jcrom; // ensure appropriate classes are mapped.. jcrom.map(JcrCalendarCollection.class); jcrom.map(JcrCalendar.class); } - - /** - * @return - * @throws RepositoryException - * @throws PathNotFoundException - * @throws ObjectStoreException - */ - Node getNode() throws PathNotFoundException, RepositoryException, ObjectStoreException { - assertConnected(); - try { - return session.getRootNode().getNode(path); - } - catch (PathNotFoundException pnfe) { - session.getRootNode().addNode(path); - session.save(); - } - return session.getRootNode().getNode(path); - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectStore#addCollection(java.lang.String) - */ - @Override - public JcrCalendarCollection addCollection(String id) throws ObjectStoreException { - assertConnected(); - JcrCalendarCollection collection = new JcrCalendarCollection(); - collection.setStore(this); - collection.setName(id); - try { - jcrom.addNode(getNode(), collection); - session.save(); - } - catch (RepositoryException re) { - throw new ObjectStoreException("Error creating collection", re); - } - return collection; - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectStore#addCollection(java.lang.String, java.lang.String, java.lang.String, java.lang.String[], net.fortuna.ical4j.model.Calendar) - */ - @Override - public JcrCalendarCollection addCollection(String id, String displayName, - String description, String[] supportedComponents, Calendar timezone) - throws ObjectStoreException { - JcrCalendarCollection collection = new JcrCalendarCollection(); - collection.setStore(this); - collection.setName(id); - collection.setDisplayName(displayName); - collection.setDescription(description); - try { - jcrom.addNode(getNode(), collection); - session.save(); - } - catch (RepositoryException re) { - throw new ObjectStoreException("Error creating collection", re); - } - return collection; - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectStore#connect() - */ - @Override - public boolean connect() throws ObjectStoreException { - if (repository == null) { - throw new ObjectStoreException("Repository not configured"); - } - - try { - session = repository.login(); - } - catch (LoginException le) { - throw new ObjectStoreException("Unable to login", le); - } - catch (RepositoryException re) { - throw new ObjectStoreException("Error connecting", re); - } - return session != null; - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectStore#connect(java.lang.String, char[]) - */ - @Override - public boolean connect(String username, char[] password) throws ObjectStoreException { - if (repository == null) { - throw new ObjectStoreException("Repository not configured"); - } - - try { - session = repository.login(new SimpleCredentials(username, password)); - } - catch (LoginException le) { - throw new ObjectStoreException("Unable to login", le); - } - catch (RepositoryException re) { - throw new ObjectStoreException("Error connecting", re); - } - return session != null; - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectStore#disconnect() - */ - @Override - public void disconnect() throws ObjectStoreException { - assertConnected(); - session.logout(); - } - - /** - * @throws ObjectStoreException - */ - private void assertConnected() throws ObjectStoreException { - if (session == null) { - throw new ObjectStoreException("Not connected"); - } - } /* (non-Javadoc) --- 58,67 ---- */ public JcrCalendarStore(Jcrom jcrom, Repository repository, String path) { ! super(repository, path, jcrom); // ensure appropriate classes are mapped.. jcrom.map(JcrCalendarCollection.class); jcrom.map(JcrCalendar.class); } /* (non-Javadoc) *************** *** 205,209 **** public JcrCalendarCollection getCollection(String id) throws ObjectStoreException, ObjectNotFoundException { try { ! JcrCalendarCollection collection = jcrom.fromNode(JcrCalendarCollection.class, getNode().getNode(id)); collection.setStore(this); return collection; --- 71,75 ---- public JcrCalendarCollection getCollection(String id) throws ObjectStoreException, ObjectNotFoundException { try { ! JcrCalendarCollection collection = getJcrom().fromNode(JcrCalendarCollection.class, getNode().getNode(id)); collection.setStore(this); return collection; *************** *** 216,254 **** } } ! /* (non-Javadoc) ! * @see net.fortuna.ical4j.connector.ObjectStore#removeCollection(java.lang.String) */ @Override ! public JcrCalendarCollection removeCollection(String id) throws ObjectStoreException, ObjectNotFoundException { ! JcrCalendarCollection collection = getCollection(id); ! Node collectionNode; ! try { ! collectionNode = getNode().getNode(id); ! collectionNode.remove(); ! session.save(); ! } ! catch (PathNotFoundException e) { ! throw new ObjectNotFoundException("Collection not found", e); ! } ! catch (RepositoryException e) { ! throw new ObjectNotFoundException("Error retrieving collection", e); ! } ! return collection; ! } ! ! /** ! * @return the session ! */ ! final Session getSession() { ! return session; ! } ! ! /** ! * @return the jcrom ! */ ! final Jcrom getJcrom() { ! return jcrom; } - } --- 82,92 ---- } } ! /* (non-Javadoc) ! * @see net.fortuna.ical4j.connector.jcr.AbstractJcrObjectStore#newCollection() */ @Override ! protected JcrCalendarCollection newCollection() { ! return new JcrCalendarCollection(); } } --- NEW FILE: AbstractJcrObjectStore.java --- /* * Copyright (c) 2009, Ben Fortuna * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * o Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * o Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * o Neither the name of Ben Fortuna nor the names of any other contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package net.fortuna.ical4j.connector.jcr; import javax.jcr.LoginException; import javax.jcr.Node; import javax.jcr.PathNotFoundException; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import net.fortuna.ical4j.connector.ObjectNotFoundException; import net.fortuna.ical4j.connector.ObjectStore; import net.fortuna.ical4j.connector.ObjectStoreException; import net.fortuna.ical4j.model.Calendar; import org.jcrom.Jcrom; /** * * * @author Ben * * Created on: 23/01/2009 * * $Id: AbstractJcrObjectStore.java,v 1.1 2009/01/23 07:59:49 fortuna Exp $ */ public abstract class AbstractJcrObjectStore<T extends AbstractJcrObjectCollection> implements ObjectStore<T> { private Repository repository; private String path; private Session session; private Jcrom jcrom; /** * @param repoitory * @param path * @param jcrom */ public AbstractJcrObjectStore(Repository repoitory, String path, Jcrom jcrom) { this.repository = repoitory; this.path = path; this.jcrom = jcrom; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#connect() */ @Override public final boolean connect() throws ObjectStoreException { if (repository == null) { throw new ObjectStoreException("Repository not configured"); } try { session = repository.login(); } catch (LoginException le) { throw new ObjectStoreException("Unable to login", le); } catch (RepositoryException re) { throw new ObjectStoreException("Error connecting", re); } return session != null; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#connect(java.lang.String, char[]) */ @Override public final boolean connect(String username, char[] password) throws ObjectStoreException { if (repository == null) { throw new ObjectStoreException("Repository not configured"); } try { session = repository.login(new SimpleCredentials(username, password)); } catch (LoginException le) { throw new ObjectStoreException("Unable to login", le); } catch (RepositoryException re) { throw new ObjectStoreException("Error connecting", re); } return session != null; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#disconnect() */ @Override public final void disconnect() throws ObjectStoreException { assertConnected(); session.logout(); } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#addCollection(java.lang.String) */ @Override public final T addCollection(String id) throws ObjectStoreException { assertConnected(); T collection = newCollection(); collection.setStore(this); collection.setName(id); try { getJcrom().addNode(getNode(), collection); getSession().save(); } catch (RepositoryException re) { throw new ObjectStoreException("Error creating collection", re); } return collection; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#addCollection(java.lang.String, java.lang.String, java.lang.String, java.lang.String[], net.fortuna.ical4j.model.Calendar) */ @Override public final T addCollection(String id, String displayName, String description, String[] supportedComponents, Calendar timezone) throws ObjectStoreException { T collection = newCollection(); collection.setStore(this); collection.setName(id); collection.setDisplayName(displayName); collection.setDescription(description); try { getJcrom().addNode(getNode(), collection); getSession().save(); } catch (RepositoryException re) { throw new ObjectStoreException("Error creating collection", re); } return collection; } /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#getCollection(java.lang.String) */ /* @Override public T getCollection(String id) throws ObjectStoreException, ObjectNotFoundException { try { T collection = getJcrom().fromNode(Class<T>, getNode().getNode(id)); collection.setStore(this); return collection; } catch (PathNotFoundException e) { throw new ObjectNotFoundException("Collection not found", e); } catch (RepositoryException e) { throw new ObjectNotFoundException("Error retrieving collection", e); } } */ /* (non-Javadoc) * @see net.fortuna.ical4j.connector.ObjectStore#removeCollection(java.lang.String) */ @Override public final T removeCollection(String id) throws ObjectStoreException, ObjectNotFoundException { T collection = getCollection(id); Node collectionNode; try { collectionNode = getNode().getNode(id); collectionNode.remove(); getSession().save(); } catch (PathNotFoundException e) { throw new ObjectNotFoundException("Collection not found", e); } catch (RepositoryException e) { throw new ObjectNotFoundException("Error retrieving collection", e); } return collection; } /** * @return * @throws RepositoryException * @throws PathNotFoundException * @throws ObjectStoreException */ final Node getNode() throws PathNotFoundException, RepositoryException, ObjectStoreException { assertConnected(); try { return session.getRootNode().getNode(path); } catch (PathNotFoundException pnfe) { session.getRootNode().addNode(path); session.save(); } return session.getRootNode().getNode(path); } /** * @return the session */ final Session getSession() { return session; } /** * @return the jcrom */ final Jcrom getJcrom() { return jcrom; } /** * @throws ObjectStoreException */ protected final void assertConnected() throws ObjectStoreException { if (session == null) { throw new ObjectStoreException("Not connected"); } } protected abstract T newCollection(); } Index: JcrCalendarCollection.java =================================================================== RCS file: /cvsroot/ical4j/ical4j-connector/src/main/java/net/fortuna/ical4j/connector/jcr/JcrCalendarCollection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** JcrCalendarCollection.java 18 Jan 2009 06:52:40 -0000 1.2 --- JcrCalendarCollection.java 23 Jan 2009 07:59:49 -0000 1.3 *************** *** 50,54 **** import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.jcrom.AbstractJcrEntity; import org.jcrom.JcrMappingException; import org.jcrom.annotations.JcrChildNode; --- 50,53 ---- *************** *** 63,67 **** * */ ! public class JcrCalendarCollection extends AbstractJcrEntity implements CalendarCollection { /** --- 62,66 ---- * */ ! public class JcrCalendarCollection extends AbstractJcrObjectCollection implements CalendarCollection { /** *************** *** 71,76 **** private static final Log LOG = LogFactory.getLog(JcrCalendarCollection.class); - - private JcrCalendarStore store; @JcrChildNode private List<JcrCalendar> calendars; --- 70,73 ---- *************** *** 85,92 **** @JcrProperty private Date minDateTime; - - @JcrProperty private String description; - - @JcrProperty private String displayName; /** --- 82,85 ---- *************** *** 98,115 **** calendars = new ArrayList<JcrCalendar>(); } - - /** - * @return the store - */ - public final JcrCalendarStore getStore() { - return store; - } - - /** - * @param store the store to set - */ - public final void setStore(JcrCalendarStore store) { - this.store = store; - } /** --- 91,94 ---- *************** *** 120,124 **** */ private Node getNode() throws PathNotFoundException, JcrMappingException, RepositoryException { ! return store.getSession().getRootNode().getNode(store.getJcrom().getPath(this).substring(1)); } --- 99,103 ---- */ private Node getNode() throws PathNotFoundException, JcrMappingException, RepositoryException { ! return getStore().getSession().getRootNode().getNode(getStore().getJcrom().getPath(this).substring(1)); } *************** *** 321,340 **** /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectCollection#getDescription() - */ - @Override - public String getDescription() { - return description; - } - - /* (non-Javadoc) - * @see net.fortuna.ical4j.connector.ObjectCollection#getDisplayName() - */ - @Override - public String getDisplayName() { - return displayName; - } - - /* (non-Javadoc) * @see net.fortuna.ical4j.connector.CalendarCollection#merge(net.fortuna.ical4j.model.Calendar) */ --- 300,303 ---- *************** *** 387,404 **** this.minDateTime = minDateTime; } - - /** - * @param description the description to set - */ - final void setDescription(String description) { - this.description = description; - } - - /** - * @param displayName the displayName to set - */ - final void setDisplayName(String displayName) { - this.displayName = displayName; - } /** --- 350,353 ---- *************** *** 413,417 **** // } ! store.getJcrom().updateNode(getNode(), this); getNode().save(); } --- 362,366 ---- // } ! getStore().getJcrom().updateNode(getNode(), this); getNode().save(); } |