From 9f196bafe904c8125a59f22d9db82f9949bcc33f Mon Sep 17 00:00:00 2001 From: Athou Date: Mon, 23 Feb 2015 14:11:23 +0100 Subject: [PATCH] preserve order during opml import (#707) --- .../com/commafeed/backend/opml/OPMLImporter.java | 13 +++++++------ .../backend/service/FeedSubscriptionService.java | 12 ++++++++++-- .../com/commafeed/frontend/resource/FeedREST.java | 2 +- .../commafeed/backend/opml/OPMLImporterTest.java | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/commafeed/backend/opml/OPMLImporter.java b/src/main/java/com/commafeed/backend/opml/OPMLImporter.java index 3084f5b1..9e910f3f 100644 --- a/src/main/java/com/commafeed/backend/opml/OPMLImporter.java +++ b/src/main/java/com/commafeed/backend/opml/OPMLImporter.java @@ -38,8 +38,8 @@ public class OPMLImporter { try { Opml feed = (Opml) input.build(new StringReader(xml)); List outlines = feed.getOutlines(); - for (Outline outline : outlines) { - handleOutline(user, outline, null); + for (int i = 0; i < outlines.size(); i++) { + handleOutline(user, outlines.get(i), null, i); } } catch (Exception e) { log.error(e.getMessage(), e); @@ -47,7 +47,7 @@ public class OPMLImporter { } - private void handleOutline(User user, Outline outline, FeedCategory parent) { + private void handleOutline(User user, Outline outline, FeedCategory parent, int position) { List children = outline.getChildren(); if (CollectionUtils.isNotEmpty(children)) { String name = FeedUtils.truncate(outline.getText(), 128); @@ -64,11 +64,12 @@ public class OPMLImporter { category.setName(name); category.setParent(parent); category.setUser(user); + category.setPosition(position); feedCategoryDAO.saveOrUpdate(category); } - for (Outline child : children) { - handleOutline(user, child, category); + for (int i = 0; i < children.size(); i++) { + handleOutline(user, children.get(i), category, i); } } else { String name = FeedUtils.truncate(outline.getText(), 128); @@ -80,7 +81,7 @@ public class OPMLImporter { } // make sure we continue with the import process even if a feed failed try { - feedSubscriptionService.subscribe(user, outline.getXmlUrl(), name, parent); + feedSubscriptionService.subscribe(user, outline.getXmlUrl(), name, parent, position); } catch (FeedSubscriptionException e) { throw e; } catch (Exception e) { diff --git a/src/main/java/com/commafeed/backend/service/FeedSubscriptionService.java b/src/main/java/com/commafeed/backend/service/FeedSubscriptionService.java index e47f96c4..f14c1328 100644 --- a/src/main/java/com/commafeed/backend/service/FeedSubscriptionService.java +++ b/src/main/java/com/commafeed/backend/service/FeedSubscriptionService.java @@ -44,7 +44,15 @@ public class FeedSubscriptionService { private final CacheService cache; private final CommaFeedConfiguration config; - public Feed subscribe(User user, String url, String title, FeedCategory category) { + public Feed subscribe(User user, String url, String title) { + return subscribe(user, url, title, null, 0); + } + + public Feed subscribe(User user, String url, String title, FeedCategory parent) { + return subscribe(user, url, title, parent, 0); + } + + public Feed subscribe(User user, String url, String title, FeedCategory category, int position) { final String pubUrl = config.getApplicationSettings().getPublicUrl(); if (StringUtils.isBlank(pubUrl)) { @@ -63,7 +71,7 @@ public class FeedSubscriptionService { sub.setUser(user); } sub.setCategory(category); - sub.setPosition(0); + sub.setPosition(position); sub.setTitle(FeedUtils.truncate(title, 128)); feedSubscriptionDAO.saveOrUpdate(sub); diff --git a/src/main/java/com/commafeed/frontend/resource/FeedREST.java b/src/main/java/com/commafeed/frontend/resource/FeedREST.java index 95ae613a..a16fe27a 100644 --- a/src/main/java/com/commafeed/frontend/resource/FeedREST.java +++ b/src/main/java/com/commafeed/frontend/resource/FeedREST.java @@ -395,7 +395,7 @@ public class FeedREST { url = fetchFeedInternal(url).getUrl(); FeedInfo info = fetchFeedInternal(url); - feedSubscriptionService.subscribe(user, info.getUrl(), info.getTitle(), null); + feedSubscriptionService.subscribe(user, info.getUrl(), info.getTitle()); } catch (Exception e) { log.info("Could not subscribe to url {} : {}", url, e.getMessage()); } diff --git a/src/test/java/com/commafeed/backend/opml/OPMLImporterTest.java b/src/test/java/com/commafeed/backend/opml/OPMLImporterTest.java index 99b63bd0..14f75260 100644 --- a/src/test/java/com/commafeed/backend/opml/OPMLImporterTest.java +++ b/src/test/java/com/commafeed/backend/opml/OPMLImporterTest.java @@ -46,7 +46,7 @@ public class OPMLImporterTest { importer.importOpml(user, xml); Mockito.verify(feedSubscriptionService).subscribe(Mockito.eq(user), Mockito.anyString(), Mockito.anyString(), - Mockito.any(FeedCategory.class)); + Mockito.any(FeedCategory.class), Mockito.anyInt()); } }