// This is the actual feed we're creating, so massage the
// document
AtomResource.mergeFeedDocument(feedDoc,parent.getUUID(),parent.getCreated(),parent.getEdited());
log.fine("Creating root...");
Status status = storage.storeFeed(parent.getPath(),parent.getUUID(),feedDoc);
if (!status.isSuccess()) {
throw new AppException(status,"Failed to create root feed.");
}
} catch (IOException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create root feed due to exception.",ex);
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create root feed due to exception.",ex);
}
} else {
if (parent==null) {
try {
parent = db.createRoot();
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to get root feed due to exception.",ex);
}
log.fine("Creating root feed.");
try {
Status status = storage.storeFeed(parent.getPath(),parent.getUUID(),AtomResource.createFeedDocument("",parent.getUUID(),parent.getCreated()));
if (!status.isSuccess()) {
throw new AppException(status,"Failed to create root feed");
}
} catch (XMLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create root feed due to exception.",ex);
} catch (IOException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create root feed due to exception.",ex);
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create root feed due to exception.",ex);
}
}
if (feedPath.length()==0) {
throw new AppException(Status.CLIENT_ERROR_CONFLICT,"The root feed already exists.");
}
int current = 0;
for (; current<segments.length; current++) {
try {
Feed next = parent.getChild(segments[current]);
if (next==null) {
break;
} else {
parent = next;
}
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot get segment "+segments[current]+" from database due to exception.",ex);
}
}
// If we found the whole path, the feed alreayd exists
if (current==segments.length) {
throw new AppException(Status.CLIENT_ERROR_CONFLICT,"Feed already exists at path: "+feedPath);
}
if (log.isLoggable(Level.FINE)) {
log.fine("Parentage '"+join(segments,0,current,'/')+"' already exists...");
}
// Create all the path segments
for (int i=current; i<segments.length; i++) {
String path = join(segments,0,i+1,'/');
if (log.isLoggable(Level.FINE)) {
log.fine("Creating path: "+path);
}
boolean last = i==(segments.length-1);
boolean created = false;
try {
Feed next = parent.getChild(segments[i]);
if (next!=null && last) {
// This shouldn't happen
throw new AppException(Status.CLIENT_ERROR_CONFLICT,"Collection already exists at "+path);
}
if (next==null) {
parent = parent.createChild(segments[i],last ? index.getId() : null);
created = true;
} else {
parent = next;
}
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create segment "+segments[i]+" due to SQL exception.",ex);
}
// Create the feeds along the way
if (last) {
// This is the actual feed we're creating, so massage the
// document
AtomResource.mergeFeedDocument(feedDoc,parent.getUUID(),parent.getCreated(),parent.getEdited());
}
if (created) {
if (log.isLoggable(Level.FINE)) {
log.fine("Creating feed for: "+path);
}
try {
Status status = storage.storeFeed(parent.getPath(),parent.getUUID(),last ? feedDoc : AtomResource.createFeedDocument(segments[i],parent.getUUID(),parent.getCreated()));
if (!status.isSuccess()) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create feed at path "+path);
}
} catch (XMLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Failed to create feed at path "+path+" due to exception.",ex);
} catch (IOException ex) {