Skip to content

Commit

Permalink
Added workspace support
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jan 25, 2024
1 parent 08662a4 commit 191167b
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public interface ObjectStore<T, C extends ObjectCollection<T>> extends ObjectSto
* exists in the store
*/
C addCollection(String id) throws ObjectStoreException;


C addCollection(String id, String workspace) throws ObjectStoreException;

/**
* @param id a collection identifier
* @param displayName the collection name
Expand All @@ -93,7 +95,10 @@ public interface ObjectStore<T, C extends ObjectCollection<T>> extends ObjectSto
*/
C addCollection(String id, String displayName, String description,
String[] supportedComponents, Calendar timezone) throws ObjectStoreException;


C addCollection(String id, String displayName, String description,
String[] supportedComponents, Calendar timezone, String workspace) throws ObjectStoreException;

/**
* Removes the collection with specified id from the store.
* @param id a collection identifier
Expand All @@ -105,7 +110,7 @@ C addCollection(String id, String displayName, String description,
*/
@Deprecated
C removeCollection(String id) throws ObjectStoreException, ObjectNotFoundException;

/**
* @param id a collection identifier
* @return an object collection with the specified id. If no collection with the specified id
Expand All @@ -115,5 +120,11 @@ C addCollection(String id, String displayName, String description,
*/
C getCollection(String id) throws ObjectStoreException, ObjectNotFoundException;

C getCollection(String id, String workspace) throws ObjectStoreException, ObjectNotFoundException;

List<C> getCollections() throws ObjectStoreException, ObjectNotFoundException;

List<C> getCollections(String workspace) throws ObjectStoreException, ObjectNotFoundException;

List<String> listWorkspaces();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public boolean isConnected() {

@Override
public C addCollection(String id) throws ObjectStoreException {
File collectionDir = new File(root, id);
return addCollection(id, "default");
}

@Override
public C addCollection(String id, String workspace) throws ObjectStoreException {
File collectionDir = new File(getWorkspaceDir(workspace), id);
if ((collectionDir.exists() && !collectionDir.isDirectory()) ||
(!collectionDir.exists() && !collectionDir.mkdirs())) {
throw new ObjectStoreException("Unable to initialise collection");
Expand All @@ -61,7 +66,7 @@ public C addCollection(String id) throws ObjectStoreException {
collection = getCollection(id);
} catch (ObjectNotFoundException e) {
try {
collection = newCollection(id);
collection = newCollection(id, workspace);
} catch (IOException ex) {
throw new ObjectStoreException(ex);
}
Expand All @@ -73,11 +78,17 @@ public C addCollection(String id) throws ObjectStoreException {
return collection;
}

protected abstract C newCollection(String id) throws IOException;
protected abstract C newCollection(String id, String workspace) throws IOException;

@Override
public C addCollection(String id, String displayName, String description, String[] supportedComponents, Calendar timezone) throws ObjectStoreException {
C collection = addCollection(id);
return addCollection(id, displayName, description, supportedComponents, timezone, "default");
}

@Override
public C addCollection(String id, String displayName, String description, String[] supportedComponents,
Calendar timezone, String workspace) throws ObjectStoreException {
C collection = addCollection(id, workspace);
try {
collection.setDisplayName(displayName);
collection.setDescription(description);
Expand Down Expand Up @@ -106,25 +117,48 @@ public C removeCollection(String id) throws ObjectNotFoundException, ObjectStore

@Override
public C getCollection(String id) throws ObjectStoreException, ObjectNotFoundException {
File collectionDir = new File(root, id);
return getCollection(id, "default");
}

@Override
public C getCollection(String id, String workspace) throws ObjectStoreException, ObjectNotFoundException {
File collectionDir = new File(getWorkspaceDir(workspace), id);
if (!collectionDir.exists() || !collectionDir.isDirectory()) {
throw new ObjectNotFoundException("Unable to retrieve collection");
}
try {
return newCollection(id);
return newCollection(id, workspace);
} catch (IOException e) {
throw new ObjectStoreException(e);
}
}

@Override
public List<C> getCollections() {
return Arrays.stream(root.list()).map(name -> {
return getCollections("default");
}

@Override
public List<C> getCollections(String workspace) {
return Arrays.stream(Objects.requireNonNull(getWorkspaceDir(workspace).list())).map(name -> {
try {
return newCollection(name);
return newCollection(name, workspace);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
}

protected File getWorkspaceDir(String workspace) {
File workspaceDir = new File(root, workspace);
if (!workspaceDir.exists() && !workspaceDir.mkdir()) {
throw new IllegalArgumentException("Invalid workspace");
}
return workspaceDir;
}

@Override
public List<String> listWorkspaces() {
return Arrays.asList(Objects.requireNonNull(root.list()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public LocalCalendarStore(File root) {
}

@Override
protected LocalCalendarCollection newCollection(String id) throws IOException {
return new LocalCalendarCollection(new File(getRoot(), id));
protected LocalCalendarCollection newCollection(String id, String workspace) throws IOException {
return new LocalCalendarCollection(new File(getWorkspaceDir(workspace), id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public LocalCardStore(File root) {
}

@Override
protected LocalCardCollection newCollection(String id) throws IOException {
return new LocalCardCollection(new File(getRoot(), id));
protected LocalCardCollection newCollection(String id, String workspace) throws IOException {
return new LocalCardCollection(new File(getWorkspaceDir(workspace), id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import spock.lang.Specification
abstract class AbstractLocalTest extends Specification {

@Shared
File storeLocation
File storeLocation, workspaceLocation

def setup() {
storeLocation = ['build', getClass().name]
workspaceLocation = [storeLocation, 'default']
}

def cleanup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LocalCalendarCollectionTest extends AbstractLocalTest {
collection.add(calendar)

then: 'a new calendar file is created'
new File(storeLocation,
new File(workspaceLocation,
"public_holidays/${calendar.getComponent(Component.VEVENT).get().getRequiredProperty(Property.UID).getValue()}.ics").exists()

and: 'the listener is notified'
Expand All @@ -63,7 +63,7 @@ class LocalCalendarCollectionTest extends AbstractLocalTest {
def removed = collection.removeAll(calendar.getUid().value)

then: 'the exsiting calendar file is deleted'
!new File(storeLocation,
!new File(workspaceLocation,
"public_holidays/${calendar.getComponent(Component.VEVENT).get().getRequiredProperty(Property.UID).getValue()}.ics").exists()

and: 'removed calendar is identical to added'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LocalCalendarStoreTest extends AbstractLocalTest {
LocalCalendarCollection collection = calendarStore.addCollection('public_holidays')

then: 'a local collection directory is added'
new File(storeLocation, 'public_holidays').exists()
new File(workspaceLocation, 'public_holidays').exists()

and: 'the listener is notified'
event != null && event.collection == collection
Expand All @@ -41,7 +41,7 @@ class LocalCalendarStoreTest extends AbstractLocalTest {
'Victorian public holidays', [Component.VEVENT] as String[], timezone)

then: 'a local collection directory is added'
new File(storeLocation, 'public_holidays').exists()
new File(workspaceLocation, 'public_holidays').exists()

and: 'the collection properties are saved'
collection.displayName == 'Public Holidays'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LocalCardCollectionTest extends AbstractLocalTest {
collection.add(card)

then: 'a new card file is created'
new File(storeLocation,
new File(workspaceLocation,
"contacts/${card.getRequiredProperty(PropertyName.UID).getValue()}.vcf").exists()

and: 'the listener is notified'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LocalCardStoreTest extends AbstractLocalTest {
LocalCardCollection collection = cardStore.addCollection('contacts')

then: 'a local collection directory is added'
new File(storeLocation, 'contacts').exists()
new File(workspaceLocation, 'contacts').exists()

and: 'the listener is notified'
event != null && event.collection == collection
Expand All @@ -41,7 +41,7 @@ class LocalCardStoreTest extends AbstractLocalTest {
'Personal Contacts', ['VCARD'] as String[], timezone)

then: 'a local collection directory is added'
new File(storeLocation, 'contacts').exists()
new File(workspaceLocation, 'contacts').exists()

and: 'the collection properties are saved'
collection.displayName == 'Contacts'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public CalDavCalendarStore(String prodId, URL url, PathResolver pathResolver) {
/**
* {@inheritDoc}
*/
@Override
public CalDavCalendarCollection addCollection(String id) throws ObjectStoreException {
CalDavCalendarCollection collection = new CalDavCalendarCollection(this, id);
try {
Expand All @@ -106,9 +107,15 @@ public CalDavCalendarCollection addCollection(String id) throws ObjectStoreExcep
return collection;
}

@Override
public CalDavCalendarCollection addCollection(String id, String workspace) throws ObjectStoreException {
throw new UnsupportedOperationException("Workspaces not yet implemented");
}

/**
* {@inheritDoc}
*/
@Override
public CalDavCalendarCollection addCollection(String id, String displayName, String description,
String[] supportedComponents, Calendar timezone) throws ObjectStoreException {

Expand All @@ -121,6 +128,11 @@ public CalDavCalendarCollection addCollection(String id, String displayName, Str
return collection;
}

@Override
public CalDavCalendarCollection addCollection(String id, String displayName, String description, String[] supportedComponents, Calendar timezone, String workspace) throws ObjectStoreException {
throw new UnsupportedOperationException("Workspaces not yet implemented");
}

/**
* {@inheritDoc}
*/
Expand All @@ -137,6 +149,7 @@ public CalDavCalendarCollection addCollection(String id, DavPropertySet properti
/**
* {@inheritDoc}
*/
@Override
public CalDavCalendarCollection getCollection(String id) throws ObjectStoreException, ObjectNotFoundException {
try {
String resourcePath = pathResolver.getCalendarPath(id, "test");
Expand All @@ -157,6 +170,11 @@ public CalDavCalendarCollection getCollection(String id) throws ObjectStoreExcep
}
}

@Override
public CalDavCalendarCollection getCollection(String id, String workspace) throws ObjectStoreException, ObjectNotFoundException {
throw new UnsupportedOperationException("Workspaces not yet implemented");
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -202,6 +220,7 @@ protected String findCalendarHomeSet(String propfindUri) throws IOException {
* @throws IOException where a communications error occurs
* @throws DavException where an error occurs calling the DAV method
*/
@Override
public List<CalDavCalendarCollection> getCollections() throws ObjectStoreException, ObjectNotFoundException {
try {
String calHomeSetPath = findCalendarHomeSet();
Expand All @@ -213,9 +232,14 @@ public List<CalDavCalendarCollection> getCollections() throws ObjectStoreExcepti
throw new ObjectStoreException(de);
}
}


@Override
public List<CalDavCalendarCollection> getCollections(String workspace) throws ObjectStoreException, ObjectNotFoundException {
throw new UnsupportedOperationException("Workspaces not yet implemented");
}

protected List<CalDavCalendarCollection> getCollectionsForHomeSet(CalDavCalendarStore store,
String urlForcalendarHomeSet) throws IOException, DavException {
String urlForcalendarHomeSet) throws IOException, DavException {

DavPropertyNameSet principalsProps = CalDavCalendarCollection.propertiesForFetch();
return getClient().propFindResources(urlForcalendarHomeSet, principalsProps,
Expand Down Expand Up @@ -271,6 +295,11 @@ public CalDavCalendarCollection removeCollection(String id) throws ObjectStoreEx
return collection;
}

@Override
public List<String> listWorkspaces() {
throw new UnsupportedOperationException("Workspaces not yet implemented");
}

// public CalendarCollection replace(String id, CalendarCollection calendar) {
// // TODO Auto-generated method stub
// return null;
Expand Down
Loading

0 comments on commit 191167b

Please sign in to comment.