Created
February 23, 2017 12:53
-
-
Save serpensalbus/331197c2d230268aad9b76f95e35649b to your computer and use it in GitHub Desktop.
Custom sorting of columns in Magnolia 5 content apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.ui.vaadin.integration.jcr.JcrItemId; | |
import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil; | |
import java.util.Comparator; | |
import javax.jcr.Item; | |
import javax.jcr.Node; | |
import org.apache.commons.lang3.StringUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.serpensalbus.magnolia.articles.app.util.ArticleUtil; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class CategoryColumnComparator implements Comparator<Object> { | |
private static final Logger log = LoggerFactory.getLogger(CategoryColumnComparator.class); | |
private boolean asc; | |
public CategoryColumnComparator(boolean asc) { | |
this.asc = asc; | |
} | |
@Override | |
public int compare(Object jcrItemId1, Object jcrItemId2) { | |
try { | |
if (!(jcrItemId1 instanceof JcrItemId) || !(jcrItemId2 instanceof JcrItemId)) { | |
return 0; | |
} | |
Item item1 = JcrItemUtil.getJcrItem((JcrItemId) jcrItemId1); | |
Item item2 = JcrItemUtil.getJcrItem((JcrItemId) jcrItemId2); | |
if (item1.isNode() && item2.isNode()) { | |
Node node1 = (Node) item1; | |
Node node2 = (Node) item2; | |
if (StringUtils.equalsIgnoreCase(node1.getPrimaryNodeType().getName(), "mgnl:article") | |
&& StringUtils.equalsIgnoreCase(node2.getPrimaryNodeType().getName(), "mgnl:article")) { | |
String categories1 = ArticleUtil.getCategoryNamesFromReferences(node1); | |
String categories2 = ArticleUtil.getCategoryNamesFromReferences(node2); | |
if (asc) { | |
return categories1.compareTo(categories2); | |
} else { | |
return (categories1.compareTo(categories2)) * -1; | |
} | |
} | |
} | |
} catch (Exception e) { | |
log.error("Problem while sorting article categories.", e); | |
} | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnectorDefinition; | |
import info.magnolia.ui.workbench.list.FlatJcrContainer; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.lang3.StringUtils; | |
import com.serpensalbus.magnolia.articles.app.util.ArticleUtil; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedHierarchicalFlatJcrContainer extends FlatJcrContainer { | |
private String currentSortProperty; | |
private boolean sortAscending = true; | |
public SortedHierarchicalFlatJcrContainer(JcrContentConnectorDefinition connectorDefinition) { | |
super(connectorDefinition); | |
} | |
@Override | |
public List<?> getItemIds(int startIndex, int numberOfItems) { | |
List<?> listItems = new ArrayList<>(super.getItemIds(startIndex, numberOfItems)); | |
if (StringUtils.equalsIgnoreCase(currentSortProperty, ArticleUtil.SORT_FIELD)) { | |
listItems.sort(new CategoryColumnComparator(sortAscending)); | |
} | |
return listItems; | |
} | |
@Override | |
public void sort(Object[] propertyId, boolean[] ascending) { | |
if (propertyId != null && propertyId.length > 0) { | |
setCurrentSortProperty((String) propertyId[0]); | |
} | |
if (ascending != null && ascending.length > 0) { | |
setSortAscending(ascending[0]); | |
} | |
assert propertyId != null; | |
super.sort(propertyId, ascending); | |
} | |
public void setCurrentSortProperty(String currentSortProperty) { | |
this.currentSortProperty = currentSortProperty; | |
} | |
public void setSortAscending(boolean sortAscending) { | |
this.sortAscending = sortAscending; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnectorDefinition; | |
import info.magnolia.ui.vaadin.integration.jcr.JcrItemId; | |
import info.magnolia.ui.workbench.tree.HierarchicalJcrContainer; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import org.apache.commons.lang3.StringUtils; | |
import com.serpensalbus.magnolia.articles.app.util.ArticleUtil; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedHierarchicalJcrContainer extends HierarchicalJcrContainer { | |
private String currentSortProperty; | |
private boolean sortAscending = true; | |
public SortedHierarchicalJcrContainer(JcrContentConnectorDefinition definition) { | |
super(definition); | |
} | |
@Override | |
public Collection<JcrItemId> getChildren(Object itemId) { | |
List<JcrItemId> listItems = new ArrayList<>(super.getChildren(itemId)); | |
if (StringUtils.equalsIgnoreCase(currentSortProperty, ArticleUtil.SORT_FIELD)) { | |
listItems.sort(new CategoryColumnComparator(sortAscending)); | |
} | |
return listItems; | |
} | |
@Override | |
public void sort(Object[] propertyId, boolean[] ascending) { | |
if (propertyId != null && propertyId.length > 0) { | |
setCurrentSortProperty((String) propertyId[0]); | |
} | |
if (ascending != null && ascending.length > 0) { | |
setSortAscending(ascending[0]); | |
} | |
assert propertyId != null; | |
super.sort(propertyId, ascending); | |
} | |
private void setCurrentSortProperty(String currentSortProperty) { | |
this.currentSortProperty = currentSortProperty; | |
} | |
private void setSortAscending(boolean sortAscending) { | |
this.sortAscending = sortAscending; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.objectfactory.ComponentProvider; | |
import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnector; | |
import info.magnolia.ui.workbench.list.ListPresenter; | |
import info.magnolia.ui.workbench.list.ListView; | |
import com.vaadin.data.Container; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedListPresenter extends ListPresenter { | |
public SortedListPresenter(ListView view, ComponentProvider componentProvider) { | |
super(view, componentProvider); | |
} | |
@Override | |
protected Container createContainer() { | |
return new SortedHierarchicalFlatJcrContainer(((JcrContentConnector) contentConnector).getContentConnectorDefinition()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.ui.workbench.list.ListPresenterDefinition; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedListPresenterDefinition extends ListPresenterDefinition { | |
public SortedListPresenterDefinition() { | |
setImplementationClass(SortedListPresenter.class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.objectfactory.ComponentProvider; | |
import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnector; | |
import info.magnolia.ui.workbench.tree.TreePresenter; | |
import info.magnolia.ui.workbench.tree.TreeView; | |
import com.vaadin.data.Container; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedTreePresenter extends TreePresenter { | |
public SortedTreePresenter(TreeView view, ComponentProvider componentProvider) { | |
super(view, componentProvider); | |
} | |
@Override | |
protected Container.Hierarchical createContainer() { | |
return new SortedHierarchicalJcrContainer(((JcrContentConnector) contentConnector).getContentConnectorDefinition()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serpensalbus.magnolia.articles.app.workbench; | |
import info.magnolia.ui.workbench.tree.TreePresenterDefinition; | |
/** | |
* @author Lars Fischer | |
*/ | |
public class SortedTreePresenterDefinition extends TreePresenterDefinition { | |
public SortedTreePresenterDefinition() { | |
setImplementationClass(SortedTreePresenter.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment