Hi. Solution for that problem is indeed comparator class, but if you want to 
sort page templates by order that they appear in config, it's a little more 
difficult. In comparator class you get not a nodes to compare, but 
SelectFieldOptionDefinition objects, which have more-less just node id (not 
even path). 

In my case that code solved a problem:

[code]public final class PageTemplateComparator implements 
java.util.Comparator<SelectFieldOptionDefinition> {

    public static final String ABSOLUTE_PAGES_PATH = 
"/modules/myWebsite/templates/";
    public static final String CONFIGURATION_REPOSITORY = "config";

    private static final Logger LOG = 
LoggerFactory.getLogger(PageTemplateComparator.class);
 
    @Override
    public int compare(SelectFieldOptionDefinition o1, 
SelectFieldOptionDefinition o2) {
        try {
            String subpath = 
o1.getValue().substring(o1.getValue().lastIndexOf(':')+1,o1.getValue().lastIndexOf('/')+1);
            Node parentNode = JcrUtils.getNodeIfExists(ABSOLUTE_PAGES_PATH + 
subpath, MgnlContext.getJCRSession(CONFIGURATION_REPOSITORY));
            NodeIterator i = parentNode.getNodes();

            List<String> nodeNamesList = new ArrayList<>();
            while (i.hasNext()) {
                Node node = (Node) i.next();
                nodeNamesList.add(node.toString());
            }

            String path1 = 
o1.getValue().substring(o1.getValue().lastIndexOf(':') + 1);
            Node node1 = JcrUtils.getNodeIfExists(ABSOLUTE_PAGES_PATH + path1, 
MgnlContext.getJCRSession(CONFIGURATION_REPOSITORY));
            int index1 = nodeNamesList.indexOf(node1.toString());

            String path2 = 
o2.getValue().substring(o2.getValue().lastIndexOf(':') + 1);
            Node node2 = JcrUtils.getNodeIfExists(ABSOLUTE_PAGES_PATH + path2, 
MgnlContext.getJCRSession(CONFIGURATION_REPOSITORY));
            int index2 = nodeNamesList.indexOf(node2.toString());

            return index1 - index2;
        } catch (RepositoryException e) {
           LOG.error("Error occurred during data fetch from repository " + e);
            return 0;
        }
    }
}[/code]

-- 
Context is everything: 
http://forum.magnolia-cms.com/forum/thread.html?threadId=d212f191-cda7-443e-b8ca-ae6083558aa8


----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <user-list-unsubscr...@magnolia-cms.com>
----------------------------------------------------------------

Reply via email to