/*
* Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
*
* Licensed 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 com.semagia.atomico.server.impl.restlet.resources;
import java.util.ArrayList;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;
import com.semagia.atomico.MediaType;
import com.semagia.atomico.server.FeedHandlerRegistry;
import com.semagia.atomico.server.dm.ICollectionEntry;
import com.semagia.atomico.server.dm.ICollectionInfo;
import com.semagia.atomico.server.dm.impl.DefaultCollectionEntry;
import com.semagia.atomico.server.dm.impl.DefaultFeed;
import com.semagia.atomico.server.feed.impl.OverviewFeedWriter;
import com.semagia.atomico.server.storage.StorageException;
/**
* Resource representing a list of available collections.
*
* @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
* @version $Rev: 66 $ - $Date: 2010-09-06 10:29:07 -0500 (Mon, 06 Sep 2010) $
*/
public class OverviewResource extends AbstractFeedBaseResource {
/**
* The modification time.
*/
private long _lastModification;
/**
* Initializes the instance.
*/
public OverviewResource() {
super();
}
@Override
public boolean isExisting() {
// A true value needs to be returned for the overview feed to be displayed
return true;
}
/* (non-Javadoc)
* @see org.restlet.resource.UniformResource#doInit()
*/
@Override
protected void doInit() throws ResourceException {
try {
_lastModification = _storage.lastModification();
}
catch (StorageException ex) {
throw new ResourceException(ex);
}
}
/* (non-Javadoc)
* @see com.semagia.atomico.server.impl.restlet.resources.AbstractFeedBaseResource#lastModification()
*/
@Override
protected long lastModification() {
return _lastModification;
}
/* (non-Javadoc)
* @see org.restlet.resource.ServerResource#get(org.restlet.representation.Variant)
*/
@Override
public Representation get(Variant variant) throws ResourceException {
final String iri = getRequestIRI();
DefaultFeed<ICollectionEntry> feed = new DefaultFeed<ICollectionEntry>(iri,
getConfiguration().getOverviewFeedTitle(),
_lastModification);
applyAuthorInfo(feed);
for (ICollectionInfo info: _getCollections()) {
ICollectionEntry entry = new DefaultCollectionEntry(info, super.linkToCollection(info), new ArrayList<MediaType>(FeedHandlerRegistry.getMediaTypes()));
feed.addEntry(entry);
}
return new WritableAwareRepresentation(variant.getMediaType(),
new OverviewFeedWriter(feed, makeFeedHandler(variant)),
_lastModification);
}
/**
* Returns the collections belonging to the overview in the configured
* sort order.
*
* @return The available collection infos.
* @throws ResourceException In case of {@link StorageException}.
*/
private Iterable<ICollectionInfo> _getCollections() throws ResourceException {
try {
return _storage.getCollectionInfos(getConfiguration().getCollectionSortOrder());
}
catch (StorageException ex) {
throw new ResourceException(ex);
}
}
}