Item list(s) are now configurable using page properties
parent
ceef94b81a
commit
81482c578f
|
|
@ -59,6 +59,8 @@ import org.librecms.contentsection.privileges.ItemPrivileges;
|
||||||
|
|
||||||
import com.arsdigita.kernel.KernelConfig;
|
import com.arsdigita.kernel.KernelConfig;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
|
@ -67,6 +69,24 @@ import com.arsdigita.kernel.KernelConfig;
|
||||||
@Named("CmsPagesItemListModel")
|
@Named("CmsPagesItemListModel")
|
||||||
public class ItemListModel {
|
public class ItemListModel {
|
||||||
|
|
||||||
|
private static final String ITEM_LIST_SETTINGS_PREFIX = "itemlist";
|
||||||
|
|
||||||
|
private static final String LIMIT_TO_TYPE_SETTING = "limitToType";
|
||||||
|
|
||||||
|
private static final String LIST_ORDER_SETTING = "listOrder";
|
||||||
|
|
||||||
|
private static final String PAGE_SIZE_SETTING = "pageSize";
|
||||||
|
|
||||||
|
private static final boolean DESCENDING_DEFAULT = false;
|
||||||
|
|
||||||
|
private static final String LIMIT_TO_TYPE_DEFAULT = ContentItem.class
|
||||||
|
.getName();
|
||||||
|
|
||||||
|
private static final List<String> LIST_ORDER_DEFAULT = List
|
||||||
|
.of("displayName");
|
||||||
|
|
||||||
|
private static final int PAGE_SIZE_DEFAULT = 20;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CategoryModel categoryModel;
|
private CategoryModel categoryModel;
|
||||||
|
|
||||||
|
|
@ -100,62 +120,12 @@ public class ItemListModel {
|
||||||
@Inject
|
@Inject
|
||||||
private Shiro shiro;
|
private Shiro shiro;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PagePropertiesModel pagePropertiesModel;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PageUrlModel pageUrlModel;
|
private PageUrlModel pageUrlModel;
|
||||||
|
|
||||||
private boolean descending;
|
|
||||||
|
|
||||||
private String limitToType;
|
|
||||||
|
|
||||||
private List<String> listOrder;
|
|
||||||
|
|
||||||
private int pageSize;
|
|
||||||
|
|
||||||
private List<? extends AbstractContentItemListItemModel> itemList;
|
|
||||||
|
|
||||||
public ItemListModel() {
|
|
||||||
descending = false;
|
|
||||||
limitToType = ContentItem.class.getName();
|
|
||||||
listOrder = List.of("displayName");
|
|
||||||
pageSize = 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDescending() {
|
|
||||||
return descending;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDescending(final boolean descending) {
|
|
||||||
this.descending = descending;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLimitToType() {
|
|
||||||
return limitToType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLimitToType(final String limitToType) {
|
|
||||||
this.limitToType = limitToType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getListOrder() {
|
|
||||||
return Collections.unmodifiableList(listOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setListOrder(final List<String> listOrder) {
|
|
||||||
this.listOrder = new ArrayList<>(listOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPageSize() {
|
|
||||||
return pageSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPageSize(final int pageSize) {
|
|
||||||
this.pageSize = pageSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getFirstItem() {
|
|
||||||
return getOffset(pageSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public int getListSize() {
|
public int getListSize() {
|
||||||
return getItems().size();
|
return getItems().size();
|
||||||
|
|
@ -163,9 +133,16 @@ public class ItemListModel {
|
||||||
|
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public List<? extends AbstractContentItemListItemModel> getItems() {
|
public List<? extends AbstractContentItemListItemModel> getItems() {
|
||||||
// if (itemList == null) {
|
return getItems("");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
public List<? extends AbstractContentItemListItemModel> getItems(
|
||||||
|
final String listName
|
||||||
|
) {
|
||||||
|
return Collections.unmodifiableList(
|
||||||
buildList(
|
buildList(
|
||||||
buildLimitToType(limitToType),
|
buildLimitToType(getLimitToTypeSetting(listName)),
|
||||||
collectCategories(
|
collectCategories(
|
||||||
categoryRepository
|
categoryRepository
|
||||||
.findById(categoryModel.getCategory().getCategoryId())
|
.findById(categoryModel.getCategory().getCategoryId())
|
||||||
|
|
@ -180,15 +157,13 @@ public class ItemListModel {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
listOrder,
|
getListOrderSetting(listName),
|
||||||
pageSize
|
getPageSizeSetting(listName)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
// }
|
|
||||||
|
|
||||||
return Collections.unmodifiableList(itemList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildList(
|
private List<? extends AbstractContentItemListItemModel> buildList(
|
||||||
final Class<? extends ContentItem> limitToType,
|
final Class<? extends ContentItem> limitToType,
|
||||||
final List<Category> categories,
|
final List<Category> categories,
|
||||||
final List<String> listOrder,
|
final List<String> listOrder,
|
||||||
|
|
@ -228,7 +203,7 @@ public class ItemListModel {
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList())
|
||||||
);
|
);
|
||||||
|
|
||||||
itemList = entityManager
|
return entityManager
|
||||||
.createQuery(criteriaQuery)
|
.createQuery(criteriaQuery)
|
||||||
.getResultList()
|
.getResultList()
|
||||||
.stream()
|
.stream()
|
||||||
|
|
@ -402,6 +377,78 @@ public class ItemListModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getLimitToTypeSetting(final String listName) {
|
||||||
|
return Optional
|
||||||
|
.ofNullable(
|
||||||
|
pagePropertiesModel
|
||||||
|
.getProperties()
|
||||||
|
.get(
|
||||||
|
buildSettingKey(
|
||||||
|
listName,
|
||||||
|
LIMIT_TO_TYPE_SETTING
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.orElse(LIMIT_TO_TYPE_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getListOrderSetting(final String listName) {
|
||||||
|
return Optional
|
||||||
|
.ofNullable(
|
||||||
|
pagePropertiesModel
|
||||||
|
.getProperties()
|
||||||
|
.get(
|
||||||
|
buildSettingKey(
|
||||||
|
listName,
|
||||||
|
LIST_ORDER_SETTING
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.map(value -> value.split(","))
|
||||||
|
.map(
|
||||||
|
values -> Arrays
|
||||||
|
.asList(values)
|
||||||
|
.stream()
|
||||||
|
.map(String::strip)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
)
|
||||||
|
.orElse(LIST_ORDER_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getPageSizeSetting(final String listName) {
|
||||||
|
return Optional
|
||||||
|
.ofNullable(
|
||||||
|
pagePropertiesModel
|
||||||
|
.getProperties()
|
||||||
|
.get(
|
||||||
|
buildSettingKey(
|
||||||
|
listName,
|
||||||
|
PAGE_SIZE_SETTING
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(value -> value.matches("\\d+"))
|
||||||
|
.map(value -> Integer.parseUnsignedInt(value))
|
||||||
|
.orElse(PAGE_SIZE_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildSettingKey(
|
||||||
|
final String listName,
|
||||||
|
final String settingKey
|
||||||
|
) {
|
||||||
|
return List
|
||||||
|
.of(
|
||||||
|
ITEM_LIST_SETTINGS_PREFIX,
|
||||||
|
listName,
|
||||||
|
settingKey
|
||||||
|
)
|
||||||
|
.stream()
|
||||||
|
.filter(token -> token != null && !token.isBlank())
|
||||||
|
.collect(
|
||||||
|
Collectors.joining(".")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private AbstractContentItemListItemModel buildListItemModel(
|
private AbstractContentItemListItemModel buildListItemModel(
|
||||||
final ContentItem contentItem
|
final ContentItem contentItem
|
||||||
) {
|
) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue