Search in sources :

Example 1 with UINodeDescendants

use of org.alfresco.web.ui.repo.component.UINodeDescendants in project acs-community-packaging by Alfresco.

the class NodeDescendantsLinkRenderer method encodeEnd.

/**
 * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
 */
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    // always check for this flag - as per the spec
    if (component.isRendered() == true) {
        Writer out = context.getResponseWriter();
        UINodeDescendants control = (UINodeDescendants) component;
        // make sure we have a NodeRef from the 'value' property ValueBinding
        Object val = control.getValue();
        if (val instanceof NodeRef == false) {
            throw new IllegalArgumentException("UINodeDescendants component 'value' property must resolve to a NodeRef!");
        }
        NodeRef parentRef = (NodeRef) val;
        // use Spring JSF integration to get the node service bean
        NodeService service = getNodeService(context);
        DictionaryService dd = getDictionaryService(context);
        UserTransaction tx = null;
        try {
            tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true);
            tx.begin();
            // TODO: need a comparator to sort node refs (based on childref qname)
            // as currently the list is returned in a random order per request!
            String separator = (String) component.getAttributes().get("separator");
            if (separator == null) {
                separator = DEFAULT_SEPARATOR;
            }
            // calculate the number of displayed child refs
            if (service.exists(parentRef) == true) {
                List<ChildAssociationRef> childRefs = service.getChildAssocs(parentRef, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
                List<Node> nodes = new ArrayList<Node>(childRefs.size());
                for (int index = 0; index < childRefs.size(); index++) {
                    ChildAssociationRef ref = childRefs.get(index);
                    QName type = service.getType(ref.getChildRef());
                    TypeDefinition typeDef = dd.getType(type);
                    if (typeDef != null && dd.isSubClass(type, ContentModel.TYPE_FOLDER) && dd.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER) == false) {
                        nodes.add(new Node(ref.getChildRef()));
                    }
                }
                QuickSort sorter = new QuickSort(nodes, "name", true, IDataContainer.SORT_CASEINSENSITIVE);
                sorter.sort();
                // walk each child ref and output a descendant link control for each item
                int total = 0;
                int maximum = nodes.size() > control.getMaxChildren() ? control.getMaxChildren() : nodes.size();
                for (int index = 0; index < maximum; index++) {
                    Node node = nodes.get(index);
                    QName type = service.getType(node.getNodeRef());
                    TypeDefinition typeDef = dd.getType(type);
                    if (typeDef != null && dd.isSubClass(type, ContentModel.TYPE_FOLDER) && dd.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER) == false) {
                        // output separator if appropriate
                        if (total > 0) {
                            out.write(separator);
                        }
                        out.write(renderDescendant(context, control, node.getNodeRef(), false));
                        total++;
                    }
                }
                // do we need to render ellipses to indicate more items than the maximum
                if (control.getShowEllipses() == true && nodes.size() > maximum) {
                    out.write(separator);
                    // TODO: is this the correct way to get the information we need?
                    // e.g. primary parent may not be the correct path? how do we make sure we find
                    // the correct parent and more importantly the correct Display Name value!
                    out.write(renderDescendant(context, control, service.getPrimaryParent(parentRef).getChildRef(), true));
                }
            }
            tx.commit();
        } catch (Throwable err) {
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
            throw new RuntimeException(err);
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) QName(org.alfresco.service.namespace.QName) NodeService(org.alfresco.service.cmr.repository.NodeService) Node(org.alfresco.web.bean.repository.Node) ArrayList(java.util.ArrayList) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) UINodeDescendants(org.alfresco.web.ui.repo.component.UINodeDescendants) IOException(java.io.IOException) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) QuickSort(org.alfresco.web.data.QuickSort) Writer(java.io.Writer)

Aggregations

IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 UserTransaction (javax.transaction.UserTransaction)1 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)1 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1 QName (org.alfresco.service.namespace.QName)1 Node (org.alfresco.web.bean.repository.Node)1 QuickSort (org.alfresco.web.data.QuickSort)1 UINodeDescendants (org.alfresco.web.ui.repo.component.UINodeDescendants)1