added rest methods for updating positions

This commit is contained in:
Athou
2013-06-02 11:51:20 +02:00
parent 7ffd044a86
commit 555f35407b
6 changed files with 120 additions and 7 deletions

View File

@@ -51,6 +51,13 @@ public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
return category;
}
public List<FeedCategory> findByParent(User user, FeedCategory parent) {
EasyCriteria<FeedCategory> criteria = createCriteria();
criteria.andEquals(FeedCategory_.user.getName(), user);
criteria.andEquals(FeedCategory_.parent.getName(), parent);
return criteria.getResultList();
}
public List<FeedCategory> findAllChildrenCategories(User user,
FeedCategory parent) {
List<FeedCategory> list = Lists.newArrayList();
@@ -77,4 +84,5 @@ public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
}
return isChild;
}
}

View File

@@ -24,6 +24,9 @@ public class CategoryModificationRequest implements Serializable {
@ApiProperty(value = "new parent category id")
private String parentId;
@ApiProperty(value = "new display position, null if not changed")
private Integer position;
public Long getId() {
return id;
}
@@ -48,4 +51,12 @@ public class CategoryModificationRequest implements Serializable {
this.parentId = parentId;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
}

View File

@@ -24,6 +24,9 @@ public class FeedModificationRequest implements Serializable {
@ApiProperty(value = "new parent category id")
private String categoryId;
@ApiProperty(value = "new display position, null if not changed")
private Integer position;
public Long getId() {
return id;
}
@@ -48,4 +51,12 @@ public class FeedModificationRequest implements Serializable {
this.categoryId = categoryId;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
}

View File

@@ -266,6 +266,31 @@ public class CategoryREST extends AbstractResourceREST {
Long.valueOf(req.getParentId()));
}
category.setParent(parent);
if (req.getPosition() != null) {
List<FeedCategory> categories = feedCategoryDAO.findByParent(
getUser(), parent);
int existingIndex = -1;
for (int i = 0; i < categories.size(); i++) {
if (ObjectUtils.equals(categories.get(i).getId(),
category.getId())) {
existingIndex = i;
}
}
if (existingIndex != -1) {
categories.remove(existingIndex);
}
categories.add(Math.min(req.getPosition(), categories.size()),
category);
for (int i = 0; i < categories.size(); i++) {
categories.get(i).setPosition(i);
}
feedCategoryDAO.update(categories);
} else {
feedCategoryDAO.update(category);
}
feedCategoryDAO.update(category);
return Response.ok(Status.OK).build();

View File

@@ -24,6 +24,7 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -327,7 +328,29 @@ public class FeedREST extends AbstractResourceREST {
Long.valueOf(req.getCategoryId()));
}
subscription.setCategory(parent);
feedSubscriptionDAO.update(subscription);
if (req.getPosition() != null) {
List<FeedSubscription> subs = feedSubscriptionDAO.findByCategory(
getUser(), parent);
int existingIndex = -1;
for (int i = 0; i < subs.size(); i++) {
if (ObjectUtils.equals(subs.get(i).getId(),
subscription.getId())) {
existingIndex = i;
}
}
if (existingIndex != -1) {
subs.remove(existingIndex);
}
subs.add(Math.min(req.getPosition(), subs.size()), subscription);
for (int i = 0; i < subs.size(); i++) {
subs.get(i).setPosition(i);
}
feedSubscriptionDAO.update(subs);
} else {
feedSubscriptionDAO.update(subscription);
}
return Response.ok(Status.OK).build();
}