View Javadoc

1   package net.sf.mmapps.commons.bridge;
2   
3   import java.util.*;
4   
5   import net.sf.mmapps.commons.util.StringUtil;
6   
7   import org.mmbase.bridge.*;
8   
9   
10  public class RelationUtil {
11  
12     public final static String RELATION_POS_FIELD = "pos";    
13      
14     public static void reorder(Cloud cloud, String parent, String childs, String role) {
15         reorder(cloud, parent, childs, role, null);
16     }
17      
18  	public static void reorder(Cloud cloud, String parent, String children, String role, String nodeManagerName) {
19  		Node parentNode = cloud.getNode(parent);
20  		reorder(parentNode, children, role, nodeManagerName);
21  	}
22  
23      public static void reorder(Cloud cloud, String parent, String[] children, String role) {
24          reorder(cloud, parent, children, role, null);
25      }
26      
27  	public static void reorder(Cloud cloud, String parent, String[] children, String role, String nodeManagerName) {
28  		Node parentNode = cloud.getNode(parent);
29  		reorder(parentNode, children, role, nodeManagerName);
30  	}
31  
32      public static void reorder(Node parentNode, List children, String role) {
33          reorder(parentNode, children, role, null);
34      }
35      
36      public static void reorder(Node parentNode, Object children, String role, String nodeManagerName) {
37      	List<String> childrenList = null;
38          if (children instanceof List) {
39              childrenList = (List<String>) children;
40          }
41          if (children instanceof String) {
42              StringTokenizer tokenizer = new StringTokenizer((String) children, ",");
43              childrenList = new ArrayList<String>();
44              while (tokenizer.hasMoreTokens()) {
45                  childrenList.add(tokenizer.nextToken());
46              }
47          }
48          if (children instanceof String[]) {
49              childrenList = new ArrayList<String>();
50              for (int i = 0; i < ((String[]) children).length; i++) {
51                  childrenList.add(((String[]) children)[i]);
52              }
53          }
54          if (childrenList == null) {
55              throw new IllegalArgumentException("Children not of the supported types (String, String[], List)");
56          }
57          
58          RelationList list = null;
59          Cloud cloud = parentNode.getCloud();
60          if (!StringUtil.isEmpty(nodeManagerName)) {
61              list = parentNode.getRelations(role,  cloud.getNodeManager(nodeManagerName), "DESTINATION");
62          }
63          else {
64  		    list = parentNode.getRelations(role);
65          }
66  		RelationIterator iter = list.relationIterator();
67  		while (iter.hasNext()) {
68  			Relation rel = iter.nextRelation();
69  			int destination = rel.getDestination().getNumber();
70  			rel.setIntValue(RELATION_POS_FIELD, childrenList.indexOf("" + destination));
71  			rel.commit();
72  		}
73  	}
74      
75      public static Relation createRelation(Node sourceNode, Node destNode, String role) {
76          Cloud cloud = sourceNode.getCloud();
77          RelationManager relationManager = cloud.getRelationManager(sourceNode.getNodeManager(), 
78                                                  destNode.getNodeManager(), role);
79          Relation relation = sourceNode.createRelation(destNode, relationManager);
80          relation.commit();
81          
82          return relation;
83      }
84      
85      public static Relation createCountedRelation(Node parentNode, Node childNode, String relationName, String countField) {
86          Cloud cloud = parentNode.getCloud();
87          NodeManager nm = childNode.getNodeManager();
88  
89          int childCount = parentNode.countRelatedNodes(nm, relationName, "DESTINATION");
90          
91          RelationManager relationManager = cloud.getRelationManager(parentNode.getNodeManager(), nm, relationName);
92          Relation relation = parentNode.createRelation(childNode, relationManager);
93          relation.setIntValue(countField, childCount + 1);
94          relation.commit();
95          
96          return relation;
97      }
98  
99  }